File indexing completed on 2024-11-15 09:49:05
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 #ifndef TColStd_PackedMapOfInteger_HeaderFile
0017 #define TColStd_PackedMapOfInteger_HeaderFile
0018
0019 #include <Standard.hxx>
0020 #include <Standard_Address.hxx>
0021 #include <Standard_Boolean.hxx>
0022 #include <Standard_DefineAlloc.hxx>
0023 #include <Standard_Integer.hxx>
0024 #include <Standard_NoSuchObject.hxx>
0025 #include <Standard_OStream.hxx>
0026 #include <Standard_Handle.hxx>
0027
0028
0029
0030
0031 class TColStd_PackedMapOfInteger
0032 {
0033 public:
0034 DEFINE_STANDARD_ALLOC
0035
0036 private:
0037
0038
0039 static const unsigned int MASK_LOW = 0x001f;
0040
0041
0042 static const unsigned int MASK_HIGH = ~MASK_LOW;
0043
0044
0045
0046
0047
0048
0049
0050 class TColStd_intMapNode
0051 {
0052 public:
0053 TColStd_intMapNode (TColStd_intMapNode* thePtr = NULL)
0054 : myNext (thePtr), myMask (0), myData (0) {}
0055
0056 TColStd_intMapNode (Standard_Integer theValue, TColStd_intMapNode*& thePtr)
0057 : myNext (thePtr),
0058 myMask ((unsigned int) (theValue & MASK_HIGH)),
0059 myData (1 << (theValue & MASK_LOW)) {}
0060
0061 TColStd_intMapNode (unsigned int theMask, unsigned int theData, TColStd_intMapNode* thePtr)
0062 : myNext (thePtr),
0063 myMask (theMask),
0064 myData (theData) {}
0065
0066 unsigned int Mask() const { return myMask; }
0067
0068 unsigned int Data() const { return myData; }
0069
0070 unsigned int& ChangeMask() { return myMask; }
0071
0072 unsigned int& ChangeData() { return myData; }
0073
0074
0075 Standard_Integer Key() const { return Standard_Integer (myMask & MASK_HIGH); }
0076
0077
0078 size_t NbValues() const { return size_t(myMask & MASK_LOW) + 1; }
0079
0080
0081 Standard_Boolean HasValues() const { return (myData != 0); }
0082
0083
0084 Standard_Integer HasValue (Standard_Integer theValue) const { return (myData & (1 << (theValue & MASK_LOW))); }
0085
0086
0087
0088 Standard_Boolean AddValue (Standard_Integer theValue)
0089 {
0090 const Standard_Integer aValInt = (1 << (theValue & MASK_LOW));
0091 if ((myData & aValInt) == 0)
0092 {
0093 myData ^= aValInt;
0094 ++myMask;
0095 return Standard_True;
0096 }
0097 return Standard_False;
0098 }
0099
0100
0101
0102 Standard_Boolean DelValue (Standard_Integer theValue)
0103 {
0104 const Standard_Integer aValInt = (1 << (theValue & MASK_LOW));
0105 if ((myData & aValInt) != 0)
0106 {
0107 myData ^= aValInt;
0108 myMask--;
0109 return Standard_True;
0110 }
0111 return Standard_False;
0112 }
0113
0114
0115
0116 Standard_Integer FindNext (unsigned int& theMask) const;
0117
0118
0119 TColStd_intMapNode* Next() const { return myNext; }
0120
0121
0122 void SetNext (TColStd_intMapNode* theNext) { myNext = theNext; }
0123
0124 public:
0125
0126 Standard_Integer HashCode (Standard_Integer theUpper) const
0127 {
0128 return ::HashCode (Standard_Integer(myMask >> 5), theUpper);
0129 }
0130
0131
0132 Standard_Boolean IsEqual (Standard_Integer theOther) const
0133 {
0134 return ((myMask >> 5) == (unsigned)theOther);
0135 }
0136
0137 private:
0138 TColStd_intMapNode* myNext;
0139 unsigned int myMask;
0140 unsigned int myData;
0141 };
0142
0143 public:
0144
0145
0146 class Iterator
0147 {
0148 public:
0149
0150
0151 Iterator()
0152 : myBuckets (NULL),
0153 myNode (NULL),
0154 myNbBuckets (-1),
0155 myBucket (-1),
0156 myIntMask (~0U),
0157 myKey (0) {}
0158
0159
0160 Iterator (const TColStd_PackedMapOfInteger& theMap)
0161 : myBuckets (theMap.myData1),
0162 myNode (NULL),
0163 myNbBuckets (theMap.myData1 != NULL ? theMap.myNbBuckets : -1),
0164 myBucket (-1),
0165 myIntMask (~0U)
0166 {
0167 next();
0168 myKey = myNode != NULL ? TColStd_intMapNode_findNext (myNode, myIntMask) : 0;
0169 }
0170
0171
0172 void Initialize (const TColStd_PackedMapOfInteger& theMap)
0173 {
0174 myBuckets = theMap.myData1;
0175 myBucket = -1;
0176 myNode = NULL;
0177 myNbBuckets = theMap.myData1 != NULL ? theMap.myNbBuckets : -1;
0178 next();
0179
0180 myIntMask = ~0U;
0181 myKey = myNode != NULL ? TColStd_intMapNode_findNext (myNode, myIntMask) : 0;
0182 }
0183
0184
0185 void Reset()
0186 {
0187 myBucket = -1;
0188 myNode = NULL;
0189 next();
0190
0191 myIntMask = ~0U;
0192 myKey = myNode != NULL ? TColStd_intMapNode_findNext (myNode, myIntMask) : 0;
0193 }
0194
0195
0196 Standard_Integer Key() const
0197 {
0198 Standard_NoSuchObject_Raise_if ((myIntMask == ~0U), "TColStd_MapIteratorOfPackedMapOfInteger::Key");
0199 return myKey;
0200 }
0201
0202
0203 Standard_Boolean More() const { return myNode != NULL; }
0204
0205
0206 void Next()
0207 {
0208 for (; myNode != NULL; next())
0209 {
0210 myKey = TColStd_intMapNode_findNext (myNode, myIntMask);
0211 if (myIntMask != ~0u)
0212 {
0213 break;
0214 }
0215 }
0216 }
0217 private:
0218
0219 void next()
0220 {
0221 if (myBuckets == NULL)
0222 {
0223 return;
0224 }
0225
0226 if (myNode != NULL)
0227 {
0228 myNode = myNode->Next();
0229 }
0230
0231 while (myNode == NULL)
0232 {
0233 ++myBucket;
0234 if (myBucket > myNbBuckets)
0235 {
0236 return;
0237 }
0238 myNode = myBuckets[myBucket];
0239 }
0240 }
0241
0242 private:
0243 TColStd_intMapNode** myBuckets;
0244 TColStd_intMapNode* myNode;
0245 Standard_Integer myNbBuckets;
0246 Standard_Integer myBucket;
0247
0248 unsigned int myIntMask;
0249 Standard_Integer myKey;
0250 };
0251
0252 public:
0253
0254
0255 TColStd_PackedMapOfInteger (const Standard_Integer theNbBuckets = 1)
0256 : myData1 (NULL),
0257 myNbBuckets (theNbBuckets),
0258 myNbPackedMapNodes (0),
0259 myExtent (0) {}
0260
0261
0262 TColStd_PackedMapOfInteger (const TColStd_PackedMapOfInteger& theOther)
0263 : myData1 (NULL),
0264 myNbBuckets (1),
0265 myNbPackedMapNodes (0),
0266 myExtent (0)
0267 {
0268 Assign (theOther);
0269 }
0270
0271 inline TColStd_PackedMapOfInteger&
0272 operator = (const TColStd_PackedMapOfInteger& Other)
0273 { return Assign(Other); }
0274
0275 Standard_EXPORT TColStd_PackedMapOfInteger&
0276 Assign (const TColStd_PackedMapOfInteger&);
0277 Standard_EXPORT void ReSize (const Standard_Integer NbBuckets);
0278 Standard_EXPORT void Clear ();
0279 ~TColStd_PackedMapOfInteger() { Clear(); }
0280 Standard_EXPORT Standard_Boolean
0281 Add (const Standard_Integer aKey);
0282 Standard_EXPORT Standard_Boolean
0283 Contains (const Standard_Integer aKey) const;
0284 Standard_EXPORT Standard_Boolean
0285 Remove (const Standard_Integer aKey);
0286
0287
0288 Standard_Integer NbBuckets() const { return myNbBuckets; }
0289
0290
0291 Standard_Integer Extent() const { return Standard_Integer (myExtent); }
0292
0293
0294 Standard_Boolean IsEmpty() const { return myNbPackedMapNodes == 0; }
0295
0296
0297
0298
0299 Standard_EXPORT Standard_Integer GetMinimalMapped () const;
0300
0301
0302
0303
0304 Standard_EXPORT Standard_Integer GetMaximalMapped () const;
0305
0306
0307
0308 Standard_EXPORT void Statistics (Standard_OStream& theStream) const;
0309
0310 public:
0311
0312
0313
0314
0315
0316
0317
0318
0319 Standard_EXPORT void Union (const TColStd_PackedMapOfInteger&,
0320 const TColStd_PackedMapOfInteger&);
0321
0322
0323
0324
0325
0326
0327
0328 Standard_EXPORT Standard_Boolean Unite (const TColStd_PackedMapOfInteger&);
0329
0330
0331
0332
0333 TColStd_PackedMapOfInteger& operator |= (const TColStd_PackedMapOfInteger& MM)
0334 { Unite(MM); return *this; }
0335
0336
0337
0338
0339
0340
0341
0342 Standard_EXPORT void Intersection (const TColStd_PackedMapOfInteger&,
0343 const TColStd_PackedMapOfInteger&);
0344
0345
0346
0347
0348
0349
0350
0351 Standard_EXPORT Standard_Boolean Intersect (const TColStd_PackedMapOfInteger&);
0352
0353
0354
0355
0356 TColStd_PackedMapOfInteger& operator &= (const TColStd_PackedMapOfInteger& MM)
0357 { Intersect(MM); return *this; }
0358
0359
0360
0361
0362
0363
0364
0365
0366 Standard_EXPORT void Subtraction (const TColStd_PackedMapOfInteger&,
0367 const TColStd_PackedMapOfInteger&);
0368
0369
0370
0371
0372
0373
0374
0375 Standard_EXPORT Standard_Boolean Subtract (const TColStd_PackedMapOfInteger&);
0376
0377
0378
0379
0380 TColStd_PackedMapOfInteger& operator -= (const TColStd_PackedMapOfInteger& MM)
0381 { Subtract(MM); return *this; }
0382
0383
0384
0385
0386
0387
0388
0389 Standard_EXPORT void Difference (const TColStd_PackedMapOfInteger&,
0390 const TColStd_PackedMapOfInteger&);
0391
0392
0393
0394
0395
0396
0397
0398 Standard_EXPORT Standard_Boolean Differ (const TColStd_PackedMapOfInteger&);
0399
0400
0401
0402
0403 TColStd_PackedMapOfInteger& operator ^= (const TColStd_PackedMapOfInteger& MM)
0404 { Differ(MM); return *this; }
0405
0406
0407
0408
0409
0410 Standard_EXPORT Standard_Boolean IsEqual (const TColStd_PackedMapOfInteger&) const;
0411
0412
0413
0414
0415 Standard_Boolean operator == (const TColStd_PackedMapOfInteger& MM) const
0416 { return IsEqual(MM); }
0417
0418
0419
0420
0421
0422
0423 Standard_EXPORT Standard_Boolean IsSubset (const TColStd_PackedMapOfInteger&) const;
0424
0425
0426
0427
0428 Standard_Boolean operator <= (const TColStd_PackedMapOfInteger& MM) const
0429 { return IsSubset(MM); }
0430
0431
0432
0433
0434 Standard_EXPORT Standard_Boolean HasIntersection (const TColStd_PackedMapOfInteger&) const;
0435
0436
0437
0438 protected:
0439
0440
0441 Standard_Boolean Resizable() const { return IsEmpty() || (myNbPackedMapNodes > myNbBuckets); }
0442
0443
0444 static Standard_Integer packedKeyIndex (Standard_Integer theKey) { return (unsigned)theKey >> 5; }
0445
0446 private:
0447
0448
0449
0450 Standard_EXPORT static Standard_Integer TColStd_intMapNode_findNext (const TColStd_intMapNode* theNode,
0451 unsigned int& theMask);
0452
0453
0454
0455 Standard_EXPORT static Standard_Integer TColStd_intMapNode_findPrev (const TColStd_intMapNode* theNode,
0456 unsigned int& theMask);
0457
0458
0459
0460
0461 static size_t TColStd_Population (unsigned int& theMask, unsigned int theData)
0462 {
0463 unsigned int aRes = theData - ((theData>>1) & 0x55555555);
0464 aRes = (aRes & 0x33333333) + ((aRes>>2) & 0x33333333);
0465 aRes = (aRes + (aRes>>4)) & 0x0f0f0f0f;
0466 aRes = aRes + (aRes>>8);
0467 aRes = aRes + (aRes>>16);
0468 theMask = (theMask & TColStd_PackedMapOfInteger::MASK_HIGH) | ((aRes - 1) & TColStd_PackedMapOfInteger::MASK_LOW);
0469 return size_t(aRes & 0x3f);
0470 }
0471
0472 private:
0473
0474 TColStd_intMapNode** myData1;
0475 Standard_Integer myNbBuckets;
0476 Standard_Integer myNbPackedMapNodes;
0477 Standard_Size myExtent;
0478 };
0479
0480 #endif