OFFIS DCMTK  Version 3.6.0
ofoset.h
1 /*
2  *
3  * Copyright (C) 2002-2011, 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 ordered set of elements
19  * of an arbitrary type.
20  *
21  * Last Update: $Author: joergr $
22  * Update Date: $Date: 2011-11-17 16:13:18 $
23  * CVS/RCS Revision: $Revision: 1.12 $
24  * Status: $State: Exp $
25  *
26  * CVS/RCS Log at end of file
27  *
28  */
29 
30 #ifndef OFOrderedSet_h
31 #define OFOrderedSet_h
32 
33 #include "dcmtk/config/osconfig.h"
34 #include "dcmtk/ofstd/oftypes.h"
35 #include "dcmtk/ofstd/ofset.h"
36 
51 template <class T> class OFOrderedSet : public OFSet<T>
52 {
53  protected:
54 
55  public:
59  : OFSet<T>()
60  {
61  }
62 
63 
68  : OFSet<T>( src )
69  {
70  }
71 
72 
75  virtual ~OFOrderedSet()
76  {
77  }
78 
79 
85  {
86  return( assign( src ) );
87  }
88 
89 
93  const OFOrderedSet<T> &assign( const OFOrderedSet<T> &src )
94  {
95  if( this != &src )
96  this->operator=( src );
97 
98  return( *this );
99  }
100 
101 
108  virtual OFBool operator==( const OFOrderedSet<T> &other ) const
109  {
110  // check if both sets contain the same
111  // amount of items; if not, return OFFalse
112  if( this->num != other.num )
113  return( OFFalse );
114 
115  // initialize result with OFTrue
116  OFBool result = OFTrue;
117 
118  // as long as result is OFTrue go through all items in this
119  for( unsigned int i=0 ; i < this->num && result == OFTrue ; i++ )
120  {
121  // in case the current element does not equal the current
122  // element in other, result shall be set to OFFalse
123  if( *this->items[i] != *other.items[i] )
124  result = OFFalse;
125  }
126 
127  // return result
128  return( result );
129  }
130 
131 
136  virtual OFBool operator!=( const OFOrderedSet<T> &other ) const
137  {
138  return( !( *this == other ) );
139  }
140 
141 
145  virtual void Insert( const T &item )
146  {
147  // if size equals num, we need more space
148  if( this->size == this->num )
149  this->Resize( this->size * 2 );
150 
151  // copy item
152  T *newItem = new T( item );
153 
154  // insert copy into array
155  this->items[this->num] = newItem;
156 
157  // increase counter
158  this->num++;
159  }
160 
161 
165  virtual void Insert( const OFOrderedSet<T> &other )
166  {
167  // go through all items in other and insert each item into this
168  for( unsigned int i=0 ; i<other.num ; i++ )
169  Insert( *other.items[i] );
170  }
171 
172 
180  virtual void InsertAt( const T &item, unsigned int idx )
181  {
182  unsigned int i;
183 
184  // in case index is greater than the index of the last item,
185  // insert the new item right behind the last item of the set
186  if( idx > this->num - 1 )
187  Insert( item );
188  else
189  {
190  // if size equals num, we need more space
191  if( this->size == this->num )
192  this->Resize( this->size * 2 );
193 
194  // copy item
195  T *newItem = new T( item );
196 
197  // create a new temporary array and assign all pointers correspondingly
198  T **tmp = new T*[this->size];
199 
200  for( i=0 ; i<idx ; i++ )
201  tmp[i] = this->items[i];
202 
203  tmp[idx] = newItem;
204 
205  for( i=idx ; i < this->size - 1 ; i++ )
206  {
207  if( i < this->num )
208  tmp[i+1] = this->items[i];
209  else
210  tmp[i+1] = NULL;
211  }
212 
213  // delete old array
214  delete this->items;
215 
216  // assign new array to member variable
217  this->items = tmp;
218 
219  // increase counter
220  this->num++;
221  }
222  }
223 
224 
228  virtual void Remove( const T &item )
229  {
230  // so far, nothing was deleted
231  OFBool itemDeleted = OFFalse;
232 
233  // go through all items
234  for( unsigned int i=0 ; i < this->num && !itemDeleted ; i++ )
235  {
236  // if current item is the one which shall be deleted
237  if( *this->items[i] == item )
238  {
239  // delete item
240  delete this->items[i];
241 
242  // and - so that there are no holes in the array - move all elements
243  // behind the current element up one array field; only do so in case
244  // we did _not_ delete the last item
245  if( i != this->num - 1 )
246  {
247  unsigned int j;
248  for( j=i+1 ; j < this->num ; j++ )
249  {
250  this->items[j-1] = this->items[j];
251  }
252 
253  this->items[j-1] = NULL;
254  }
255  else
256  this->items[i] = NULL;
257 
258  // reduce counter
259  this->num--;
260 
261  // remember that an item was deleted (so that always only one item will be deleted)
262  itemDeleted = OFTrue;
263  }
264  }
265  }
266 
267 
271  virtual void RemoveByIndex( unsigned int idx )
272  {
273  // do something only if the given index is not out of range
274  if( idx < this->num )
275  {
276  // delete item with given index
277  delete this->items[idx];
278 
279  // and - so that there are no holes in the array - move all elements
280  // behind the current element up one array field; only do so in case
281  // we did _not_ delete the last item
282  if( idx != this->num - 1 )
283  {
284  unsigned int j;
285  for( j=idx+1 ; j < this->num ; j++ )
286  {
287  this->items[j-1] = this->items[j];
288  }
289 
290  this->items[j-1] = NULL;
291  }
292  else
293  this->items[idx] = NULL;
294 
295  // reduce counter
296  this->num--;
297  }
298  }
299 
300 
307  virtual T *Find( const T &item ) const
308  {
309  unsigned int i;
310  OFBool itemFound = OFFalse;
311 
312  for( i=0 ; i < this->num && !itemFound ; i++ )
313  {
314  if( *this->items[i] == item )
315  itemFound = OFTrue;
316  }
317 
318  if( itemFound )
319  return( this->items[i-1] );
320  else
321  return( NULL );
322  }
323 
324 
329  virtual OFBool Contains( const T &item ) const
330  {
331  OFBool itemFound = OFFalse;
332 
333  for( unsigned int i=0 ; i < this->num && !itemFound ; i++ )
334  {
335  if( *this->items[i] == item )
336  itemFound = OFTrue;
337  }
338 
339  return( itemFound );
340  }
341 
342 
349  virtual OFBool IsSupersetOf( const OFOrderedSet<T> &other ) const
350  {
351  // if this contains less or the same amount of items than other, return OFFalse
352  if( this->num <= other.num )
353  return( OFFalse );
354 
355  // initialize result with OFTrue
356  OFBool result = OFTrue;
357 
358  // make a copy of this
359  OFOrderedSet<T> s = *this;
360 
361  // as long as result is OFTrue go through all items in other
362  for( unsigned int i=0 ; i<other.num && result == OFTrue ; i++ )
363  {
364  // in case s contains the current item of other
365  if( s.Contains( *other.items[i] ) )
366  {
367  // remove this item from s so that it will not be
368  // considered again in a later call to s.Contains()
369  s.Remove( *other.items[i] );
370  }
371  // in case s does not contain the current item of other the result is OFFalse
372  else
373  result = OFFalse;
374  }
375 
376  // return result
377  return( result );
378  }
379 
380 
387  virtual OFBool IsSubsetOf( const OFOrderedSet<T> &other ) const
388  {
389  return( other.IsSupersetOf( *this ) );
390  }
391 
392 
399  OFOrderedSet<T> Union( const OFOrderedSet<T> &other ) const
400  {
401  // initialize result set
402  OFOrderedSet<T> resultSet = *this;
403 
404  // insert other set into result set
405  resultSet.Insert( other );
406 
407  // return result set
408  return( resultSet );
409  }
410 
411 
419  {
420  // initialize result set
421  OFOrderedSet<T> resultSet;
422 
423  // make a copy of other
424  OFOrderedSet<T> s = other;
425 
426  // go through all items in this
427  for( unsigned int i=0 ; i < this->num ; i++ )
428  {
429  // if s contains the current item
430  if( s.Contains( *this->items[i] ) )
431  {
432  // insert the item into the result set
433  resultSet.Insert( *this->items[i] );
434 
435  // and remove the item from s so that it will not be
436  // considered again in a later call to s.Contains()
437  s.Remove( *this->items[i] );
438  }
439  }
440 
441  // return result set
442  return( resultSet );
443  }
444 
445 
453  {
454  // initialize result set
455  OFOrderedSet<T> resultSet;
456 
457  // make a copy of other
458  OFOrderedSet<T> s = other;
459 
460  // go through all items in this
461  for( unsigned int i=0 ; i < this->num ; i++ )
462  {
463  // if s does not contain the current item
464  if( !s.Contains( *this->items[i] ) )
465  {
466  // insert the item into the result set
467  resultSet.Insert( *this->items[i] );
468  }
469  else
470  {
471  // else remove the item from s so that it will not be
472  // considered again in a later call to s.Contains()
473  s.Remove( *this->items[i] );
474  }
475  }
476 
477  // return result set
478  return( resultSet );
479  }
480 
481 
490  {
491  // determine s1 = this - other
492  OFOrderedSet<T> s1 = (*this).Difference( other );
493 
494  // determine s2 = other - this
495  OFOrderedSet<T> s2 = other.Difference( *this );
496 
497  // determine the union of s1 and s2
498  OFOrderedSet<T> resultSet = s1.Union( s2 );
499 
500  // return result set
501  return( resultSet );
502  }
503 };
504 
505 
506 #endif
507 
508 /*
509 ** CVS/RCS Log:
510 ** $Log: ofoset.h,v $
511 ** Revision 1.12 2011-11-17 16:13:18 joergr
512 ** Minor fixes to keep XCode 4.2 on Mac OS X Lion (clang compiler) quiet.
513 **
514 ** Revision 1.11 2010-10-14 13:15:50 joergr
515 ** Updated copyright header. Added reference to COPYRIGHT file.
516 **
517 ** Revision 1.10 2005/12/08 16:06:00 meichel
518 ** Changed include path schema for all DCMTK header files
519 **
520 ** Revision 1.9 2004/04/21 10:00:52 meichel
521 ** Minor modifications for compilation with gcc 3.4.0
522 **
523 ** Revision 1.8 2002/12/17 17:01:33 wilkens
524 ** Modified code again to keep Sun CC 2.0.1 happy on Solaris 2.5.1 (template
525 ** errors).
526 **
527 ** Revision 1.7 2002/12/16 10:40:24 wilkens
528 ** Removed superfluous implementation files and modified header and make files.
529 **
530 ** Revision 1.6 2002/12/13 12:26:50 wilkens
531 ** Modified code to keep Sun CC 2.0.1 happy on Solaris 2.5.1 (template errors).
532 **
533 ** Revision 1.5 2002/12/09 13:03:55 joergr
534 ** Renamed parameter to avoid name clash with global function index().
535 **
536 ** Revision 1.4 2002/07/09 18:29:45 wilkens
537 ** Added some more functionality.
538 **
539 ** Revision 1.2 2002/07/02 15:41:33 wilkens
540 ** Made some modifications to keep gcc version egcs-2.91.66 quiet.
541 **
542 ** Revision 1.1 2002/07/02 15:19:54 wilkens
543 ** Added container classes OFOrderedSet and OFUnorderedSet which
544 ** are based on the new abstract class OFSet.
545 **
546 **
547 */


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