0022773: Compiler warnings on 64-bit MSVC in NCollection_Vector.hxx
[occt.git] / src / NCollection / NCollection_BaseVector.hxx
1 // File:      NCollection_BaseVector.hxx
2 // Created:   24.04.02 09:41:39
3 // Author:    Alexander GRIGORIEV
4 // Copyright: Open Cascade 2002
5
6
7 #ifndef NCollection_BaseVector_HeaderFile
8 #define NCollection_BaseVector_HeaderFile
9
10 #include <Standard_TypeDef.hxx>
11 #include <NCollection_BaseAllocator.hxx>
12 #include <stddef.h>
13
14 #if !defined No_Exception && !defined No_Standard_OutOfRange
15 #include <Standard_OutOfRange.hxx>
16 #endif
17
18 #ifdef WNT
19 #pragma warning(push, 1)
20 #pragma warning(disable:4355)
21 #endif
22
23 // this value defines the number of blocks that are reserved
24 // when the capacity of vector is increased
25 inline Standard_Integer GetCapacity (const Standard_Integer theIncrement)
26 {
27   return Max(theIncrement/8, 1);
28 }
29
30 /**
31  *  Class NCollection_BaseVector - base for generic vector
32  */
33 class NCollection_BaseVector 
34 {
35  public:
36   // ------------ Class MemBlock ------------
37   class MemBlock {
38   protected:
39     MemBlock (NCollection_BaseAllocator* theAlloc)
40       : myAlloc(theAlloc),
41         myFirstInd(0), myLength(0), mySize(0), myData(0L) {}
42     MemBlock (const Standard_Integer theFirstInd,
43               const Standard_Integer theLength,
44               NCollection_BaseAllocator* theAlloc)
45       : myAlloc(theAlloc),
46         myFirstInd(theFirstInd), myLength(0), mySize(theLength), myData(0L) {}
47     virtual void        Reinit     (const Standard_Integer,
48                                     const Standard_Integer) {}
49     Standard_Integer    FirstIndex () const     { return myFirstInd; }
50     Standard_Integer    Size       () const     { return mySize; }
51   public:
52     virtual             ~MemBlock () {}
53     void                SetLength  (const Standard_Integer theLen)
54                                                 { myLength = theLen; }
55     Standard_Integer    Length     () const     { return myLength; }
56     void *              Find       (const Standard_Integer theInd,
57                                     const size_t           theSize) const
58                                     { return ((char *) myData)+theInd*theSize;}
59     Standard_EXPORT Standard_Integer
60                         GetIndexV  (void * theItem, const size_t theSz) const;
61   protected:
62     Standard_Integer             myFirstInd;
63     Standard_Integer             myLength;
64     Standard_Integer             mySize;
65     NCollection_BaseAllocator    * myAlloc;
66     void                         * myData;
67     friend class NCollection_BaseVector;
68   };
69
70   class Iterator {
71   protected:
72     Iterator () :
73       myICurBlock (0), myIEndBlock (0), myCurIndex (0), myEndIndex (0) {}
74     Iterator (const NCollection_BaseVector& theVector) { InitV (theVector); }
75     Iterator (const Iterator& theVector)               { CopyV (theVector); }
76     Standard_EXPORT void InitV (const NCollection_BaseVector& theVector);
77     Standard_EXPORT void CopyV (const Iterator&);
78     Standard_Boolean     MoreV () const
79         { return (myICurBlock < myIEndBlock || myCurIndex < myEndIndex); }
80     void                 NextV ()
81         { if (++myCurIndex >= myVector -> myData[myICurBlock].Length() &&
82               myICurBlock < myIEndBlock)
83           { ++myICurBlock; myCurIndex = 0; } }
84     const MemBlock *     CurBlockV () const
85         { return &myVector -> myData[myICurBlock]; }
86
87     const NCollection_BaseVector * myVector;   // the Master vector
88     Standard_Integer             myICurBlock;  // # of the current block
89     Standard_Integer             myIEndBlock;
90     Standard_Integer             myCurIndex;   // Index in the current block
91     Standard_Integer             myEndIndex;
92   };
93
94  protected:
95   // ------------ Block initializer ---------
96   typedef MemBlock * (* FuncPtrDataInit) (const NCollection_BaseVector&,
97                                           const Standard_Integer aCapacity,
98                                           const void             * aSource,
99                                           const Standard_Integer aSize);
100   typedef void (* FuncPtrDataFree)       (const NCollection_BaseVector&,
101                                           MemBlock *);
102
103   friend class Iterator;
104
105   // ---------- PROTECTED METHODS ----------
106
107   //! Empty constructor
108   NCollection_BaseVector (const size_t           theSize,
109                           const Standard_Integer theInc,
110                           FuncPtrDataInit        theDataInit,
111                           FuncPtrDataFree        theDataFree)
112      : myItemSize  (theSize),
113        myIncrement (theInc),
114        myLength    (0),
115        myCapacity  (GetCapacity(myIncrement)),
116        myNBlocks   (0),
117        myData      (theDataInit (* this, myCapacity, NULL, 0)),
118        myDataInit  (theDataInit),
119        myDataFree  (theDataFree)
120   {
121 //    myData = (MemBlock *) new char [myCapacity * sizeof(MemBlock)];
122 //    for (Standard_Integer i = 0; i < myCapacity; i++)
123 //      new (&myData[i]) MemBlock;
124   }
125
126   //! Copy constructor
127   NCollection_BaseVector (const NCollection_BaseVector& theOther,
128                           FuncPtrDataInit               theDataInit,
129                           FuncPtrDataFree               theDataFree)
130     : myItemSize  (theOther.myItemSize),
131       myIncrement (theOther.myIncrement),
132       myLength    (theOther.Length()),
133       myCapacity  (GetCapacity(myIncrement)+theOther.Length()/theOther.myIncrement),
134       myNBlocks   (1 + (theOther.Length() - 1)/theOther.myIncrement),
135       myData      (theDataInit (* this, myCapacity, NULL, 0)),
136       myDataInit  (theDataInit),
137       myDataFree  (theDataFree)  {}
138
139   //! Destructor
140   Standard_EXPORT ~NCollection_BaseVector ();
141
142   //! Operator =
143   Standard_EXPORT NCollection_BaseVector& operator =
144                                  (const NCollection_BaseVector&);
145
146   //! ExpandV: returns pointer to memory where to put the new item
147   Standard_EXPORT void * ExpandV (const Standard_Integer theIndex);
148
149   //! Find: locate the memory holding the desired value
150   inline          void * Find    (const Standard_Integer theIndex) const;
151
152  public:
153   //! Total number of items
154   Standard_Integer      Length  () const      { return myLength; }
155
156   //! Empty the vector of its objects
157   Standard_EXPORT void  Clear   ();
158
159  protected:
160   // ---------- PRIVATE FIELDS ----------
161   size_t                myItemSize;
162   Standard_Integer      myIncrement;
163   Standard_Integer      myLength;
164   Standard_Integer      myCapacity;
165   Standard_Integer      myNBlocks;
166   MemBlock              * myData;
167   FuncPtrDataInit       myDataInit;
168   FuncPtrDataFree       myDataFree;
169 };
170
171 //=======================================================================
172 //function : Find
173 //purpose  : locate the memory holding the desired value
174 //=======================================================================
175
176 inline void * NCollection_BaseVector::Find
177                                         (const Standard_Integer theIndex) const
178 {
179 #if !defined No_Exception && !defined No_Standard_OutOfRange
180   if (theIndex < 0 || theIndex >= myLength)
181     Standard_OutOfRange::Raise ("NCollection_BaseVector::Find");
182 #endif
183   const Standard_Integer aBlock = theIndex / myIncrement;
184   return myData[aBlock].Find (theIndex - aBlock * myIncrement, myItemSize);
185 }
186
187 #ifdef WNT
188 #pragma warning(pop)
189 #endif
190
191 #endif