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 <NCollection_BaseAllocator.hxx>
23 #include <Standard_OutOfRange.hxx>
26 #pragma warning(push, 1)
27 #pragma warning(disable:4355)
30 // this value defines the number of blocks that are reserved
31 // when the capacity of vector is increased
32 inline Standard_Integer GetCapacity (const Standard_Integer theIncrement)
34 return Max(theIncrement/8, 1);
37 //! Class NCollection_BaseVector - base for NCollection_Vector template
38 class NCollection_BaseVector
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)
82 Iterator (const Iterator& theVector)
87 Standard_EXPORT void initV (const NCollection_BaseVector& theVector);
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)
106 const MemBlock* curBlockV() const
108 return &myVector->myData[myICurBlock];
111 const NCollection_BaseVector* myVector; //!< the Master vector
112 Standard_Integer myICurBlock; //!< # of the current block
113 Standard_Integer myIEndBlock;
114 Standard_Integer myCurIndex; //!< Index in the current block
115 Standard_Integer myEndIndex;
118 protected: //! @name Block initializer
120 typedef void (*initMemBlocks_t) (NCollection_BaseVector& theVector,
122 const Standard_Integer theFirst,
123 const Standard_Integer theSize);
125 //! Allocate memory for array of memory blocks.
126 //! @param theAllocator Memory allocator to use
127 //! @param theCapacity Number of memory blocks in array
128 //! @param theSource Original array of memory blocks, will be automatically deallocated
129 //! @param theSourceSize Number of memory blocks in original array
130 Standard_EXPORT static MemBlock* allocMemBlocks (Handle(NCollection_BaseAllocator)& theAllocator,
131 const Standard_Integer theCapacity,
132 MemBlock* theSource = NULL,
133 const Standard_Integer theSourceSize = 0);
135 protected: //! @name protected methods
137 //! Empty constructor
138 NCollection_BaseVector (Handle(NCollection_BaseAllocator)& theAllocator,
139 initMemBlocks_t theInitBlocks,
140 const size_t theSize,
141 const Standard_Integer theInc)
142 : myItemSize (theSize),
143 myIncrement (theInc),
145 myCapacity (GetCapacity (myIncrement)),
147 myData (allocMemBlocks (theAllocator, myCapacity)),
148 myInitBlocks (theInitBlocks) {}
151 NCollection_BaseVector (Handle(NCollection_BaseAllocator)& theAllocator,
152 initMemBlocks_t theInitBlocks,
153 const NCollection_BaseVector& theOther)
154 : myItemSize (theOther.myItemSize),
155 myIncrement (theOther.myIncrement),
156 myLength (theOther.myLength),
157 myCapacity (GetCapacity(myIncrement) + theOther.myLength / theOther.myIncrement),
158 myNBlocks (1 + (theOther.myLength - 1)/theOther.myIncrement),
159 myData (allocMemBlocks (theAllocator, myCapacity)),
160 myInitBlocks (theInitBlocks) {}
162 //! @return pointer to memory where to put the new item
163 Standard_EXPORT void* expandV (Handle(NCollection_BaseAllocator)& theAllocator,
164 const Standard_Integer theIndex);
166 //! Locate the memory holding the desired value
167 inline void* findV (const Standard_Integer theIndex) const
169 Standard_OutOfRange_Raise_if (theIndex < 0 || theIndex >= myLength,
170 "NCollection_BaseVector::findV");
171 const Standard_Integer aBlock = theIndex / myIncrement;
172 return myData[aBlock].findV (theIndex - aBlock * myIncrement, myItemSize);
175 public: //! @name public API
177 //! Empty the vector of its objects
178 Standard_EXPORT void Clear();
180 protected: //! @name Private fields
183 Standard_Integer myIncrement;
184 Standard_Integer myLength;
185 Standard_Integer myCapacity;
186 Standard_Integer myNBlocks;
188 initMemBlocks_t myInitBlocks;
192 friend class Iterator;
196 #if defined(_MSC_VER)
200 #endif // NCollection_BaseVector_HeaderFile