1 // Created on: 2002-04-24
2 // Created by: Alexander GRIGORIEV
3 // Copyright (c) 2002-2013 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <NCollection_BaseVector.hxx>
18 #include <Standard_RangeError.hxx>
21 //=======================================================================
23 //purpose : Initialisation of iterator by a vector
24 //=======================================================================
26 void NCollection_BaseVector::Iterator::initV (const NCollection_BaseVector& theVector, Standard_Boolean theToEnd)
28 myVector = &theVector;
30 if (theVector.myNBlocks == 0)
39 myIEndBlock = theVector.myNBlocks - 1;
40 myEndIndex = theVector.myData[myIEndBlock].Length;
42 myICurBlock = !theToEnd ? 0 : myIEndBlock;
43 myCurIndex = !theToEnd ? 0 : myEndIndex;
47 //=======================================================================
48 //function : allocMemBlocks
50 //=======================================================================
52 NCollection_BaseVector::MemBlock* NCollection_BaseVector
53 ::allocMemBlocks (const Standard_Integer theCapacity,
55 const Standard_Integer theSourceSize)
57 MemBlock* aData = (MemBlock* )myAllocator->Allocate (theCapacity * sizeof(MemBlock));
59 // copy content from source array
60 Standard_Integer aCapacity = 0;
61 if (theSource != NULL)
63 memcpy (aData, theSource, theSourceSize * sizeof(MemBlock));
64 aCapacity = theSourceSize;
65 myAllocator->Free (theSource);
68 // Nullify newly allocated blocks
69 if (aCapacity < theCapacity)
71 memset (&aData[aCapacity], 0, (theCapacity - aCapacity) * sizeof(MemBlock));
76 //=======================================================================
79 //=======================================================================
81 void NCollection_BaseVector::Clear()
85 for (Standard_Integer anItemIter = 0; anItemIter < myCapacity; ++anItemIter)
87 myInitBlocks (*this, myData[anItemIter], 0, 0);
94 //=======================================================================
96 //purpose : returns the pointer where the new data item is supposed to be put
97 //=======================================================================
99 void* NCollection_BaseVector::expandV (const Standard_Integer theIndex)
101 const Standard_Integer aNewLength = theIndex + 1;
104 // Take the last array in the vector of arrays
105 MemBlock& aLastBlock = myData[myNBlocks - 1];
106 Standard_RangeError_Raise_if (theIndex < aLastBlock.FirstIndex,
107 "NColelction_BaseVector::expandV");
108 Standard_Integer anIndLastBlock = theIndex - aLastBlock.FirstIndex;
109 // Is there still room for 1 item in the last array?
110 if (anIndLastBlock < aLastBlock.Size)
112 myLength = aNewLength;
113 aLastBlock.Length = anIndLastBlock + 1;
114 return aLastBlock.findV (anIndLastBlock, myItemSize);
116 myLength = aLastBlock.FirstIndex + aLastBlock.Size;
119 // There is no room in the last array
120 // or the whole vector is not yet initialised.
121 // Initialise a new array, but before that check whether it is available within myCapacity.
122 const Standard_Integer nNewBlock = myNBlocks + 1 + (theIndex - myLength) / myIncrement;
123 if (myCapacity < nNewBlock)
125 // Reallocate the array myData
126 do myCapacity += GetCapacity(myIncrement); while (myCapacity <= nNewBlock);
128 myData = allocMemBlocks (myCapacity, myData, myNBlocks);
132 // Change length of old last block to myIncrement
133 MemBlock& aLastBlock = myData[myNBlocks - 1];
134 aLastBlock.Length = myIncrement;
137 // Initialise new blocks
138 MemBlock* aNewBlock = &myData[myNBlocks++];
139 myInitBlocks (*this, *aNewBlock, myLength, myIncrement);
140 while (myNBlocks < nNewBlock)
142 aNewBlock->Length = myIncrement;
143 myLength += myIncrement;
144 aNewBlock = &myData[myNBlocks++];
145 myInitBlocks (*this, *aNewBlock, myLength, myIncrement);
147 aNewBlock->Length = aNewLength - myLength;
148 myLength = aNewLength;
149 return aNewBlock->findV (theIndex - aNewBlock->FirstIndex, myItemSize);