1 // Lastly modified by :
2 // +---------------------------------------------------------------------------+
3 // ! szy ! Modified Assign method ! 7-05-2003! 3.0-00-3!
4 // +---------------------------------------------------------------------------+
5 // File: TCollection_DoubleMap.gxx
6 // Created: Fri Jan 8 18:15:52 1993
7 // Author: Remi LEQUETTE
11 #include <Standard_DomainError.hxx>
12 #include <Standard_MultiplyDefined.hxx>
13 #include <Standard_NoSuchObject.hxx>
16 //=======================================================================
17 //function : TCollection_DoubleMap
19 //=======================================================================
21 TCollection_DoubleMap::TCollection_DoubleMap(const Standard_Integer NbBuckets):
22 TCollection_BasicMap(NbBuckets,Standard_False)
26 //=======================================================================
27 //function : TCollection_DoubleMap
29 //=======================================================================
31 TCollection_DoubleMap::TCollection_DoubleMap
32 (const TCollection_DoubleMap& Other) :
33 TCollection_BasicMap(Other.NbBuckets(),Standard_False)
35 if (Other.Extent() != 0)
36 Standard_DomainError::Raise("TCollection:Copy of DoubleMap");
39 //=======================================================================
42 //=======================================================================
44 TCollection_DoubleMap& TCollection_DoubleMap::Assign
45 (const TCollection_DoubleMap& Other)
47 if (this == &Other) return *this;
49 // ReSize(Other.NbBuckets());
50 if (!Other.IsEmpty()) {
51 ReSize(Other.Extent());
52 for (TCollection_DoubleMapIterator It(Other); It.More(); It.Next()) {
53 Bind(It.Key1(),It.Key2());
60 //=======================================================================
63 //=======================================================================
65 void TCollection_DoubleMap::ReSize(const Standard_Integer N)
67 Standard_Integer newBuck;
68 Standard_Address newData1=NULL, newData2=NULL;
69 if (BeginResize(N,newBuck,newData1,newData2)) {
71 TCollection_DoubleMapNode** newdata1 = (TCollection_DoubleMapNode**) newData1;
72 TCollection_DoubleMapNode** newdata2 = (TCollection_DoubleMapNode**) newData2;
73 TCollection_DoubleMapNode** olddata1 = (TCollection_DoubleMapNode**) myData1;
74 TCollection_DoubleMapNode *p, *q;
75 Standard_Integer i,k1,k2;
76 for (i = 0; i <= NbBuckets(); i++) {
80 k1 = Hasher1::HashCode(p->Key1(),newBuck);
81 k2 = Hasher2::HashCode(p->Key2(),newBuck);
82 q = (TCollection_DoubleMapNode*) p->Next();
83 p->Next() = newdata1[k1];
84 p->Next2() = newdata2[k2];
92 EndResize(N,newBuck,newData1,newData2);
96 //=======================================================================
99 //=======================================================================
101 void TCollection_DoubleMap::Clear()
105 TCollection_DoubleMapNode** data1 = (TCollection_DoubleMapNode**) myData1;
106 // TCollection_DoubleMapNode** data2 = (TCollection_DoubleMapNode**) myData2;
107 TCollection_DoubleMapNode *p,*q;
108 for (i = 0; i <= NbBuckets(); i++) {
111 q = (TCollection_DoubleMapNode*) p->Next();
117 TCollection_BasicMap::Destroy();
120 //=======================================================================
123 //=======================================================================
125 void TCollection_DoubleMap::Bind(const TheKey1& K1, const TheKey2& K2)
127 if (Resizable()) ReSize(Extent());
128 TCollection_DoubleMapNode** data1 = (TCollection_DoubleMapNode**)myData1;
129 TCollection_DoubleMapNode** data2 = (TCollection_DoubleMapNode**)myData2;
130 Standard_Integer k1 = Hasher1::HashCode(K1,NbBuckets());
131 Standard_Integer k2 = Hasher2::HashCode(K2,NbBuckets());
132 TCollection_DoubleMapNode* p;
135 if (Hasher1::IsEqual(p->Key1(),K1))
136 Standard_MultiplyDefined::Raise("DoubleMap:Bind");
137 p = (TCollection_DoubleMapNode*) p->Next();
141 if (Hasher2::IsEqual(p->Key2(),K2))
142 Standard_MultiplyDefined::Raise("DoubleMap:Bind");
143 p = (TCollection_DoubleMapNode*)p->Next2();
145 p = new TCollection_DoubleMapNode(K1,K2,data1[k1],data2[k2]);
151 //=======================================================================
152 //function : AreBound
154 //=======================================================================
156 Standard_Boolean TCollection_DoubleMap::AreBound(const TheKey1& K1,
157 const TheKey2& K2) const
159 if (IsEmpty()) return Standard_False;
160 TCollection_DoubleMapNode** data1 = (TCollection_DoubleMapNode**)myData1;
161 TCollection_DoubleMapNode** data2 = (TCollection_DoubleMapNode**)myData2;
162 Standard_Integer k1 = Hasher1::HashCode(K1,NbBuckets());
163 Standard_Integer k2 = Hasher2::HashCode(K2,NbBuckets());
164 TCollection_DoubleMapNode *p1, *p2;
167 if (Hasher1::IsEqual(p1->Key1(),K1)) break;
168 p1 = (TCollection_DoubleMapNode*) p1->Next();
170 if (p1 == NULL) return Standard_False;
173 if (Hasher2::IsEqual(p2->Key2(),K2))
175 p2 = (TCollection_DoubleMapNode*)p2->Next2();
177 if (p2 == NULL) return Standard_False;
181 //=======================================================================
182 //function : IsBound1
184 //=======================================================================
186 Standard_Boolean TCollection_DoubleMap::IsBound1(const TheKey1& K1) const
188 if (IsEmpty()) return Standard_False;
189 TCollection_DoubleMapNode** data1 = (TCollection_DoubleMapNode**)myData1;
190 Standard_Integer k1 = Hasher1::HashCode(K1,NbBuckets());
191 TCollection_DoubleMapNode *p1;
194 if (Hasher1::IsEqual(p1->Key1(),K1)) return Standard_True;
195 p1 = (TCollection_DoubleMapNode*) p1->Next();
197 return Standard_False;
200 //=======================================================================
201 //function : IsBound2
203 //=======================================================================
205 Standard_Boolean TCollection_DoubleMap::IsBound2(const TheKey2& K2) const
207 if (IsEmpty()) return Standard_False;
208 TCollection_DoubleMapNode** data2 = (TCollection_DoubleMapNode**)myData2;
209 Standard_Integer k2 = Hasher2::HashCode(K2,NbBuckets());
210 TCollection_DoubleMapNode *p2;
213 if (Hasher2::IsEqual(p2->Key2(),K2)) return Standard_True;
214 p2 = (TCollection_DoubleMapNode*)p2->Next2();
216 return Standard_False;
219 //=======================================================================
222 //=======================================================================
224 const TheKey2& TCollection_DoubleMap::Find1(const TheKey1& K1) const
226 Standard_NoSuchObject_Raise_if(IsEmpty(),"TCollection_DoubleMap::Find1");
227 TCollection_DoubleMapNode** data1 = (TCollection_DoubleMapNode**)myData1;
228 Standard_Integer k1 = Hasher1::HashCode(K1,NbBuckets());
229 TCollection_DoubleMapNode *p1;
232 if (Hasher1::IsEqual(p1->Key1(),K1)) return p1->Key2();
233 p1 = (TCollection_DoubleMapNode*) p1->Next();
235 Standard_NoSuchObject::Raise("TCollection_DoubleMap::Find1");
239 //=======================================================================
242 //=======================================================================
244 const TheKey1& TCollection_DoubleMap::Find2(const TheKey2& K2) const
246 Standard_NoSuchObject_Raise_if(IsEmpty(),"TCollection_DoubleMap::Find2");
247 TCollection_DoubleMapNode** data2 = (TCollection_DoubleMapNode**)myData2;
248 Standard_Integer k2 = Hasher2::HashCode(K2,NbBuckets());
249 TCollection_DoubleMapNode *p2;
252 if (Hasher2::IsEqual(p2->Key2(),K2)) return p2->Key1();
253 p2 = (TCollection_DoubleMapNode*)p2->Next2();
255 Standard_NoSuchObject::Raise("TCollection_DoubleMap::Find2");
259 //=======================================================================
262 //=======================================================================
264 Standard_Boolean TCollection_DoubleMap::UnBind1(const TheKey1& K1)
266 if (IsEmpty()) return Standard_False;
267 TCollection_DoubleMapNode** data1 = (TCollection_DoubleMapNode**)myData1;
268 TCollection_DoubleMapNode** data2 = (TCollection_DoubleMapNode**)myData2;
269 Standard_Integer k1 = Hasher1::HashCode(K1,NbBuckets());
271 TCollection_DoubleMapNode *p1, *p2, *q1, *q2;
275 if (Hasher1::IsEqual(p1->Key1(),K1)) {
276 // remove from the first
278 q1->Next() = p1->Next();
280 data1[k1] = (TCollection_DoubleMapNode*) p1->Next();
281 // remove from the second
282 k2 = Hasher2::HashCode(p1->Key2(),NbBuckets());
287 q2->Next2() = p2->Next2();
289 data2[k2] = (TCollection_DoubleMapNode*)p2->Next2();
293 p2 = (TCollection_DoubleMapNode*)p2->Next2();
297 return Standard_True;
300 p1 = (TCollection_DoubleMapNode*) p1->Next();
302 return Standard_False;
305 //=======================================================================
308 //=======================================================================
310 Standard_Boolean TCollection_DoubleMap::UnBind2(const TheKey2& K2)
312 if (IsEmpty()) return Standard_False;
313 TCollection_DoubleMapNode** data1 = (TCollection_DoubleMapNode**)myData1;
314 TCollection_DoubleMapNode** data2 = (TCollection_DoubleMapNode**)myData2;
315 Standard_Integer k2 = Hasher2::HashCode(K2,NbBuckets());
317 TCollection_DoubleMapNode *p1, *p2, *q1, *q2;
321 if (Hasher2::IsEqual(p2->Key2(),K2)) {
322 // remove from the second
324 q2->Next2() = p2->Next2();
326 data2[k2] = (TCollection_DoubleMapNode*)p2->Next2();
327 // remove from the first
328 k1 = Hasher1::HashCode(p2->Key1(),NbBuckets());
333 q1->Next() = p1->Next();
335 data1[k1] = (TCollection_DoubleMapNode*) p1->Next();
339 p1 = (TCollection_DoubleMapNode*) p1->Next();
343 return Standard_True;
346 p2 = (TCollection_DoubleMapNode*)p2->Next2();
348 return Standard_False;
351 // method of the iterator
353 //=======================================================================
356 //=======================================================================
358 const TheKey1& TCollection_DoubleMapIterator::Key1() const
360 Standard_NoSuchObject_Raise_if(!More(),"TCollection_DoubleMapIterator::Key1");
361 return ((TCollection_DoubleMapNode*) myNode)->Key1();
364 //=======================================================================
367 //=======================================================================
369 const TheKey2& TCollection_DoubleMapIterator::Key2() const
371 Standard_NoSuchObject_Raise_if(!More(),"TCollection_DoubleMapIterator::Key2");
372 return ((TCollection_DoubleMapNode*) myNode)->Key2();
378 // Copyright Open CasCade......................................Version 5.0-00
379 // Lastly modified by : szy Date : 7-05-2003
381 // File history synopsis (creation,modification,correction)
382 // +---------------------------------------------------------------------------+
383 // ! Developer ! Comments ! Date ! Version !
384 // +-----------!-----------------------------------------!----------!----------+
385 // ! rle ! Creation ! 8-01-1993! 5.0-00-3!
386 // ! szy ! Modified Assign method ! 7-05-2003! 5.0-00-3!
387 // +---------------------------------------------------------------------------+