1 // Created on: 2002-04-17
2 // Created by: Alexander Kartomin (akm)
3 // Copyright (c) 2002-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #ifndef NCollection_Set_HeaderFile
17 #define NCollection_Set_HeaderFile
19 #include <NCollection_BaseCollection.hxx>
20 #include <NCollection_BaseList.hxx>
21 #include <NCollection_TListNode.hxx>
22 #include <NCollection_TListIterator.hxx>
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.
30 template <class TheItemType> class NCollection_Set
31 : public NCollection_BaseCollection<TheItemType>,
32 public NCollection_BaseList
35 typedef NCollection_TListNode<TheItemType> SetNode;
36 typedef NCollection_TListIterator<TheItemType> Iterator;
39 // ---------- PUBLIC METHODS ------------
42 NCollection_Set(const Handle(NCollection_BaseAllocator)& theAllocator=0L) :
43 NCollection_BaseCollection<TheItemType>(theAllocator),
44 NCollection_BaseList() {}
47 NCollection_Set (const NCollection_Set& theOther) :
48 NCollection_BaseCollection<TheItemType>(theOther.myAllocator),
49 NCollection_BaseList()
52 //! Size - Number of items
53 virtual Standard_Integer Size (void) const
56 //! Replace this list by the items of theOther collection
57 virtual void Assign (const NCollection_BaseCollection<TheItemType>& theOther)
59 if (this == &theOther)
62 TYPENAME NCollection_BaseCollection<TheItemType>::Iterator& anIter =
63 theOther.CreateIterator();
64 for (; anIter.More(); anIter.Next())
66 SetNode* pNew = new (this->myAllocator) SetNode(anIter.Value());
71 //! Replace this list by the items of theOther Set
72 NCollection_Set& operator= (const NCollection_Set& theOther)
74 if (this == &theOther)
77 SetNode * pCur = (SetNode *) theOther.PFirst();
80 SetNode* pNew = new (this->myAllocator) SetNode(pCur->Value());
82 pCur = (SetNode *) pCur->Next();
89 { PClear (SetNode::delNode, this->myAllocator); }
92 Standard_Boolean Add (const TheItemType& theItem)
94 Iterator anIter(*this);
97 if (anIter.Value() == theItem)
98 return Standard_False;
101 SetNode * pNew = new (this->myAllocator) SetNode(theItem);
103 return Standard_True;
107 Standard_Boolean Remove (const TheItemType& theItem)
109 Iterator anIter(*this);
110 while (anIter.More())
112 if (anIter.Value() == theItem)
114 PRemove (anIter, SetNode::delNode, this->myAllocator);
115 return Standard_True;
119 return Standard_False;
122 //! Remove - wrapper against 'hiding' warnings
123 void Remove (Iterator& theIter)
124 { NCollection_BaseList::PRemove (theIter,
126 this->myAllocator); }
128 //! Contains - item inclusion query
129 Standard_Boolean Contains (const TheItemType& theItem) const
131 Iterator anIter(*this);
132 for (; anIter.More(); anIter.Next())
133 if (anIter.Value() == theItem)
134 return Standard_True;
135 return Standard_False;
139 Standard_Boolean IsASubset (const NCollection_Set& theOther)
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;
151 Standard_Boolean IsAProperSubset (const NCollection_Set& theOther)
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;
163 void Union (const NCollection_Set& theOther)
165 if (this == &theOther)
167 Iterator anIter(theOther);
169 Standard_Integer i, iLength=myLength;
170 for (; anIter.More(); anIter.Next())
172 Standard_Boolean isIn=Standard_False;
173 const TheItemType& theItem = anIter.Value();
174 for (aMyIter.Init(*this), i=1;
177 if (theItem == aMyIter.Value())
178 isIn = Standard_True;
181 SetNode * pNew = new (this->myAllocator) SetNode(theItem);
188 void Intersection (const NCollection_Set& theOther)
190 if (this == &theOther)
192 Iterator anIter(*this);
193 while (anIter.More())
194 if (theOther.Contains(anIter.Value()))
197 NCollection_BaseList::PRemove (anIter, SetNode::delNode, this->myAllocator);
200 //! Difference (Subtraction)
201 void Difference (const NCollection_Set& theOther)
203 if (this == &theOther)
205 Iterator anIter(*this);
206 while (anIter.More())
207 if (theOther.Contains(anIter.Value()))
208 NCollection_BaseList::PRemove (anIter, SetNode::delNode, this->myAllocator);
213 //! Destructor - clears the List
214 ~NCollection_Set (void)
218 // ----------- PRIVATE METHODS -----------
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)); }