a2df411c470b7001d79fd248b2e793fcf04d15b3
[occt.git] / src / TColStd / TColStd_PackedMapOfInteger.hxx
1 // File:      TColStd_PackedMapOfInteger.hxx
2 // Created:   05.11.05 10:43:14
3 // Author:    Alexander GRIGORIEV
4 // Copyright: Open Cascade 2005
5
6
7 #ifndef TColStd_PackedMapOfInteger_HeaderFile
8 #define TColStd_PackedMapOfInteger_HeaderFile
9
10 #include <Standard_DefineAlloc.hxx>
11 #include <TCollection_BasicMap.hxx>
12
13 /**
14  *  Optimized Map of integer values. Each block of 32 integers is stored in
15  *  8 bytes in memory.
16  */
17
18 class TColStd_PackedMapOfInteger : private TCollection_BasicMap
19 {
20  public:
21   // operators new and delete must be defined explicitly 
22   // since inherited ones are not accessible
23   DEFINE_STANDARD_ALLOC
24   
25  public:
26   // ---------- PUBLIC METHODS ----------
27
28   /// Constructor
29   inline  TColStd_PackedMapOfInteger  (const Standard_Integer NbBuckets = 1)
30     : TCollection_BasicMap (NbBuckets, Standard_True),
31       myExtent             (0) {}
32
33   inline TColStd_PackedMapOfInteger&
34                           operator =  (const TColStd_PackedMapOfInteger& Other) 
35   { return Assign(Other); }
36
37   Standard_EXPORT TColStd_PackedMapOfInteger&
38                           Assign        (const TColStd_PackedMapOfInteger&);
39   Standard_EXPORT  void   ReSize        (const Standard_Integer NbBuckets);
40   Standard_EXPORT  void   Clear         ();
41   ~TColStd_PackedMapOfInteger() { Clear(); }
42   Standard_EXPORT  Standard_Boolean
43                           Add           (const Standard_Integer aKey);
44   Standard_EXPORT  Standard_Boolean
45                           Contains      (const Standard_Integer aKey) const;
46   Standard_EXPORT  Standard_Boolean
47                           Remove        (const Standard_Integer aKey);
48
49   inline Standard_Integer NbBuckets     () const
50   { return TCollection_BasicMap::NbBuckets(); }
51
52   inline Standard_Integer Extent        () const
53   { return Standard_Integer (myExtent); }
54
55   inline Standard_Integer IsEmpty       () const
56   { return TCollection_BasicMap::IsEmpty(); }
57
58   inline void             Statistics    (Standard_OStream& outStream) const
59   { TCollection_BasicMap::Statistics (outStream); }
60
61   /**
62    * Query the minimal contained key value.
63    */
64   Standard_EXPORT Standard_Integer GetMinimalMapped () const;
65
66   /**
67    * Query the maximal contained key value.
68    */
69   Standard_EXPORT Standard_Integer GetMaximalMapped () const;
70
71 public:
72   //!@name Boolean operations with maps as sets of integers
73   //!@{
74   
75   /**
76    * Sets this Map to be the result of union (aka addition, fuse, merge, 
77    * boolean OR) operation between two given Maps.
78    * The new Map contains the values that are contained either in the
79    * first map or in the second map or in both.<br>
80    * All previous contents of this Map is cleared. This map (result of the
81    * boolean operation) can also be passed as one of operands.<br>
82    */
83   Standard_EXPORT void Union (const TColStd_PackedMapOfInteger&,
84                               const TColStd_PackedMapOfInteger&);
85
86   /**
87    * Apply to this Map the boolean operation union (aka addition, fuse, merge,
88    * boolean OR) with another (given) Map.
89    * The result contains the values that were previously contained in
90    * this map or contained in the given (operand) map.<br>
91    * This algorithm is similar to method Union().
92    * Returns True if contents of this map is changed.
93    */
94   Standard_EXPORT Standard_Boolean Unite (const TColStd_PackedMapOfInteger&);
95
96   /**
97    * Overloaded operator version of Unite().
98    */
99   TColStd_PackedMapOfInteger& operator |= (const TColStd_PackedMapOfInteger& MM)
100   { Unite(MM); return *this; }
101
102   /**
103    * Sets this Map to be the result of intersection (aka multiplication, common, 
104    * boolean AND) operation between two given Maps.
105    * The new Map contains only the values that are contained in both map
106    * operands.<br>
107    * All previous contents of this Map is cleared. This same map (result of the
108    * boolean operation) can also be used as one of operands.<br>
109    * The order of operands makes no difference; the method minimizes internally
110    * the number of iterations using the smallest map for the loop.
111    */
112   Standard_EXPORT void Intersection (const TColStd_PackedMapOfInteger&,
113                                      const TColStd_PackedMapOfInteger&);
114
115   /**
116    * Apply to this Map the intersection operation (aka multiplication, common, 
117    * boolean AND) with another (given) Map.
118    * The result contains only the values that are contained in both this and
119    * the given maps.<br>
120    * This algorithm is similar to method Intersection().
121    * Returns True if contents of this map is changed.
122    */
123   Standard_EXPORT Standard_Boolean Intersect (const TColStd_PackedMapOfInteger&);
124
125   /**
126    * Overloaded operator version of Intersect().
127    */
128   TColStd_PackedMapOfInteger& operator &= (const TColStd_PackedMapOfInteger& MM)
129   { Intersect(MM); return *this; }
130
131   /**
132    * Sets this Map to be the result of subtraction (aka set-theoretic difference, 
133    * relative complement, exclude, cut, boolean NOT) operation between two given Maps.
134    * The new Map contains only the values that are contained in the first map
135    * operands and not contained in the second one.<br>
136    * All previous contents of this Map is cleared. This map (result of the
137    * boolean operation) can also be used as the first operand.<br>
138    */
139   Standard_EXPORT void Subtraction (const TColStd_PackedMapOfInteger&,
140                                     const TColStd_PackedMapOfInteger&);
141
142   /**
143    * Apply to this Map the subtraction (aka set-theoretic difference, relative 
144    * complement, exclude, cut, boolean NOT) operation with another (given) Map.
145    * The result contains only the values that were previously contained in
146    * this map and not contained in this map.<br>
147    * This algorithm is similar to method Subtract() with two operands.
148    * Returns True if contents of this map is changed.
149    */
150   Standard_EXPORT Standard_Boolean Subtract (const TColStd_PackedMapOfInteger&);
151
152   /**
153    * Overloaded operator version of Subtract().
154    */
155   TColStd_PackedMapOfInteger& operator -= (const TColStd_PackedMapOfInteger& MM)
156   { Subtract(MM); return *this; }
157
158   /**
159    * Sets this Map to be the result of symmetric difference (aka exclusive 
160    * disjunction, boolean XOR) operation between two given Maps.
161    * The new Map contains the values that are contained only in the 
162    * first or the second operand maps but not in both.<br>
163    * All previous contents of this Map is cleared. This map (result of the
164    * boolean operation) can also be used as one of operands.<br>
165    */
166   Standard_EXPORT void Difference (const TColStd_PackedMapOfInteger&,
167                                    const TColStd_PackedMapOfInteger&);
168
169   /**
170    * Apply to this Map the symmetric difference (aka exclusive 
171    * disjunction, boolean XOR) operation with another (given) Map.
172    * The result contains the values that are contained only in this
173    * or the operand map, but not in both.<br>
174    * This algorithm is similar to method Difference().
175    * Returns True if contents of this map is changed.
176    */
177   Standard_EXPORT Standard_Boolean Differ (const TColStd_PackedMapOfInteger&);
178
179   /**
180    * Overloaded operator version of Differ().
181    */
182   TColStd_PackedMapOfInteger& operator ^= (const TColStd_PackedMapOfInteger& MM)
183   { Differ(MM); return *this; }
184
185   /**
186    * Returns True if this map is equal to the given one, i.e. they contain the
187    * same sets of elements
188    */
189   Standard_EXPORT Standard_Boolean IsEqual (const TColStd_PackedMapOfInteger&) const;
190
191   /**
192    * Overloaded operator version of IsEqual().
193    */
194   Standard_Boolean operator == (const TColStd_PackedMapOfInteger& MM) const
195   { return IsEqual(MM); }
196
197   /**
198    * Returns True if this map is subset of the given one, i.e. all elements 
199    * contained in this map is contained also in the operand map.
200    * if this map is empty that this method returns true for any operand map.
201    */
202   Standard_EXPORT Standard_Boolean IsSubset (const TColStd_PackedMapOfInteger&) const;
203
204   /**
205    * Overloaded operator version of IsSubset().
206    */
207   Standard_Boolean operator <= (const TColStd_PackedMapOfInteger& MM) const
208   { return IsSubset(MM); }
209
210   /**
211    * Returns True if this map has common items with the given one.
212    */
213   Standard_EXPORT Standard_Boolean HasIntersection (const TColStd_PackedMapOfInteger&) const;
214
215   //!@}
216   
217  protected:
218   // ---------- PROTECTED METHODS ----------
219   inline Standard_Integer InternalExtent () const
220   { return TCollection_BasicMap::Extent(); }
221
222
223  private:
224   // ----------- PRIVATE (PROHIBITED) methods
225   /// Copy constructor - prohibited.
226   TColStd_PackedMapOfInteger (const TColStd_PackedMapOfInteger& theOther);
227
228
229   // ---------- PRIVATE FIELDS ----------
230
231   size_t                myExtent;
232
233   friend class TColStd_MapIteratorOfPackedMapOfInteger;
234 };
235
236 #endif