00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #ifndef OFOrderedSet_h
00031 #define OFOrderedSet_h
00032
00033 #include "dcmtk/config/osconfig.h"
00034 #include "dcmtk/ofstd/oftypes.h"
00035 #include "dcmtk/ofstd/ofset.h"
00036
00051 template <class T> class OFOrderedSet : public OFSet<T>
00052 {
00053 protected:
00054
00055 public:
00058 OFOrderedSet()
00059 : OFSet<T>()
00060 {
00061 }
00062
00063
00067 OFOrderedSet( const OFOrderedSet<T> &src )
00068 : OFSet<T>( src )
00069 {
00070 }
00071
00072
00075 virtual ~OFOrderedSet()
00076 {
00077 }
00078
00079
00084 const OFOrderedSet<T> &operator=( const OFOrderedSet<T> &src )
00085 {
00086 return( assign( src ) );
00087 }
00088
00089
00093 const OFOrderedSet<T> &assign( const OFOrderedSet<T> &src )
00094 {
00095 if( this != &src )
00096 this->operator=( src );
00097
00098 return( *this );
00099 }
00100
00101
00108 virtual OFBool operator==( const OFOrderedSet<T> &other ) const
00109 {
00110
00111
00112 if( this->num != other.num )
00113 return( OFFalse );
00114
00115
00116 OFBool result = OFTrue;
00117
00118
00119 for( unsigned int i=0 ; i < this->num && result == OFTrue ; i++ )
00120 {
00121
00122
00123 if( *this->items[i] != *other.items[i] )
00124 result = OFFalse;
00125 }
00126
00127
00128 return( result );
00129 }
00130
00131
00136 virtual OFBool operator!=( const OFOrderedSet<T> &other ) const
00137 {
00138 return( !( *this == other ) );
00139 }
00140
00141
00145 virtual void Insert( const T &item )
00146 {
00147
00148 if( this->size == this->num )
00149 Resize( this->size * 2 );
00150
00151
00152 T *newItem = new T( item );
00153
00154
00155 this->items[this->num] = newItem;
00156
00157
00158 this->num++;
00159 }
00160
00161
00165 virtual void Insert( const OFOrderedSet<T> &other )
00166 {
00167
00168 for( unsigned int i=0 ; i<other.num ; i++ )
00169 Insert( *other.items[i] );
00170 }
00171
00172
00180 virtual void InsertAt( const T &item, unsigned int idx )
00181 {
00182 unsigned int i;
00183
00184
00185
00186 if( idx > this->num - 1 )
00187 Insert( item );
00188 else
00189 {
00190
00191 if( this->size == this->num )
00192 Resize( this->size * 2 );
00193
00194
00195 T *newItem = new T( item );
00196
00197
00198 T **tmp = new T*[this->size];
00199
00200 for( i=0 ; i<idx ; i++ )
00201 tmp[i] = this->items[i];
00202
00203 tmp[idx] = newItem;
00204
00205 for( i=idx ; i < this->size - 1 ; i++ )
00206 {
00207 if( i < this->num )
00208 tmp[i+1] = this->items[i];
00209 else
00210 tmp[i+1] = NULL;
00211 }
00212
00213
00214 delete this->items;
00215
00216
00217 this->items = tmp;
00218
00219
00220 this->num++;
00221 }
00222 }
00223
00224
00228 virtual void Remove( const T &item )
00229 {
00230
00231 OFBool itemDeleted = OFFalse;
00232
00233
00234 for( unsigned int i=0 ; i < this->num && !itemDeleted ; i++ )
00235 {
00236
00237 if( *this->items[i] == item )
00238 {
00239
00240 delete this->items[i];
00241
00242
00243
00244
00245 if( i != this->num - 1 )
00246 {
00247 unsigned int j;
00248 for( j=i+1 ; j < this->num ; j++ )
00249 {
00250 this->items[j-1] = this->items[j];
00251 }
00252
00253 this->items[j-1] = NULL;
00254 }
00255 else
00256 this->items[i] = NULL;
00257
00258
00259 this->num--;
00260
00261
00262 itemDeleted = OFTrue;
00263 }
00264 }
00265 }
00266
00267
00271 virtual void RemoveByIndex( unsigned int idx )
00272 {
00273
00274 if( idx < this->num )
00275 {
00276
00277 delete this->items[idx];
00278
00279
00280
00281
00282 if( idx != this->num - 1 )
00283 {
00284 unsigned int j;
00285 for( j=idx+1 ; j < this->num ; j++ )
00286 {
00287 this->items[j-1] = this->items[j];
00288 }
00289
00290 this->items[j-1] = NULL;
00291 }
00292 else
00293 this->items[idx] = NULL;
00294
00295
00296 this->num--;
00297 }
00298 }
00299
00300
00307 virtual T *Find( const T &item ) const
00308 {
00309 unsigned int i;
00310 OFBool itemFound = OFFalse;
00311
00312 for( i=0 ; i < this->num && !itemFound ; i++ )
00313 {
00314 if( *this->items[i] == item )
00315 itemFound = OFTrue;
00316 }
00317
00318 if( itemFound )
00319 return( this->items[i-1] );
00320 else
00321 return( NULL );
00322 }
00323
00324
00329 virtual OFBool Contains( const T &item ) const
00330 {
00331 OFBool itemFound = OFFalse;
00332
00333 for( unsigned int i=0 ; i < this->num && !itemFound ; i++ )
00334 {
00335 if( *this->items[i] == item )
00336 itemFound = OFTrue;
00337 }
00338
00339 return( itemFound );
00340 }
00341
00342
00349 virtual OFBool IsSupersetOf( const OFOrderedSet<T> &other ) const
00350 {
00351
00352 if( this->num <= other.num )
00353 return( OFFalse );
00354
00355
00356 OFBool result = OFTrue;
00357
00358
00359 OFOrderedSet<T> s = *this;
00360
00361
00362 for( unsigned int i=0 ; i<other.num && result == OFTrue ; i++ )
00363 {
00364
00365 if( s.Contains( *other.items[i] ) )
00366 {
00367
00368
00369 s.Remove( *other.items[i] );
00370 }
00371
00372 else
00373 result = OFFalse;
00374 }
00375
00376
00377 return( result );
00378 }
00379
00380
00387 virtual OFBool IsSubsetOf( const OFOrderedSet<T> &other ) const
00388 {
00389 return( other.IsSupersetOf( *this ) );
00390 }
00391
00392
00399 OFOrderedSet<T> Union( const OFOrderedSet<T> &other ) const
00400 {
00401
00402 OFOrderedSet<T> resultSet = *this;
00403
00404
00405 resultSet.Insert( other );
00406
00407
00408 return( resultSet );
00409 }
00410
00411
00418 OFOrderedSet<T> Intersection( const OFOrderedSet<T> &other ) const
00419 {
00420
00421 OFOrderedSet<T> resultSet;
00422
00423
00424 OFOrderedSet<T> s = other;
00425
00426
00427 for( unsigned int i=0 ; i < this->num ; i++ )
00428 {
00429
00430 if( s.Contains( *this->items[i] ) )
00431 {
00432
00433 resultSet.Insert( *this->items[i] );
00434
00435
00436
00437 s.Remove( *this->items[i] );
00438 }
00439 }
00440
00441
00442 return( resultSet );
00443 }
00444
00445
00452 OFOrderedSet<T> Difference( const OFOrderedSet<T> &other ) const
00453 {
00454
00455 OFOrderedSet<T> resultSet;
00456
00457
00458 OFOrderedSet<T> s = other;
00459
00460
00461 for( unsigned int i=0 ; i < this->num ; i++ )
00462 {
00463
00464 if( !s.Contains( *this->items[i] ) )
00465 {
00466
00467 resultSet.Insert( *this->items[i] );
00468 }
00469 else
00470 {
00471
00472
00473 s.Remove( *this->items[i] );
00474 }
00475 }
00476
00477
00478 return( resultSet );
00479 }
00480
00481
00489 OFOrderedSet<T> SymmetricDifference( const OFOrderedSet<T> &other ) const
00490 {
00491
00492 OFOrderedSet<T> s1 = (*this).Difference( other );
00493
00494
00495 OFOrderedSet<T> s2 = other.Difference( *this );
00496
00497
00498 OFOrderedSet<T> resultSet = s1.Union( s2 );
00499
00500
00501 return( resultSet );
00502 }
00503 };
00504
00505
00506 #endif
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544