0031671: Coding Rules - eliminate warnings issued by clang 11
[occt.git] / src / NCollection / NCollection_BaseVector.cxx
CommitLineData
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>
99ee8f1a 17
7fd59977 18#include <Standard_RangeError.hxx>
f4aad56f 19#include <cstdlib>
7fd59977 20
21//=======================================================================
f4aad56f 22//function : initV
23//purpose : Initialisation of iterator by a vector
7fd59977 24//=======================================================================
25
79a35943 26void NCollection_BaseVector::Iterator::initV (const NCollection_BaseVector& theVector, Standard_Boolean theToEnd)
7fd59977 27{
79a35943 28 myVector = &theVector;
29
f4aad56f 30 if (theVector.myNBlocks == 0)
31 {
79a35943 32 myCurIndex = 0;
f4aad56f 33 myEndIndex = 0;
79a35943 34 myICurBlock = 0;
35 myIEndBlock = 0;
f4aad56f 36 }
37 else
38 {
39 myIEndBlock = theVector.myNBlocks - 1;
40 myEndIndex = theVector.myData[myIEndBlock].Length;
79a35943 41
42 myICurBlock = !theToEnd ? 0 : myIEndBlock;
43 myCurIndex = !theToEnd ? 0 : myEndIndex;
23be7421 44 }
7fd59977 45}
46
47//=======================================================================
f4aad56f 48//function : allocMemBlocks
49//purpose :
7fd59977 50//=======================================================================
51
f4aad56f 52NCollection_BaseVector::MemBlock* NCollection_BaseVector
ddf2fe8e 53 ::allocMemBlocks (const Standard_Integer theCapacity,
f4aad56f 54 MemBlock* theSource,
55 const Standard_Integer theSourceSize)
7fd59977 56{
ddf2fe8e 57 MemBlock* aData = (MemBlock* )myAllocator->Allocate (theCapacity * sizeof(MemBlock));
f4aad56f 58
59 // copy content from source array
60 Standard_Integer aCapacity = 0;
61 if (theSource != NULL)
62 {
63 memcpy (aData, theSource, theSourceSize * sizeof(MemBlock));
64 aCapacity = theSourceSize;
ddf2fe8e 65 myAllocator->Free (theSource);
f4aad56f 66 }
7fd59977 67
f4aad56f 68 // Nullify newly allocated blocks
69 if (aCapacity < theCapacity)
70 {
71 memset (&aData[aCapacity], 0, (theCapacity - aCapacity) * sizeof(MemBlock));
7fd59977 72 }
f4aad56f 73 return aData;
7fd59977 74}
75
76//=======================================================================
f4aad56f 77//function : Clear
78//purpose :
7fd59977 79//=======================================================================
80
f4aad56f 81void NCollection_BaseVector::Clear()
7fd59977 82{
f4aad56f 83 if (myLength > 0)
84 {
85 for (Standard_Integer anItemIter = 0; anItemIter < myCapacity; ++anItemIter)
86 {
87 myInitBlocks (*this, myData[anItemIter], 0, 0);
88 }
89 myLength = 0;
90 myNBlocks = 0;
91 }
7fd59977 92}
93
94//=======================================================================
f4aad56f 95//function : expandV
7fd59977 96//purpose : returns the pointer where the new data item is supposed to be put
97//=======================================================================
98
ddf2fe8e 99void* NCollection_BaseVector::expandV (const Standard_Integer theIndex)
7fd59977 100{
101 const Standard_Integer aNewLength = theIndex + 1;
f4aad56f 102 if (myNBlocks > 0)
103 {
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)
111 {
7fd59977 112 myLength = aNewLength;
f4aad56f 113 aLastBlock.Length = anIndLastBlock + 1;
114 return aLastBlock.findV (anIndLastBlock, myItemSize);
7fd59977 115 }
f4aad56f 116 myLength = aLastBlock.FirstIndex + aLastBlock.Size;
7fd59977 117 }
118
f4aad56f 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)
124 {
7fd59977 125 // Reallocate the array myData
23be7421 126 do myCapacity += GetCapacity(myIncrement); while (myCapacity <= nNewBlock);
f4aad56f 127
ddf2fe8e 128 myData = allocMemBlocks (myCapacity, myData, myNBlocks);
7fd59977 129 }
f4aad56f 130 if (myNBlocks > 0)
131 {
7fd59977 132 // Change length of old last block to myIncrement
f4aad56f 133 MemBlock& aLastBlock = myData[myNBlocks - 1];
134 aLastBlock.Length = myIncrement;
7fd59977 135 }
f4aad56f 136
7fd59977 137 // Initialise new blocks
f4aad56f 138 MemBlock* aNewBlock = &myData[myNBlocks++];
139 myInitBlocks (*this, *aNewBlock, myLength, myIncrement);
140 while (myNBlocks < nNewBlock)
141 {
142 aNewBlock->Length = myIncrement;
7fd59977 143 myLength += myIncrement;
f4aad56f 144 aNewBlock = &myData[myNBlocks++];
145 myInitBlocks (*this, *aNewBlock, myLength, myIncrement);
7fd59977 146 }
f4aad56f 147 aNewBlock->Length = aNewLength - myLength;
7fd59977 148 myLength = aNewLength;
f4aad56f 149 return aNewBlock->findV (theIndex - aNewBlock->FirstIndex, myItemSize);
7fd59977 150}