1 // Created on: 2002-04-24
2 // Created by: Alexander KARTOMIN (akm)
3 // Copyright (c) 2002-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #ifndef NCollection_IndexedMap_HeaderFile
17 #define NCollection_IndexedMap_HeaderFile
19 #include <NCollection_BaseMap.hxx>
20 #include <NCollection_TListNode.hxx>
21 #include <NCollection_StlIterator.hxx>
22 #include <Standard_NoSuchObject.hxx>
24 #include <NCollection_DefaultHasher.hxx>
26 #include <Standard_OutOfRange.hxx>
29 * Purpose: An indexed map is used to store keys and to bind
30 * an index to them. Each new key stored in the map
31 * gets an index. Index are incremented as keys are
32 * stored in the map. A key can be found by the index
33 * and an index by the key. No key but the last can
34 * be removed so the indices are in the range 1..Extent.
35 * See the class Map from NCollection for a
36 * discussion about the number of buckets.
39 template < class TheKeyType,
40 class Hasher = NCollection_DefaultHasher<TheKeyType> >
41 class NCollection_IndexedMap : public NCollection_BaseMap
44 //! STL-compliant typedef for key type
45 typedef TheKeyType key_type;
48 //! Adaptation of the TListNode to the INDEXEDmap
49 class IndexedMapNode : public NCollection_TListNode<TheKeyType>
52 //! Constructor with 'Next'
53 IndexedMapNode (const TheKeyType& theKey1,
54 const Standard_Integer theIndex,
55 NCollection_ListNode* theNext1)
56 : NCollection_TListNode<TheKeyType> (theKey1, theNext1),
61 TheKeyType& Key1() { return this->ChangeValue(); }
64 Standard_Integer& Index() { return myIndex; }
66 //! Static deleter to be passed to BaseList
67 static void delNode (NCollection_ListNode * theNode,
68 Handle(NCollection_BaseAllocator)& theAl)
70 ((IndexedMapNode *) theNode)->~IndexedMapNode();
75 Standard_Integer myIndex;
79 // **************** Implementation of the Iterator interface.
88 Iterator (const NCollection_IndexedMap& theMap) :
89 myMap((NCollection_IndexedMap *) &theMap),
91 //! Query if the end of collection is reached by iterator
92 Standard_Boolean More(void) const
93 { return (myMap != NULL) && (myIndex <= myMap->Extent()); }
94 //! Make a step along the collection
98 const TheKeyType& Value(void) const
100 Standard_NoSuchObject_Raise_if(!More(), "NCollection_IndexedMap::Iterator::Value");
101 return myMap->FindKey(myIndex);
104 //! Performs comparison of two iterators.
105 Standard_Boolean IsEqual (const Iterator& theOther) const
107 return myMap == theOther.myMap && myIndex == theOther.myIndex;
111 NCollection_IndexedMap * myMap; // Pointer to the map being iterated
112 Standard_Integer myIndex; // Current index
115 //! Shorthand for a constant iterator type.
116 typedef NCollection_StlIterator<std::forward_iterator_tag, Iterator, TheKeyType, true> const_iterator;
118 //! Returns a const iterator pointing to the first element in the map.
119 const_iterator cbegin() const { return Iterator (*this); }
121 //! Returns a const iterator referring to the past-the-end element in the map.
122 const_iterator cend() const { return Iterator(); }
125 // ---------- PUBLIC METHODS ------------
127 //! Empty constructor.
128 NCollection_IndexedMap() : NCollection_BaseMap (1, Standard_False, Handle(NCollection_BaseAllocator)()) {}
131 explicit NCollection_IndexedMap (const Standard_Integer theNbBuckets,
132 const Handle(NCollection_BaseAllocator)& theAllocator=0L)
133 : NCollection_BaseMap (theNbBuckets, Standard_False, theAllocator) {}
136 NCollection_IndexedMap (const NCollection_IndexedMap& theOther)
137 : NCollection_BaseMap (theOther.NbBuckets(), Standard_False, theOther.myAllocator)
138 { *this = theOther; }
140 //! Exchange the content of two maps without re-allocations.
141 //! Notice that allocators will be swapped as well!
142 void Exchange (NCollection_IndexedMap& theOther)
144 this->exchangeMapsData (theOther);
148 //! This method does not change the internal allocator.
149 NCollection_IndexedMap& Assign (const NCollection_IndexedMap& theOther)
151 if (this == &theOther)
155 ReSize (theOther.Extent()-1);
156 const Standard_Integer iLength = theOther.Extent();
157 for (Standard_Integer anIndexIter = 1; anIndexIter <= iLength; ++anIndexIter)
159 const TheKeyType& aKey1 = theOther.FindKey (anIndexIter);
160 const Standard_Integer iK1 = Hasher::HashCode (aKey1, NbBuckets());
161 IndexedMapNode* pNode = new (this->myAllocator) IndexedMapNode (aKey1, anIndexIter, myData1[iK1]);
162 myData1[iK1] = pNode;
163 myData2[anIndexIter - 1] = pNode;
169 //! Assignment operator
170 NCollection_IndexedMap& operator= (const NCollection_IndexedMap& theOther)
172 return Assign (theOther);
176 void ReSize (const Standard_Integer theExtent)
178 NCollection_ListNode** ppNewData1 = NULL;
179 NCollection_ListNode** ppNewData2 = NULL;
180 Standard_Integer newBuck;
181 if (BeginResize (theExtent, newBuck, ppNewData1, ppNewData2))
185 memcpy (ppNewData2, myData2, sizeof(IndexedMapNode*) * Extent());
186 for (Standard_Integer aBucketIter = 0; aBucketIter <= NbBuckets(); ++aBucketIter)
188 if (myData1[aBucketIter])
190 IndexedMapNode* p = (IndexedMapNode* )myData1[aBucketIter];
193 const Standard_Integer iK1 = Hasher::HashCode (p->Key1(), newBuck);
194 IndexedMapNode* q = (IndexedMapNode* )p->Next();
195 p->Next() = ppNewData1[iK1];
202 EndResize (theExtent, newBuck, ppNewData1, ppNewData2);
207 Standard_Integer Add (const TheKeyType& theKey1)
214 Standard_Integer iK1 = Hasher::HashCode (theKey1, NbBuckets());
215 IndexedMapNode* pNode = (IndexedMapNode* )myData1[iK1];
218 if (Hasher::IsEqual (pNode->Key1(), theKey1))
220 return pNode->Index();
222 pNode = (IndexedMapNode *) pNode->Next();
225 const Standard_Integer aNewIndex = Increment();
226 pNode = new (this->myAllocator) IndexedMapNode (theKey1, aNewIndex, myData1[iK1]);
227 myData1[iK1] = pNode;
228 myData2[aNewIndex - 1] = pNode;
233 Standard_Boolean Contains (const TheKeyType& theKey1) const
236 return Standard_False;
237 Standard_Integer iK1 = Hasher::HashCode (theKey1, NbBuckets());
238 IndexedMapNode * pNode1;
239 pNode1 = (IndexedMapNode *) myData1[iK1];
242 if (Hasher::IsEqual(pNode1->Key1(), theKey1))
243 return Standard_True;
244 pNode1 = (IndexedMapNode *) pNode1->Next();
246 return Standard_False;
250 void Substitute (const Standard_Integer theIndex,
251 const TheKeyType& theKey1)
253 Standard_OutOfRange_Raise_if (theIndex < 1 || theIndex > Extent(),
254 "NCollection_IndexedMap::Substitute : "
255 "Index is out of range");
257 // check if theKey1 is not already in the map
258 Standard_Integer iK1 = Hasher::HashCode (theKey1, NbBuckets());
259 IndexedMapNode* p = (IndexedMapNode *) myData1[iK1];
262 if (Hasher::IsEqual (p->Key1(), theKey1))
264 if (p->Index() != theIndex)
266 throw Standard_DomainError ("NCollection_IndexedMap::Substitute : "
267 "Attempt to substitute existing key");
272 p = (IndexedMapNode *) p->Next();
275 // Find the node for the index I
276 p = (IndexedMapNode* )myData2[theIndex - 1];
278 // remove the old key
279 Standard_Integer iK = Hasher::HashCode (p->Key1(), NbBuckets());
280 IndexedMapNode * q = (IndexedMapNode *) myData1[iK];
282 myData1[iK] = (IndexedMapNode *) p->Next();
285 while (q->Next() != p)
286 q = (IndexedMapNode *) q->Next();
287 q->Next() = p->Next();
292 p->Next() = myData1[iK1];
296 //! Swaps two elements with the given indices.
297 void Swap (const Standard_Integer theIndex1,
298 const Standard_Integer theIndex2)
300 Standard_OutOfRange_Raise_if (theIndex1 < 1 || theIndex1 > Extent()
301 || theIndex2 < 1 || theIndex2 > Extent(), "NCollection_IndexedMap::Swap");
303 if (theIndex1 == theIndex2)
308 IndexedMapNode* aP1 = (IndexedMapNode* )myData2[theIndex1 - 1];
309 IndexedMapNode* aP2 = (IndexedMapNode* )myData2[theIndex2 - 1];
310 std::swap (aP1->Index(), aP2->Index());
311 myData2[theIndex2 - 1] = aP1;
312 myData2[theIndex1 - 1] = aP2;
316 void RemoveLast (void)
318 const Standard_Integer aLastIndex = Extent();
319 Standard_OutOfRange_Raise_if (aLastIndex == 0, "NCollection_IndexedMap::RemoveLast");
321 // Find the node for the last index and remove it
322 IndexedMapNode* p = (IndexedMapNode* )myData2[aLastIndex - 1];
323 myData2[aLastIndex - 1] = NULL;
326 Standard_Integer iK1 = Hasher::HashCode (p->Key1(), NbBuckets());
327 IndexedMapNode* q = (IndexedMapNode *) myData1[iK1];
329 myData1[iK1] = (IndexedMapNode *) p->Next();
332 while (q->Next() != p)
333 q = (IndexedMapNode *) q->Next();
334 q->Next() = p->Next();
336 p->~IndexedMapNode();
337 this->myAllocator->Free(p);
341 //! Remove the key of the given index.
342 //! Caution! The index of the last key can be changed.
343 void RemoveFromIndex(const Standard_Integer theIndex)
345 Standard_OutOfRange_Raise_if(theIndex < 1 || theIndex > Extent(), "NCollection_IndexedMap::RemoveFromIndex");
346 const Standard_Integer aLastInd = Extent();
347 if (theIndex != aLastInd)
349 Swap(theIndex, aLastInd);
354 //! Remove the given key.
355 //! Caution! The index of the last key can be changed.
356 Standard_Boolean RemoveKey (const TheKeyType& theKey1)
358 Standard_Integer anIndToRemove = FindIndex(theKey1);
359 if (anIndToRemove < 1)
361 return Standard_False;
364 RemoveFromIndex (anIndToRemove);
365 return Standard_True;
369 const TheKeyType& FindKey (const Standard_Integer theIndex) const
371 Standard_OutOfRange_Raise_if (theIndex < 1 || theIndex > Extent(), "NCollection_IndexedMap::FindKey");
372 IndexedMapNode* pNode2 = (IndexedMapNode* )myData2[theIndex - 1];
373 return pNode2->Key1();
377 const TheKeyType& operator() (const Standard_Integer theIndex) const
378 { return FindKey (theIndex); }
381 Standard_Integer FindIndex(const TheKeyType& theKey1) const
383 if (IsEmpty()) return 0;
384 IndexedMapNode* pNode1 = (IndexedMapNode* )myData1[Hasher::HashCode(theKey1,NbBuckets())];
387 if (Hasher::IsEqual (pNode1->Key1(), theKey1))
389 return pNode1->Index();
391 pNode1 = (IndexedMapNode*) pNode1->Next();
396 //! Clear data. If doReleaseMemory is false then the table of
397 //! buckets is not released and will be reused.
398 void Clear(const Standard_Boolean doReleaseMemory = Standard_True)
399 { Destroy (IndexedMapNode::delNode, doReleaseMemory); }
401 //! Clear data and reset allocator
402 void Clear (const Handle(NCollection_BaseAllocator)& theAllocator)
405 this->myAllocator = ( ! theAllocator.IsNull() ? theAllocator :
406 NCollection_BaseAllocator::CommonBaseAllocator() );
410 virtual ~NCollection_IndexedMap (void)
414 Standard_Integer Size(void) const