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 OFUnorderedSet_h
00036
#define OFUnorderedSet_h
00037
00038
#include "osconfig.h"
00039
#include "oftypes.h"
00040
#include "ofset.h"
00041
00057 template <
class T>
class OFUnorderedSet :
public OFSet<T>
00058 {
00059
protected:
00060
00061
public:
00064 OFUnorderedSet()
00065 :
OFSet<T>()
00066 {
00067 }
00068
00069
00073 OFUnorderedSet(
const OFUnorderedSet<T> &src )
00074 :
OFSet<T>( src )
00075 {
00076 }
00077
00078
00081 virtual ~OFUnorderedSet()
00082 {
00083 }
00084
00085
00090 const OFUnorderedSet<T> &
operator=(
const OFUnorderedSet<T> &src )
00091 {
00092
if(
this == &src )
00093
return( *this );
00094
00095
OFSet<T>::operator=( src );
00096
00097
return( *this );
00098 }
00099
00100
00105 virtual OFBool
operator==(
const OFUnorderedSet<T> &other )
const
00106
{
00107
00108
00109
if( num != other.
num )
00110
return( OFFalse );
00111
00112
00113 OFBool result = OFTrue;
00114
00115
00116
OFUnorderedSet<T> s = *
this;
00117
00118
00119
for(
unsigned int i=0 ; i<other.
num && result == OFTrue ; i++ )
00120 {
00121
00122
if( s.
Contains( *other.
items[i] ) )
00123 {
00124
00125
00126 s.
Remove( *other.
items[i] );
00127 }
00128
00129
else
00130 result = OFFalse;
00131 }
00132
00133
00134
return( result );
00135 }
00136
00137
00142 virtual OFBool
operator!=(
const OFUnorderedSet<T> &other )
const
00143
{
00144
return( !( *
this == other ) );
00145 }
00146
00147
00151 virtual void Insert(
const T &item )
00152 {
00153
00154
if( size == num )
00155
Resize( size * 2 );
00156
00157
00158 T *newItem =
new T( item );
00159
00160
00161 items[num] = newItem;
00162
00163
00164 num++;
00165 }
00166
00167
00171 virtual void Insert(
const OFUnorderedSet<T> &other )
00172 {
00173
00174
for(
unsigned int i=0 ; i<other.
num ; i++ )
00175
Insert( *other.
items[i] );
00176 }
00177
00178
00182 virtual void Remove(
const T &item )
00183 {
00184
00185 OFBool itemDeleted = OFFalse;
00186
00187
00188
for(
unsigned int i=0 ; i<num && !itemDeleted ; i++ )
00189 {
00190
00191
if( *items[i] == item )
00192 {
00193
00194
delete items[i];
00195
00196
00197
00198
00199
if( i != num - 1 )
00200 {
00201 items[i] = items[num-1];
00202 items[num-1] = NULL;
00203 }
00204
else
00205 items[i] = NULL;
00206
00207
00208 num--;
00209
00210
00211 itemDeleted = OFTrue;
00212 }
00213 }
00214 }
00215
00216
00220 virtual void RemoveByIndex(
unsigned int index )
00221 {
00222
00223
if( index < num )
00224 {
00225
00226
delete items[index];
00227
00228
00229
00230
00231
if( index != num - 1 )
00232 {
00233 items[index] = items[num-1];
00234 items[num-1] = NULL;
00235 }
00236
else
00237 items[index] = NULL;
00238
00239
00240 num--;
00241 }
00242 }
00243
00244
00251 virtual T *
Find(
const T &item )
const
00252
{
00253
unsigned int i;
00254 OFBool itemFound = OFFalse;
00255
00256
for( i=0 ; i<num && !itemFound ; i++ )
00257 {
00258
if( *items[i] == item )
00259 itemFound = OFTrue;
00260 }
00261
00262
if( itemFound )
00263
return( items[i-1] );
00264
else
00265
return( NULL );
00266 }
00267
00268
00273 virtual OFBool
Contains(
const T &item )
const
00274
{
00275 OFBool itemFound = OFFalse;
00276
00277
for(
unsigned int i=0 ; i<num && !itemFound ; i++ )
00278 {
00279
if( *items[i] == item )
00280 itemFound = OFTrue;
00281 }
00282
00283
return( itemFound );
00284 }
00285
00286
00293 virtual OFBool
IsSupersetOf(
const OFUnorderedSet<T> &other )
const
00294
{
00295
00296
if( num <= other.
num )
00297
return( OFFalse );
00298
00299
00300 OFBool result = OFTrue;
00301
00302
00303
OFUnorderedSet<T> s = *
this;
00304
00305
00306
for(
unsigned int i=0 ; i<other.
num && result == OFTrue ; i++ )
00307 {
00308
00309
if( s.
Contains( *other.
items[i] ) )
00310 {
00311
00312
00313 s.
Remove( *other.
items[i] );
00314 }
00315
00316
else
00317 result = OFFalse;
00318 }
00319
00320
00321
return( result );
00322 }
00323
00324
00331 virtual OFBool
IsSubsetOf(
const OFUnorderedSet<T> &other )
const
00332
{
00333
return( other.
IsSupersetOf( *
this ) );
00334 }
00335
00336
00343 OFUnorderedSet<T> Union(
const OFUnorderedSet<T> &other )
const
00344
{
00345
00346
OFUnorderedSet<T> resultSet = *
this;
00347
00348
00349 resultSet.
Insert( other );
00350
00351
00352
return( resultSet );
00353 }
00354
00355
00362 OFUnorderedSet<T> Intersection(
const OFUnorderedSet<T> &other )
const
00363
{
00364
00365
OFUnorderedSet<T> resultSet;
00366
00367
00368
OFUnorderedSet<T> s = other;
00369
00370
00371
for(
unsigned int i=0 ; i<num ; i++ )
00372 {
00373
00374
if( s.
Contains( *items[i] ) )
00375 {
00376
00377 resultSet.
Insert( *items[i] );
00378
00379
00380
00381 s.
Remove( *items[i] );
00382 }
00383 }
00384
00385
00386
return( resultSet );
00387 }
00388
00389
00396 OFUnorderedSet<T> Difference(
const OFUnorderedSet<T> &other )
const
00397
{
00398
00399
OFUnorderedSet<T> resultSet;
00400
00401
00402
OFUnorderedSet<T> s = other;
00403
00404
00405
for(
unsigned int i=0 ; i<num ; i++ )
00406 {
00407
00408
if( !s.
Contains( *items[i] ) )
00409 {
00410
00411 resultSet.
Insert( *items[i] );
00412 }
00413
else
00414 {
00415
00416
00417 s.
Remove( *items[i] );
00418 }
00419 }
00420
00421
00422
return( resultSet );
00423 }
00424
00425
00433 OFUnorderedSet<T> SymmetricDifference(
const OFUnorderedSet<T> &other )
const
00434
{
00435
00436
OFUnorderedSet<T> s1 = (*this).Difference( other );
00437
00438
00439
OFUnorderedSet<T> s2 = other.
Difference( *
this );
00440
00441
00442
OFUnorderedSet<T> resultSet = s1.
Union( s2 );
00443
00444
00445
return( resultSet );
00446 }
00447 };
00448
00449
#endif
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461