OFFIS DCMTK  Version 3.6.0
ofuoset.h
1 /*
2  *
3  * Copyright (C) 1997-2010, OFFIS e.V.
4  * All rights reserved. See COPYRIGHT file for details.
5  *
6  * This software and supporting documentation were developed by
7  *
8  * OFFIS e.V.
9  * R&D Division Health
10  * Escherweg 2
11  * D-26121 Oldenburg, Germany
12  *
13  *
14  * Module: ofstd
15  *
16  * Author: Thomas Wilkens
17  *
18  * Purpose: Template class for administrating an unordered set of elements
19  * of an arbitrary type.
20  *
21  * Last Update: $Author: joergr $
22  * Update Date: $Date: 2010-10-14 13:15:51 $
23  * CVS/RCS Revision: $Revision: 1.7 $
24  * Status: $State: Exp $
25  *
26  * CVS/RCS Log at end of file
27  *
28  */
29 
30 #ifndef OFUnorderedSet_h
31 #define OFUnorderedSet_h
32 
33 #include "dcmtk/config/osconfig.h"
34 #include "dcmtk/ofstd/oftypes.h"
35 #include "dcmtk/ofstd/ofset.h"
36 
52 template <class T> class OFUnorderedSet : public OFSet<T>
53 {
54  protected:
55 
56  public:
60  : OFSet<T>()
61  {
62  }
63 
64 
69  : OFSet<T>( src )
70  {
71  }
72 
73 
76  virtual ~OFUnorderedSet()
77  {
78  }
79 
80 
86  {
87  if( this == &src )
88  return( *this );
89 
90  OFSet<T>::operator=( src );
91 
92  return( *this );
93  }
94 
95 
100  virtual OFBool operator==( const OFUnorderedSet<T> &other ) const
101  {
102  // check if both sets contain the same
103  // amount of items; if not, return OFFalse
104  if( OFSet<T>::num != other.num )
105  return( OFFalse );
106 
107  // initialize result with OFTrue
108  OFBool result = OFTrue;
109 
110  // make a copy of this
111  OFUnorderedSet<T> s = *this;
112 
113  // as long as result is OFTrue go through all items in other
114  for( unsigned int i=0 ; i<other.num && result == OFTrue ; i++ )
115  {
116  // in case s contains the current item of other
117  if( s.Contains( *other.items[i] ) )
118  {
119  // remove this item from s so that it will not be
120  // considered again in a later call to s.Contains()
121  s.Remove( *other.items[i] );
122  }
123  // in case s doesn't contain the current item of other the result is OFFalse
124  else
125  result = OFFalse;
126  }
127 
128  // return result
129  return( result );
130  }
131 
132 
137  virtual OFBool operator!=( const OFUnorderedSet<T> &other ) const
138  {
139  return( !( *this == other ) );
140  }
141 
142 
146  virtual void Insert( const T &item )
147  {
148  // if size equals num, we need more space
150  Resize( OFSet<T>::size * 2 );
151 
152  // copy item
153  T *newItem = new T( item );
154 
155  // insert copy into array
156  OFSet<T>::items[OFSet<T>::num] = newItem;
157 
158  // increase counter
159  OFSet<T>::num++;
160  }
161 
162 
166  virtual void Insert( const OFUnorderedSet<T> &other )
167  {
168  // go through all items in other and insert each item into this
169  for( unsigned int i=0 ; i<other.num ; i++ )
170  Insert( *other.items[i] );
171  }
172 
173 
177  virtual void Remove( const T &item )
178  {
179  // so far, nothing was deleted
180  OFBool itemDeleted = OFFalse;
181 
182  // go through all items
183  for( unsigned int i=0 ; i<OFSet<T>::num && !itemDeleted ; i++ )
184  {
185  // if current item is the one which shall be deleted
186  if( *OFSet<T>::items[i] == item )
187  {
188  // delete item
189  delete OFSet<T>::items[i];
190 
191  // and - so that there are no holes in the array - move the last
192  // item to the array field from which the item was deleted;
193  // only do so in case we did _not_ delete the last item
194  if( i != OFSet<T>::num - 1 )
195  {
197  OFSet<T>::items[OFSet<T>::num-1] = NULL;
198  }
199  else
200  OFSet<T>::items[i] = NULL;
201 
202  // reduce counter
203  OFSet<T>::num--;
204 
205  // remember that an item was deleted (so that always only one item will be deleted)
206  itemDeleted = OFTrue;
207  }
208  }
209  }
210 
211 
215  virtual void RemoveByIndex( unsigned int index )
216  {
217  // do something only if the given index is not out of range
218  if( index < OFSet<T>::num )
219  {
220  // delete item with given index
221  delete OFSet<T>::items[index];
222 
223  // and - so that there are no holes in the array - move the last
224  // item to the array field from which the item was deleted;
225  // only do so in case we did _not_ delete the last item
226  if( index != OFSet<T>::num - 1 )
227  {
229  OFSet<T>::items[OFSet<T>::num-1] = NULL;
230  }
231  else
232  OFSet<T>::items[index] = NULL;
233 
234  // reduce counter
235  OFSet<T>::num--;
236  }
237  }
238 
239 
246  virtual T *Find( const T &item ) const
247  {
248  unsigned int i;
249  OFBool itemFound = OFFalse;
250 
251  for( i=0 ; i<OFSet<T>::num && !itemFound ; i++ )
252  {
253  if( *OFSet<T>::items[i] == item )
254  itemFound = OFTrue;
255  }
256 
257  if( itemFound )
258  return( OFSet<T>::items[i-1] );
259  else
260  return( NULL );
261  }
262 
263 
268  virtual OFBool Contains( const T &item ) const
269  {
270  OFBool itemFound = OFFalse;
271 
272  for( unsigned int i=0 ; i<OFSet<T>::num && !itemFound ; i++ )
273  {
274  if( *OFSet<T>::items[i] == item )
275  itemFound = OFTrue;
276  }
277 
278  return( itemFound );
279  }
280 
281 
288  virtual OFBool IsSupersetOf( const OFUnorderedSet<T> &other ) const
289  {
290  // if this contains less or the same amount of items than other, return OFFalse
291  if( OFSet<T>::num <= other.num )
292  return( OFFalse );
293 
294  // initialize result with OFTrue
295  OFBool result = OFTrue;
296 
297  // make a copy of this
298  OFUnorderedSet<T> s = *this;
299 
300  // as long as result is OFTrue go through all items in other
301  for( unsigned int i=0 ; i<other.num && result == OFTrue ; i++ )
302  {
303  // in case s contains the current item of other
304  if( s.Contains( *other.items[i] ) )
305  {
306  // remove this item from s so that it will not be
307  // considered again in a later call to s.Contains()
308  s.Remove( *other.items[i] );
309  }
310  // in case s does not contain the current item of other the result is OFFalse
311  else
312  result = OFFalse;
313  }
314 
315  // return result
316  return( result );
317  }
318 
319 
326  virtual OFBool IsSubsetOf( const OFUnorderedSet<T> &other ) const
327  {
328  return( other.IsSupersetOf( *this ) );
329  }
330 
331 
339  {
340  // initialize result set
341  OFUnorderedSet<T> resultSet = *this;
342 
343  // insert other set into result set
344  resultSet.Insert( other );
345 
346  // return result set
347  return( resultSet );
348  }
349 
350 
358  {
359  // initialize result set
360  OFUnorderedSet<T> resultSet;
361 
362  // make a copy of other
363  OFUnorderedSet<T> s = other;
364 
365  // go through all items in this
366  for( unsigned int i=0 ; i< OFSet<T>::num ; i++ )
367  {
368  // if s contains the current item
369  if( s.Contains( *OFSet<T>::items[i] ) )
370  {
371  // insert the item into the result set
372  resultSet.Insert( *OFSet<T>::items[i] );
373 
374  // and remove the item from s so that it will not be
375  // considered again in a later call to s.Contains()
376  s.Remove( *OFSet<T>::items[i] );
377  }
378  }
379 
380  // return result set
381  return( resultSet );
382  }
383 
384 
392  {
393  // initialize result set
394  OFUnorderedSet<T> resultSet;
395 
396  // make a copy of other
397  OFUnorderedSet<T> s = other;
398 
399  // go through all items in this
400  for( unsigned int i=0 ; i< OFSet<T>::num ; i++ )
401  {
402  // if s does not contain the current item
403  if( !s.Contains( *OFSet<T>::items[i] ) )
404  {
405  // insert the item into the result set
406  resultSet.Insert( *OFSet<T>::items[i] );
407  }
408  else
409  {
410  // else remove the item from s so that it will not be
411  // considered again in a later call to s.Contains()
412  s.Remove( *OFSet<T>::items[i] );
413  }
414  }
415 
416  // return result set
417  return( resultSet );
418  }
419 
420 
429  {
430  // determine s1 = this - other
431  OFUnorderedSet<T> s1 = (*this).Difference( other );
432 
433  // determine s2 = other - this
434  OFUnorderedSet<T> s2 = other.Difference( *this );
435 
436  // determine the union of s1 and s2
437  OFUnorderedSet<T> resultSet = s1.Union( s2 );
438 
439  // return result set
440  return( resultSet );
441  }
442 };
443 
444 #endif
445 
446 /*
447 ** CVS/RCS Log:
448 ** $Log: ofuoset.h,v $
449 ** Revision 1.7 2010-10-14 13:15:51 joergr
450 ** Updated copyright header. Added reference to COPYRIGHT file.
451 **
452 ** Revision 1.6 2005/12/12 09:24:27 meichel
453 ** Added explicit references to parent template class, needed for
454 ** gcc 3.4.2 (MinGW port)
455 **
456 ** Revision 1.5 2005/12/08 16:06:12 meichel
457 ** Changed include path schema for all DCMTK header files
458 **
459 ** Revision 1.4 2002/12/16 10:40:25 wilkens
460 ** Removed superfluous implementation files and modified header and make files.
461 **
462 ** Revision 1.3 2002/07/09 18:29:47 wilkens
463 ** Added some more functionality.
464 **
465 **
466 */


Generated on Thu Dec 20 2012 for OFFIS DCMTK Version 3.6.0 by Doxygen 1.8.2