Commit | Line | Data |
---|---|---|
b311480e | 1 | // Created on: 2002-04-24 |
2 | // Created by: Alexander GRIGORIEV | |
3 | // Copyright (c) 2002-2012 OPEN CASCADE SAS | |
4 | // | |
5 | // The content of this file is subject to the Open CASCADE Technology Public | |
6 | // License Version 6.5 (the "License"). You may not use the content of this file | |
7 | // except in compliance with the License. Please obtain a copy of the License | |
8 | // at http://www.opencascade.org and read it completely before using this file. | |
9 | // | |
10 | // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its | |
11 | // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France. | |
12 | // | |
13 | // The Original Code and all software distributed under the License is | |
14 | // distributed on an "AS IS" basis, without warranty of any kind, and the | |
15 | // Initial Developer hereby disclaims all such warranties, including without | |
16 | // limitation, any warranties of merchantability, fitness for a particular | |
17 | // purpose or non-infringement. Please see the License for the specific terms | |
18 | // and conditions governing the rights and limitations under the License. | |
19 | ||
7fd59977 | 20 | |
21 | ||
22 | #ifndef NCollection_BaseVector_HeaderFile | |
23 | #define NCollection_BaseVector_HeaderFile | |
24 | ||
25 | #include <Standard_TypeDef.hxx> | |
23be7421 | 26 | #include <NCollection_BaseAllocator.hxx> |
7fd59977 | 27 | #include <stddef.h> |
28 | ||
29 | #if !defined No_Exception && !defined No_Standard_OutOfRange | |
30 | #include <Standard_OutOfRange.hxx> | |
31 | #endif | |
32 | ||
33 | #ifdef WNT | |
34 | #pragma warning(push, 1) | |
35 | #pragma warning(disable:4355) | |
36 | #endif | |
37 | ||
23be7421 M |
38 | // this value defines the number of blocks that are reserved |
39 | // when the capacity of vector is increased | |
40 | inline Standard_Integer GetCapacity (const Standard_Integer theIncrement) | |
41 | { | |
42 | return Max(theIncrement/8, 1); | |
43 | } | |
44 | ||
7fd59977 | 45 | /** |
46 | * Class NCollection_BaseVector - base for generic vector | |
47 | */ | |
48 | class NCollection_BaseVector | |
49 | { | |
50 | public: | |
51 | // ------------ Class MemBlock ------------ | |
52 | class MemBlock { | |
53 | protected: | |
23be7421 M |
54 | MemBlock (NCollection_BaseAllocator* theAlloc) |
55 | : myAlloc(theAlloc), | |
56 | myFirstInd(0), myLength(0), mySize(0), myData(0L) {} | |
7fd59977 | 57 | MemBlock (const Standard_Integer theFirstInd, |
23be7421 M |
58 | const Standard_Integer theLength, |
59 | NCollection_BaseAllocator* theAlloc) | |
60 | : myAlloc(theAlloc), | |
61 | myFirstInd(theFirstInd), myLength(0), mySize(theLength), myData(0L) {} | |
7fd59977 | 62 | virtual void Reinit (const Standard_Integer, |
0f633a22 | 63 | const Standard_Integer) {} |
7fd59977 | 64 | Standard_Integer FirstIndex () const { return myFirstInd; } |
0f633a22 | 65 | Standard_Integer Size () const { return mySize; } |
7fd59977 | 66 | public: |
23be7421 | 67 | virtual ~MemBlock () {} |
0f633a22 | 68 | void SetLength (const Standard_Integer theLen) |
7fd59977 | 69 | { myLength = theLen; } |
0f633a22 | 70 | Standard_Integer Length () const { return myLength; } |
7fd59977 | 71 | void * Find (const Standard_Integer theInd, |
72 | const size_t theSize) const | |
73 | { return ((char *) myData)+theInd*theSize;} | |
74 | Standard_EXPORT Standard_Integer | |
75 | GetIndexV (void * theItem, const size_t theSz) const; | |
76 | protected: | |
77 | Standard_Integer myFirstInd; | |
0f633a22 D |
78 | Standard_Integer myLength; |
79 | Standard_Integer mySize; | |
23be7421 | 80 | NCollection_BaseAllocator * myAlloc; |
7fd59977 | 81 | void * myData; |
82 | friend class NCollection_BaseVector; | |
83 | }; | |
84 | ||
85 | class Iterator { | |
86 | protected: | |
87 | Iterator () : | |
88 | myICurBlock (0), myIEndBlock (0), myCurIndex (0), myEndIndex (0) {} | |
89 | Iterator (const NCollection_BaseVector& theVector) { InitV (theVector); } | |
90 | Iterator (const Iterator& theVector) { CopyV (theVector); } | |
91 | Standard_EXPORT void InitV (const NCollection_BaseVector& theVector); | |
92 | Standard_EXPORT void CopyV (const Iterator&); | |
93 | Standard_Boolean MoreV () const | |
94 | { return (myICurBlock < myIEndBlock || myCurIndex < myEndIndex); } | |
95 | void NextV () | |
96 | { if (++myCurIndex >= myVector -> myData[myICurBlock].Length() && | |
97 | myICurBlock < myIEndBlock) | |
98 | { ++myICurBlock; myCurIndex = 0; } } | |
99 | const MemBlock * CurBlockV () const | |
100 | { return &myVector -> myData[myICurBlock]; } | |
101 | ||
102 | const NCollection_BaseVector * myVector; // the Master vector | |
0f633a22 D |
103 | Standard_Integer myICurBlock; // # of the current block |
104 | Standard_Integer myIEndBlock; | |
105 | Standard_Integer myCurIndex; // Index in the current block | |
106 | Standard_Integer myEndIndex; | |
7fd59977 | 107 | }; |
108 | ||
109 | protected: | |
110 | // ------------ Block initializer --------- | |
111 | typedef MemBlock * (* FuncPtrDataInit) (const NCollection_BaseVector&, | |
112 | const Standard_Integer aCapacity, | |
113 | const void * aSource, | |
114 | const Standard_Integer aSize); | |
115 | typedef void (* FuncPtrDataFree) (const NCollection_BaseVector&, | |
116 | MemBlock *); | |
117 | ||
118 | friend class Iterator; | |
119 | ||
120 | // ---------- PROTECTED METHODS ---------- | |
121 | ||
122 | //! Empty constructor | |
123 | NCollection_BaseVector (const size_t theSize, | |
124 | const Standard_Integer theInc, | |
125 | FuncPtrDataInit theDataInit, | |
23be7421 | 126 | FuncPtrDataFree theDataFree) |
7fd59977 | 127 | : myItemSize (theSize), |
128 | myIncrement (theInc), | |
129 | myLength (0), | |
23be7421 | 130 | myCapacity (GetCapacity(myIncrement)), |
7fd59977 | 131 | myNBlocks (0), |
132 | myData (theDataInit (* this, myCapacity, NULL, 0)), | |
133 | myDataInit (theDataInit), | |
134 | myDataFree (theDataFree) | |
135 | { | |
136 | // myData = (MemBlock *) new char [myCapacity * sizeof(MemBlock)]; | |
137 | // for (Standard_Integer i = 0; i < myCapacity; i++) | |
138 | // new (&myData[i]) MemBlock; | |
139 | } | |
140 | ||
141 | //! Copy constructor | |
142 | NCollection_BaseVector (const NCollection_BaseVector& theOther, | |
143 | FuncPtrDataInit theDataInit, | |
23be7421 | 144 | FuncPtrDataFree theDataFree) |
7fd59977 | 145 | : myItemSize (theOther.myItemSize), |
146 | myIncrement (theOther.myIncrement), | |
147 | myLength (theOther.Length()), | |
23be7421 | 148 | myCapacity (GetCapacity(myIncrement)+theOther.Length()/theOther.myIncrement), |
7fd59977 | 149 | myNBlocks (1 + (theOther.Length() - 1)/theOther.myIncrement), |
150 | myData (theDataInit (* this, myCapacity, NULL, 0)), | |
151 | myDataInit (theDataInit), | |
152 | myDataFree (theDataFree) {} | |
153 | ||
154 | //! Destructor | |
155 | Standard_EXPORT ~NCollection_BaseVector (); | |
156 | ||
157 | //! Operator = | |
158 | Standard_EXPORT NCollection_BaseVector& operator = | |
159 | (const NCollection_BaseVector&); | |
160 | ||
161 | //! ExpandV: returns pointer to memory where to put the new item | |
162 | Standard_EXPORT void * ExpandV (const Standard_Integer theIndex); | |
163 | ||
164 | //! Find: locate the memory holding the desired value | |
165 | inline void * Find (const Standard_Integer theIndex) const; | |
166 | ||
167 | public: | |
168 | //! Total number of items | |
169 | Standard_Integer Length () const { return myLength; } | |
170 | ||
171 | //! Empty the vector of its objects | |
172 | Standard_EXPORT void Clear (); | |
173 | ||
174 | protected: | |
175 | // ---------- PRIVATE FIELDS ---------- | |
176 | size_t myItemSize; | |
177 | Standard_Integer myIncrement; | |
178 | Standard_Integer myLength; | |
179 | Standard_Integer myCapacity; | |
180 | Standard_Integer myNBlocks; | |
181 | MemBlock * myData; | |
182 | FuncPtrDataInit myDataInit; | |
183 | FuncPtrDataFree myDataFree; | |
184 | }; | |
185 | ||
186 | //======================================================================= | |
187 | //function : Find | |
188 | //purpose : locate the memory holding the desired value | |
189 | //======================================================================= | |
190 | ||
191 | inline void * NCollection_BaseVector::Find | |
192 | (const Standard_Integer theIndex) const | |
193 | { | |
194 | #if !defined No_Exception && !defined No_Standard_OutOfRange | |
195 | if (theIndex < 0 || theIndex >= myLength) | |
196 | Standard_OutOfRange::Raise ("NCollection_BaseVector::Find"); | |
197 | #endif | |
198 | const Standard_Integer aBlock = theIndex / myIncrement; | |
199 | return myData[aBlock].Find (theIndex - aBlock * myIncrement, myItemSize); | |
200 | } | |
201 | ||
202 | #ifdef WNT | |
203 | #pragma warning(pop) | |
204 | #endif | |
205 | ||
206 | #endif |