1 // Created on: 2002-04-24
2 // Created by: Alexander GRIGORIEV
3 // Copyright (c) 2002-2013 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_BaseVector_HeaderFile
17 #define NCollection_BaseVector_HeaderFile
19 #include <Standard_TypeDef.hxx>
20 #include <Standard_OutOfRange.hxx>
21 #include <NCollection_BaseAllocator.hxx>
22 #include <NCollection_DefineAlloc.hxx>
26 // this value defines the number of blocks that are reserved
27 // when the capacity of vector is increased
28 inline Standard_Integer GetCapacity (const Standard_Integer theIncrement)
30 return Max(theIncrement/8, 1);
33 //! Class NCollection_BaseVector - base for NCollection_Vector template
34 class NCollection_BaseVector
39 DEFINE_NCOLLECTION_ALLOC
43 // Auxiliary structure for memory blocks
49 //! @param theIndex Item index in the block
50 //! @param theItemSize Element size in bytes
51 //! @return the address of specified item in this memory block
52 void* findV (const Standard_Integer theIndex,
53 const size_t theItemSize) const
55 return (char* )DataPtr + size_t(theIndex) * theItemSize;
60 void* DataPtr; //!< block of elements
61 Standard_Integer FirstIndex; //!< index of the first element (among all memory blocks in collection)
62 Standard_Integer Length;
63 Standard_Integer Size;
67 //! Base class for Iterator implementation
77 Iterator (const NCollection_BaseVector& theVector, Standard_Boolean theToEnd = Standard_False)
79 initV (theVector, theToEnd);
82 Iterator (const Iterator& theVector)
87 Standard_EXPORT void initV (const NCollection_BaseVector& theVector, Standard_Boolean theToEnd = Standard_False);
89 Standard_EXPORT void copyV (const Iterator&);
91 Standard_Boolean moreV() const
93 return (myICurBlock < myIEndBlock || myCurIndex < myEndIndex);
98 if (++myCurIndex >= myVector->myData[myICurBlock].Length
99 && myICurBlock < myIEndBlock)
108 if (--myCurIndex < 0 && myICurBlock > 0)
111 myCurIndex = myVector->myData[myICurBlock].Length - 1;
115 void offsetV (Standard_Integer theOffset)
117 const Standard_Integer anIndex = myCurIndex + myICurBlock * myVector->myIncrement + theOffset;
118 myICurBlock = anIndex / myVector->myIncrement;
119 myCurIndex = anIndex % myVector->myIncrement;
122 Standard_Integer differV (const Iterator& theOther) const
124 return (myCurIndex - theOther.myCurIndex) + (myICurBlock - theOther.myICurBlock) * myVector->myIncrement;
127 const MemBlock* curBlockV() const
129 return &myVector->myData[myICurBlock];
132 const NCollection_BaseVector* myVector; //!< the Master vector
133 Standard_Integer myICurBlock; //!< # of the current block
134 Standard_Integer myIEndBlock;
135 Standard_Integer myCurIndex; //!< Index in the current block
136 Standard_Integer myEndIndex;
139 protected: //! @name Block initializer
141 typedef void (*initMemBlocks_t) (NCollection_BaseVector& theVector,
143 const Standard_Integer theFirst,
144 const Standard_Integer theSize);
146 //! Allocate memory for array of memory blocks.
147 //! @param theCapacity Number of memory blocks in array
148 //! @param theSource Original array of memory blocks, will be automatically deallocated
149 //! @param theSourceSize Number of memory blocks in original array
150 Standard_EXPORT MemBlock* allocMemBlocks (const Standard_Integer theCapacity,
151 MemBlock* theSource = NULL,
152 const Standard_Integer theSourceSize = 0);
154 protected: //! @name protected methods
156 //! Empty constructor
157 NCollection_BaseVector (const Handle(NCollection_BaseAllocator)& theAllocator,
158 initMemBlocks_t theInitBlocks,
159 const size_t theSize,
160 const Standard_Integer theInc)
161 : myItemSize (theSize),
162 myIncrement (theInc),
164 myCapacity (GetCapacity (myIncrement)),
166 myInitBlocks (theInitBlocks)
168 myAllocator = (theAllocator.IsNull() ? NCollection_BaseAllocator::CommonBaseAllocator() : theAllocator);
169 myData = allocMemBlocks (myCapacity);
173 NCollection_BaseVector (const Handle(NCollection_BaseAllocator)& theAllocator,
174 initMemBlocks_t theInitBlocks,
175 const NCollection_BaseVector& theOther)
176 : myItemSize (theOther.myItemSize),
177 myIncrement (theOther.myIncrement),
178 myLength (theOther.myLength),
179 myCapacity (GetCapacity(myIncrement) + theOther.myLength / theOther.myIncrement),
180 myNBlocks (theOther.myLength == 0 ? 0 : 1 + (theOther.myLength - 1)/theOther.myIncrement),
181 myInitBlocks (theInitBlocks)
183 myAllocator = (theAllocator.IsNull() ? NCollection_BaseAllocator::CommonBaseAllocator() : theAllocator);
184 myData = allocMemBlocks (myCapacity);
187 //! @return pointer to memory where to put the new item
188 Standard_EXPORT void* expandV (const Standard_Integer theIndex);
190 //! Locate the memory holding the desired value
191 inline void* findV (const Standard_Integer theIndex) const
193 Standard_OutOfRange_Raise_if (theIndex < 0 || theIndex >= myLength,
194 "NCollection_BaseVector::findV");
195 const Standard_Integer aBlock = theIndex / myIncrement;
196 return myData[aBlock].findV (theIndex - aBlock * myIncrement, myItemSize);
199 public: //! @name public API
201 //! Empty the vector of its objects
202 Standard_EXPORT void Clear();
203 // to set the size of increment dynamically
204 void SetIncrement(const Standard_Integer aIncrement) {
205 if (aIncrement > 0) {
207 myIncrement=aIncrement;
212 protected: //! @name Protected fields
214 Handle(NCollection_BaseAllocator) myAllocator;
216 Standard_Integer myIncrement;
217 Standard_Integer myLength;
218 Standard_Integer myCapacity;
219 Standard_Integer myNBlocks;
221 initMemBlocks_t myInitBlocks;
225 friend class Iterator;
228 #endif // NCollection_BaseVector_HeaderFile