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