Commit | Line | Data |
---|---|---|
7fd59977 | 1 | // File: NCollection_BaseVector.hxx |
2 | // Created: 24.04.02 09:41:39 | |
3 | // Author: Alexander GRIGORIEV | |
4 | // Copyright: Open Cascade 2002 | |
5 | ||
6 | ||
7 | #ifndef NCollection_BaseVector_HeaderFile | |
8 | #define NCollection_BaseVector_HeaderFile | |
9 | ||
10 | #include <Standard_TypeDef.hxx> | |
23be7421 | 11 | #include <NCollection_BaseAllocator.hxx> |
7fd59977 | 12 | #include <stddef.h> |
13 | ||
14 | #if !defined No_Exception && !defined No_Standard_OutOfRange | |
15 | #include <Standard_OutOfRange.hxx> | |
16 | #endif | |
17 | ||
18 | #ifdef WNT | |
19 | #pragma warning(push, 1) | |
20 | #pragma warning(disable:4355) | |
21 | #endif | |
22 | ||
23be7421 M |
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) | |
26 | { | |
27 | return Max(theIncrement/8, 1); | |
28 | } | |
29 | ||
7fd59977 | 30 | /** |
31 | * Class NCollection_BaseVector - base for generic vector | |
32 | */ | |
33 | class NCollection_BaseVector | |
34 | { | |
35 | public: | |
36 | // ------------ Class MemBlock ------------ | |
37 | class MemBlock { | |
38 | protected: | |
23be7421 M |
39 | MemBlock (NCollection_BaseAllocator* theAlloc) |
40 | : myAlloc(theAlloc), | |
41 | myFirstInd(0), myLength(0), mySize(0), myData(0L) {} | |
7fd59977 | 42 | MemBlock (const Standard_Integer theFirstInd, |
23be7421 M |
43 | const Standard_Integer theLength, |
44 | NCollection_BaseAllocator* theAlloc) | |
45 | : myAlloc(theAlloc), | |
46 | myFirstInd(theFirstInd), myLength(0), mySize(theLength), myData(0L) {} | |
7fd59977 | 47 | virtual void Reinit (const Standard_Integer, |
48 | const size_t) {} | |
49 | Standard_Integer FirstIndex () const { return myFirstInd; } | |
50 | size_t Size () const { return mySize; } | |
51 | public: | |
23be7421 | 52 | virtual ~MemBlock () {} |
7fd59977 | 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; | |
61 | protected: | |
62 | Standard_Integer myFirstInd; | |
63 | size_t myLength; | |
64 | size_t mySize; | |
23be7421 | 65 | NCollection_BaseAllocator * myAlloc; |
7fd59977 | 66 | void * myData; |
67 | friend class NCollection_BaseVector; | |
68 | }; | |
69 | ||
70 | class Iterator { | |
71 | protected: | |
72 | Iterator () : | |
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); } | |
80 | void NextV () | |
81 | { if (++myCurIndex >= myVector -> myData[myICurBlock].Length() && | |
82 | myICurBlock < myIEndBlock) | |
83 | { ++myICurBlock; myCurIndex = 0; } } | |
84 | const MemBlock * CurBlockV () const | |
85 | { return &myVector -> myData[myICurBlock]; } | |
86 | ||
87 | const NCollection_BaseVector * myVector; // the Master vector | |
88 | size_t myICurBlock; // # of the current block | |
89 | size_t myIEndBlock; | |
90 | size_t myCurIndex; // Index in the current block | |
91 | size_t myEndIndex; | |
92 | }; | |
93 | ||
94 | protected: | |
95 | // ------------ Block initializer --------- | |
96 | typedef MemBlock * (* FuncPtrDataInit) (const NCollection_BaseVector&, | |
97 | const Standard_Integer aCapacity, | |
98 | const void * aSource, | |
99 | const Standard_Integer aSize); | |
100 | typedef void (* FuncPtrDataFree) (const NCollection_BaseVector&, | |
101 | MemBlock *); | |
102 | ||
103 | friend class Iterator; | |
104 | ||
105 | // ---------- PROTECTED METHODS ---------- | |
106 | ||
107 | //! Empty constructor | |
108 | NCollection_BaseVector (const size_t theSize, | |
109 | const Standard_Integer theInc, | |
110 | FuncPtrDataInit theDataInit, | |
23be7421 | 111 | FuncPtrDataFree theDataFree) |
7fd59977 | 112 | : myItemSize (theSize), |
113 | myIncrement (theInc), | |
114 | myLength (0), | |
23be7421 | 115 | myCapacity (GetCapacity(myIncrement)), |
7fd59977 | 116 | myNBlocks (0), |
117 | myData (theDataInit (* this, myCapacity, NULL, 0)), | |
118 | myDataInit (theDataInit), | |
119 | myDataFree (theDataFree) | |
120 | { | |
121 | // myData = (MemBlock *) new char [myCapacity * sizeof(MemBlock)]; | |
122 | // for (Standard_Integer i = 0; i < myCapacity; i++) | |
123 | // new (&myData[i]) MemBlock; | |
124 | } | |
125 | ||
126 | //! Copy constructor | |
127 | NCollection_BaseVector (const NCollection_BaseVector& theOther, | |
128 | FuncPtrDataInit theDataInit, | |
23be7421 | 129 | FuncPtrDataFree theDataFree) |
7fd59977 | 130 | : myItemSize (theOther.myItemSize), |
131 | myIncrement (theOther.myIncrement), | |
132 | myLength (theOther.Length()), | |
23be7421 | 133 | myCapacity (GetCapacity(myIncrement)+theOther.Length()/theOther.myIncrement), |
7fd59977 | 134 | myNBlocks (1 + (theOther.Length() - 1)/theOther.myIncrement), |
135 | myData (theDataInit (* this, myCapacity, NULL, 0)), | |
136 | myDataInit (theDataInit), | |
137 | myDataFree (theDataFree) {} | |
138 | ||
139 | //! Destructor | |
140 | Standard_EXPORT ~NCollection_BaseVector (); | |
141 | ||
142 | //! Operator = | |
143 | Standard_EXPORT NCollection_BaseVector& operator = | |
144 | (const NCollection_BaseVector&); | |
145 | ||
146 | //! ExpandV: returns pointer to memory where to put the new item | |
147 | Standard_EXPORT void * ExpandV (const Standard_Integer theIndex); | |
148 | ||
149 | //! Find: locate the memory holding the desired value | |
150 | inline void * Find (const Standard_Integer theIndex) const; | |
151 | ||
152 | public: | |
153 | //! Total number of items | |
154 | Standard_Integer Length () const { return myLength; } | |
155 | ||
156 | //! Empty the vector of its objects | |
157 | Standard_EXPORT void Clear (); | |
158 | ||
159 | protected: | |
160 | // ---------- PRIVATE FIELDS ---------- | |
161 | size_t myItemSize; | |
162 | Standard_Integer myIncrement; | |
163 | Standard_Integer myLength; | |
164 | Standard_Integer myCapacity; | |
165 | Standard_Integer myNBlocks; | |
166 | MemBlock * myData; | |
167 | FuncPtrDataInit myDataInit; | |
168 | FuncPtrDataFree myDataFree; | |
169 | }; | |
170 | ||
171 | //======================================================================= | |
172 | //function : Find | |
173 | //purpose : locate the memory holding the desired value | |
174 | //======================================================================= | |
175 | ||
176 | inline void * NCollection_BaseVector::Find | |
177 | (const Standard_Integer theIndex) const | |
178 | { | |
179 | #if !defined No_Exception && !defined No_Standard_OutOfRange | |
180 | if (theIndex < 0 || theIndex >= myLength) | |
181 | Standard_OutOfRange::Raise ("NCollection_BaseVector::Find"); | |
182 | #endif | |
183 | const Standard_Integer aBlock = theIndex / myIncrement; | |
184 | return myData[aBlock].Find (theIndex - aBlock * myIncrement, myItemSize); | |
185 | } | |
186 | ||
187 | #ifdef WNT | |
188 | #pragma warning(pop) | |
189 | #endif | |
190 | ||
191 | #endif |