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)
56 myFirstInd(0), myLength(0), mySize(0), myData(0L) {}
57 MemBlock (const Standard_Integer theFirstInd,
58 const Standard_Integer theLength,
59 NCollection_BaseAllocator* theAlloc)
61 myFirstInd(theFirstInd), myLength(0), mySize(theLength), myData(0L) {}
62 virtual void Reinit (const Standard_Integer,
63 const Standard_Integer) {}
64 Standard_Integer FirstIndex () const { return myFirstInd; }
65 Standard_Integer Size () const { return mySize; }
67 virtual ~MemBlock () {}
68 void SetLength (const Standard_Integer theLen)
69 { myLength = theLen; }
70 Standard_Integer Length () const { return myLength; }
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;
77 Standard_Integer myFirstInd;
78 Standard_Integer myLength;
79 Standard_Integer mySize;
80 NCollection_BaseAllocator * myAlloc;
82 friend class NCollection_BaseVector;
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); }
96 { if (++myCurIndex >= myVector -> myData[myICurBlock].Length() &&
97 myICurBlock < myIEndBlock)
98 { ++myICurBlock; myCurIndex = 0; } }
99 const MemBlock * CurBlockV () const
100 { return &myVector -> myData[myICurBlock]; }
102 const NCollection_BaseVector * myVector; // the Master vector
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;
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&,
118 friend class Iterator;
120 // ---------- PROTECTED METHODS ----------
122 //! Empty constructor
123 NCollection_BaseVector (const size_t theSize,
124 const Standard_Integer theInc,
125 FuncPtrDataInit theDataInit,
126 FuncPtrDataFree theDataFree)
127 : myItemSize (theSize),
128 myIncrement (theInc),
130 myCapacity (GetCapacity(myIncrement)),
132 myData (theDataInit (* this, myCapacity, NULL, 0)),
133 myDataInit (theDataInit),
134 myDataFree (theDataFree)
136 // myData = (MemBlock *) new char [myCapacity * sizeof(MemBlock)];
137 // for (Standard_Integer i = 0; i < myCapacity; i++)
138 // new (&myData[i]) MemBlock;
142 NCollection_BaseVector (const NCollection_BaseVector& theOther,
143 FuncPtrDataInit theDataInit,
144 FuncPtrDataFree theDataFree)
145 : myItemSize (theOther.myItemSize),
146 myIncrement (theOther.myIncrement),
147 myLength (theOther.Length()),
148 myCapacity (GetCapacity(myIncrement)+theOther.Length()/theOther.myIncrement),
149 myNBlocks (1 + (theOther.Length() - 1)/theOther.myIncrement),
150 myData (theDataInit (* this, myCapacity, NULL, 0)),
151 myDataInit (theDataInit),
152 myDataFree (theDataFree) {}
155 Standard_EXPORT ~NCollection_BaseVector ();
158 Standard_EXPORT NCollection_BaseVector& operator =
159 (const NCollection_BaseVector&);
161 //! ExpandV: returns pointer to memory where to put the new item
162 Standard_EXPORT void * ExpandV (const Standard_Integer theIndex);
164 //! Find: locate the memory holding the desired value
165 inline void * Find (const Standard_Integer theIndex) const;
168 //! Total number of items
169 Standard_Integer Length () const { return myLength; }
171 //! Empty the vector of its objects
172 Standard_EXPORT void Clear ();
175 // ---------- PRIVATE FIELDS ----------
177 Standard_Integer myIncrement;
178 Standard_Integer myLength;
179 Standard_Integer myCapacity;
180 Standard_Integer myNBlocks;
182 FuncPtrDataInit myDataInit;
183 FuncPtrDataFree myDataFree;
186 //=======================================================================
188 //purpose : locate the memory holding the desired value
189 //=======================================================================
191 inline void * NCollection_BaseVector::Find
192 (const Standard_Integer theIndex) const
194 #if !defined No_Exception && !defined No_Standard_OutOfRange
195 if (theIndex < 0 || theIndex >= myLength)
196 Standard_OutOfRange::Raise ("NCollection_BaseVector::Find");
198 const Standard_Integer aBlock = theIndex / myIncrement;
199 return myData[aBlock].Find (theIndex - aBlock * myIncrement, myItemSize);