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
00031
00032
00033
00034
00035
#ifndef OFOrderedSet_h
00036
#define OFOrderedSet_h
00037
00038
#include "osconfig.h"
00039
#include "oftypes.h"
00040
#include "ofset.h"
00041
00056 template <
class T>
class OFOrderedSet :
public OFSet<T>
00057 {
00058
protected:
00059
00060
public:
00063 OFOrderedSet()
00064 :
OFSet<T>()
00065 {
00066 }
00067
00068
00072 OFOrderedSet(
const OFOrderedSet<T> &src )
00073 :
OFSet<T>( src )
00074 {
00075 }
00076
00077
00080 virtual ~OFOrderedSet()
00081 {
00082 }
00083
00084
00089 const OFOrderedSet<T> &
operator=(
const OFOrderedSet<T> &src )
00090 {
00091
return(
assign( src ) );
00092 }
00093
00094
00098 const OFOrderedSet<T> &
assign(
const OFOrderedSet<T> &src )
00099 {
00100
if(
this != &src )
00101 this->
operator=( src );
00102
00103
return( *this );
00104 }
00105
00106
00113 virtual OFBool
operator==(
const OFOrderedSet<T> &other )
const
00114
{
00115
00116
00117
if( this->num != other.
num )
00118
return( OFFalse );
00119
00120
00121 OFBool result = OFTrue;
00122
00123
00124
for(
unsigned int i=0 ; i < this->num && result == OFTrue ; i++ )
00125 {
00126
00127
00128
if( *this->items[i] != *other.
items[i] )
00129 result = OFFalse;
00130 }
00131
00132
00133
return( result );
00134 }
00135
00136
00141 virtual OFBool
operator!=(
const OFOrderedSet<T> &other )
const
00142
{
00143
return( !( *
this == other ) );
00144 }
00145
00146
00150 virtual void Insert(
const T &item )
00151 {
00152
00153
if( this->size == this->num )
00154
Resize( this->size * 2 );
00155
00156
00157 T *newItem =
new T( item );
00158
00159
00160 this->items[this->num] = newItem;
00161
00162
00163 this->num++;
00164 }
00165
00166
00170 virtual void Insert(
const OFOrderedSet<T> &other )
00171 {
00172
00173
for(
unsigned int i=0 ; i<other.
num ; i++ )
00174
Insert( *other.
items[i] );
00175 }
00176
00177
00185 virtual void InsertAt(
const T &item,
unsigned int idx )
00186 {
00187
unsigned int i;
00188
00189
00190
00191
if( idx > this->num - 1 )
00192
Insert( item );
00193
else
00194 {
00195
00196
if( this->size == this->num )
00197
Resize( this->size * 2 );
00198
00199
00200 T *newItem =
new T( item );
00201
00202
00203 T **tmp =
new T*[this->size];
00204
00205
for( i=0 ; i<idx ; i++ )
00206 tmp[i] = this->items[i];
00207
00208 tmp[idx] = newItem;
00209
00210
for( i=idx ; i < this->size - 1 ; i++ )
00211 {
00212
if( i < this->num )
00213 tmp[i+1] = this->items[i];
00214
else
00215 tmp[i+1] = NULL;
00216 }
00217
00218
00219
delete this->items;
00220
00221
00222 this->items = tmp;
00223
00224
00225 this->num++;
00226 }
00227 }
00228
00229
00233 virtual void Remove(
const T &item )
00234 {
00235
00236 OFBool itemDeleted = OFFalse;
00237
00238
00239
for(
unsigned int i=0 ; i < this->num && !itemDeleted ; i++ )
00240 {
00241
00242
if( *this->items[i] == item )
00243 {
00244
00245
delete this->items[i];
00246
00247
00248
00249
00250
if( i != this->num - 1 )
00251 {
00252
unsigned int j;
00253
for( j=i+1 ; j < this->num ; j++ )
00254 {
00255 this->items[j-1] = this->items[j];
00256 }
00257
00258 this->items[j-1] = NULL;
00259 }
00260
else
00261 this->items[i] = NULL;
00262
00263
00264 this->num--;
00265
00266
00267 itemDeleted = OFTrue;
00268 }
00269 }
00270 }
00271
00272
00276 virtual void RemoveByIndex(
unsigned int idx )
00277 {
00278
00279
if( idx < this->num )
00280 {
00281
00282
delete this->items[idx];
00283
00284
00285
00286
00287
if( idx != this->num - 1 )
00288 {
00289
unsigned int j;
00290
for( j=idx+1 ; j < this->num ; j++ )
00291 {
00292 this->items[j-1] = this->items[j];
00293 }
00294
00295 this->items[j-1] = NULL;
00296 }
00297
else
00298 this->items[idx] = NULL;
00299
00300
00301 this->num--;
00302 }
00303 }
00304
00305
00312 virtual T *
Find(
const T &item )
const
00313
{
00314
unsigned int i;
00315 OFBool itemFound = OFFalse;
00316
00317
for( i=0 ; i < this->num && !itemFound ; i++ )
00318 {
00319
if( *this->items[i] == item )
00320 itemFound = OFTrue;
00321 }
00322
00323
if( itemFound )
00324
return( this->items[i-1] );
00325
else
00326
return( NULL );
00327 }
00328
00329
00334 virtual OFBool
Contains(
const T &item )
const
00335
{
00336 OFBool itemFound = OFFalse;
00337
00338
for(
unsigned int i=0 ; i < this->num && !itemFound ; i++ )
00339 {
00340
if( *this->items[i] == item )
00341 itemFound = OFTrue;
00342 }
00343
00344
return( itemFound );
00345 }
00346
00347
00354 virtual OFBool
IsSupersetOf(
const OFOrderedSet<T> &other )
const
00355
{
00356
00357
if( this->num <= other.
num )
00358
return( OFFalse );
00359
00360
00361 OFBool result = OFTrue;
00362
00363
00364
OFOrderedSet<T> s = *
this;
00365
00366
00367
for(
unsigned int i=0 ; i<other.
num && result == OFTrue ; i++ )
00368 {
00369
00370
if( s.
Contains( *other.
items[i] ) )
00371 {
00372
00373
00374 s.
Remove( *other.
items[i] );
00375 }
00376
00377
else
00378 result = OFFalse;
00379 }
00380
00381
00382
return( result );
00383 }
00384
00385
00392 virtual OFBool
IsSubsetOf(
const OFOrderedSet<T> &other )
const
00393
{
00394
return( other.
IsSupersetOf( *
this ) );
00395 }
00396
00397
00404 OFOrderedSet<T> Union(
const OFOrderedSet<T> &other )
const
00405
{
00406
00407
OFOrderedSet<T> resultSet = *
this;
00408
00409
00410 resultSet.
Insert( other );
00411
00412
00413
return( resultSet );
00414 }
00415
00416
00423 OFOrderedSet<T> Intersection(
const OFOrderedSet<T> &other )
const
00424
{
00425
00426
OFOrderedSet<T> resultSet;
00427
00428
00429
OFOrderedSet<T> s = other;
00430
00431
00432
for(
unsigned int i=0 ; i < this->num ; i++ )
00433 {
00434
00435
if( s.
Contains( *this->items[i] ) )
00436 {
00437
00438 resultSet.
Insert( *this->items[i] );
00439
00440
00441
00442 s.
Remove( *this->items[i] );
00443 }
00444 }
00445
00446
00447
return( resultSet );
00448 }
00449
00450
00457 OFOrderedSet<T> Difference(
const OFOrderedSet<T> &other )
const
00458
{
00459
00460
OFOrderedSet<T> resultSet;
00461
00462
00463
OFOrderedSet<T> s = other;
00464
00465
00466
for(
unsigned int i=0 ; i < this->num ; i++ )
00467 {
00468
00469
if( !s.
Contains( *this->items[i] ) )
00470 {
00471
00472 resultSet.
Insert( *this->items[i] );
00473 }
00474
else
00475 {
00476
00477
00478 s.
Remove( *this->items[i] );
00479 }
00480 }
00481
00482
00483
return( resultSet );
00484 }
00485
00486
00494 OFOrderedSet<T> SymmetricDifference(
const OFOrderedSet<T> &other )
const
00495
{
00496
00497
OFOrderedSet<T> s1 = (*this).Difference( other );
00498
00499
00500
OFOrderedSet<T> s2 = other.
Difference( *
this );
00501
00502
00503
OFOrderedSet<T> resultSet = s1.
Union( s2 );
00504
00505
00506
return( resultSet );
00507 }
00508 };
00509
00510
00511
#endif
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