0024271: Provide Boolean operations for NCollection_Map
[occt.git] / src / NCollection / NCollection_BaseMap.hxx
CommitLineData
b311480e 1// Created on: 2002-04-18
2// Created by: Alexander KARTOMIN (akm)
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_BaseMap_HeaderFile
23#define NCollection_BaseMap_HeaderFile
24
25#include <Standard_Boolean.hxx>
26#include <Standard_Integer.hxx>
27#include <Standard_OStream.hxx>
28
29#include <Standard.hxx>
30#include <Standard_Macro.hxx>
31#include <NCollection_BaseAllocator.hxx>
32#include <NCollection_ListNode.hxx>
33
7fd59977 34typedef void (* NCollection_DelMapNode)
35 (NCollection_ListNode*, Handle(NCollection_BaseAllocator)& theAl);
36
37/**
38 * Purpose: This is a base class for all Maps:
39 * Map
40 * DataMap
41 * DoubleMap
42 * IndexedMap
43 * IndexedDataMap
44 * Provides utilitites for managing the buckets.
45 */
46
47class NCollection_BaseMap
48{
49 public:
50 // **************************************** Class Iterator ****************
51 class Iterator
52 {
53 protected:
54 //! Empty constructor
55 Iterator (void) :
56 myNbBuckets (0),
57 myBuckets (NULL),
58 myBucket (0),
59 myNode (NULL) {}
60
61 //! Constructor
62 Iterator (const NCollection_BaseMap& theMap) :
63 myNbBuckets (theMap.myNbBuckets),
64 myBuckets (theMap.myData1),
65 myBucket (-1),
66 myNode (NULL)
67 {
68 if (!myBuckets)
69 myNbBuckets = -1;
70 else
71 do {
72 myBucket++;
73 if (myBucket > myNbBuckets)
74 return;
75 myNode = myBuckets[myBucket];
76 } while (!myNode);
77 }
78
79 public:
80 //! Initialize
81 void Initialize (const NCollection_BaseMap& theMap)
82 {
83 myNbBuckets = theMap.myNbBuckets;
84 myBuckets = theMap.myData1;
85 myBucket = -1;
86 myNode = NULL;
87 if (!myBuckets)
88 myNbBuckets = -1;
89 PNext();
90 }
91
92 //! Reset
93 void Reset (void)
94 {
95 myBucket = -1;
96 myNode = NULL;
97 PNext();
98 }
99
100 protected:
101 //! PMore
102 Standard_Boolean PMore (void) const
103 { return (myNode != NULL); }
104
105 //! PNext
106 void PNext (void)
107 {
108 if (!myBuckets)
109 return;
110 if (myNode)
111 {
112 myNode = myNode->Next();
113 if (myNode)
114 return;
115 }
116 while (!myNode)
117 {
118 myBucket++;
119 if (myBucket > myNbBuckets)
120 return;
121 myNode = myBuckets[myBucket];
122 }
123 }
124
125 protected:
126 // ---------- PRIVATE FIELDS ------------
127 Standard_Integer myNbBuckets; //!< Total buckets in the map
128 NCollection_ListNode **myBuckets; //!< Location in memory
129 Standard_Integer myBucket; //!< Current bucket
130 NCollection_ListNode * myNode; //!< Current node
131 };
132
133 public:
134 // ---------- PUBLIC METHODS ------------
135
136 //! NbBuckets
137 Standard_Integer NbBuckets() const
138 { return myNbBuckets; }
139
140 //! Extent
141 Standard_Integer Extent() const
142 { return mySize; }
143
144 //! IsEmpty
145 Standard_Boolean IsEmpty() const
146 { return mySize == 0; }
147
148 //! Statistics
149 Standard_EXPORT void Statistics(Standard_OStream& S) const;
150
151 protected:
152 // -------- PROTECTED METHODS -----------
153
154 //! Constructor
155 NCollection_BaseMap (const Standard_Integer NbBuckets,
156 const Standard_Boolean single) :
157 myData1(NULL),
158 myData2(NULL),
159 isDouble(!single),
160 mySaturated(Standard_False),
161 myNbBuckets(NbBuckets),
162 mySize(0) {}
163
164 //! BeginResize
165 Standard_EXPORT Standard_Boolean BeginResize
166 (const Standard_Integer NbBuckets,
167 Standard_Integer& NewBuckets,
168 NCollection_ListNode**& data1,
169 NCollection_ListNode**& data2,
170 Handle(NCollection_BaseAllocator)& theAllocator) const;
171
172 //! EndResize
173 Standard_EXPORT void EndResize
174 (const Standard_Integer NbBuckets,
175 const Standard_Integer NewBuckets,
176 NCollection_ListNode** data1,
177 NCollection_ListNode** data2,
178 Handle(NCollection_BaseAllocator)& theAllocator);
179
180 //! Resizable
181 Standard_Boolean Resizable() const
182 { return IsEmpty() || (!mySaturated && (mySize > myNbBuckets)); }
183
184 //! Increment
185 void Increment()
186 { mySize++; }
187
188 //! Decrement
189 void Decrement()
190 { mySize--; }
191
192 //! Destroy
193 Standard_EXPORT void Destroy(NCollection_DelMapNode fDel,
194 Handle(NCollection_BaseAllocator)& theAllocator,
195 const Standard_Boolean doReleaseMemory
196 = Standard_True);
197
198 //! NextPrimeForMap
199 Standard_EXPORT Standard_Integer NextPrimeForMap
200 (const Standard_Integer N) const;
201
ab2db9a5 202 //! Exchange content of two maps without data copying
203 void exchangeMapsData (NCollection_BaseMap& theOther)
204 {
205 std::swap (myData1, theOther.myData1);
206 std::swap (myData2, theOther.myData2);
207 //std::swap (isDouble, theOther.isDouble);
208 std::swap (mySaturated, theOther.mySaturated);
209 std::swap (myNbBuckets, theOther.myNbBuckets);
210 std::swap (mySize, theOther.mySize);
211 }
212
7fd59977 213 protected:
214 // --------- PROTECTED FIELDS -----------
215 NCollection_ListNode ** myData1;
216 NCollection_ListNode ** myData2;
217
218 private:
219 // ---------- PRIVATE FIELDS ------------
220 Standard_Boolean isDouble;
221 Standard_Boolean mySaturated;
222 Standard_Integer myNbBuckets;
223 Standard_Integer mySize;
224
225 // ---------- FRIEND CLASSES ------------
226 friend class Iterator;
227
228};
229
230#endif