1 // Created on: 2002-04-24
2 // Created by: Alexander GRIGORIEV
3 // Copyright (c) 2002-2012 OPEN CASCADE SAS
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.
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.
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.
22 #ifndef NCollection_BaseVector_HeaderFile
23 #define NCollection_BaseVector_HeaderFile
25 #include <Standard_TypeDef.hxx>
26 #include <NCollection_BaseAllocator.hxx>
29 #if !defined No_Exception && !defined No_Standard_OutOfRange
30 #include <Standard_OutOfRange.hxx>
34 #pragma warning(push, 1)
35 #pragma warning(disable:4355)
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)
42 return Max(theIncrement/8, 1);
46 * Class NCollection_BaseVector - base for generic vector
48 class NCollection_BaseVector
51 // ------------ Class MemBlock ------------
54 MemBlock (NCollection_BaseAllocator* theAlloc)
60 MemBlock (const Standard_Integer theFirstInd,
61 const Standard_Integer theLength,
62 NCollection_BaseAllocator* theAlloc)
65 myFirstInd(theFirstInd),
68 virtual void Reinit (const Standard_Integer,
69 const Standard_Integer) {}
70 Standard_Integer FirstIndex () const { return myFirstInd; }
71 Standard_Integer Size () const { return mySize; }
73 virtual ~MemBlock () {}
74 void SetLength (const Standard_Integer theLen)
75 { myLength = theLen; }
76 Standard_Integer Length () const { return myLength; }
77 void * Find (const Standard_Integer theInd,
78 const size_t theSize) const
79 { return ((char *) myData)+theInd*theSize;}
80 Standard_EXPORT Standard_Integer
81 GetIndexV (void * theItem, const size_t theSz) const;
83 NCollection_BaseAllocator* myAlloc;
85 Standard_Integer myFirstInd;
86 Standard_Integer myLength;
87 Standard_Integer mySize;
88 friend class NCollection_BaseVector;
94 myICurBlock (0), myIEndBlock (0), myCurIndex (0), myEndIndex (0) {}
95 Iterator (const NCollection_BaseVector& theVector) { InitV (theVector); }
96 Iterator (const Iterator& theVector) { CopyV (theVector); }
97 Standard_EXPORT void InitV (const NCollection_BaseVector& theVector);
98 Standard_EXPORT void CopyV (const Iterator&);
99 Standard_Boolean MoreV () const
100 { return (myICurBlock < myIEndBlock || myCurIndex < myEndIndex); }
102 { if (++myCurIndex >= myVector -> myData[myICurBlock].Length() &&
103 myICurBlock < myIEndBlock)
104 { ++myICurBlock; myCurIndex = 0; } }
105 const MemBlock * CurBlockV () const
106 { return &myVector -> myData[myICurBlock]; }
108 const NCollection_BaseVector * myVector; // the Master vector
109 Standard_Integer myICurBlock; // # of the current block
110 Standard_Integer myIEndBlock;
111 Standard_Integer myCurIndex; // Index in the current block
112 Standard_Integer myEndIndex;
116 // ------------ Block initializer ---------
117 typedef MemBlock * (* FuncPtrDataInit) (const NCollection_BaseVector&,
118 const Standard_Integer aCapacity,
119 const void * aSource,
120 const Standard_Integer aSize);
121 typedef void (* FuncPtrDataFree) (const NCollection_BaseVector&,
124 friend class Iterator;
126 // ---------- PROTECTED METHODS ----------
128 //! Empty constructor
129 NCollection_BaseVector (const size_t theSize,
130 const Standard_Integer theInc,
131 FuncPtrDataInit theDataInit,
132 FuncPtrDataFree theDataFree)
133 : myItemSize (theSize),
134 myIncrement (theInc),
136 myCapacity (GetCapacity(myIncrement)),
138 myData (theDataInit (* this, myCapacity, NULL, 0)),
139 myDataInit (theDataInit),
140 myDataFree (theDataFree)
142 // myData = (MemBlock *) new char [myCapacity * sizeof(MemBlock)];
143 // for (Standard_Integer i = 0; i < myCapacity; i++)
144 // new (&myData[i]) MemBlock;
148 NCollection_BaseVector (const NCollection_BaseVector& theOther,
149 FuncPtrDataInit theDataInit,
150 FuncPtrDataFree theDataFree)
151 : myItemSize (theOther.myItemSize),
152 myIncrement (theOther.myIncrement),
153 myLength (theOther.Length()),
154 myCapacity (GetCapacity(myIncrement)+theOther.Length()/theOther.myIncrement),
155 myNBlocks (1 + (theOther.Length() - 1)/theOther.myIncrement),
156 myData (theDataInit (* this, myCapacity, NULL, 0)),
157 myDataInit (theDataInit),
158 myDataFree (theDataFree) {}
161 Standard_EXPORT ~NCollection_BaseVector ();
164 Standard_EXPORT NCollection_BaseVector& operator =
165 (const NCollection_BaseVector&);
167 //! ExpandV: returns pointer to memory where to put the new item
168 Standard_EXPORT void * ExpandV (const Standard_Integer theIndex);
170 //! Find: locate the memory holding the desired value
171 inline void * Find (const Standard_Integer theIndex) const;
174 //! Total number of items
175 Standard_Integer Length () const { return myLength; }
177 //! Empty the vector of its objects
178 Standard_EXPORT void Clear ();
181 // ---------- PRIVATE FIELDS ----------
183 Standard_Integer myIncrement;
184 Standard_Integer myLength;
185 Standard_Integer myCapacity;
186 Standard_Integer myNBlocks;
188 FuncPtrDataInit myDataInit;
189 FuncPtrDataFree myDataFree;
192 //=======================================================================
194 //purpose : locate the memory holding the desired value
195 //=======================================================================
197 inline void * NCollection_BaseVector::Find
198 (const Standard_Integer theIndex) const
200 #if !defined No_Exception && !defined No_Standard_OutOfRange
201 if (theIndex < 0 || theIndex >= myLength)
202 Standard_OutOfRange::Raise ("NCollection_BaseVector::Find");
204 const Standard_Integer aBlock = theIndex / myIncrement;
205 return myData[aBlock].Find (theIndex - aBlock * myIncrement, myItemSize);