0023040: Annoying warnings in NCollection_Vector
[occt.git] / src / NCollection / NCollection_BaseVector.hxx
CommitLineData
b311480e 1// Created on: 2002-04-24
2// Created by: Alexander GRIGORIEV
3// Copyright (c) 2002-2012 OPEN CASCADE SAS
4//
5// The content of this file is subject to the Open CASCADE Technology Public
6// License Version 6.5 (the "License"). You may not use the content of this file
7// except in compliance with the License. Please obtain a copy of the License
8// at http://www.opencascade.org and read it completely before using this file.
9//
10// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12//
13// The Original Code and all software distributed under the License is
14// distributed on an "AS IS" basis, without warranty of any kind, and the
15// Initial Developer hereby disclaims all such warranties, including without
16// limitation, any warranties of merchantability, fitness for a particular
17// purpose or non-infringement. Please see the License for the specific terms
18// and conditions governing the rights and limitations under the License.
19
7fd59977 20
21
22#ifndef NCollection_BaseVector_HeaderFile
23#define NCollection_BaseVector_HeaderFile
24
25#include <Standard_TypeDef.hxx>
23be7421 26#include <NCollection_BaseAllocator.hxx>
7fd59977 27#include <stddef.h>
28
29#if !defined No_Exception && !defined No_Standard_OutOfRange
30#include <Standard_OutOfRange.hxx>
31#endif
32
33#ifdef WNT
34#pragma warning(push, 1)
35#pragma warning(disable:4355)
36#endif
37
23be7421
M
38// this value defines the number of blocks that are reserved
39// when the capacity of vector is increased
40inline Standard_Integer GetCapacity (const Standard_Integer theIncrement)
41{
42 return Max(theIncrement/8, 1);
43}
44
7fd59977 45/**
46 * Class NCollection_BaseVector - base for generic vector
47 */
50a0744c 48class NCollection_BaseVector
7fd59977 49{
50 public:
51 // ------------ Class MemBlock ------------
52 class MemBlock {
53 protected:
23be7421
M
54 MemBlock (NCollection_BaseAllocator* theAlloc)
55 : myAlloc(theAlloc),
50a0744c 56 myData(NULL),
57 myFirstInd(0),
58 myLength(0),
59 mySize(0) {}
7fd59977 60 MemBlock (const Standard_Integer theFirstInd,
23be7421
M
61 const Standard_Integer theLength,
62 NCollection_BaseAllocator* theAlloc)
63 : myAlloc(theAlloc),
50a0744c 64 myData(NULL),
65 myFirstInd(theFirstInd),
66 myLength(0),
67 mySize(theLength) {}
7fd59977 68 virtual void Reinit (const Standard_Integer,
0f633a22 69 const Standard_Integer) {}
7fd59977 70 Standard_Integer FirstIndex () const { return myFirstInd; }
0f633a22 71 Standard_Integer Size () const { return mySize; }
7fd59977 72 public:
23be7421 73 virtual ~MemBlock () {}
0f633a22 74 void SetLength (const Standard_Integer theLen)
7fd59977 75 { myLength = theLen; }
0f633a22 76 Standard_Integer Length () const { return myLength; }
7fd59977 77 void * Find (const Standard_Integer theInd,
78 const size_t theSize) const
79 { return ((char *) myData)+theInd*theSize;}
80 Standard_EXPORT Standard_Integer
81 GetIndexV (void * theItem, const size_t theSz) const;
82 protected:
50a0744c 83 NCollection_BaseAllocator* myAlloc;
84 void* myData;
7fd59977 85 Standard_Integer myFirstInd;
0f633a22
D
86 Standard_Integer myLength;
87 Standard_Integer mySize;
7fd59977 88 friend class NCollection_BaseVector;
89 };
90
91 class Iterator {
92 protected:
93 Iterator () :
94 myICurBlock (0), myIEndBlock (0), myCurIndex (0), myEndIndex (0) {}
95 Iterator (const NCollection_BaseVector& theVector) { InitV (theVector); }
96 Iterator (const Iterator& theVector) { CopyV (theVector); }
97 Standard_EXPORT void InitV (const NCollection_BaseVector& theVector);
98 Standard_EXPORT void CopyV (const Iterator&);
99 Standard_Boolean MoreV () const
100 { return (myICurBlock < myIEndBlock || myCurIndex < myEndIndex); }
101 void NextV ()
102 { if (++myCurIndex >= myVector -> myData[myICurBlock].Length() &&
103 myICurBlock < myIEndBlock)
104 { ++myICurBlock; myCurIndex = 0; } }
105 const MemBlock * CurBlockV () const
106 { return &myVector -> myData[myICurBlock]; }
107
108 const NCollection_BaseVector * myVector; // the Master vector
0f633a22
D
109 Standard_Integer myICurBlock; // # of the current block
110 Standard_Integer myIEndBlock;
111 Standard_Integer myCurIndex; // Index in the current block
112 Standard_Integer myEndIndex;
7fd59977 113 };
114
115 protected:
116 // ------------ Block initializer ---------
117 typedef MemBlock * (* FuncPtrDataInit) (const NCollection_BaseVector&,
118 const Standard_Integer aCapacity,
119 const void * aSource,
120 const Standard_Integer aSize);
121 typedef void (* FuncPtrDataFree) (const NCollection_BaseVector&,
122 MemBlock *);
123
124 friend class Iterator;
125
126 // ---------- PROTECTED METHODS ----------
127
128 //! Empty constructor
129 NCollection_BaseVector (const size_t theSize,
130 const Standard_Integer theInc,
131 FuncPtrDataInit theDataInit,
23be7421 132 FuncPtrDataFree theDataFree)
7fd59977 133 : myItemSize (theSize),
134 myIncrement (theInc),
135 myLength (0),
23be7421 136 myCapacity (GetCapacity(myIncrement)),
7fd59977 137 myNBlocks (0),
138 myData (theDataInit (* this, myCapacity, NULL, 0)),
139 myDataInit (theDataInit),
140 myDataFree (theDataFree)
141 {
142// myData = (MemBlock *) new char [myCapacity * sizeof(MemBlock)];
143// for (Standard_Integer i = 0; i < myCapacity; i++)
144// new (&myData[i]) MemBlock;
145 }
146
147 //! Copy constructor
148 NCollection_BaseVector (const NCollection_BaseVector& theOther,
149 FuncPtrDataInit theDataInit,
23be7421 150 FuncPtrDataFree theDataFree)
7fd59977 151 : myItemSize (theOther.myItemSize),
152 myIncrement (theOther.myIncrement),
153 myLength (theOther.Length()),
23be7421 154 myCapacity (GetCapacity(myIncrement)+theOther.Length()/theOther.myIncrement),
7fd59977 155 myNBlocks (1 + (theOther.Length() - 1)/theOther.myIncrement),
156 myData (theDataInit (* this, myCapacity, NULL, 0)),
157 myDataInit (theDataInit),
158 myDataFree (theDataFree) {}
159
160 //! Destructor
161 Standard_EXPORT ~NCollection_BaseVector ();
162
163 //! Operator =
164 Standard_EXPORT NCollection_BaseVector& operator =
165 (const NCollection_BaseVector&);
166
167 //! ExpandV: returns pointer to memory where to put the new item
168 Standard_EXPORT void * ExpandV (const Standard_Integer theIndex);
169
170 //! Find: locate the memory holding the desired value
171 inline void * Find (const Standard_Integer theIndex) const;
172
173 public:
174 //! Total number of items
175 Standard_Integer Length () const { return myLength; }
176
177 //! Empty the vector of its objects
178 Standard_EXPORT void Clear ();
179
180 protected:
181 // ---------- PRIVATE FIELDS ----------
182 size_t myItemSize;
183 Standard_Integer myIncrement;
184 Standard_Integer myLength;
185 Standard_Integer myCapacity;
186 Standard_Integer myNBlocks;
187 MemBlock * myData;
188 FuncPtrDataInit myDataInit;
189 FuncPtrDataFree myDataFree;
190};
191
192//=======================================================================
193//function : Find
194//purpose : locate the memory holding the desired value
195//=======================================================================
196
197inline void * NCollection_BaseVector::Find
198 (const Standard_Integer theIndex) const
199{
200#if !defined No_Exception && !defined No_Standard_OutOfRange
201 if (theIndex < 0 || theIndex >= myLength)
202 Standard_OutOfRange::Raise ("NCollection_BaseVector::Find");
203#endif
204 const Standard_Integer aBlock = theIndex / myIncrement;
205 return myData[aBlock].Find (theIndex - aBlock * myIncrement, myItemSize);
206}
207
208#ifdef WNT
209#pragma warning(pop)
210#endif
211
212#endif