summaryrefslogtreecommitdiff
path: root/src/TColStd/TColStd_PackedMapOfInteger.cxx
blob: f8f5746426cf76dba2b22f673236f563501b205e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
// File:      TColStd_PackedMapOfInteger.cxx
// Created:   05.11.05 11:25:51
// Author:    Alexander GRIGORIEV
// Copyright: Open Cascade 2005


#include <TColStd_PackedMapOfInteger.hxx>
#include <TColStd_MapIteratorOfPackedMapOfInteger.hxx>
#include <TCollection_MapNode.hxx>
#include <Standard_DefineHandle.hxx>

// 5 lower bits
#define MASK_LOW  0x001f
// 27 upper bits
#define MASK_HIGH (~MASK_LOW)

/**
 * Class implementing a block of 32 consecutive integer values as a node of
 * a Map collection. The data are stored in 64 bits as:
 * - bits  0 - 4 : (number of integers stored in the block) - 1;
 * - bits  5 - 31: base address of the block of integers (low bits assumed 0)
 * - bits 32 - 63: 32-bit field where each bit indicates the presence of the
 *                 corresponding integer in the block. Number of non-zero bits
 *                 must be equal to the number expressed in bits 0-4.
 */
class TColStd_intMapNode : public TCollection_MapNode
{
public:
  inline TColStd_intMapNode (TCollection_MapNode * ptr = 0L)
    : TCollection_MapNode       (ptr),
      myMask                    (0),
      myData                    (0) {}

  inline TColStd_intMapNode (const Standard_Integer theValue,
                             TCollection_MapNode *& ptr)
    : TCollection_MapNode (ptr),
      myMask              ((unsigned int) (theValue & MASK_HIGH)),
      myData              (1 << (theValue & MASK_LOW)) {}

  inline TColStd_intMapNode (unsigned int        theMask,
                             unsigned int        theData,
                             TCollection_MapNode * ptr)
    : TCollection_MapNode (ptr),
      myMask              (theMask),
      myData              (theData) {}

  inline unsigned int           Mask        () const
  { return myMask; }

  inline unsigned int           Data        () const
  { return myData; }

  inline unsigned int&          ChangeMask  ()
  { return myMask; }

  inline unsigned int&          ChangeData  ()
  { return myData; }

  inline Standard_Integer       Key         () const
  { return Standard_Integer (myMask & MASK_HIGH); }

  inline size_t                 NbValues    () const
  { return size_t(myMask & MASK_LOW) + 1; }

  inline Standard_Boolean       HasValues   () const
  { return (myData != 0); }

  Standard_Integer              HasValue (const Standard_Integer theValue) const
  { return (myData & (1 << (theValue & MASK_LOW))); }

  Standard_Boolean              AddValue (const Standard_Integer theValue);

  Standard_Boolean              DelValue (const Standard_Integer theValue);

  /**
   * Find the smallest non-zero bit under the given mask. Outputs the new mask
   * that does not contain the detected bit.
   */
  Standard_Integer              FindNext (unsigned int& theMask) const;

  /**
   * Support of Map interface.
   */
  inline Standard_Integer       HashCode (const Standard_Integer theUpper) const
  {
    return ::HashCode (Standard_Integer(myMask >> 5), theUpper);
  }

  /**
   * Support of Map interface.
   */
  inline Standard_Boolean       IsEqual  (const Standard_Integer theOther) const
  {
    return ((myMask >> 5) == (unsigned)theOther);
  }

  /**
   * Local friend function, used in TColStd_MapIteratorOfPackedMapOfInteger.
   */
  friend Standard_Integer TColStd_intMapNode_findNext(const TColStd_intMapNode*,
                                                      unsigned int& );

  /**
   * Local friend function.
   */
  friend Standard_Integer TColStd_intMapNode_findPrev(const TColStd_intMapNode*,
                                                      unsigned int& );

private:
  unsigned int      myMask;
  unsigned int      myData;
};


//=======================================================================
//function : TColStd_intMapNode::AddValue
//purpose  : 
//=======================================================================

Standard_Boolean TColStd_intMapNode::AddValue (const Standard_Integer theValue)
{
  Standard_Boolean aResult (Standard_False);
  const Standard_Integer aValInt = (1 << (theValue & MASK_LOW));
  if ((myData & aValInt) == 0) {
    myData ^= aValInt;
    myMask++;
    aResult = Standard_True;
  }
  return aResult;
}

//=======================================================================
//function : TColStd_intMapNode::DelValue
//purpose  : 
//=======================================================================

Standard_Boolean TColStd_intMapNode::DelValue (const Standard_Integer theValue)
{
  Standard_Boolean aResult (Standard_False);
  const Standard_Integer aValInt = (1 << (theValue & MASK_LOW));
  if ((myData & aValInt) != 0) {
    myData ^= aValInt;
    myMask--;
    aResult = Standard_True;
  }
  return aResult;
}

//=======================================================================
//function : TColStd_Population
//purpose  : Compute the population (i.e., the number of non-zero bits) of
//           the 32-bit word theData. The population is stored decremented
//           as it is defined in TColStd_intMapNode.
//source   : H.S.Warren, Hacker''s Delight, Addison-Wesley Inc. 2002, Ch.5.1
//=======================================================================

inline size_t TColStd_Population (unsigned int&      theMask,
                                  const unsigned int theData)
{
  unsigned int aRes = theData - ((theData>>1) & 0x55555555);
  aRes  = (aRes & 0x33333333) + ((aRes>>2)    & 0x33333333);
  aRes  = (aRes + (aRes>>4)) & 0x0f0f0f0f;
  aRes = aRes + (aRes>>8);
  aRes = aRes + (aRes>>16);
  theMask = (theMask & MASK_HIGH) | ((aRes-1) & MASK_LOW);
  return (size_t) (aRes & 0x3f);
}

//=======================================================================
//function : TColStd_intMapNode_findNext
//purpose  : Find the smallest non-zero bit under the given mask. Outputs
//           the new mask that does not contain the detected bit.
//=======================================================================

Standard_Integer TColStd_intMapNode_findNext (const TColStd_intMapNode* theNode,
                                              unsigned int&             theMask)
{
  unsigned int val = theNode->myData & theMask;
  int nZeros (0);
  if (val == 0)
    theMask = ~0u;   // void, nothing to do
  else{
    unsigned int aMask = ~0u;
    if ((val & 0x0000ffff) == 0) {
      aMask = 0xffff0000;
      nZeros = 16;
      val >>= 16;
    }
    if ((val & 0x000000ff) == 0) {
      aMask <<= 8;
      nZeros += 8;
      val >>= 8;
    }
    if ((val & 0x0000000f) == 0) {
      aMask <<= 4;
      nZeros += 4;
      val >>= 4;
    }
    if ((val & 0x00000003) == 0) {
      aMask <<= 2;
      nZeros += 2;
      val >>= 2;
    }
    if ((val & 0x00000001) == 0) {
      aMask <<= 1;
      nZeros ++;
    }
    theMask = (aMask << 1);
  }
  return nZeros + theNode->Key();
}

//=======================================================================
//function : TColStd_intMapNode_findPrev
//purpose  : Find the highest non-zero bit under the given mask. Outputs
//           the new mask that does not contain the detected bit.
//=======================================================================

Standard_Integer TColStd_intMapNode_findPrev (const TColStd_intMapNode* theNode,
                                              unsigned int&             theMask)
{
  unsigned int val = theNode->myData & theMask;
  int nZeros (0);
  if (val == 0)
    theMask = ~0u;   // void, nothing to do
  else {
    unsigned int aMask = ~0u;
    if ((val & 0xffff0000) == 0) {
      aMask = 0x0000ffff;
      nZeros = 16;
      val <<= 16;
    }
    if ((val & 0xff000000) == 0) {
      aMask >>= 8;
      nZeros += 8;
      val <<= 8;
    }
    if ((val & 0xf0000000) == 0) {
      aMask >>= 4;
      nZeros += 4;
      val <<= 4;
    }
    if ((val & 0xc0000000) == 0) {
      aMask >>= 2;
      nZeros += 2;
      val <<= 2;
    }
    if ((val & 0x80000000) == 0) {
      aMask >>= 1;
      nZeros ++;
    }
    theMask = (aMask >> 1);
  }
  return (31 - nZeros) + theNode->Key();
}

//=======================================================================
//function : Assign
//purpose  : 
//=======================================================================

TColStd_PackedMapOfInteger& TColStd_PackedMapOfInteger::Assign
                                  (const TColStd_PackedMapOfInteger& theOther)
{
  if (this != &theOther) {
    Clear();
    if  (!theOther.IsEmpty()) { 
      ReSize (theOther.InternalExtent());
      const Standard_Integer nBucketsSrc = theOther.NbBuckets();
      const Standard_Integer nBuckets    = NbBuckets();
      const TColStd_intMapNode** aDataSrc =
        (const TColStd_intMapNode**) theOther.myData1;
      TColStd_intMapNode** aData = (TColStd_intMapNode**) myData1;
      for (Standard_Integer i = 0; i <= nBucketsSrc; i++) {
        const TColStd_intMapNode * p = aDataSrc[i];
        while (p) {
          const Standard_Integer aHashCode = p->HashCode(nBuckets);
          aData[aHashCode] =
            new TColStd_intMapNode (p->Mask(), p->Data(), aData[aHashCode]);
          Increment();
          p = reinterpret_cast <const TColStd_intMapNode*> (p->Next());
        }
      }
//       TColStd_MapIteratorOfPackedMapOfInteger anIt (theOther);
//       for (; anIt.More(); anIt.Next())
//         Add (anIt.Key());
    }
  }
  myExtent  = theOther.myExtent;
  return * this;
}

//=======================================================================
//function : ReSize
//purpose  : 
//=======================================================================

void TColStd_PackedMapOfInteger::ReSize (const Standard_Integer nbBuckets)
{
  Standard_Integer newBuck;
  Standard_Address newData1=NULL, dummy=NULL;
  if (BeginResize (nbBuckets, newBuck, newData1, dummy))
  {
    if (myData1) {
      TColStd_intMapNode** newdata = 
        reinterpret_cast <TColStd_intMapNode**> (newData1);
      TColStd_intMapNode** olddata =
        reinterpret_cast <TColStd_intMapNode**> (myData1);
      TColStd_intMapNode * p;
      Standard_Integer i,k;
      for (i = 0; i <= NbBuckets(); i++) {
	if (olddata[i]) {
	  p = olddata[i];
	  while (p) {
	    k = p->HashCode(newBuck);
            TCollection_MapNode * q = p->Next();
	    p->Next() = newdata[k];
	    newdata[k] = p;
	    p = static_cast <TColStd_intMapNode*> (q);
	  }
	}
      }
    }
    EndResize (nbBuckets, newBuck, newData1, dummy);
  }
}

//=======================================================================
//function : Clear
//purpose  : 
//=======================================================================

void TColStd_PackedMapOfInteger::Clear ()
{
  if (!IsEmpty()) {
    Standard_Integer i;
    TColStd_intMapNode** data =
      reinterpret_cast <TColStd_intMapNode**> (myData1);
    TColStd_intMapNode * p;
    for (i = 0; i <= NbBuckets(); i++) {
      if (data[i]) {
	p = data[i];
	while (p) {
          TCollection_MapNode * q = p->Next();
	  delete p;
          p = static_cast <TColStd_intMapNode*> (q);
	}
      }
    }
  }
  TCollection_BasicMap::Destroy();
  myExtent = 0;
}

//=======================================================================
//function : Add
//purpose  : 
//=======================================================================

Standard_Boolean TColStd_PackedMapOfInteger::Add (const Standard_Integer aKey)
{
  if (Resizable())
    ReSize(InternalExtent());
  Standard_Boolean aResult (Standard_False);
  TColStd_intMapNode  ** data =
    reinterpret_cast <TColStd_intMapNode**> (myData1);
  const Standard_Integer aKeyInt = (unsigned)aKey >> 5;
  const Standard_Integer aHashCode = HashCode (aKeyInt, NbBuckets());
  TCollection_MapNodePtr aBucketHead = data[aHashCode];
  TColStd_intMapNode   * p = static_cast<TColStd_intMapNode*> (aBucketHead);
  while (p) {
    if (p->IsEqual(aKeyInt)) {
      aResult = p->AddValue (aKey);
//       break;
      goto finish;  // goto saves us 4 CPU clocks or 4% performance
    }
    p = reinterpret_cast <TColStd_intMapNode*> (p->Next());
  }
//    if (!p) {         // not needed, as long as we exit the loop by goto
    data[aHashCode] = new TColStd_intMapNode(aKey, aBucketHead);
    Increment();
    aResult = Standard_True;
//    }
 finish:
  if (aResult)
    myExtent++;
  return aResult;
}

//=======================================================================
//function : Contains
//purpose  : 
//=======================================================================

Standard_Boolean TColStd_PackedMapOfInteger::Contains
                                        (const Standard_Integer aKey) const
{
  Standard_Boolean aResult (Standard_False);
  if (!IsEmpty()) {
    TColStd_intMapNode** data = (TColStd_intMapNode**) myData1;
    const Standard_Integer aKeyInt = (unsigned)aKey >> 5;
    TColStd_intMapNode * p = data[HashCode (aKeyInt, NbBuckets())];
    while (p) {
      if (p->IsEqual(aKeyInt)) {
        aResult = (p->HasValue (aKey) != 0);
        break;
      }
      p = reinterpret_cast <TColStd_intMapNode*> (p->Next());
    }
  }
  return aResult;
}

//=======================================================================
//function : Remove
//purpose  : 
//=======================================================================

Standard_Boolean TColStd_PackedMapOfInteger::Remove(const Standard_Integer aKey)
{
  Standard_Boolean aResult (Standard_False);
  if (!IsEmpty()) {
    TColStd_intMapNode**   data =
      reinterpret_cast <TColStd_intMapNode**> (myData1);
    const Standard_Integer aKeyInt = (unsigned)aKey >> 5;
    TColStd_intMapNode*&   aBucketHead = data[HashCode(aKeyInt, NbBuckets())];
    TColStd_intMapNode*    p = aBucketHead;
    TColStd_intMapNode*    q = 0L;
    while (p) {
      if (p->IsEqual(aKeyInt)) {
        aResult = p->DelValue (aKey);
        if (aResult) {
          myExtent--;
          if (p->HasValues() == Standard_False) {
            Decrement();
            if (q) q->Next() = p->Next();
            else aBucketHead = reinterpret_cast<TColStd_intMapNode*>(p->Next());
            delete p;
          }
        }
        break;
      }
      q = p;
      p = reinterpret_cast <TColStd_intMapNode*> (p->Next());
    }
  }
  return aResult;
}

//=======================================================================
//function : GetMinimalMapped
//purpose  : Query the minimal contained key value.
//=======================================================================

Standard_Integer TColStd_PackedMapOfInteger::GetMinimalMapped () const
{
  Standard_Integer aResult (IntegerLast());
  if (!IsEmpty()) {
    const TCollection_MapNode** aData = (const TCollection_MapNode**) myData1;
    const TColStd_intMapNode * pFoundNode = 0L;
    for (Standard_Integer i = 0; i <= NbBuckets(); i++) {
      for (const TCollection_MapNode * p = aData[i]; p != 0L; p = p->Next()) {
        const Standard_Integer aKey =
          reinterpret_cast <const TColStd_intMapNode *>(p)->Key();
        if (aResult > aKey) {
          aResult = aKey;
          pFoundNode = reinterpret_cast<const TColStd_intMapNode *>(p);
        }
      }
    }
    if (pFoundNode) {
      unsigned int aFullMask (0xffffffff);
      aResult = TColStd_intMapNode_findNext (pFoundNode, aFullMask);
    }
  }
  return aResult;
}

//=======================================================================
//function : GetMaximalMapped
//purpose  : Query the maximal contained key value.
//=======================================================================

Standard_Integer TColStd_PackedMapOfInteger::GetMaximalMapped () const
{
  Standard_Integer aResult (IntegerFirst());
  if (!IsEmpty()) {
    const TCollection_MapNode** aData = (const TCollection_MapNode**) myData1;
    const TColStd_intMapNode * pFoundNode = 0L;
    for (Standard_Integer i = 0; i <= NbBuckets(); i++) {
      for (const TCollection_MapNode * p = aData[i]; p != 0L; p = p->Next()) {
        const Standard_Integer aKey =
          reinterpret_cast <const TColStd_intMapNode *>(p)->Key();
        if (aResult < aKey) {
          aResult = aKey;
          pFoundNode = reinterpret_cast<const TColStd_intMapNode *>(p);
        }
      }
    }
    if (pFoundNode) {
      unsigned int aFullMask (0xffffffff);
      aResult = TColStd_intMapNode_findPrev (pFoundNode, aFullMask);
    }
  }
  return aResult;
}

//=======================================================================
//function : Union
//purpose  : Boolean operation OR between 2 maps
//=======================================================================

void TColStd_PackedMapOfInteger::Union (const TColStd_PackedMapOfInteger& theMap1,
                                        const TColStd_PackedMapOfInteger& theMap2)
{
  if (theMap1.IsEmpty()) // 0 | B == B
    Assign (theMap2);
  else if (theMap2.IsEmpty()) // A | 0 == A
    Assign (theMap1);
  else if (myData1 == theMap1.myData1)
    Unite (theMap2);
  else if (myData1 == theMap2.myData1)
    Unite (theMap1);
  else {
    Standard_Integer i;
    const TColStd_intMapNode** aData1 =
      (const TColStd_intMapNode**) theMap1.myData1;
    const TColStd_intMapNode** aData2 =
      (const TColStd_intMapNode**) theMap2.myData1;
    const Standard_Integer nBuckets1 = theMap1.NbBuckets();
    const Standard_Integer nBuckets2 = theMap2.NbBuckets();
    Clear();
    TColStd_intMapNode** aData = (TColStd_intMapNode**) myData1;
    // Iteration of the 1st map.
    for (i = 0; i <= nBuckets1; i++) {
      const TColStd_intMapNode * p1 = aData1[i];
      while (p1 != 0L) {
        // Find aKey - the base address of currently iterated block
        const Standard_Integer aKey = p1->Key();
        const Standard_Integer aKeyInt = (unsigned)aKey >> 5;
        unsigned int aNewMask = p1->Mask();
        unsigned int aNewData = p1->Data();
        size_t       nValues (p1->NbValues());
        // Find the corresponding block in the 2nd map
        const TColStd_intMapNode * p2 =
          aData2 [HashCode (aKeyInt, nBuckets2)];
        while (p2) {
          if (p2->IsEqual(aKeyInt)) {
            aNewData |= p2->Data();
            nValues = TColStd_Population (aNewMask, aNewData);
            break;
          }
          p2 = reinterpret_cast <const TColStd_intMapNode*> (p2->Next());
        }
        // Store the block - result of operation
        if (Resizable()) {
          ReSize(InternalExtent());
          aData = (TColStd_intMapNode**) myData1;
        }
        const Standard_Integer aHashCode = HashCode (aKeyInt, NbBuckets());
        aData[aHashCode] = new TColStd_intMapNode (aNewMask, aNewData,
                                                   aData[aHashCode]);
        Increment();
        myExtent += nValues;
        p1 = reinterpret_cast <const TColStd_intMapNode*> (p1->Next());
      }
    }
    // Iteration of the 2nd map.
    for (i = 0; i <= nBuckets2; i++) {
      const TColStd_intMapNode * p2 = aData2[i];
      while (p2 != 0L) {
        // Find aKey - the base address of currently iterated block
        const Standard_Integer aKey = p2->Key();
        const Standard_Integer aKeyInt = (unsigned)aKey >> 5;
        // Find the corresponding block in the 1st map
        const TColStd_intMapNode * p1 =
          aData1 [HashCode (aKeyInt, nBuckets1)];
        while (p1) {
          if (p1->IsEqual(aKeyInt))
            break;
          p1 = reinterpret_cast <const TColStd_intMapNode*> (p1->Next());
        }
        // Add the block from the 2nd map only in the case when the similar
        // block has not been found in the 1st map
        if (p1 == 0L) {
          if (Resizable()) {
            ReSize(InternalExtent());
            aData = (TColStd_intMapNode**) myData1;
          }
          const Standard_Integer aHashCode = HashCode (aKeyInt, NbBuckets());
          aData[aHashCode]= new TColStd_intMapNode (p2->Mask(), p2->Data(),
                                                    aData[aHashCode]);
          Increment();
          myExtent += p2->NbValues();
        }
        p2 = reinterpret_cast <const TColStd_intMapNode*> (p2->Next());
      }
    }
  }
}

//=======================================================================
//function : Unite
//purpose  : Boolean operation OR with the given map
//=======================================================================

Standard_Boolean TColStd_PackedMapOfInteger::Unite(const TColStd_PackedMapOfInteger& theMap)
{
  if (theMap.IsEmpty() || myData1 == theMap.myData1) // A | 0 == A | A == A
    return Standard_False;
  else if ( IsEmpty() ) { // 0 | B == B
    Assign ( theMap );
    return Standard_True;
  }
  else {
    size_t aNewExtent (myExtent);
    TColStd_intMapNode** aData = (TColStd_intMapNode**) myData1;
    const TColStd_intMapNode** aData2 =
      (const TColStd_intMapNode**) theMap.myData1;
    const Standard_Integer nBuckets2 = theMap.NbBuckets();

    // Iteration of the 2nd map.
    for (Standard_Integer i = 0; i <= nBuckets2; i++) {
      const TColStd_intMapNode * p2 = aData2[i];
      while (p2 != 0L) {
        // Find aKey - the base address of currently iterated block of integers
        const Standard_Integer aKey = p2->Key();
        const Standard_Integer aKeyInt = (unsigned)aKey >> 5;
        // Find the corresponding block in the 1st (this) map
        Standard_Integer aHashCode = HashCode (aKeyInt, NbBuckets());
        TColStd_intMapNode * p1 = aData[aHashCode];
        while (p1) {
          if (p1->IsEqual(aKeyInt)) {
            const size_t anOldPop = p1->NbValues();
            unsigned int newData = p1->Data() | p2->Data();
            if ( newData != p1->Data() ) {
              p1->ChangeData() = newData;
              aNewExtent = aNewExtent - anOldPop +
                           TColStd_Population (p1->ChangeMask(), newData);
            }
            break;
          }
          p1 = reinterpret_cast <TColStd_intMapNode*> (p1->Next());
        }
        // If the block is not found in the 1st map, add it to the 1st map
        if (p1 == 0L) {
          if (Resizable()) {
            ReSize(InternalExtent());
            aData = (TColStd_intMapNode**) myData1;
            aHashCode = HashCode (aKeyInt, NbBuckets());
          }
          aData[aHashCode] = new TColStd_intMapNode (p2->Mask(), p2->Data(),
                                                     aData[aHashCode]);
          Increment();
          aNewExtent += p2->NbValues();
        }
        p2 = reinterpret_cast <TColStd_intMapNode*> (p2->Next());
      }
    }
    Standard_Boolean isChanged = ( myExtent != aNewExtent );
    myExtent = aNewExtent;
    return isChanged;
  }
}

//=======================================================================
//function : Intersection
//purpose  : Boolean operation AND between 2 maps
//=======================================================================

void TColStd_PackedMapOfInteger::Intersection
                                (const TColStd_PackedMapOfInteger& theMap1,
                                 const TColStd_PackedMapOfInteger& theMap2)
{
  if (theMap1.IsEmpty() || theMap2.IsEmpty()) // A & 0 == 0 & B == 0
    Clear();
  else if (myData1 == theMap1.myData1)
    Intersect (theMap2);
  else if (myData1 == theMap2.myData1)
    Intersect (theMap1);
  else {
    const TColStd_intMapNode** aData1, ** aData2;
    Standard_Integer nBuckets1, nBuckets2;
    if (theMap1.Extent() < theMap2.Extent()) {
      aData1 = (const TColStd_intMapNode**) theMap1.myData1;
      aData2 = (const TColStd_intMapNode**) theMap2.myData1;
      nBuckets1 = theMap1.NbBuckets();
      nBuckets2 = theMap2.NbBuckets();
    } 
    else {
      aData1 = (const TColStd_intMapNode**) theMap2.myData1;
      aData2 = (const TColStd_intMapNode**) theMap1.myData1;
      nBuckets1 = theMap2.NbBuckets();
      nBuckets2 = theMap1.NbBuckets();
    }
    Clear();
    TColStd_intMapNode** aData = (TColStd_intMapNode**) myData1;
    // Iteration of the 1st map.
    for (Standard_Integer i = 0; i <= nBuckets1; i++) {
      const TColStd_intMapNode * p1 = aData1[i];
      while (p1 != 0L) {
        // Find aKey - the base address of currently iterated block
        const Standard_Integer aKey = p1->Key();
        const Standard_Integer aKeyInt = (unsigned)aKey >> 5;
        // Find the corresponding block in the 2nd map
        const TColStd_intMapNode * p2 =
          aData2 [HashCode (aKeyInt, nBuckets2)];
        while (p2) {
          if (p2->IsEqual(aKeyInt)) {
            const unsigned int aNewData = p1->Data() & p2->Data();
            // Store the block - result of operation
            if (aNewData) {
              if (Resizable()) {
                ReSize(InternalExtent());
                aData = (TColStd_intMapNode**) myData1;
              }
              const Standard_Integer aHashCode = HashCode (aKeyInt,
                                                           NbBuckets());
              unsigned int aNewMask = p1->Mask();
              myExtent += TColStd_Population (aNewMask, aNewData);
              aData[aHashCode]= new TColStd_intMapNode(aNewMask, aNewData,
                                                       aData[aHashCode]);
              Increment();
            }
            break;
          }
          p2 = reinterpret_cast <const TColStd_intMapNode*> (p2->Next());
        }
        p1 = reinterpret_cast <const TColStd_intMapNode*> (p1->Next());
      }
    }
  }
}

//=======================================================================
//function : Intersect
//purpose  : Boolean operation AND with the given map
//=======================================================================

Standard_Boolean TColStd_PackedMapOfInteger::Intersect
                 (const TColStd_PackedMapOfInteger& theMap)
{
  if ( IsEmpty() ) // 0 & B == 0
    return Standard_False;
  else if (theMap.IsEmpty()) { // A & 0 == 0
    Clear();
    return Standard_True;
  }
  else if (myData1 == theMap.myData1) // A & A == A
    return Standard_False;
  else {
    size_t aNewExtent (0);
    TColStd_intMapNode** aData = (TColStd_intMapNode**) myData1;
    const TColStd_intMapNode** aData2 =
      (const TColStd_intMapNode**) theMap.myData1;
    const Standard_Integer nBuckets2 = theMap.NbBuckets();

    // Iteration of this map.
    for (Standard_Integer i = 0; i <= NbBuckets(); i++) {
      TColStd_intMapNode * q  = 0L;
      TColStd_intMapNode * p1 = aData[i];
      while (p1 != 0L) {
        // Find aKey - the base address of currently iterated block of integers
        const Standard_Integer aKey = p1->Key();
        const Standard_Integer aKeyInt = (unsigned)aKey >> 5;
        // Find the corresponding block in the 2nd map
        const TColStd_intMapNode * p2 =
          aData2 [HashCode (aKeyInt, nBuckets2)];
        while (p2) {
          if (p2->IsEqual(aKeyInt)) {
            const unsigned int aNewData = p1->Data() & p2->Data();
            // Store the block - result of operation
            if (aNewData == 0)
              p2 = 0L;  // no match - the block has to be removed
            else
            {
              if ( aNewData != p1->Data() )
                p1->ChangeData() = aNewData;
              aNewExtent += TColStd_Population (p1->ChangeMask(), aNewData);
            }
            break;
          }
          p2 = reinterpret_cast <const TColStd_intMapNode*> (p2->Next());
        }
        TColStd_intMapNode* pNext =
          reinterpret_cast <TColStd_intMapNode*> (p1->Next());
        // If p2!=NULL, then the map node is kept and we move to the next one
        // Otherwise we should remove the current node
        if (p2)
          q = p1;
        else {
          Decrement();
          if (q)  q->Next() = pNext;
          else    aData[i]  = pNext;
          delete p1;
        }
        p1 = pNext;
      }
    }
    Standard_Boolean isChanged = ( myExtent != aNewExtent );
    myExtent = aNewExtent;
    return isChanged;
  }
}

//=======================================================================
//function : Subtraction
//purpose  : Boolean operation SUBTRACT between two maps
//=======================================================================

void TColStd_PackedMapOfInteger::Subtraction
                                (const TColStd_PackedMapOfInteger& theMap1,
                                 const TColStd_PackedMapOfInteger& theMap2)
{
  if (theMap1.IsEmpty() || theMap2.myData1 == theMap1.myData1) // 0 \ A == A \ A == 0
    Clear();
  else if (theMap2.IsEmpty()) // A \ 0 == A
    Assign (theMap1);
  else if (myData1 == theMap1.myData1)
    Subtract (theMap2);
  else if (myData1 == theMap2.myData1) {
    TColStd_PackedMapOfInteger aMap;
    aMap.Subtraction ( theMap1, theMap2 );
    Assign ( aMap );
  }
  else {
    const TColStd_intMapNode** aData1 =
      (const TColStd_intMapNode**) theMap1.myData1;
    const TColStd_intMapNode** aData2 =
      (const TColStd_intMapNode**) theMap2.myData1;
    const Standard_Integer nBuckets1 = theMap1.NbBuckets();
    const Standard_Integer nBuckets2 = theMap2.NbBuckets();
    Clear();
    TColStd_intMapNode** aData = (TColStd_intMapNode**) myData1;
    // Iteration of the 1st map.
    for (Standard_Integer i = 0; i <= nBuckets1; i++) {
      const TColStd_intMapNode * p1 = aData1[i];
      while (p1 != 0L) {
        // Find aKey - the base address of currently iterated block of integers
        const Standard_Integer aKey = p1->Key();
        const Standard_Integer aKeyInt = (unsigned)aKey >> 5;
        unsigned int aNewMask = p1->Mask();
        unsigned int aNewData = p1->Data();
        size_t       nValues (p1->NbValues());
        // Find the corresponding block in the 2nd map
        const TColStd_intMapNode * p2 =
          aData2 [HashCode (aKeyInt, nBuckets2)];
        while (p2) {
          if (p2->IsEqual(aKeyInt)) {
            aNewData &= ~p2->Data();
            nValues = TColStd_Population (aNewMask, aNewData);
            break;
          }
          p2 = reinterpret_cast <const TColStd_intMapNode*> (p2->Next());
        }
        // Store the block - result of operation
        if (aNewData) {
          if (Resizable()) {
            ReSize(InternalExtent());
            aData = (TColStd_intMapNode**) myData1;
          }
          const Standard_Integer aHashCode = HashCode (aKeyInt, NbBuckets());
          aData[aHashCode]= new TColStd_intMapNode (aNewMask, aNewData,
                                                    aData[aHashCode]);
          Increment();
          myExtent += nValues;
        }
        p1 = reinterpret_cast <const TColStd_intMapNode*> (p1->Next());
      }
    }
  }
}

//=======================================================================
//function : Subtract
//purpose  : Boolean operation SUBTRACT with the given map
//=======================================================================

Standard_Boolean TColStd_PackedMapOfInteger::Subtract
                                (const TColStd_PackedMapOfInteger& theMap)
{
  if ( IsEmpty() || theMap.IsEmpty() ) // 0 \ B == 0; A \ 0 == A
    return Standard_False;
  else if (myData1 == theMap.myData1) { // A \ A == 0
    Clear();
    return Standard_True;
  }
  else {
    size_t aNewExtent (0);
    TColStd_intMapNode** aData = (TColStd_intMapNode**) myData1;
    const TColStd_intMapNode** aData2 =
      (const TColStd_intMapNode**) theMap.myData1;
    const Standard_Integer nBuckets2 = theMap.NbBuckets();
    // Iteration of this map.
    for (Standard_Integer i = 0; i <= NbBuckets(); i++) {
      TColStd_intMapNode * q  = 0L;
      TColStd_intMapNode * p1 = aData[i];
      while (p1 != 0L) {
        // Find aKey - the base address of currently iterated block of integers
        const Standard_Integer aKey = p1->Key();
        const Standard_Integer aKeyInt = (unsigned)aKey >> 5;
        TColStd_intMapNode* pNext =
          reinterpret_cast <TColStd_intMapNode*> (p1->Next());
        // Find the corresponding block in the 2nd map
        const TColStd_intMapNode * p2 =
          aData2 [HashCode (aKeyInt, nBuckets2)];
        while (p2) {
          if (p2->IsEqual(aKeyInt)) {
            const unsigned int aNewData = p1->Data() & ~p2->Data();
            // Store the block - result of operation
            if (aNewData == 0) {
              // no match - the block has to be removed
              Decrement();
              if (q)  q->Next() = pNext;
              else    aData[i]  = pNext;
              delete p1;
            } 
            else if ( aNewData != p1->Data() ) {
              p1->ChangeData() = aNewData;
              aNewExtent += TColStd_Population (p1->ChangeMask(), aNewData);
              q = p1;
            }
            else {
              aNewExtent += p1->NbValues();
              q = p1;
            }
            break;
          }
          p2 = reinterpret_cast <const TColStd_intMapNode*> (p2->Next());
        }
        if (p2 == 0L) {
          aNewExtent += p1->NbValues();
          q = p1;
        }
        p1 = pNext;
      }
    }
    Standard_Boolean isChanged = ( myExtent != aNewExtent );
    myExtent = aNewExtent;
    return isChanged;
  }
}

//=======================================================================
//function : Difference
//purpose  : Boolean operation XOR 
//=======================================================================

void TColStd_PackedMapOfInteger::Difference  (const TColStd_PackedMapOfInteger& theMap1,
                                              const TColStd_PackedMapOfInteger& theMap2)
{
  if (theMap1.IsEmpty()) // 0 ^ B == B
    Assign (theMap2);
  else if (theMap2.IsEmpty()) // A ^ 0 == A
    Assign (theMap1);
  else if (myData1 == theMap1.myData1)
    Differ(theMap2);
  else if (myData1 == theMap2.myData1)
    Differ(theMap1);
  else {
    Standard_Integer i;
    const TColStd_intMapNode** aData1 =
      (const TColStd_intMapNode**) theMap1.myData1;
    const TColStd_intMapNode** aData2 =
      (const TColStd_intMapNode**) theMap2.myData1;
    const Standard_Integer nBuckets1 = theMap1.NbBuckets();
    const Standard_Integer nBuckets2 = theMap2.NbBuckets();
    Clear();
    TColStd_intMapNode** aData = (TColStd_intMapNode**) myData1;

    // Iteration of the 1st map.
    for (i = 0; i <= nBuckets1; i++) {
      const TColStd_intMapNode * p1 = aData1[i];
      while (p1 != 0L) {
        // Find aKey - the base address of currently iterated block of integers
        const Standard_Integer aKey = p1->Key();
        const Standard_Integer aKeyInt = (unsigned)aKey >> 5;
        unsigned int aNewMask = p1->Mask();
        unsigned int aNewData = p1->Data();
        size_t       nValues (p1->NbValues());
        // Find the corresponding block in the 2nd map
        const TColStd_intMapNode * p2 =
          aData2 [HashCode (aKeyInt, nBuckets2)];
        while (p2) {
          if (p2->IsEqual(aKeyInt)) {
            aNewData ^= p2->Data();
            nValues = TColStd_Population (aNewMask, aNewData);
            break;
          }
          p2 = reinterpret_cast <const TColStd_intMapNode*> (p2->Next());
        }
        // Store the block - result of operation
        if (aNewData) {
          if (Resizable()) {
            ReSize(InternalExtent());
            aData = (TColStd_intMapNode**) myData1;
          }
          const Standard_Integer aHashCode = HashCode (aKeyInt, NbBuckets());
          aData[aHashCode]= new TColStd_intMapNode (aNewMask, aNewData,
                                                    aData[aHashCode]);
          Increment();
          myExtent += nValues;
        }
        p1 = reinterpret_cast <const TColStd_intMapNode*> (p1->Next());
      }
    }
    
    // Iteration of the 2nd map.
    for (i = 0; i <= nBuckets2; i++) {
      const TColStd_intMapNode * p2 = aData2[i];
      while (p2 != 0L) {
        // Find aKey - the base address of currently iterated block
        const Standard_Integer aKey = p2->Key();
        const Standard_Integer aKeyInt = (unsigned)aKey >> 5;
        // Find the corresponding block in the 1st map
        const TColStd_intMapNode * p1 =
          aData1 [HashCode (aKeyInt, nBuckets1)];
        while (p1) {
          if (p1->IsEqual(aKeyInt))
            break;
          p1 = reinterpret_cast <const TColStd_intMapNode*> (p1->Next());
        }
        // Add the block from the 2nd map only in the case when the similar
        // block has not been found in the 1st map
        if (p1 == 0L) {
          if (Resizable()) {
            ReSize(InternalExtent());
            aData = (TColStd_intMapNode**) myData1;
          }
          const Standard_Integer aHashCode = HashCode (aKeyInt, NbBuckets());
          aData[aHashCode]= new TColStd_intMapNode (p2->Mask(), p2->Data(),
                                                    aData[aHashCode]);
          Increment();
          myExtent += p2->NbValues();
        }
        p2 = reinterpret_cast <const TColStd_intMapNode*> (p2->Next());
      }
    }
  }
}

//=======================================================================
//function : Differ
//purpose  : Boolean operation XOR 
//=======================================================================
  
Standard_Boolean TColStd_PackedMapOfInteger::Differ(const TColStd_PackedMapOfInteger& theMap)
{
  if (theMap.IsEmpty()) // A ^ 0 = A
    return Standard_False;    
  else if (IsEmpty()) { // 0 ^ B = B
    Assign ( theMap );
    return Standard_True;
  }
  else if( myData1 == theMap.myData1) { // A ^ A == 0
    Clear();
    return Standard_True;
  }
  else {
    size_t aNewExtent (0);
    TColStd_intMapNode** aData1 = (TColStd_intMapNode**) myData1;
    const TColStd_intMapNode** aData2 =
      (const TColStd_intMapNode**) theMap.myData1;
    const Standard_Integer nBuckets2 = theMap.NbBuckets();
    Standard_Boolean isChanged = Standard_False;
    Standard_Integer i = 0;
    // Iteration by other map
    for ( ; i <= nBuckets2; i++) {
       TColStd_intMapNode * q  = 0L;
      const TColStd_intMapNode * p2 = aData2[i];
      while (p2 != 0L) {
        // Find aKey - the base address of currently iterated block
        const Standard_Integer aKey = p2->Key();
        const Standard_Integer aKeyInt = (unsigned)aKey >> 5;
        
        // Find the corresponding block in the 1st map
        TColStd_intMapNode * p1 =
          aData1[HashCode (aKeyInt, NbBuckets())];
        TColStd_intMapNode* pNext =
          reinterpret_cast <TColStd_intMapNode*> (p1->Next());
        
        while (p1) {
          if (p1->IsEqual(aKeyInt)) {
            const unsigned int aNewData = p1->Data() ^ p2->Data();
            // Store the block - result of operation
            if (aNewData == 0) {
              // no match - the block has to be removed
              Decrement();
              if (q)  q->Next() = pNext;
              else    aData1[i]  = pNext;
              delete p1;
            } 
            else if ( aNewData != p1->Data() ) {
              p1->ChangeData() = aNewData;
              isChanged = Standard_True;
              aNewExtent += TColStd_Population (p1->ChangeMask(), aNewData);
              q = p1;
            }
            break;
          }
          p1 = pNext;
        }
        // Add the block from the 2nd map only in the case when the similar
        // block has not been found in the 1st map
        TColStd_intMapNode** aData = NULL;
        if (p1 == 0L) {
          if (Resizable()) {
            ReSize(InternalExtent());
            aData = (TColStd_intMapNode**) myData1;
          }
          const Standard_Integer aHashCode = HashCode (aKeyInt, NbBuckets());
          aData[aHashCode]= new TColStd_intMapNode (p2->Mask(), p2->Data(),
                                                    aData[aHashCode]);
          Increment();
          aNewExtent += p2->NbValues();
          isChanged = Standard_True;
        }
        p2 = reinterpret_cast <const TColStd_intMapNode*> (p2->Next());
      }
    }
    myExtent = aNewExtent;
    return isChanged;
  }
}

//=======================================================================
//function : IsEqual
//purpose  : Boolean operation returns true if this map is equal to the other map  
//=======================================================================

Standard_Boolean TColStd_PackedMapOfInteger::IsEqual(const TColStd_PackedMapOfInteger& theMap) const
{
  if (IsEmpty() && theMap.IsEmpty())
    return Standard_True;
  else if ( Extent() != theMap.Extent() )
    return Standard_False;
  else {
    const TColStd_intMapNode** aData1 = (const TColStd_intMapNode**) myData1;
    const TColStd_intMapNode** aData2 = (const TColStd_intMapNode**) theMap.myData1;
    const Standard_Integer nBuckets2 = theMap.NbBuckets();
    if(aData1 == aData2)
      return Standard_True;
   
    Standard_Integer i = 0;
    // Iteration of this map.
    for (; i <= NbBuckets(); i++) {
      const TColStd_intMapNode * p1 = aData1[i];
      while (p1 != 0L) {
        // Find aKey - the base address of currently iterated block of integers
        const Standard_Integer aKey = p1->Key();
        const Standard_Integer aKeyInt = (unsigned)aKey >> 5;
        TColStd_intMapNode* pNext =
          reinterpret_cast <TColStd_intMapNode*> (p1->Next());
        // Find the corresponding block in the 2nd map
        const TColStd_intMapNode * p2 =
          aData2 [HashCode (aKeyInt, nBuckets2)];
        while (p2) {
          if ( p2->IsEqual(aKeyInt) ) {
            if ( p1->Data() != p2->Data() )
              return Standard_False;
            break;
          }
          p2 = reinterpret_cast <const TColStd_intMapNode*> (p2->Next());
        }
        // if the same block not found, maps are different
        if (p2 == 0L) 
          return Standard_False;
        
        p1 = pNext;
      }
    }
    return Standard_True;
  }
}

//=======================================================================
//function : IsSubset
//purpose  : Boolean operation returns true if this map if subset of other map
//=======================================================================

Standard_Boolean TColStd_PackedMapOfInteger::IsSubset (const TColStd_PackedMapOfInteger& theMap) const
{
  if ( IsEmpty() ) // 0 <= A 
    return Standard_True;
  else if ( theMap.IsEmpty() ) // ! ( A <= 0 )
    return Standard_False;
  else if ( Extent() > theMap.Extent() )
    return Standard_False;
  else {
    const TColStd_intMapNode** aData1 = (const TColStd_intMapNode**) myData1;
    const TColStd_intMapNode** aData2 = (const TColStd_intMapNode**) theMap.myData1;
    if(aData1 == aData2)
      return Standard_True;
    const Standard_Integer nBuckets2 = theMap.NbBuckets();
        
    Standard_Integer i = 0;
    // Iteration of this map.
    for (; i <= NbBuckets(); i++) {
      const TColStd_intMapNode * p1 = aData1[i];
      while (p1 != 0L) {
        // Find aKey - the base address of currently iterated block of integers
        const Standard_Integer aKey = p1->Key();
        const Standard_Integer aKeyInt = (unsigned)aKey >> 5;
        TColStd_intMapNode* pNext =
          reinterpret_cast <TColStd_intMapNode*> (p1->Next());
        // Find the corresponding block in the 2nd map
        const TColStd_intMapNode * p2 =
          aData2 [HashCode (aKeyInt, nBuckets2)];
        while (p2) {
          if ( p2->IsEqual(aKeyInt) ) {
            if ( p1->Data() & ~p2->Data() ) // at least one bit set in p1 is not set in p2
              return Standard_False;
            break;
          }
          p2 = reinterpret_cast <const TColStd_intMapNode*> (p2->Next());
        }
        p1 = pNext;
      }
    }
    return Standard_True;
  }
}

//=======================================================================
//function : HasIntersection
//purpose  : Boolean operation returns true if this map intersects with other map
//=======================================================================

Standard_Boolean TColStd_PackedMapOfInteger::HasIntersection (const TColStd_PackedMapOfInteger& theMap) const
{
  if (IsEmpty() || theMap.IsEmpty()) // A * 0 == 0 * B == 0
    return Standard_False;
  else {
    const TColStd_intMapNode** aData1 = (const TColStd_intMapNode**) myData1;
    const TColStd_intMapNode** aData2 = (const TColStd_intMapNode**) theMap.myData1;
    const Standard_Integer nBuckets2 = theMap.NbBuckets();
    if(aData1 == aData2)
      return Standard_True;
        
    Standard_Integer i = 0;
    // Iteration of this map.
    for (; i <= NbBuckets(); i++) {
      const TColStd_intMapNode * p1 = aData1[i];
      while (p1 != 0L) {
        // Find aKey - the base address of currently iterated block of integers
        const Standard_Integer aKey = p1->Key();
        const Standard_Integer aKeyInt = (unsigned)aKey >> 5;
        TColStd_intMapNode* pNext =
          reinterpret_cast <TColStd_intMapNode*> (p1->Next());
        // Find the corresponding block in the 2nd map
        const TColStd_intMapNode * p2 =
          aData2 [HashCode (aKeyInt, nBuckets2)];
        while (p2) {
          if (p2->IsEqual(aKeyInt)) {
            if ( p1->Data() & p2->Data() )
              return Standard_True;
            break;
          }
          p2 = reinterpret_cast <const TColStd_intMapNode*> (p2->Next());
        }
        p1 = pNext;
      }
    }
    return Standard_False;
  }
}