20- (id) initWithEnumerator:(NSEnumerator *)enumerator;
22+ (instancetype) enumeratorWithCollection:(
id)collection;
27@interface OOWeakSet (OOPrivate)
42- (id) initWithCapacity:(NSUInteger)capacity
44 if ((
self = [super init]))
46 _objects = [[NSMutableSet alloc] initWithCapacity:capacity];
59 return [[[
self alloc] init] autorelease];
63+ (instancetype) setWithCapacity:(NSUInteger)capacity
65 return [[[
self alloc] initWithCapacity:capacity] autorelease];
77- (NSString *) description
79 NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p>{", [
self class], self];
80 NSEnumerator *selfEnum = [
self objectEnumerator];
83 while ((
object = [selfEnum nextObject]))
85 if (!first) [result appendString:@", "];
89 if ([
object respondsToSelector:
@selector(shortDescription)]) desc = [object shortDescription];
90 else desc = [object description];
92 [result appendString:desc];
95 [result appendString:@"}"];
102- (id) copyWithZone:(NSZone *)zone
111- (id) mutableCopyWithZone:(NSZone *)zone
113 return [
self copyWithZone:zone];
117- (BOOL) isEqual:(
id)other
119 if (![other isKindOfClass:[
OOWeakSet class]])
return NO;
123 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
124 NSEnumerator *selfEnum = [
self objectEnumerator];
126 while ((
object = [selfEnum nextObject]))
128 if (![other containsObject:
object])
145 return [_objects count];
149- (BOOL) containsObject:(
id<OOWeakReferenceSupport>)object
153 BOOL result = [_objects containsObject:weakObj];
159- (NSEnumerator *) objectEnumerator
165- (void) addObject:(
id<OOWeakReferenceSupport>)object
167 if (
object ==
nil)
return;
168 NSAssert([
object conformsToProtocol:
@protocol(OOWeakReferenceSupport)],
@"Attempt to add object to OOWeakSet which does not conform to OOWeakReferenceSupport.");
171 [_objects addObject:weakObj];
176- (void) removeObject:(
id<OOWeakReferenceSupport>)object
179 [_objects removeObject:weakObj];
184- (void) addObjectsByEnumerating:(NSEnumerator *)enumerator
188 while ((
object = [enumerator nextObject]))
190 [
self addObject:object];
195- (void) makeObjectsPerformSelector:(
SEL)selector
198 foreach (weakRef, _objects)
205- (void) makeObjectsPerformSelector:(
SEL)selector withObject:(
id)argument
208 foreach (weakRef, _objects)
215- (NSArray *) allObjects
217 NSMutableArray *result = [NSMutableArray arrayWithCapacity:[_objects count]];
219 foreach (weakRef, _objects)
222 if (
object !=
nil) [result addObject:object];
228 return [NSArray arrayWithArray:result];
233- (void) removeAllObjects
235 [_objects removeAllObjects];
242 BOOL compactRequired = NO;
243 foreach (weakRef, _objects)
245 if ([weakRef weakRefUnderlyingObject] ==
nil)
247 compactRequired = YES;
254 NSMutableSet *newObjects = [[NSMutableSet alloc] initWithCapacity:[_objects count]];
255 foreach (weakRef, _objects)
257 if ([weakRef weakRefUnderlyingObject] !=
nil)
259 [newObjects addObject:weakRef];
264 _objects = newObjects;
273- (id) initWithEnumerator:(NSEnumerator *)enumerator
275 if (enumerator ==
nil)
281 if ((
self = [super init]))
283 _enumerator = [enumerator retain];
290+ (instancetype) enumeratorWithCollection:(
id)collection
292 return [[[
self alloc] initWithEnumerator:[collection objectEnumerator]] autorelease];
298 [_enumerator release];
307 while ((next = [_enumerator nextObject]))
309 next = [next weakRefUnderlyingObject];
310 if (next !=
nil)
return next;
instancetype enumeratorWithCollection:(id collection)
NSEnumerator * _enumerator
id weakRefUnderlyingObject()
void addObjectsByEnumerating:(NSEnumerator *enumerator)
id initWithCapacity:(NSUInteger capacity)