1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
|
// File: NCollection_Vector.hxx
// Created: 23.04.02 19:24:33
// Author: Alexander GRIGORIEV
// Copyright: Open Cascade 2002
#ifndef NCollection_Vector_HeaderFile
#define NCollection_Vector_HeaderFile
#include <NCollection_BaseVector.hxx>
#include <NCollection_BaseCollection.hxx>
#if !defined No_Exception && !defined No_Standard_OutOfRange
#include <Standard_OutOfRange.hxx>
#endif
#ifdef WNT
// Disable the warning: "operator new unmatched by delete"
#pragma warning (push)
#pragma warning (disable:4291)
#endif
/**
* Class NCollection_Vector (dynamic array of objects)
*
* This class is similar to NCollection_Array1 though the indices always start
* at 0 (in Array1 the first index must be specified)
*
* The Vector is always created with 0 length. It can be enlarged by two means:
* 1. Calling the method Append (val) - then "val" is added to the end of the
* vector (the vector length is incremented)
* 2. Calling the method SetValue (i, val) - if "i" is greater than or equal
* to the current length of the vector, the vector is enlarged to accomo-
* date this index
*
* The methods Append and SetValue return a non-const reference to the copied
* object inside the vector. This reference is guaranteed to be valid until
* the vector is destroyed. It can be used to access the vector member directly
* or to pass its address to other data structures.
*
* The vector iterator remembers the length of the vector at the moment of the
* creation or initialisation of the iterator. Therefore the iteration begins
* at index 0 and stops at the index equal to (remembered_length-1). It is OK
* to enlarge the vector during the iteration.
*/
template <class TheItemType> class NCollection_Vector
: public NCollection_BaseCollection<TheItemType>,
public NCollection_BaseVector
{
public:
typedef TheItemType TheItemTypeD;
// ----------------------------------------------------------------------
//! Nested class MemBlock
class MemBlock : public NCollection_BaseVector::MemBlock
{
public:
void * operator new (size_t, void * theAddress) { return theAddress; }
//! Empty constructor
MemBlock (NCollection_BaseAllocator* theAlloc)
: NCollection_BaseVector::MemBlock(0,0,theAlloc)
{}
//! Constructor
MemBlock (const Standard_Integer theFirstInd,
const Standard_Integer theSize,
NCollection_BaseAllocator* theAlloc)
: NCollection_BaseVector::MemBlock (theFirstInd, theSize, theAlloc)
{
myData = myAlloc->Allocate(theSize * sizeof(TheItemType));
for (size_t i=0; i < theSize; i++)
new (&((TheItemType *) myData)[i]) TheItemType;
}
//! Copy constructor
MemBlock (const MemBlock& theOther)
: NCollection_BaseVector::MemBlock (theOther.FirstIndex(),theOther.Size(),
theOther.myAlloc)
{
myLength = theOther.Length();
myData = myAlloc->Allocate(Size() * sizeof(TheItemType));
size_t i;
for (i=0; i < Length(); i++)
new (&((TheItemType *) myData)[i]) TheItemType(theOther.Value(i));
for (; i < Size(); i++)
new (&((TheItemType *) myData)[i]) TheItemType;
}
//! Reinit
virtual void Reinit (const Standard_Integer theFirst,
const size_t theSize)
{
if (myData) {
for (size_t i=0; i < mySize; i++)
#ifdef __BORLANDC__
((TheItemType *) myData)[i].~TheItemType();
#else
((TheItemType *) myData)[i].~TheItemTypeD();
#endif
myAlloc->Free(myData);
myData = NULL;
}
if (theSize > 0) {
myData = myAlloc->Allocate(theSize * sizeof(TheItemType));
for (size_t i=0; i < theSize; i++)
new (&((TheItemType *) myData)[i]) TheItemType;
}
myFirstInd = theFirst;
mySize = theSize;
myLength = 0;
}
//! Destructor
virtual ~MemBlock ()
{
if (myData) {
for (size_t i=0; i < Size(); i++)
#ifdef __BORLANDC__
((TheItemType *) myData)[i].~TheItemType();
#else
((TheItemType *) myData)[i].~TheItemTypeD();
#endif
myAlloc->Free(myData);
myData = NULL;
}
}
//! Operator () const
const TheItemType& Value (const Standard_Integer theIndex) const
{ return ((TheItemType *) myData) [theIndex]; }
//! Operator ()
TheItemType& ChangeValue (const Standard_Integer theIndex)
{ return ((TheItemType *) myData) [theIndex]; }
//! GetIndex
Standard_Integer GetIndex (const TheItemType& theItem) const {
return GetIndexV ((void *)&theItem, sizeof(TheItemType));
}
}; // End of the nested class MemBlock
// ----------------------------------------------------------------------
// ------------------------ Nested class Iterator -----------------------
// ----------------------------------------------------------------------
class Iterator : public NCollection_BaseCollection<TheItemType>::Iterator,
public NCollection_BaseVector::Iterator
{
public:
//! Empty constructor - for later Init
Iterator (void) {}
//! Constructor with initialisation
Iterator (const NCollection_Vector& theVector) :
NCollection_BaseVector::Iterator (theVector) {}
//! Copy constructor
Iterator (const Iterator& theOther) :
NCollection_BaseVector::Iterator (theOther) {}
//! Initialisation
void Init (const NCollection_Vector& theVector) { InitV (theVector); }
//! Assignment
Iterator& operator = (const Iterator& theOther) {
CopyV (theOther);
return * this;
}
//! Check end
virtual Standard_Boolean More (void) const { return MoreV (); }
//! Make step
virtual void Next (void) { NextV(); }
//! Constant value access
virtual const TheItemType& Value (void) const {
return ((const MemBlock *) CurBlockV()) -> Value(myCurIndex); }
//! Variable value access
virtual TheItemType& ChangeValue (void) const {
return ((MemBlock *) CurBlockV()) -> ChangeValue(myCurIndex); }
//! Operator new for allocating iterators
void* operator new(size_t theSize,
const Handle(NCollection_BaseAllocator)& theAllocator)
{ return theAllocator->Allocate(theSize); }
}; // End of the nested class Iterator
// ----------------------------------------------------------------------
// ------------------------ Class Vector itself -------------------------
// ----------------------------------------------------------------------
public:
// ---------- PUBLIC METHODS ----------
//! Constructor
NCollection_Vector (const Standard_Integer theIncrement = 256,
const Handle_NCollection_BaseAllocator& theAlloc = 0L)
: NCollection_BaseCollection<TheItemType>(theAlloc),
NCollection_BaseVector (sizeof(TheItemType), theIncrement,
FuncDataInit, FuncDataFree) {}
//! Copy constructor
NCollection_Vector (const NCollection_Vector& theOther)
: NCollection_BaseCollection<TheItemType>(theOther.myAllocator),
NCollection_BaseVector (theOther, FuncDataInit, FuncDataFree)
{ CopyData (theOther); }
//! Operator =
NCollection_Vector& operator = (const NCollection_Vector& theOther) {
if (this != &theOther) {
this->myAllocator = theOther.myAllocator;
NCollection_BaseVector::operator = (theOther);
CopyData (theOther);
}
return * this;
}
//! Total number of items in the vector
virtual Standard_Integer Size () const { return Length(); }
//! Virtual assignment (any collection to this array)
virtual void Assign (const NCollection_BaseCollection<TheItemType>& theOther)
{
if (this != &theOther) {
TYPENAME NCollection_BaseCollection<TheItemType>::Iterator& anIter2 =
theOther.CreateIterator();
while (anIter2.More()) {
Append (anIter2.Value());
anIter2.Next();
}
}
}
//! Assignment to the collection of the same type
void Assign (const NCollection_Vector& theOther)
{
if (this != &theOther) {
NCollection_BaseVector::operator = (theOther);
CopyData (theOther);
}
}
//! Method to create iterators for base collections
virtual TYPENAME NCollection_BaseCollection<TheItemType>::Iterator&
CreateIterator(void) const
{ return *(new (this->IterAllocator()) Iterator(*this)); }
//! Append
TheItemType& Append (const TheItemType& theValue) {
TheItemType& anAppended = * (TheItemType *) ExpandV (myLength);
anAppended = theValue;
return anAppended;
}
//! Operator () - query the const value
const TheItemType& operator () (const Standard_Integer theIndex) const
{ return Value (theIndex); }
const TheItemType& Value (const Standard_Integer theIndex) const {
// if (myNBlocks == 1) return ((MemBlock *) myData) -> Value(theIndex);
return * (const TheItemType *) Find (theIndex);
}
//! Operator () - query the value
TheItemType& operator () (const Standard_Integer theIndex)
{ return ChangeValue (theIndex); }
TheItemType& ChangeValue (const Standard_Integer theIndex) {
// if (myNBlocks == 1) return ((MemBlock *) myData) -> ChangeValue(theIndex);
return * (TheItemType *) Find (theIndex);
}
//! SetValue () - set or append a value
TheItemType& SetValue (const Standard_Integer theIndex,
const TheItemType& theValue) {
#if !defined No_Exception && !defined No_Standard_OutOfRange
if (theIndex < 0)
Standard_OutOfRange::Raise ("NCollection_Vector::SetValue");
#endif
TheItemType * const aVecValue =
(TheItemType *)(theIndex<myLength? Find(theIndex): ExpandV(theIndex));
* aVecValue = theValue;
return * aVecValue;
}
private:
// ---------- PRIVATE METHODS ----------
void CopyData (const NCollection_Vector& theOther) {
Standard_Integer i, iBlock = 0;
/*NCollection_Vector::*/Iterator anIter (theOther);
for (int aLength = 0; aLength < myLength; aLength += myIncrement) {
MemBlock& aBlock = (MemBlock&) myData[iBlock];
aBlock.Reinit (aLength, myIncrement);
for (i = 0; i < myIncrement; i++) {
if (!anIter.More()) break;
aBlock.ChangeValue(i) = anIter.Value();
anIter.Next();
}
aBlock.SetLength(i);
iBlock++;
}
}
static NCollection_BaseVector::MemBlock * FuncDataInit
(const NCollection_BaseVector& theVector,
const Standard_Integer aCapacity,
const void * aSource,
const Standard_Integer aSize)
{
const NCollection_Vector& aSelf =
static_cast<const NCollection_Vector&> (theVector);
MemBlock * aData =
(MemBlock *) aSelf.myAllocator->Allocate(aCapacity * sizeof(MemBlock));
Standard_Integer i = 0;
if (aSource != NULL) {
memcpy (aData, aSource, aSize * sizeof(MemBlock));
i = aSize;
}
while (i < aCapacity)
new (&aData[i++]) MemBlock(aSelf.myAllocator.operator->());
return aData;
}
static void FuncDataFree (const NCollection_BaseVector& theVector,
NCollection_BaseVector::MemBlock * aData)
{
const NCollection_Vector& aSelf =
static_cast<const NCollection_Vector&> (theVector);
aSelf.myAllocator->Free(aData);
}
friend class Iterator;
};
#ifdef WNT
#pragma warning (pop)
#endif
#endif
|