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
7 #ifndef NCollection_Set_HeaderFile
8 #define NCollection_Set_HeaderFile
10 #include <NCollection_BaseCollection.hxx>
11 #include <NCollection_BaseList.hxx>
12 #include <NCollection_TListNode.hxx>
13 #include <NCollection_TListIterator.hxx>
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.
21 template <class TheItemType> class NCollection_Set
22 : public NCollection_BaseCollection<TheItemType>,
23 public NCollection_BaseList
26 typedef NCollection_TListNode<TheItemType> SetNode;
27 typedef NCollection_TListIterator<TheItemType> Iterator;
30 // ---------- PUBLIC METHODS ------------
33 NCollection_Set(const Handle(NCollection_BaseAllocator)& theAllocator=0L) :
34 NCollection_BaseCollection<TheItemType>(theAllocator),
35 NCollection_BaseList() {}
38 NCollection_Set (const NCollection_Set& theOther) :
39 NCollection_BaseCollection<TheItemType>(theOther.myAllocator),
40 NCollection_BaseList()
43 //! Size - Number of items
44 virtual Standard_Integer Size (void) const
47 //! Replace this list by the items of theOther collection
48 virtual void Assign (const NCollection_BaseCollection<TheItemType>& theOther)
50 if (this == &theOther)
53 TYPENAME NCollection_BaseCollection<TheItemType>::Iterator& anIter =
54 theOther.CreateIterator();
55 for (; anIter.More(); anIter.Next())
57 SetNode* pNew = new (this->myAllocator) SetNode(anIter.Value());
62 //! Replace this list by the items of theOther Set
63 NCollection_Set& operator= (const NCollection_Set& theOther)
65 if (this == &theOther)
68 SetNode * pCur = (SetNode *) theOther.PFirst();
71 SetNode* pNew = new (this->myAllocator) SetNode(pCur->Value());
73 pCur = (SetNode *) pCur->Next();
80 { PClear (SetNode::delNode, this->myAllocator); }
83 Standard_Boolean Add (const TheItemType& theItem)
85 Iterator anIter(*this);
88 if (anIter.Value() == theItem)
89 return Standard_False;
92 SetNode * pNew = new (this->myAllocator) SetNode(theItem);
98 Standard_Boolean Remove (const TheItemType& theItem)
100 Iterator anIter(*this);
101 while (anIter.More())
103 if (anIter.Value() == theItem)
105 PRemove (anIter, SetNode::delNode, this->myAllocator);
106 return Standard_True;
110 return Standard_False;
113 //! Remove - wrapper against 'hiding' warnings
114 void Remove (Iterator& theIter)
115 { NCollection_BaseList::PRemove (theIter,
117 this->myAllocator); }
119 //! Contains - item inclusion query
120 Standard_Boolean Contains (const TheItemType& theItem) const
122 Iterator anIter(*this);
123 for (; anIter.More(); anIter.Next())
124 if (anIter.Value() == theItem)
125 return Standard_True;
126 return Standard_False;
130 Standard_Boolean IsASubset (const NCollection_Set& theOther)
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;
142 Standard_Boolean IsAProperSubset (const NCollection_Set& theOther)
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;
154 void Union (const NCollection_Set& theOther)
156 if (this == &theOther)
158 Iterator anIter(theOther);
160 Standard_Integer i, iLength=myLength;
161 for (; anIter.More(); anIter.Next())
163 Standard_Boolean isIn=Standard_False;
164 const TheItemType& theItem = anIter.Value();
165 for (aMyIter.Init(*this), i=1;
168 if (theItem == aMyIter.Value())
169 isIn = Standard_True;
172 SetNode * pNew = new (this->myAllocator) SetNode(theItem);
179 void Intersection (const NCollection_Set& theOther)
181 if (this == &theOther)
183 Iterator anIter(*this);
184 while (anIter.More())
185 if (theOther.Contains(anIter.Value()))
188 NCollection_BaseList::PRemove (anIter, SetNode::delNode, this->myAllocator);
191 //! Difference (Subtraction)
192 void Difference (const NCollection_Set& theOther)
194 if (this == &theOther)
196 Iterator anIter(*this);
197 while (anIter.More())
198 if (theOther.Contains(anIter.Value()))
199 NCollection_BaseList::PRemove (anIter, SetNode::delNode, this->myAllocator);
204 //! Destructor - clears the List
205 ~NCollection_Set (void)
209 // ----------- PRIVATE METHODS -----------
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)); }