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