summaryrefslogtreecommitdiff
path: root/inc/NCollection_Set.hxx
blob: 100434f382a5fc9193507325ca7ed69a48c5c9d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// File:         NCollection_Set.hxx
// Created:      17.04.02 10:12:48
// Author:       Alexander Kartomin (akm)
//               <a-kartomin@opencascade.com>
// Copyright:    Open Cascade 2002

#ifndef NCollection_Set_HeaderFile
#define NCollection_Set_HeaderFile

#include <NCollection_BaseCollection.hxx>
#include <NCollection_BaseList.hxx>
#include <NCollection_TListNode.hxx>
#include <NCollection_TListIterator.hxx>

#ifdef WNT
// Disable the warning "operator new unmatched by delete"
#pragma warning (disable:4291)
#endif

/**
 * Purpose:      A set is an  unordered  collection  of items without
 *               duplications. To test for duplications the operators == and !=
 *               are used on the items.
 *               Inherits BaseList, adding the data item to each node.
 */
template <class TheItemType> class NCollection_Set
  : public NCollection_BaseCollection<TheItemType>,
    public NCollection_BaseList
{
 public:
  typedef NCollection_TListNode<TheItemType> SetNode;
  typedef NCollection_TListIterator<TheItemType> Iterator;

 public:
  // ---------- PUBLIC METHODS ------------

  //! Constructor
  NCollection_Set(const Handle(NCollection_BaseAllocator)& theAllocator=0L) :
    NCollection_BaseCollection<TheItemType>(theAllocator),
    NCollection_BaseList() {}

  //! Copy constructor
  NCollection_Set (const NCollection_Set& theOther) :
    NCollection_BaseCollection<TheItemType>(theOther.myAllocator),
    NCollection_BaseList()
  { *this = theOther; }

  //! Size - Number of items
  virtual Standard_Integer Size (void) const
  { return Extent(); }

  //! Replace this list by the items of theOther collection
  virtual void Assign (const NCollection_BaseCollection<TheItemType>& theOther)
  {
    if (this == &theOther) 
      return;
    Clear();
    TYPENAME NCollection_BaseCollection<TheItemType>::Iterator& anIter = 
      theOther.CreateIterator();
    for (; anIter.More(); anIter.Next())
    {
      SetNode* pNew = new (this->myAllocator) SetNode(anIter.Value());
      PAppend (pNew);
    }
  }

  //! Replace this list by the items of theOther Set
  NCollection_Set& operator= (const NCollection_Set& theOther)
  { 
    if (this == &theOther) 
      return *this;
    Clear ();
    SetNode * pCur = (SetNode *) theOther.PFirst();
    while (pCur)
    {
      SetNode* pNew = new (this->myAllocator) SetNode(pCur->Value());
      PAppend (pNew);
      pCur = (SetNode *) pCur->Next();
    }
    return *this;
  }

  //! Clear this set
  void Clear (void)
  { PClear (SetNode::delNode, this->myAllocator); }

  //! Add item
  Standard_Boolean Add (const TheItemType& theItem)
  { 
    Iterator anIter(*this);
    while (anIter.More())
    {
      if (anIter.Value() == theItem)
        return Standard_False;
      anIter.Next();
    }
    SetNode * pNew = new (this->myAllocator) SetNode(theItem);
    PPrepend (pNew);
    return Standard_True;
  }

  //! Remove item
  Standard_Boolean Remove (const TheItemType& theItem)
  {
    Iterator anIter(*this);
    while (anIter.More())
    {
      if (anIter.Value() == theItem)
      {
        PRemove (anIter, SetNode::delNode, this->myAllocator);
        return Standard_True;
      }
      anIter.Next();
    }
    return Standard_False;
  }

  //! Remove - wrapper against 'hiding' warnings
  void Remove (Iterator& theIter) 
  { NCollection_BaseList::PRemove (theIter,
                                   SetNode::delNode,
                                   this->myAllocator); }

  //! Contains - item inclusion query
  Standard_Boolean Contains (const TheItemType& theItem) const
  {
    Iterator anIter(*this);
    for (; anIter.More(); anIter.Next())
      if (anIter.Value() == theItem)
        return Standard_True;
    return Standard_False;
  }

  //! IsASubset
  Standard_Boolean IsASubset (const NCollection_Set& theOther)
  { 
    if (this == &theOther) 
      return Standard_True;
    Iterator anIter(theOther);
    for (; anIter.More(); anIter.Next())
      if (!Contains(anIter.Value()))
        return Standard_False;
    return Standard_True;
  }

  //! IsAProperSubset
  Standard_Boolean IsAProperSubset (const NCollection_Set& theOther)
  {
    if (myLength <= theOther.Extent())
      return Standard_False;
    Iterator anIter(theOther);
    for (; anIter.More(); anIter.Next())
      if (!Contains(anIter.Value()))
        return Standard_False;
    return Standard_True;
  }

  //! Union
  void Union (const NCollection_Set& theOther)
  { 
    if (this == &theOther) 
      return;
    Iterator anIter(theOther);
    Iterator aMyIter;
    Standard_Integer i, iLength=myLength;
    for (; anIter.More(); anIter.Next())
    {
      Standard_Boolean isIn=Standard_False;
      const TheItemType& theItem = anIter.Value();
      for (aMyIter.Init(*this), i=1; 
           i<=iLength;
           aMyIter.Next(), i++)
        if (theItem == aMyIter.Value())
          isIn = Standard_True;
      if (!isIn)
      {
        SetNode * pNew = new (this->myAllocator) SetNode(theItem);
        PAppend (pNew);
      }
    }
  }

  //! Intersection
  void Intersection (const NCollection_Set& theOther)
  { 
    if (this == &theOther) 
      return;
    Iterator anIter(*this);
    while (anIter.More())
      if (theOther.Contains(anIter.Value()))
        anIter.Next();
      else
        NCollection_BaseList::PRemove (anIter, SetNode::delNode, this->myAllocator);
  }

  //! Difference (Subtraction)
  void Difference (const NCollection_Set& theOther)
  { 
    if (this == &theOther) 
      return;
    Iterator anIter(*this);
    while (anIter.More())
      if (theOther.Contains(anIter.Value()))
        NCollection_BaseList::PRemove (anIter, SetNode::delNode, this->myAllocator);
      else
        anIter.Next();
  }

  //! Destructor - clears the List
  ~NCollection_Set (void)
  { Clear(); }

 private:
  // ----------- PRIVATE METHODS -----------

  //! Creates Iterator for use on BaseCollection
  virtual TYPENAME NCollection_BaseCollection<TheItemType>::Iterator& 
    CreateIterator(void) const
  { return *(new (this->IterAllocator()) Iterator(*this)); }

};

#ifdef WNT
#pragma warning (default:4291)
#endif

#endif