b311480e |
1 | // Created on: 2002-04-24 |
2 | // Created by: Alexander GRIGORIEV |
f4aad56f |
3 | // Copyright (c) 2002-2013 OPEN CASCADE SAS |
b311480e |
4 | // |
973c2be1 |
5 | // This file is part of Open CASCADE Technology software library. |
b311480e |
6 | // |
d5f74e42 |
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 |
973c2be1 |
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. |
b311480e |
12 | // |
973c2be1 |
13 | // Alternatively, this file may be used under the terms of Open CASCADE |
14 | // commercial license or contractual agreement. |
b311480e |
15 | |
7fd59977 |
16 | #include <NCollection_BaseVector.hxx> |
17 | #include <Standard_RangeError.hxx> |
f4aad56f |
18 | #include <cstdlib> |
7fd59977 |
19 | |
20 | //======================================================================= |
f4aad56f |
21 | //function : NCollection_BaseVector::Iterator::copyV |
22 | //purpose : Copy from another iterator |
7fd59977 |
23 | //======================================================================= |
24 | |
f4aad56f |
25 | void NCollection_BaseVector::Iterator::copyV (const NCollection_BaseVector::Iterator& theOth) |
7fd59977 |
26 | { |
f4aad56f |
27 | myVector = theOth.myVector; |
28 | myICurBlock = theOth.myICurBlock; |
29 | myIEndBlock = theOth.myIEndBlock; |
30 | myCurIndex = theOth.myCurIndex; |
31 | myEndIndex = theOth.myEndIndex; |
7fd59977 |
32 | } |
33 | |
34 | //======================================================================= |
f4aad56f |
35 | //function : initV |
36 | //purpose : Initialisation of iterator by a vector |
7fd59977 |
37 | //======================================================================= |
38 | |
f4aad56f |
39 | void NCollection_BaseVector::Iterator::initV (const NCollection_BaseVector& theVector) |
7fd59977 |
40 | { |
f4aad56f |
41 | myVector = &theVector; |
42 | myICurBlock = 0; |
43 | myCurIndex = 0; |
44 | if (theVector.myNBlocks == 0) |
45 | { |
46 | myIEndBlock = 0; |
47 | myEndIndex = 0; |
48 | } |
49 | else |
50 | { |
51 | myIEndBlock = theVector.myNBlocks - 1; |
52 | myEndIndex = theVector.myData[myIEndBlock].Length; |
23be7421 |
53 | } |
7fd59977 |
54 | } |
55 | |
56 | //======================================================================= |
f4aad56f |
57 | //function : allocMemBlocks |
58 | //purpose : |
7fd59977 |
59 | //======================================================================= |
60 | |
f4aad56f |
61 | NCollection_BaseVector::MemBlock* NCollection_BaseVector |
62 | ::allocMemBlocks (Handle(NCollection_BaseAllocator)& theAllocator, |
63 | const Standard_Integer theCapacity, |
64 | MemBlock* theSource, |
65 | const Standard_Integer theSourceSize) |
7fd59977 |
66 | { |
f4aad56f |
67 | MemBlock* aData = (MemBlock* )theAllocator->Allocate (theCapacity * sizeof(MemBlock)); |
68 | |
69 | // copy content from source array |
70 | Standard_Integer aCapacity = 0; |
71 | if (theSource != NULL) |
72 | { |
73 | memcpy (aData, theSource, theSourceSize * sizeof(MemBlock)); |
74 | aCapacity = theSourceSize; |
75 | theAllocator->Free (theSource); |
76 | } |
7fd59977 |
77 | |
f4aad56f |
78 | // Nullify newly allocated blocks |
79 | if (aCapacity < theCapacity) |
80 | { |
81 | memset (&aData[aCapacity], 0, (theCapacity - aCapacity) * sizeof(MemBlock)); |
7fd59977 |
82 | } |
f4aad56f |
83 | return aData; |
7fd59977 |
84 | } |
85 | |
86 | //======================================================================= |
f4aad56f |
87 | //function : Clear |
88 | //purpose : |
7fd59977 |
89 | //======================================================================= |
90 | |
f4aad56f |
91 | void NCollection_BaseVector::Clear() |
7fd59977 |
92 | { |
f4aad56f |
93 | if (myLength > 0) |
94 | { |
95 | for (Standard_Integer anItemIter = 0; anItemIter < myCapacity; ++anItemIter) |
96 | { |
97 | myInitBlocks (*this, myData[anItemIter], 0, 0); |
98 | } |
99 | myLength = 0; |
100 | myNBlocks = 0; |
101 | } |
7fd59977 |
102 | } |
103 | |
104 | //======================================================================= |
f4aad56f |
105 | //function : expandV |
7fd59977 |
106 | //purpose : returns the pointer where the new data item is supposed to be put |
107 | //======================================================================= |
108 | |
f4aad56f |
109 | void* NCollection_BaseVector::expandV (Handle(NCollection_BaseAllocator)& theAllocator, |
110 | const Standard_Integer theIndex) |
7fd59977 |
111 | { |
112 | const Standard_Integer aNewLength = theIndex + 1; |
f4aad56f |
113 | if (myNBlocks > 0) |
114 | { |
115 | // Take the last array in the vector of arrays |
116 | MemBlock& aLastBlock = myData[myNBlocks - 1]; |
117 | Standard_RangeError_Raise_if (theIndex < aLastBlock.FirstIndex, |
118 | "NColelction_BaseVector::expandV"); |
119 | Standard_Integer anIndLastBlock = theIndex - aLastBlock.FirstIndex; |
120 | // Is there still room for 1 item in the last array? |
121 | if (anIndLastBlock < aLastBlock.Size) |
122 | { |
7fd59977 |
123 | myLength = aNewLength; |
f4aad56f |
124 | aLastBlock.Length = anIndLastBlock + 1; |
125 | return aLastBlock.findV (anIndLastBlock, myItemSize); |
7fd59977 |
126 | } |
f4aad56f |
127 | myLength = aLastBlock.FirstIndex + aLastBlock.Size; |
7fd59977 |
128 | } |
129 | |
f4aad56f |
130 | // There is no room in the last array |
131 | // or the whole vector is not yet initialised. |
132 | // Initialise a new array, but before that check whether it is available within myCapacity. |
133 | const Standard_Integer nNewBlock = myNBlocks + 1 + (theIndex - myLength) / myIncrement; |
134 | if (myCapacity < nNewBlock) |
135 | { |
7fd59977 |
136 | // Reallocate the array myData |
23be7421 |
137 | do myCapacity += GetCapacity(myIncrement); while (myCapacity <= nNewBlock); |
f4aad56f |
138 | |
139 | myData = allocMemBlocks (theAllocator, myCapacity, myData, myNBlocks); |
7fd59977 |
140 | } |
f4aad56f |
141 | if (myNBlocks > 0) |
142 | { |
7fd59977 |
143 | // Change length of old last block to myIncrement |
f4aad56f |
144 | MemBlock& aLastBlock = myData[myNBlocks - 1]; |
145 | aLastBlock.Length = myIncrement; |
7fd59977 |
146 | } |
f4aad56f |
147 | |
7fd59977 |
148 | // Initialise new blocks |
f4aad56f |
149 | MemBlock* aNewBlock = &myData[myNBlocks++]; |
150 | myInitBlocks (*this, *aNewBlock, myLength, myIncrement); |
151 | while (myNBlocks < nNewBlock) |
152 | { |
153 | aNewBlock->Length = myIncrement; |
7fd59977 |
154 | myLength += myIncrement; |
f4aad56f |
155 | aNewBlock = &myData[myNBlocks++]; |
156 | myInitBlocks (*this, *aNewBlock, myLength, myIncrement); |
7fd59977 |
157 | } |
f4aad56f |
158 | aNewBlock->Length = aNewLength - myLength; |
7fd59977 |
159 | myLength = aNewLength; |
f4aad56f |
160 | return aNewBlock->findV (theIndex - aNewBlock->FirstIndex, myItemSize); |
7fd59977 |
161 | } |