0022815: Missing delete operator for placement new
[occt.git] / src / NCollection / NCollection_Set.hxx
1 // File:         NCollection_Set.hxx
2 // Created:      17.04.02 10:12:48
3 // Author:       Alexander Kartomin (akm)
4 //               <a-kartomin@opencascade.com>
5 // Copyright:    Open Cascade 2002
6
7 #ifndef NCollection_Set_HeaderFile
8 #define NCollection_Set_HeaderFile
9
10 #include <NCollection_BaseCollection.hxx>
11 #include <NCollection_BaseList.hxx>
12 #include <NCollection_TListNode.hxx>
13 #include <NCollection_TListIterator.hxx>
14
15 /**
16  * Purpose:      A set is an  unordered  collection  of items without
17  *               duplications. To test for duplications the operators == and !=
18  *               are used on the items.
19  *               Inherits BaseList, adding the data item to each node.
20  */
21 template <class TheItemType> class NCollection_Set
22   : public NCollection_BaseCollection<TheItemType>,
23     public NCollection_BaseList
24 {
25  public:
26   typedef NCollection_TListNode<TheItemType> SetNode;
27   typedef NCollection_TListIterator<TheItemType> Iterator;
28
29  public:
30   // ---------- PUBLIC METHODS ------------
31
32   //! Constructor
33   NCollection_Set(const Handle(NCollection_BaseAllocator)& theAllocator=0L) :
34     NCollection_BaseCollection<TheItemType>(theAllocator),
35     NCollection_BaseList() {}
36
37   //! Copy constructor
38   NCollection_Set (const NCollection_Set& theOther) :
39     NCollection_BaseCollection<TheItemType>(theOther.myAllocator),
40     NCollection_BaseList()
41   { *this = theOther; }
42
43   //! Size - Number of items
44   virtual Standard_Integer Size (void) const
45   { return Extent(); }
46
47   //! Replace this list by the items of theOther collection
48   virtual void Assign (const NCollection_BaseCollection<TheItemType>& theOther)
49   {
50     if (this == &theOther) 
51       return;
52     Clear();
53     TYPENAME NCollection_BaseCollection<TheItemType>::Iterator& anIter = 
54       theOther.CreateIterator();
55     for (; anIter.More(); anIter.Next())
56     {
57       SetNode* pNew = new (this->myAllocator) SetNode(anIter.Value());
58       PAppend (pNew);
59     }
60   }
61
62   //! Replace this list by the items of theOther Set
63   NCollection_Set& operator= (const NCollection_Set& theOther)
64   { 
65     if (this == &theOther) 
66       return *this;
67     Clear ();
68     SetNode * pCur = (SetNode *) theOther.PFirst();
69     while (pCur)
70     {
71       SetNode* pNew = new (this->myAllocator) SetNode(pCur->Value());
72       PAppend (pNew);
73       pCur = (SetNode *) pCur->Next();
74     }
75     return *this;
76   }
77
78   //! Clear this set
79   void Clear (void)
80   { PClear (SetNode::delNode, this->myAllocator); }
81
82   //! Add item
83   Standard_Boolean Add (const TheItemType& theItem)
84   { 
85     Iterator anIter(*this);
86     while (anIter.More())
87     {
88       if (anIter.Value() == theItem)
89         return Standard_False;
90       anIter.Next();
91     }
92     SetNode * pNew = new (this->myAllocator) SetNode(theItem);
93     PPrepend (pNew);
94     return Standard_True;
95   }
96
97   //! Remove item
98   Standard_Boolean Remove (const TheItemType& theItem)
99   {
100     Iterator anIter(*this);
101     while (anIter.More())
102     {
103       if (anIter.Value() == theItem)
104       {
105         PRemove (anIter, SetNode::delNode, this->myAllocator);
106         return Standard_True;
107       }
108       anIter.Next();
109     }
110     return Standard_False;
111   }
112
113   //! Remove - wrapper against 'hiding' warnings
114   void Remove (Iterator& theIter) 
115   { NCollection_BaseList::PRemove (theIter,
116                                    SetNode::delNode,
117                                    this->myAllocator); }
118
119   //! Contains - item inclusion query
120   Standard_Boolean Contains (const TheItemType& theItem) const
121   {
122     Iterator anIter(*this);
123     for (; anIter.More(); anIter.Next())
124       if (anIter.Value() == theItem)
125         return Standard_True;
126     return Standard_False;
127   }
128
129   //! IsASubset
130   Standard_Boolean IsASubset (const NCollection_Set& theOther)
131   { 
132     if (this == &theOther) 
133       return Standard_True;
134     Iterator anIter(theOther);
135     for (; anIter.More(); anIter.Next())
136       if (!Contains(anIter.Value()))
137         return Standard_False;
138     return Standard_True;
139   }
140
141   //! IsAProperSubset
142   Standard_Boolean IsAProperSubset (const NCollection_Set& theOther)
143   {
144     if (myLength <= theOther.Extent())
145       return Standard_False;
146     Iterator anIter(theOther);
147     for (; anIter.More(); anIter.Next())
148       if (!Contains(anIter.Value()))
149         return Standard_False;
150     return Standard_True;
151   }
152
153   //! Union
154   void Union (const NCollection_Set& theOther)
155   { 
156     if (this == &theOther) 
157       return;
158     Iterator anIter(theOther);
159     Iterator aMyIter;
160     Standard_Integer i, iLength=myLength;
161     for (; anIter.More(); anIter.Next())
162     {
163       Standard_Boolean isIn=Standard_False;
164       const TheItemType& theItem = anIter.Value();
165       for (aMyIter.Init(*this), i=1; 
166            i<=iLength;
167            aMyIter.Next(), i++)
168         if (theItem == aMyIter.Value())
169           isIn = Standard_True;
170       if (!isIn)
171       {
172         SetNode * pNew = new (this->myAllocator) SetNode(theItem);
173         PAppend (pNew);
174       }
175     }
176   }
177
178   //! Intersection
179   void Intersection (const NCollection_Set& theOther)
180   { 
181     if (this == &theOther) 
182       return;
183     Iterator anIter(*this);
184     while (anIter.More())
185       if (theOther.Contains(anIter.Value()))
186         anIter.Next();
187       else
188         NCollection_BaseList::PRemove (anIter, SetNode::delNode, this->myAllocator);
189   }
190
191   //! Difference (Subtraction)
192   void Difference (const NCollection_Set& theOther)
193   { 
194     if (this == &theOther) 
195       return;
196     Iterator anIter(*this);
197     while (anIter.More())
198       if (theOther.Contains(anIter.Value()))
199         NCollection_BaseList::PRemove (anIter, SetNode::delNode, this->myAllocator);
200       else
201         anIter.Next();
202   }
203
204   //! Destructor - clears the List
205   ~NCollection_Set (void)
206   { Clear(); }
207
208  private:
209   // ----------- PRIVATE METHODS -----------
210
211   //! Creates Iterator for use on BaseCollection
212   virtual TYPENAME NCollection_BaseCollection<TheItemType>::Iterator& 
213     CreateIterator(void) const
214   { return *(new (this->IterAllocator()) Iterator(*this)); }
215
216 };
217
218 #endif