0031671: Coding Rules - eliminate warnings issued by clang 11
[occt.git] / src / NCollection / NCollection_BaseVector.hxx
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#ifndef NCollection_BaseVector_HeaderFile
17#define NCollection_BaseVector_HeaderFile
18
19#include <Standard_TypeDef.hxx>
7fd59977 20#include <Standard_OutOfRange.hxx>
ddf2fe8e 21#include <NCollection_BaseAllocator.hxx>
22#include <NCollection_DefineAlloc.hxx>
7fd59977 23
ddf2fe8e 24#include <stddef.h>
7fd59977 25
23be7421
M
26// this value defines the number of blocks that are reserved
27// when the capacity of vector is increased
28inline Standard_Integer GetCapacity (const Standard_Integer theIncrement)
29{
30 return Max(theIncrement/8, 1);
31}
32
f4aad56f 33//! Class NCollection_BaseVector - base for NCollection_Vector template
50a0744c 34class NCollection_BaseVector
7fd59977 35{
ddf2fe8e 36public:
37 //! Memory allocation
38 DEFINE_STANDARD_ALLOC
39 DEFINE_NCOLLECTION_ALLOC
f4aad56f 40
41protected:
42
43 // Auxiliary structure for memory blocks
44 struct MemBlock
45 {
46
7fd59977 47 public:
f4aad56f 48
49 //! @param theIndex Item index in the block
50 //! @param theItemSize Element size in bytes
51 //! @return the address of specified item in this memory block
52 void* findV (const Standard_Integer theIndex,
53 const size_t theItemSize) const
54 {
55 return (char* )DataPtr + size_t(theIndex) * theItemSize;
56 }
57
58 public:
59
60 void* DataPtr; //!< block of elements
61 Standard_Integer FirstIndex; //!< index of the first element (among all memory blocks in collection)
62 Standard_Integer Length;
63 Standard_Integer Size;
64
7fd59977 65 };
66
f4aad56f 67 //! Base class for Iterator implementation
68 class Iterator
69 {
7fd59977 70 protected:
f4aad56f 71 Iterator()
72 : myICurBlock (0),
73 myIEndBlock (0),
74 myCurIndex (0),
75 myEndIndex (0) {}
76
79a35943 77 Iterator (const NCollection_BaseVector& theVector, Standard_Boolean theToEnd = Standard_False)
f4aad56f 78 {
79a35943 79 initV (theVector, theToEnd);
f4aad56f 80 }
81
79a35943 82 Standard_EXPORT void initV (const NCollection_BaseVector& theVector, Standard_Boolean theToEnd = Standard_False);
f4aad56f 83
f4aad56f 84 Standard_Boolean moreV() const
85 {
86 return (myICurBlock < myIEndBlock || myCurIndex < myEndIndex);
87 }
88
89 void nextV()
90 {
91 if (++myCurIndex >= myVector->myData[myICurBlock].Length
92 && myICurBlock < myIEndBlock)
93 {
94 ++myICurBlock;
95 myCurIndex = 0;
96 }
97 }
98
79a35943 99 void prevV()
100 {
101 if (--myCurIndex < 0 && myICurBlock > 0)
102 {
103 --myICurBlock;
104 myCurIndex = myVector->myData[myICurBlock].Length - 1;
105 }
106 }
107
ddf2fe8e 108 void offsetV (Standard_Integer theOffset)
79a35943 109 {
110 const Standard_Integer anIndex = myCurIndex + myICurBlock * myVector->myIncrement + theOffset;
111 myICurBlock = anIndex / myVector->myIncrement;
112 myCurIndex = anIndex % myVector->myIncrement;
0f5ea4b5 113 if (myICurBlock > myIEndBlock)
114 {
115 // make sure that iterator produced by Offset()
116 // is equal to the end() iterator
117 --myICurBlock;
118 myCurIndex += myVector->myIncrement;
119 }
79a35943 120 }
121
ddf2fe8e 122 Standard_Integer differV (const Iterator& theOther) const
79a35943 123 {
124 return (myCurIndex - theOther.myCurIndex) + (myICurBlock - theOther.myICurBlock) * myVector->myIncrement;
125 }
126
f4aad56f 127 const MemBlock* curBlockV() const
128 {
129 return &myVector->myData[myICurBlock];
130 }
131
99ee8f1a 132 protected:
f4aad56f 133 const NCollection_BaseVector* myVector; //!< the Master vector
134 Standard_Integer myICurBlock; //!< # of the current block
135 Standard_Integer myIEndBlock;
136 Standard_Integer myCurIndex; //!< Index in the current block
137 Standard_Integer myEndIndex;
7fd59977 138 };
139
f4aad56f 140protected: //! @name Block initializer
7fd59977 141
f4aad56f 142 typedef void (*initMemBlocks_t) (NCollection_BaseVector& theVector,
143 MemBlock& theBlock,
144 const Standard_Integer theFirst,
145 const Standard_Integer theSize);
146
147 //! Allocate memory for array of memory blocks.
f4aad56f 148 //! @param theCapacity Number of memory blocks in array
149 //! @param theSource Original array of memory blocks, will be automatically deallocated
150 //! @param theSourceSize Number of memory blocks in original array
ddf2fe8e 151 Standard_EXPORT MemBlock* allocMemBlocks (const Standard_Integer theCapacity,
152 MemBlock* theSource = NULL,
153 const Standard_Integer theSourceSize = 0);
7fd59977 154
f4aad56f 155protected: //! @name protected methods
7fd59977 156
157 //! Empty constructor
ddf2fe8e 158 NCollection_BaseVector (const Handle(NCollection_BaseAllocator)& theAllocator,
f4aad56f 159 initMemBlocks_t theInitBlocks,
160 const size_t theSize,
161 const Standard_Integer theInc)
162 : myItemSize (theSize),
163 myIncrement (theInc),
164 myLength (0),
165 myCapacity (GetCapacity (myIncrement)),
166 myNBlocks (0),
ddf2fe8e 167 myInitBlocks (theInitBlocks)
168 {
169 myAllocator = (theAllocator.IsNull() ? NCollection_BaseAllocator::CommonBaseAllocator() : theAllocator);
170 myData = allocMemBlocks (myCapacity);
171 }
f4aad56f 172
173 //! Copy constructor
ddf2fe8e 174 NCollection_BaseVector (const Handle(NCollection_BaseAllocator)& theAllocator,
f4aad56f 175 initMemBlocks_t theInitBlocks,
176 const NCollection_BaseVector& theOther)
177 : myItemSize (theOther.myItemSize),
178 myIncrement (theOther.myIncrement),
179 myLength (theOther.myLength),
180 myCapacity (GetCapacity(myIncrement) + theOther.myLength / theOther.myIncrement),
e3a6386d 181 myNBlocks (theOther.myLength == 0 ? 0 : 1 + (theOther.myLength - 1)/theOther.myIncrement),
ddf2fe8e 182 myInitBlocks (theInitBlocks)
183 {
184 myAllocator = (theAllocator.IsNull() ? NCollection_BaseAllocator::CommonBaseAllocator() : theAllocator);
185 myData = allocMemBlocks (myCapacity);
186 }
f4aad56f 187
6928e351 188 //! Destructor
189 virtual ~NCollection_BaseVector() {}
190
f4aad56f 191 //! @return pointer to memory where to put the new item
ddf2fe8e 192 Standard_EXPORT void* expandV (const Standard_Integer theIndex);
f4aad56f 193
194 //! Locate the memory holding the desired value
195 inline void* findV (const Standard_Integer theIndex) const
7fd59977 196 {
f4aad56f 197 Standard_OutOfRange_Raise_if (theIndex < 0 || theIndex >= myLength,
198 "NCollection_BaseVector::findV");
199 const Standard_Integer aBlock = theIndex / myIncrement;
200 return myData[aBlock].findV (theIndex - aBlock * myIncrement, myItemSize);
7fd59977 201 }
202
f4aad56f 203public: //! @name public API
7fd59977 204
205 //! Empty the vector of its objects
f4aad56f 206 Standard_EXPORT void Clear();
402bfe81 207 // to set the size of increment dynamically
208 void SetIncrement(const Standard_Integer aIncrement) {
209 if (aIncrement > 0) {
210 if (!myIncrement) {
211 myIncrement=aIncrement;
212 }
213 }
214 }
7fd59977 215
e1c1b6b9 216 //! Returns attached allocator
217 const Handle(NCollection_BaseAllocator)& Allocator() const
218 {
219 return myAllocator;
220 }
221
ddf2fe8e 222protected: //! @name Protected fields
7fd59977 223
ddf2fe8e 224 Handle(NCollection_BaseAllocator) myAllocator;
f4aad56f 225 size_t myItemSize;
226 Standard_Integer myIncrement;
227 Standard_Integer myLength;
228 Standard_Integer myCapacity;
229 Standard_Integer myNBlocks;
230 MemBlock* myData;
231 initMemBlocks_t myInitBlocks;
7fd59977 232
f4aad56f 233protected:
234
235 friend class Iterator;
f4aad56f 236};
237
f4aad56f 238#endif // NCollection_BaseVector_HeaderFile