File indexing completed on 2026-09-21 09:17:57
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #ifndef NCollection_PackedMapAlgo_HeaderFile
0015 #define NCollection_PackedMapAlgo_HeaderFile
0016
0017 #include <NCollection_PackedMap.hxx>
0018
0019
0020
0021
0022
0023
0024 namespace NCollection_PackedMapAlgo
0025 {
0026
0027
0028
0029
0030 template <class IntType>
0031 bool Unite(NCollection_PackedMap<IntType>& theMap,
0032 const NCollection_PackedMap<IntType>& theOtherMap)
0033 {
0034 if (theOtherMap.IsEmpty() || &theMap == &theOtherMap)
0035 return false;
0036 else if (theMap.IsEmpty())
0037 {
0038 theMap.Assign(theOtherMap);
0039 return true;
0040 }
0041
0042 const int anOldExtent = theMap.Extent();
0043 for (typename NCollection_PackedMap<IntType>::Iterator anIter(theOtherMap); anIter.More();
0044 anIter.Next())
0045 {
0046 theMap.Add(anIter.Key());
0047 }
0048 return anOldExtent != theMap.Extent();
0049 }
0050
0051
0052
0053
0054
0055
0056 template <class IntType>
0057 void Union(NCollection_PackedMap<IntType>& theMap,
0058 const NCollection_PackedMap<IntType>& theLeftMap,
0059 const NCollection_PackedMap<IntType>& theRightMap)
0060 {
0061 if (theLeftMap.IsEmpty())
0062 theMap.Assign(theRightMap);
0063 else if (theRightMap.IsEmpty())
0064 theMap.Assign(theLeftMap);
0065 else if (&theMap == &theLeftMap)
0066 Unite(theMap, theRightMap);
0067 else if (&theMap == &theRightMap)
0068 Unite(theMap, theLeftMap);
0069 else
0070 {
0071 theMap.Clear();
0072 for (typename NCollection_PackedMap<IntType>::Iterator anIter(theLeftMap); anIter.More();
0073 anIter.Next())
0074 {
0075 theMap.Add(anIter.Key());
0076 }
0077 for (typename NCollection_PackedMap<IntType>::Iterator anIter(theRightMap); anIter.More();
0078 anIter.Next())
0079 {
0080 theMap.Add(anIter.Key());
0081 }
0082 }
0083 }
0084
0085
0086
0087
0088
0089 template <class IntType>
0090 bool Intersect(NCollection_PackedMap<IntType>& theMap,
0091 const NCollection_PackedMap<IntType>& theOtherMap)
0092 {
0093 if (theMap.IsEmpty())
0094 return false;
0095 else if (theOtherMap.IsEmpty())
0096 {
0097 theMap.Clear();
0098 return true;
0099 }
0100 else if (&theMap == &theOtherMap)
0101 return false;
0102
0103 const int anOldExtent = theMap.Extent();
0104 NCollection_PackedMap<IntType> aCopy;
0105 for (typename NCollection_PackedMap<IntType>::Iterator anIter(theMap); anIter.More();
0106 anIter.Next())
0107 {
0108 if (theOtherMap.Contains(anIter.Key()))
0109 {
0110 aCopy.Add(anIter.Key());
0111 }
0112 }
0113 theMap = std::move(aCopy);
0114 return anOldExtent != theMap.Extent();
0115 }
0116
0117
0118
0119
0120
0121 template <class IntType>
0122 void Intersection(NCollection_PackedMap<IntType>& theMap,
0123 const NCollection_PackedMap<IntType>& theLeftMap,
0124 const NCollection_PackedMap<IntType>& theRightMap)
0125 {
0126 if (theLeftMap.IsEmpty() || theRightMap.IsEmpty())
0127 theMap.Clear();
0128 else if (&theMap == &theLeftMap)
0129 Intersect(theMap, theRightMap);
0130 else if (&theMap == &theRightMap)
0131 Intersect(theMap, theLeftMap);
0132 else
0133 {
0134 theMap.Clear();
0135 const NCollection_PackedMap<IntType>* aSmaller = &theLeftMap;
0136 const NCollection_PackedMap<IntType>* aLarger = &theRightMap;
0137 if (theLeftMap.Extent() > theRightMap.Extent())
0138 {
0139 aSmaller = &theRightMap;
0140 aLarger = &theLeftMap;
0141 }
0142 for (typename NCollection_PackedMap<IntType>::Iterator anIter(*aSmaller); anIter.More();
0143 anIter.Next())
0144 {
0145 if (aLarger->Contains(anIter.Key()))
0146 {
0147 theMap.Add(anIter.Key());
0148 }
0149 }
0150 }
0151 }
0152
0153
0154
0155
0156
0157 template <class IntType>
0158 bool Subtract(NCollection_PackedMap<IntType>& theMap,
0159 const NCollection_PackedMap<IntType>& theOtherMap)
0160 {
0161 if (&theMap == &theOtherMap)
0162 {
0163 if (theMap.IsEmpty())
0164 {
0165 return false;
0166 }
0167 theMap.Clear();
0168 return true;
0169 }
0170
0171 if (theMap.IsEmpty() || theOtherMap.IsEmpty())
0172 return false;
0173
0174 const int anOldExtent = theMap.Extent();
0175 for (typename NCollection_PackedMap<IntType>::Iterator anIter(theOtherMap); anIter.More();
0176 anIter.Next())
0177 {
0178 theMap.Remove(anIter.Key());
0179 }
0180 return anOldExtent != theMap.Extent();
0181 }
0182
0183
0184
0185
0186
0187 template <class IntType>
0188 void Subtraction(NCollection_PackedMap<IntType>& theMap,
0189 const NCollection_PackedMap<IntType>& theLeftMap,
0190 const NCollection_PackedMap<IntType>& theRightMap)
0191 {
0192 if (&theMap == &theLeftMap)
0193 {
0194 Subtract(theMap, theRightMap);
0195 return;
0196 }
0197 else if (&theMap == &theRightMap)
0198 {
0199 NCollection_PackedMap<IntType> aCopy;
0200 Subtraction(aCopy, theLeftMap, theRightMap);
0201 theMap = std::move(aCopy);
0202 return;
0203 }
0204
0205 theMap.Assign(theLeftMap);
0206 Subtract(theMap, theRightMap);
0207 }
0208
0209
0210
0211
0212
0213 template <class IntType>
0214 bool Differ(NCollection_PackedMap<IntType>& theMap,
0215 const NCollection_PackedMap<IntType>& theOtherMap)
0216 {
0217 if (&theMap == &theOtherMap)
0218 {
0219 if (theMap.IsEmpty())
0220 {
0221 return false;
0222 }
0223 theMap.Clear();
0224 return true;
0225 }
0226
0227 if (theOtherMap.IsEmpty())
0228 return false;
0229 else if (theMap.IsEmpty())
0230 {
0231 theMap.Assign(theOtherMap);
0232 return true;
0233 }
0234
0235 const int anOldExtent = theMap.Extent();
0236 for (typename NCollection_PackedMap<IntType>::Iterator anIter(theOtherMap); anIter.More();
0237 anIter.Next())
0238 {
0239 const IntType aKey = anIter.Key();
0240 if (theMap.Contains(aKey))
0241 theMap.Remove(aKey);
0242 else
0243 theMap.Add(aKey);
0244 }
0245 return anOldExtent != theMap.Extent();
0246 }
0247
0248
0249
0250
0251
0252
0253 template <class IntType>
0254 void Difference(NCollection_PackedMap<IntType>& theMap,
0255 const NCollection_PackedMap<IntType>& theLeftMap,
0256 const NCollection_PackedMap<IntType>& theRightMap)
0257 {
0258 if (&theLeftMap == &theRightMap)
0259 {
0260 theMap.Clear();
0261 return;
0262 }
0263 else if (&theMap == &theLeftMap)
0264 {
0265 Differ(theMap, theRightMap);
0266 return;
0267 }
0268 else if (&theMap == &theRightMap)
0269 {
0270 Differ(theMap, theLeftMap);
0271 return;
0272 }
0273
0274 theMap.Clear();
0275 for (typename NCollection_PackedMap<IntType>::Iterator anIter(theLeftMap); anIter.More();
0276 anIter.Next())
0277 {
0278 if (!theRightMap.Contains(anIter.Key()))
0279 {
0280 theMap.Add(anIter.Key());
0281 }
0282 }
0283 for (typename NCollection_PackedMap<IntType>::Iterator anIter(theRightMap); anIter.More();
0284 anIter.Next())
0285 {
0286 if (!theLeftMap.Contains(anIter.Key()))
0287 {
0288 theMap.Add(anIter.Key());
0289 }
0290 }
0291 }
0292
0293
0294 template <class IntType>
0295 bool HasIntersection(const NCollection_PackedMap<IntType>& theMap,
0296 const NCollection_PackedMap<IntType>& theOtherMap)
0297 {
0298 if (theMap.IsEmpty() || theOtherMap.IsEmpty())
0299 return false;
0300
0301 if (&theMap == &theOtherMap)
0302 return true;
0303
0304 const NCollection_PackedMap<IntType>* aSmaller = &theMap;
0305 const NCollection_PackedMap<IntType>* aLarger = &theOtherMap;
0306 if (theMap.Extent() > theOtherMap.Extent())
0307 {
0308 aSmaller = &theOtherMap;
0309 aLarger = &theMap;
0310 }
0311
0312 for (typename NCollection_PackedMap<IntType>::Iterator anIter(*aSmaller); anIter.More();
0313 anIter.Next())
0314 {
0315 if (aLarger->Contains(anIter.Key()))
0316 {
0317 return true;
0318 }
0319 }
0320
0321 return false;
0322 }
0323
0324
0325
0326 template <class IntType>
0327 bool Contains(const NCollection_PackedMap<IntType>& theMap,
0328 const NCollection_PackedMap<IntType>& theOtherMap)
0329 {
0330 if (&theMap == &theOtherMap || theOtherMap.IsEmpty())
0331 {
0332 return true;
0333 }
0334 else if (theMap.Extent() < theOtherMap.Extent())
0335 {
0336 return false;
0337 }
0338
0339 for (typename NCollection_PackedMap<IntType>::Iterator anIter(theOtherMap); anIter.More();
0340 anIter.Next())
0341 {
0342 if (!theMap.Contains(anIter.Key()))
0343 {
0344 return false;
0345 }
0346 }
0347
0348 return true;
0349 }
0350
0351
0352
0353
0354 template <class IntType>
0355 bool IsSubset(const NCollection_PackedMap<IntType>& theMap,
0356 const NCollection_PackedMap<IntType>& theOtherMap)
0357 {
0358 return Contains(theOtherMap, theMap);
0359 }
0360
0361
0362
0363 template <class IntType>
0364 bool IsEqual(const NCollection_PackedMap<IntType>& theMap,
0365 const NCollection_PackedMap<IntType>& theOtherMap)
0366 {
0367 if (&theMap == &theOtherMap)
0368 {
0369 return true;
0370 }
0371 return theMap.Extent() == theOtherMap.Extent() && Contains(theMap, theOtherMap);
0372 }
0373
0374 }
0375
0376 #endif