1 // File: NCollection_BaseVector.hxx
2 // Created: 24.04.02 09:41:39
3 // Author: Alexander GRIGORIEV
4 // Copyright: Open Cascade 2002
7 #ifndef NCollection_BaseVector_HeaderFile
8 #define NCollection_BaseVector_HeaderFile
10 #include <Standard_TypeDef.hxx>
11 #include <NCollection_BaseAllocator.hxx>
14 #if !defined No_Exception && !defined No_Standard_OutOfRange
15 #include <Standard_OutOfRange.hxx>
19 #pragma warning(push, 1)
20 #pragma warning(disable:4355)
23 // this value defines the number of blocks that are reserved
24 // when the capacity of vector is increased
25 inline Standard_Integer GetCapacity (const Standard_Integer theIncrement)
27 return Max(theIncrement/8, 1);
31 * Class NCollection_BaseVector - base for generic vector
33 class NCollection_BaseVector
36 // ------------ Class MemBlock ------------
39 MemBlock (NCollection_BaseAllocator* theAlloc)
41 myFirstInd(0), myLength(0), mySize(0), myData(0L) {}
42 MemBlock (const Standard_Integer theFirstInd,
43 const Standard_Integer theLength,
44 NCollection_BaseAllocator* theAlloc)
46 myFirstInd(theFirstInd), myLength(0), mySize(theLength), myData(0L) {}
47 virtual void Reinit (const Standard_Integer,
49 Standard_Integer FirstIndex () const { return myFirstInd; }
50 size_t Size () const { return mySize; }
52 virtual ~MemBlock () {}
53 void SetLength (const size_t theLen)
54 { myLength = theLen; }
55 size_t Length () const { return myLength; }
56 void * Find (const Standard_Integer theInd,
57 const size_t theSize) const
58 { return ((char *) myData)+theInd*theSize;}
59 Standard_EXPORT Standard_Integer
60 GetIndexV (void * theItem, const size_t theSz) const;
62 Standard_Integer myFirstInd;
65 NCollection_BaseAllocator * myAlloc;
67 friend class NCollection_BaseVector;
73 myICurBlock (0), myIEndBlock (0), myCurIndex (0), myEndIndex (0) {}
74 Iterator (const NCollection_BaseVector& theVector) { InitV (theVector); }
75 Iterator (const Iterator& theVector) { CopyV (theVector); }
76 Standard_EXPORT void InitV (const NCollection_BaseVector& theVector);
77 Standard_EXPORT void CopyV (const Iterator&);
78 Standard_Boolean MoreV () const
79 { return (myICurBlock < myIEndBlock || myCurIndex < myEndIndex); }
81 { if (++myCurIndex >= myVector -> myData[myICurBlock].Length() &&
82 myICurBlock < myIEndBlock)
83 { ++myICurBlock; myCurIndex = 0; } }
84 const MemBlock * CurBlockV () const
85 { return &myVector -> myData[myICurBlock]; }
87 const NCollection_BaseVector * myVector; // the Master vector
88 size_t myICurBlock; // # of the current block
90 size_t myCurIndex; // Index in the current block
95 // ------------ Block initializer ---------
96 typedef MemBlock * (* FuncPtrDataInit) (const NCollection_BaseVector&,
97 const Standard_Integer aCapacity,
99 const Standard_Integer aSize);
100 typedef void (* FuncPtrDataFree) (const NCollection_BaseVector&,
103 friend class Iterator;
105 // ---------- PROTECTED METHODS ----------
107 //! Empty constructor
108 NCollection_BaseVector (const size_t theSize,
109 const Standard_Integer theInc,
110 FuncPtrDataInit theDataInit,
111 FuncPtrDataFree theDataFree)
112 : myItemSize (theSize),
113 myIncrement (theInc),
115 myCapacity (GetCapacity(myIncrement)),
117 myData (theDataInit (* this, myCapacity, NULL, 0)),
118 myDataInit (theDataInit),
119 myDataFree (theDataFree)
121 // myData = (MemBlock *) new char [myCapacity * sizeof(MemBlock)];
122 // for (Standard_Integer i = 0; i < myCapacity; i++)
123 // new (&myData[i]) MemBlock;
127 NCollection_BaseVector (const NCollection_BaseVector& theOther,
128 FuncPtrDataInit theDataInit,
129 FuncPtrDataFree theDataFree)
130 : myItemSize (theOther.myItemSize),
131 myIncrement (theOther.myIncrement),
132 myLength (theOther.Length()),
133 myCapacity (GetCapacity(myIncrement)+theOther.Length()/theOther.myIncrement),
134 myNBlocks (1 + (theOther.Length() - 1)/theOther.myIncrement),
135 myData (theDataInit (* this, myCapacity, NULL, 0)),
136 myDataInit (theDataInit),
137 myDataFree (theDataFree) {}
140 Standard_EXPORT ~NCollection_BaseVector ();
143 Standard_EXPORT NCollection_BaseVector& operator =
144 (const NCollection_BaseVector&);
146 //! ExpandV: returns pointer to memory where to put the new item
147 Standard_EXPORT void * ExpandV (const Standard_Integer theIndex);
149 //! Find: locate the memory holding the desired value
150 inline void * Find (const Standard_Integer theIndex) const;
153 //! Total number of items
154 Standard_Integer Length () const { return myLength; }
156 //! Empty the vector of its objects
157 Standard_EXPORT void Clear ();
160 // ---------- PRIVATE FIELDS ----------
162 Standard_Integer myIncrement;
163 Standard_Integer myLength;
164 Standard_Integer myCapacity;
165 Standard_Integer myNBlocks;
167 FuncPtrDataInit myDataInit;
168 FuncPtrDataFree myDataFree;
171 //=======================================================================
173 //purpose : locate the memory holding the desired value
174 //=======================================================================
176 inline void * NCollection_BaseVector::Find
177 (const Standard_Integer theIndex) const
179 #if !defined No_Exception && !defined No_Standard_OutOfRange
180 if (theIndex < 0 || theIndex >= myLength)
181 Standard_OutOfRange::Raise ("NCollection_BaseVector::Find");
183 const Standard_Integer aBlock = theIndex / myIncrement;
184 return myData[aBlock].Find (theIndex - aBlock * myIncrement, myItemSize);