00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 @import "CPObject.j"
00013 @import "CPArray.j"
00014 @import "CPNumber.j"
00015 @import "CPEnumerator.j"
00016
00020 @implementation CPSet : CPObject
00021 {
00022 Object _contents;
00023 unsigned _count;
00024 }
00025
00026
00027
00028
00029 + (id)set
00030 {
00031 return [[self alloc] init];
00032 }
00033
00034
00035
00036
00037
00038 + (id)setWithArray:(CPArray)array
00039 {
00040 return [[self alloc] initWithArray:array];
00041 }
00042
00043
00044
00045
00046
00047 + (id)setWithObject:(id)anObject
00048 {
00049 return [[self alloc] initWithArray:[anObject]];
00050 }
00051
00052
00053
00054
00055
00056
00057 + (id)setWithObjects:(id)objects count:(unsigned)count
00058 {
00059 return [[self alloc] initWithObjects:objects count:count];
00060 }
00061
00062
00063
00064
00065
00066
00067 + (id)setWithObjects:(id)anObject, ...
00068 {
00069 var set = [[self alloc] init],
00070 argLength = arguments.length,
00071 i = 2;
00072
00073 for(; i < argLength && ((argument = arguments[i]) !== nil); ++i)
00074 [set addObject:argument];
00075
00076 return set;
00077 }
00078
00079
00080
00081
00082
00083 + (id)setWithSet:(CPSet)set
00084 {
00085 return [[self alloc] initWithSet:set];
00086 }
00087
00088
00089
00090
00091 - (id)init
00092 {
00093 if (self = [super init])
00094 {
00095 _count = 0;
00096 _contents = {};
00097 }
00098
00099 return self;
00100 }
00101
00102
00103
00104
00105
00106 - (id)initWithArray:(CPArray)anArray
00107 {
00108 if (self = [self init])
00109 {
00110 var count = anArray.length;
00111
00112 while (count--)
00113 [self addObject:anArray[count]];
00114 }
00115
00116 return self;
00117 }
00118
00119
00120
00121
00122
00123
00124 - (id)initWithObjects:(id)objects count:(unsigned)count
00125 {
00126 return [self initWithArray:objects.splice(0, count)];
00127 }
00128
00129
00130
00131
00132
00133
00134 - (id)initWithObjects:(id)anObject, ...
00135 {
00136 if (self = [self init])
00137 {
00138 var argLength = arguments.length,
00139 i = 2;
00140
00141 for(; i < argLength && (argument = arguments[i]) != nil; ++i)
00142 [self addObject:argument];
00143 }
00144
00145 return self;
00146 }
00147
00148
00149
00150
00151 - (id)initWithSet:(CPSet)aSet
00152 {
00153 return [self initWithSet:aSet copyItems:NO];
00154 }
00155
00156
00157
00158
00159 - (id)initWithSet:(CPSet)aSet copyItems:(BOOL)shouldCopyItems
00160 {
00161 self = [self init];
00162
00163 if (!aSet)
00164 return self;
00165
00166 var contents = aSet._contents;
00167
00168 for (var property in contents)
00169 {
00170 if (contents.hasOwnProperty(property))
00171 {
00172 if (shouldCopyItems)
00173 [self addObject:[contents[property] copy]];
00174 else
00175 [self addObject:contents[property]];
00176 }
00177 }
00178
00179 return self;
00180 }
00181
00182
00183
00184
00185 - (CPArray)allObjects
00186 {
00187 var array = [];
00188
00189 for (var property in _contents)
00190 {
00191 if (_contents.hasOwnProperty(property))
00192 array.push(_contents[property]);
00193 }
00194
00195 return array;
00196 }
00197
00198
00199
00200
00201 - (id)anyObject
00202 {
00203 for (var property in _contents)
00204 {
00205 if (_contents.hasOwnProperty(property))
00206 return _contents[property];
00207 }
00208
00209 return nil;
00210 }
00211
00212
00213
00214
00215
00216 - (BOOL)containsObject:(id)anObject
00217 {
00218 if (_contents[[anObject hash]] && [_contents[[anObject hash]] isEqual:anObject])
00219 return YES;
00220
00221 return NO;
00222 }
00223
00224
00225
00226
00227 - (unsigned)count
00228 {
00229 return _count;
00230 }
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240 - (BOOL)intersectsSet:(CPSet)set
00241 {
00242 var items = [set allObjects];
00243 for (var i = items.length; i > 0; i--)
00244 {
00245
00246 if ([self containsObject:items[i]])
00247 return YES;
00248 }
00249
00250 return NO;
00251 }
00252
00253
00254
00255
00256
00257 - (BOOL)isEqualToSet:(CPSet)set
00258 {
00259
00260 return self === set || ([self count] === [set count] && [set isSubsetOfSet:self]);
00261 }
00262
00263
00264
00265
00266
00267 - (BOOL)isSubsetOfSet:(CPSet)set
00268 {
00269 var items = [self allObjects];
00270 for (var i = 0; i < items.length; i++)
00271 {
00272
00273 if (![set containsObject:items[i]])
00274 return NO;
00275 }
00276
00277 return YES;
00278 }
00279
00280
00281
00282
00283
00284 - (void)makeObjectsPerformSelector:(SEL)aSelector
00285 {
00286 [self makeObjectsPerformSelector:aSelector withObject:nil];
00287 }
00288
00289
00290
00291
00292
00293
00294 - (void)makeObjectsPerformSelector:(SEL)aSelector withObject:(id)argument
00295 {
00296 var items = [self allObjects];
00297 for (var i = 0; i < items.length; i++)
00298 {
00299 [items[i] performSelector:aSelector withObject:argument];
00300 }
00301 }
00302
00303
00304
00305
00306
00307 - (id)member:(id)object
00308 {
00309 if ([self containsObject:object])
00310 return object;
00311
00312 return nil;
00313 }
00314
00315 - (CPEnumerator)objectEnumerator
00316 {
00317 return [[self allObjects] objectEnumerator];
00318 }
00319
00320
00321
00322
00323
00324
00325
00326 - (id)initWithCapacity:(unsigned)numItems
00327 {
00328
00329 self = [self init];
00330 return self;
00331 }
00332
00333
00334
00335
00336
00337 + (id)setWithCapacity:(unsigned)numItems
00338 {
00339 return [[self alloc] initWithCapacity:numItems];
00340 }
00341
00342
00343
00344
00345
00346 - (void)setSet:(CPSet)set
00347 {
00348 [self removeAllObjects];
00349 [self addObjectsFromArray:[set allObjects]];
00350 }
00351
00352
00353
00354
00355
00356 - (void)addObject:(id)anObject
00357 {
00358 _contents[[anObject hash]] = anObject;
00359 _count++;
00360 }
00361
00362
00363
00364
00365
00366 - (void)addObjectsFromArray:(CPArray)array
00367 {
00368 for (var i = 0, count = array.length; i < count; i++)
00369 {
00370 [self addObject:array[i]];
00371 }
00372 }
00373
00374
00375
00376
00377
00378 - (void)removeObject:(id)anObject
00379 {
00380 if ([self containsObject:anObject])
00381 {
00382 delete _contents[[anObject hash]];
00383 _count--;
00384 }
00385 }
00386
00387
00388
00389
00390 - (void)removeAllObjects
00391 {
00392 _contents = {};
00393 _count = 0;
00394 }
00395
00396
00397
00398
00399
00400 - (void)intersectSet:(CPSet)set
00401 {
00402 var items = [self allObjects];
00403 for (var i = 0, count = items.length; i < count; i++)
00404 {
00405 if (![set containsObject:items[i]])
00406 [self removeObject:items[i]];
00407 }
00408 }
00409
00410
00411
00412
00413
00414 - (void)minusSet:(CPSet)set
00415 {
00416 var items = [set allObjects];
00417 for (var i = 0; i < items.length; i++)
00418 {
00419 if ([self containsObject:items[i]])
00420 [self removeObject:items[i]];
00421 }
00422 }
00423
00424
00425
00426
00427
00428 - (void)unionSet:(CPSet)set
00429 {
00430 var items = [set allObjects];
00431 for (var i = 0, count = items.length; i < count; i++)
00432 {
00433 [self addObject:items[i]];
00434 }
00435 }
00436
00437 @end
00438
00439 @implementation CPSet (CPCopying)
00440
00441 - (id)copy
00442 {
00443 return [[CPSet alloc] initWithSet:self];
00444 }
00445
00446 - (id)mutableCopy
00447 {
00448 return [self copy];
00449 }
00450
00451 @end
00452
00453 var CPSetObjectsKey = @"CPSetObjectsKey";
00454
00455 @implementation CPSet (CPCoding)
00456
00457 - (id)initWithCoder:(CPCoder)aCoder
00458 {
00459 return [self initWithArray:[aCoder decodeObjectForKey:CPSetObjectsKey]];
00460 }
00461
00462 - (void)encodeWithCoder:(CPCoder)aCoder
00463 {
00464 [aCoder encodeObject:[self allObjects] forKey:CPSetObjectsKey];
00465 }
00466
00467 @end
00468
00469 @implementation CPMutableSet : CPSet
00470 @end
00471