forked from cappuccino/cappuccino
/
CPSet.j
537 lines (454 loc) · 13.1 KB
/
CPSet.j
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/*
* CPSet.j
* Foundation
*
* Created by Bailey Carlson
* Extended by Ross Boucher
* Extended by Nabil Elisa
* Copyright 2008, 280 North, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* TODO: Needs to implement CPCoding, CPCopying.
*/
@import "CPArray.j"
@import "CPEnumerator.j"
@import "CPNumber.j"
@import "CPObject.j"
/*!
@class CPSet
@ingroup foundation
@brief An unordered collection of objects.
*/
@implementation CPSet : CPObject
{
Object _contents;
unsigned _count;
}
/*
Creates and returns an empty set.
*/
+ (id)set
{
return [[self alloc] init];
}
/*
Creates and returns a set containing a uniqued collection of those objects contained in a given array.
@param anArray array containing the objects to add to the new set. If the same object appears more than once objects, it is added only once to the returned set.
*/
+ (id)setWithArray:(CPArray)array
{
return [[self alloc] initWithArray:array];
}
/*
Creates and returns a set that contains a single given object.
@param anObject The object to add to the new set.
*/
+ (id)setWithObject:(id)anObject
{
return [[self alloc] initWithArray:[anObject]];
}
/*
Creates and returns a set containing a specified number of objects from a given array of objects.
@param objects A array of objects to add to the new set. If the same object appears more than once objects, it is added only once to the returned set.
@param count The number of objects from objects to add to the new set.
*/
+ (id)setWithObjects:(id)objects count:(unsigned)count
{
return [[self alloc] initWithObjects:objects count:count];
}
/*
Creates and returns a set containing the objects in a given argument list.
@param anObject The first object to add to the new set.
@param ... A comma-separated list of objects, ending with nil, to add to the new set. If the same object appears more than once objects, it is added only once to the returned set.
*/
+ (id)setWithObjects:(id)anObject, ...
{
var set = [[self alloc] init],
argLength = arguments.length,
i = 2;
for (; i < argLength && ((argument = arguments[i]) !== nil); ++i)
[set addObject:argument];
return set;
}
/*
Creates and returns a set containing the objects from another set.
@param aSet A set containing the objects to add to the new set.
*/
+ (id)setWithSet:(CPSet)set
{
return [[self alloc] initWithSet:set];
}
/*
Basic initializer, returns an empty set
*/
- (id)init
{
if (self = [super init])
{
_count = 0;
_contents = {};
}
return self;
}
/*
Initializes a newly allocated set with the objects that are contained in a given array.
@param array An array of objects to add to the new set. If the same object appears more than once in array, it is represented only once in the returned set.
*/
- (id)initWithArray:(CPArray)anArray
{
if (self = [self init])
{
var count = anArray.length;
while (count--)
[self addObject:anArray[count]];
}
return self;
}
/*
Initializes a newly allocated set with members taken from the specified list of objects.
@param objects A array of objects to add to the new set. If the same object appears more than once objects, it is added only once to the returned set.
@param count The number of objects from objects to add to the new set.
*/
- (id)initWithObjects:(id)objects count:(unsigned)count
{
return [self initWithArray:objects.splice(0, count)];
}
/*
Initializes a newly allocated set with members taken from the specified list of objects.
@param anObject The first object to add to the new set.
@param ... A comma-separated list of objects, ending with nil, to add to the new set. If the same object appears more than once in the list, it is represented only once in the returned set.
*/
- (id)initWithObjects:(id)anObject, ...
{
if (self = [self init])
{
var argLength = arguments.length,
i = 2;
for (; i < argLength && (argument = arguments[i]) != nil; ++i)
[self addObject:argument];
}
return self;
}
/*
Initializes a newly allocated set and adds to it objects from another given set. Only included for compatability.
*/
- (id)initWithSet:(CPSet)aSet
{
return [self initWithSet:aSet copyItems:NO];
}
/*
Initializes a newly allocated set and adds to it members of another given set. Only included for compatability.
*/
- (id)initWithSet:(CPSet)aSet copyItems:(BOOL)shouldCopyItems
{
self = [self init];
if (!aSet)
return self;
var contents = aSet._contents,
property;
for (property in contents)
{
if (contents.hasOwnProperty(property))
{
if (shouldCopyItems)
[self addObject:[contents[property] copy]];
else
[self addObject:contents[property]];
}
}
return self;
}
/*
Returns an array containing the receiver’s members, or an empty array if the receiver has no members.
*/
- (CPArray)allObjects
{
var array = [],
property;
for (property in _contents)
{
if (_contents.hasOwnProperty(property))
array.push(_contents[property]);
}
return array;
}
/*
Returns one of the objects in the receiver, or nil if the receiver contains no objects.
*/
- (id)anyObject
{
var property;
for (property in _contents)
{
if (_contents.hasOwnProperty(property))
return _contents[property];
}
return nil;
}
/*
Returns a Boolean value that indicates whether a given object is present in the receiver.
@param anObject The object for which to test membership of the receiver.
*/
- (BOOL)containsObject:(id)anObject
{
var obj = _contents[[anObject UID]];
if (obj !== undefined && [obj isEqual:anObject])
return YES;
return NO;
}
/*
Returns the number of members in the receiver.
*/
- (unsigned)count
{
return _count;
}
//- (CPString)description;
//
//- (CPString)descriptionWithLocale:(CPDictionary)locale;
/*
Returns a Boolean value that indicates whether at least one object in the receiver is also present in another given set.
@param set The set with which to compare the receiver.
*/
- (BOOL)intersectsSet:(CPSet)aSet
{
if (self === aSet)
return YES;
var objects = [aSet allObjects],
count = [objects count];
// If the sets share at least one item, they intersect
while (count--)
if ([self containsObject:objects[count]])
return YES;
return NO;
}
/*
Compares the receiver to another set.
@param set The set with which to compare the receiver.
*/
- (BOOL)isEqualToSet:(CPSet)set
{
// If both are subsets of each other, they are equal
return self === set || ([self count] === [set count] && [set isSubsetOfSet:self]);
}
/*
Returns a Boolean value that indicates whether every object in the receiver is also present in another given set.
@param set The set with which to compare the receiver.
*/
- (BOOL)isSubsetOfSet:(CPSet)set
{
var items = [self allObjects],
i = 0,
count = items.length;
for (; i < count; i++)
{
// If at least one item is not in both sets, self isn't a subset
if (![set containsObject:items[i]])
return NO;
}
return YES;
}
/*
Sends to each object in the receiver a message specified by a given selector.
@param aSelector A selector that specifies the message to send to the members of the receiver. The method must not take any arguments. It should not have the side effect of modifying the receiver. This value must not be NULL.
*/
- (void)makeObjectsPerformSelector:(SEL)aSelector
{
[self makeObjectsPerformSelector:aSelector withObject:nil];
}
/*
Sends to each object in the receiver a message specified by a given selector.
@param aSelector A selector that specifies the message to send to the receiver's members. The method must take a single argument of type id. The method should not, as a side effect, modify the receiver. The value must not be NULL.
@param anObject The object to pass as an argument to the method specified by aSelector.
*/
- (void)makeObjectsPerformSelector:(SEL)aSelector withObject:(id)argument
{
var items = [self allObjects],
i = 0,
count = items.length;
for (; i < count; i++)
{
[items[i] performSelector:aSelector withObject:argument];
}
}
/*
Determines whether the receiver contains an object equal to a given object, and returns that object if it is present.
@param anObject The object for which to test for membership of the receiver.
*/
- (id)member:(id)object
{
if ([self containsObject:object])
return object;
return nil;
}
- (CPEnumerator)objectEnumerator
{
return [[self allObjects] objectEnumerator];
}
// Mutable Set Methods
/*
Returns an initialized set with a given initial capacity.
@param numItems, only present for compatability
*/
- (id)initWithCapacity:(unsigned)numItems
{
// Only here for compatability with Cocoa
self = [self init];
return self;
}
/*
Creates and returns a set with a given initial capacity.
@param numItems, only present for compatability
*/
+ (id)setWithCapacity:(unsigned)numItems
{
return [[self alloc] initWithCapacity:numItems];
}
/*
Empties the receiver, then adds to the receiver each object contained in another given set.
@param set The set whose members replace the receiver's content.
*/
- (void)setSet:(CPSet)set
{
[self removeAllObjects];
[self addObjectsFromArray:[set allObjects]];
}
/*
Adds a given object to the receiver.
@param anObject The object to add to the receiver.
*/
- (void)addObject:(id)anObject
{
if ([self containsObject:anObject])
return;
_contents[[anObject UID]] = anObject;
_count++;
}
/*
Adds to the receiver each object contained in a given array that is not already a member.
@param array An array of objects to add to the receiver.
*/
- (void)addObjectsFromArray:(CPArray)objects
{
var count = [objects count];
while (count--)
[self addObject:objects[count]];
}
/*
Removes a given object from the receiver.
@param anObject The object to remove from the receiver.
*/
- (void)removeObject:(id)anObject
{
if ([self containsObject:anObject])
{
delete _contents[[anObject UID]];
_count--;
}
}
- (void)removeObjectsInArray:(CPArray)objects
{
var count = [objects count];
while (count--)
[self removeObject:objects[count]];
}
/*
Empties the receiver of all of its members.
*/
- (void)removeAllObjects
{
_contents = {};
_count = 0;
}
/*
Removes from the receiver each object that isn’t a member of another given set.
@param set The set with which to perform the intersection.
*/
- (void)intersectSet:(CPSet)set
{
var items = [self allObjects],
i = 0,
count = items.length;
for (; i < count; i++)
{
if (![set containsObject:items[i]])
[self removeObject:items[i]];
}
}
/*
Removes from the receiver each object contained in another given set that is present in the receiver.
@param set The set of objects to remove from the receiver.
*/
- (void)minusSet:(CPSet)set
{
var items = [set allObjects],
i = 0,
count = items.length;
for (; i < count; i++)
{
if ([self containsObject:items[i]])
[self removeObject:items[i]];
}
}
/*
Adds to the receiver each object contained in another given set
@param set The set of objects to add to the receiver.
*/
- (void)unionSet:(CPSet)set
{
var items = [set allObjects],
i = 0,
count = items.length;
for (; i < count; i++)
{
[self addObject:items[i]];
}
}
- (CPString)description
{
return @"{(" + [self allObjects].join(", ") + ")}";
}
@end
@implementation CPSet (CPCopying)
- (id)copy
{
return [[CPSet alloc] initWithSet:self];
}
- (id)mutableCopy
{
return [self copy];
}
@end
var CPSetObjectsKey = @"CPSetObjectsKey";
@implementation CPSet (CPCoding)
- (id)initWithCoder:(CPCoder)aCoder
{
return [self initWithArray:[aCoder decodeObjectForKey:CPSetObjectsKey]];
}
- (void)encodeWithCoder:(CPCoder)aCoder
{
[aCoder encodeObject:[self allObjects] forKey:CPSetObjectsKey];
}
@end
/*!
@class CPMutableSet
@ingroup compatability
This class is just an empty subclass of CPSet.
CPSet already implements mutable methods and
this class only exists for source compatability.
*/
@implementation CPMutableSet : CPSet
@end