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 | |
19 | #include <Standard_Boolean.hxx> |
20 | #include <Standard_Integer.hxx> |
21 | #include <Standard_OStream.hxx> |
22 | |
23 | #include <Standard.hxx> |
24 | #include <Standard_Macro.hxx> |
25 | #include <NCollection_BaseAllocator.hxx> |
26 | #include <NCollection_ListNode.hxx> |
27 | |
7fd59977 |
28 | typedef void (* NCollection_DelMapNode) |
29 | (NCollection_ListNode*, Handle(NCollection_BaseAllocator)& theAl); |
30 | |
31 | /** |
32 | * Purpose: This is a base class for all Maps: |
33 | * Map |
34 | * DataMap |
35 | * DoubleMap |
36 | * IndexedMap |
37 | * IndexedDataMap |
38 | * Provides utilitites for managing the buckets. |
39 | */ |
40 | |
41 | class NCollection_BaseMap |
42 | { |
43 | public: |
44 | // **************************************** Class Iterator **************** |
45 | class Iterator |
46 | { |
47 | protected: |
48 | //! Empty constructor |
49 | Iterator (void) : |
50 | myNbBuckets (0), |
51 | myBuckets (NULL), |
52 | myBucket (0), |
53 | myNode (NULL) {} |
54 | |
55 | //! Constructor |
56 | Iterator (const NCollection_BaseMap& theMap) : |
57 | myNbBuckets (theMap.myNbBuckets), |
58 | myBuckets (theMap.myData1), |
59 | myBucket (-1), |
60 | myNode (NULL) |
61 | { |
62 | if (!myBuckets) |
63 | myNbBuckets = -1; |
64 | else |
65 | do { |
66 | myBucket++; |
67 | if (myBucket > myNbBuckets) |
68 | return; |
69 | myNode = myBuckets[myBucket]; |
70 | } while (!myNode); |
71 | } |
72 | |
73 | public: |
74 | //! Initialize |
75 | void Initialize (const NCollection_BaseMap& theMap) |
76 | { |
77 | myNbBuckets = theMap.myNbBuckets; |
78 | myBuckets = theMap.myData1; |
79 | myBucket = -1; |
80 | myNode = NULL; |
81 | if (!myBuckets) |
82 | myNbBuckets = -1; |
83 | PNext(); |
84 | } |
85 | |
86 | //! Reset |
87 | void Reset (void) |
88 | { |
89 | myBucket = -1; |
90 | myNode = NULL; |
91 | PNext(); |
92 | } |
79a35943 |
93 | |
94 | //! Performs comparison of two iterators. |
95 | virtual Standard_Boolean IsEqual (const Iterator& theOther) const |
96 | { |
97 | return myBucket == theOther.myBucket && myNode == theOther.myNode; |
98 | } |
7fd59977 |
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 |