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