/usr/include/oce/NCollection_Vector.hxx is in liboce-foundation-dev 0.15-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 | // Created on: 2002-04-23
// Created by: Alexander GRIGORIEV
// Copyright (c) 2002-2013 OPEN CASCADE SAS
//
// This file is part of Open CASCADE Technology software library.
//
// This library is free software; you can redistribute it and / or modify it
// under the terms of the GNU Lesser General Public version 2.1 as published
// by the Free Software Foundation, with special exception defined in the file
// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
// distribution for complete text of the license and disclaimer of any warranty.
//
// Alternatively, this file may be used under the terms of Open CASCADE
// commercial license or contractual agreement.
#ifndef NCollection_Vector_HeaderFile
#define NCollection_Vector_HeaderFile
#include <NCollection_BaseVector.hxx>
#include <NCollection_BaseCollection.hxx>
//! 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 Iterator
class Iterator : public NCollection_BaseCollection<TheItemType>::Iterator,
public NCollection_BaseVector::Iterator
{
public:
//! Empty constructor - for later Init
Iterator() {}
//! 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() const
{
return moreV();
}
//! Make step
virtual void Next()
{
nextV();
}
//! Constant value access
virtual const TheItemType& Value() const
{
return ((const TheItemType* )curBlockV()->DataPtr)[myCurIndex];
}
//! Variable value access
virtual TheItemType& ChangeValue() const
{
return ((TheItemType* )curBlockV()->DataPtr)[myCurIndex];
}
};
public: //! @name public methods
//! Constructor
NCollection_Vector (const Standard_Integer theIncrement = 256,
const Handle_NCollection_BaseAllocator& theAlloc = NULL)
: NCollection_BaseCollection<TheItemType> (theAlloc),
NCollection_BaseVector (NCollection_BaseCollection<TheItemType>::myAllocator, initMemBlocks, sizeof(TheItemType), theIncrement) {}
//! Copy constructor
NCollection_Vector (const NCollection_Vector& theOther)
: NCollection_BaseCollection<TheItemType> (theOther.myAllocator),
NCollection_BaseVector (NCollection_BaseCollection<TheItemType>::myAllocator, initMemBlocks, theOther)
{
copyData (theOther);
}
//! Destructor
~NCollection_Vector()
{
for (Standard_Integer anItemIter = 0; anItemIter < myCapacity; ++anItemIter)
{
initMemBlocks (*this, myData[anItemIter], 0, 0);
}
NCollection_BaseCollection<TheItemType>::myAllocator->Free (myData);
}
//! Operator=
NCollection_Vector& operator= (const NCollection_Vector& theOther)
{
Assign (theOther, Standard_False);
return *this;
}
//! Total number of items
Standard_Integer Length() const
{
return myLength;
}
//! Total number of items in the vector
virtual Standard_Integer Size() const
{
return myLength;
}
//! Method for consistency with other collections.
//! @return Lower bound (inclusive) for iteration.
Standard_Integer Lower() const
{
return 0;
}
//! Method for consistency with other collections.
//! @return Upper bound (inclusive) for iteration.
Standard_Integer Upper() const
{
return myLength - 1;
}
//! Empty query
Standard_Boolean IsEmpty() const
{
return (myLength == 0);
}
//! 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
inline void Assign (const NCollection_Vector& theOther,
const Standard_Boolean theOwnAllocator = Standard_True);
//! Method to create iterators for base collections
virtual TYPENAME NCollection_BaseCollection<TheItemType>::Iterator& CreateIterator() const
{
return *(new (this->IterAllocator()) Iterator(*this));
}
//! Append
TheItemType& Append (const TheItemType& theValue)
{
TheItemType& anAppended = *(TheItemType* )expandV (NCollection_BaseCollection<TheItemType>::myAllocator, 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
{
return *(const TheItemType* )findV (theIndex);
}
//! @return first element
const TheItemType& First() const
{
return *(const TheItemType* )findV (Lower());
}
//! @return first element
TheItemType& ChangeFirst()
{
return *(TheItemType* )findV (Lower());
}
//! @return last element
const TheItemType& Last() const
{
return *(const TheItemType* )findV (Upper());
}
//! @return last element
TheItemType& ChangeLast()
{
return *(TheItemType* )findV (Upper());
}
//! Operator() - query the value
TheItemType& operator() (const Standard_Integer theIndex)
{
return ChangeValue (theIndex);
}
TheItemType& ChangeValue (const Standard_Integer theIndex)
{
return *(TheItemType* )findV (theIndex);
}
//! SetValue () - set or append a value
TheItemType& SetValue (const Standard_Integer theIndex,
const TheItemType& theValue)
{
Standard_OutOfRange_Raise_if (theIndex < 0, "NCollection_Vector::SetValue");
TheItemType* const aVecValue = (TheItemType* )(theIndex < myLength ? findV (theIndex) : expandV (NCollection_BaseCollection<TheItemType>::myAllocator, theIndex));
*aVecValue = theValue;
return *aVecValue;
}
private: //! @name private methods
void copyData (const NCollection_Vector& theOther)
{
Standard_Integer iBlock = 0;
/*NCollection_Vector::*/Iterator anIter (theOther);
for (Standard_Integer aLength = 0; aLength < myLength; aLength += myIncrement)
{
MemBlock& aBlock = myData[iBlock];
initMemBlocks (*this, aBlock, aLength, myIncrement);
Standard_Integer anItemIter = 0;
for (; anItemIter < myIncrement; ++anItemIter)
{
if (!anIter.More())
{
break;
}
((TheItemType* )aBlock.DataPtr)[anItemIter] = anIter.Value();
anIter.Next();
}
aBlock.Length = anItemIter;
iBlock++;
}
}
//! Method to initialize memory block content
static void initMemBlocks (NCollection_BaseVector& theVector,
NCollection_BaseVector::MemBlock& theBlock,
const Standard_Integer theFirst,
const Standard_Integer theSize)
{
NCollection_Vector& aSelf = static_cast<NCollection_Vector&> (theVector);
Handle(NCollection_BaseAllocator)& anAllocator = aSelf.myAllocator;
// release current content
if (theBlock.DataPtr != NULL)
{
for (Standard_Integer anItemIter = 0; anItemIter < theBlock.Size; ++anItemIter)
{
#ifdef __BORLANDC__
((TheItemType* )theBlock.DataPtr)[anItemIter].~TheItemType();
#else
((TheItemType* )theBlock.DataPtr)[anItemIter].~TheItemTypeD();
#endif
}
anAllocator->Free (theBlock.DataPtr);
theBlock.DataPtr = NULL;
}
// allocate new content if requested
if (theSize > 0)
{
theBlock.DataPtr = anAllocator->Allocate (theSize * sizeof(TheItemType));
for (Standard_Integer anItemIter = 0; anItemIter < theSize; ++anItemIter)
{
new (&((TheItemType* )theBlock.DataPtr)[anItemIter]) TheItemType;
}
}
theBlock.FirstIndex = theFirst;
theBlock.Size = theSize;
theBlock.Length = 0;
}
friend class Iterator;
};
//! Assignment to the collection of the same type
template <class TheItemType> inline
void NCollection_Vector<TheItemType>::Assign (const NCollection_Vector& theOther,
const Standard_Boolean theOwnAllocator)
{
if (this == &theOther)
{
return;
}
// destroy current data using current allocator
for (Standard_Integer anItemIter = 0; anItemIter < myCapacity; ++anItemIter)
{
initMemBlocks (*this, myData[anItemIter], 0, 0);
}
NCollection_BaseCollection<TheItemType>::myAllocator->Free (myData);
// allocate memory blocks with new allocator
if (!theOwnAllocator)
{
NCollection_BaseCollection<TheItemType>::myAllocator = theOther.myAllocator;
}
myIncrement = theOther.myIncrement;
myLength = theOther.myLength;
myNBlocks = (myLength == 0) ? 0 : (1 + (myLength - 1)/myIncrement);
myCapacity = GetCapacity (myIncrement) + myLength / myIncrement;
myData = allocMemBlocks (NCollection_BaseCollection<TheItemType>::myAllocator, myCapacity);
// copy data
copyData (theOther);
}
#endif // NCollection_Vector_HeaderFile
|