Oolite
Loading...
Searching...
No Matches
OOWeakSet.m
Go to the documentation of this file.
1/*
2
3OOWeakSet.m
4
5Written by Jens Ayton in 2012 for Oolite.
6This code is hereby placed in the public domain.
7
8*/
9
10#import "OOWeakSet.h"
11
12@interface OOWeakRefUnpackingEnumerator : NSEnumerator {
13@private
14 NSEnumerator* _enumerator;
15}
16
17- (id)initWithEnumerator:(NSEnumerator*)enumerator;
18
19+ (instancetype)enumeratorWithCollection:(id)collection; // Collection must implement -objectEnumerator
20
21@end
22
23@interface OOWeakSet (OOPrivate)
24
25- (void)compact; // Remove any zeroed entries.
26
27@end
28
29@implementation OOWeakSet
30
31- (id)init
32{
33 return [self initWithCapacity:0];
34}
35
36- (id)initWithCapacity:(NSUInteger)capacity
37{
38 if ((self = [super init])) {
39 _objects = [[NSMutableSet alloc] initWithCapacity:capacity];
40 if (_objects == NULL) {
41 [self release];
42 return nil;
43 }
44 }
45 return self;
46}
47
48+ (instancetype)set
49{
50 return [[[self alloc] init] autorelease];
51}
52
53+ (instancetype)setWithCapacity:(NSUInteger)capacity
54{
55 return [[[self alloc] initWithCapacity:capacity] autorelease];
56}
57
58- (void)dealloc
59{
61
62 [super dealloc];
63}
64
65- (NSString*)description
66{
67 NSMutableString* result = [NSMutableString stringWithFormat:@"<%@ %p>{", [self class], self];
68 NSEnumerator* selfEnum = [self objectEnumerator];
69 id object = nil;
70 BOOL first = YES;
71 while ((object = [selfEnum nextObject])) {
72 if (!first)
73 [result appendString:@", "];
74 else
75 first = NO;
76
77 NSString* desc = nil;
78 if ([object respondsToSelector:@selector(shortDescription)])
79 desc = [object shortDescription];
80 else
81 desc = [object description];
82
83 [result appendString:desc];
84 }
85
86 [result appendString:@"}"];
87 return result;
88}
89
90// MARK: Protocol conformance
91
92- (id)copyWithZone:(NSZone*)zone
93{
94 [self compact];
95 OOWeakSet* result = [[OOWeakSet allocWithZone:zone] init];
97 return result;
98}
99
100- (id)mutableCopyWithZone:(NSZone*)zone
101{
102 return [self copyWithZone:zone];
103}
104
105- (BOOL)isEqual:(id)other
106{
107 if (![other isKindOfClass:[OOWeakSet class]])
108 return NO;
109 if ([self count] != [other count])
110 return NO;
111
112 BOOL result = YES;
113 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
114 NSEnumerator* selfEnum = [self objectEnumerator];
115 id object = nil;
116 while ((object = [selfEnum nextObject])) {
117 if (![other containsObject:object]) {
118 result = NO;
119 break;
120 }
121 }
122 DESTROY(pool);
123
124 return result;
125}
126
127// MARK: Meat and potatoes
128
129- (NSUInteger)count
130{
131 [self compact];
132 return [_objects count];
133}
134
135- (BOOL)containsObject:(id<OOWeakReferenceSupport>)object
136{
137 [self compact];
138 OOWeakReference* weakObj = [object weakRetain];
139 BOOL result = [_objects containsObject:weakObj];
140 [weakObj release];
141 return result;
142}
143
144- (NSEnumerator*)objectEnumerator
145{
147}
148
149- (void)addObject:(id<OOWeakReferenceSupport>)object
150{
151 if (object == nil)
152 return;
153 NSAssert([object conformsToProtocol:@protocol(OOWeakReferenceSupport)], @"Attempt to add object to OOWeakSet which does not conform to OOWeakReferenceSupport.");
154
155 OOWeakReference* weakObj = [object weakRetain];
156 [_objects addObject:weakObj];
157 [weakObj release];
158}
159
160- (void)removeObject:(id<OOWeakReferenceSupport>)object
161{
162 OOWeakReference* weakObj = [object weakRetain];
163 [_objects removeObject:weakObj];
164 [weakObj release];
165}
166
167- (void)addObjectsByEnumerating:(NSEnumerator*)enumerator
168{
169 id object = nil;
170 [self compact];
171 while ((object = [enumerator nextObject])) {
172 [self addObject:object];
173 }
174}
175
176- (void)makeObjectsPerformSelector:(SEL)selector
177{
178 OOWeakReference* weakRef = nil;
179 foreach (weakRef, _objects) {
180 [[weakRef weakRefUnderlyingObject] performSelector:selector];
181 }
182}
183
184- (void)makeObjectsPerformSelector:(SEL)selector withObject:(id)argument
185{
186 OOWeakReference* weakRef = nil;
187 foreach (weakRef, _objects) {
188 [[weakRef weakRefUnderlyingObject] performSelector:selector withObject:argument];
189 }
190}
191
192- (NSArray*)allObjects
193{
194 NSMutableArray* result = [NSMutableArray arrayWithCapacity:[_objects count]];
195 OOWeakReference* weakRef = nil;
196 foreach (weakRef, _objects) {
197 id object = [weakRef weakRefUnderlyingObject];
198 if (object != nil)
199 [result addObject:object];
200 }
201
202#ifdef NDEBUG
203 return result;
204#else
205 return [NSArray arrayWithArray:result];
206#endif
207}
208
210{
211 [_objects removeAllObjects];
212}
213
214- (void)compact
215{
216 OOWeakReference* weakRef = nil;
217 BOOL compactRequired = NO;
218 foreach (weakRef, _objects) {
219 if ([weakRef weakRefUnderlyingObject] == nil) {
220 compactRequired = YES;
221 break;
222 }
223 }
224
225 if (compactRequired) {
226 NSMutableSet* newObjects = [[NSMutableSet alloc] initWithCapacity:[_objects count]];
227 foreach (weakRef, _objects) {
228 if ([weakRef weakRefUnderlyingObject] != nil) {
229 [newObjects addObject:weakRef];
230 }
231 }
232
233 [_objects release];
234 _objects = newObjects;
235 }
236}
237
238@end
239
240@implementation OOWeakRefUnpackingEnumerator
241
242- (id)initWithEnumerator:(NSEnumerator*)enumerator
243{
244 if (enumerator == nil) {
245 [self release];
246 return nil;
247 }
248
249 if ((self = [super init])) {
250 _enumerator = [enumerator retain];
251 }
252
253 return self;
254}
255
256+ (instancetype)enumeratorWithCollection:(id)collection
257{
258 return [[[self alloc] initWithEnumerator:[collection objectEnumerator]] autorelease];
259}
260
261- (void)dealloc
262{
263 [_enumerator release];
264
265 [super dealloc];
266}
267
269{
270 id next = nil;
271 while ((next = [_enumerator nextObject])) {
272 next = [next weakRefUnderlyingObject];
273 if (next != nil)
274 return next;
275 }
276
277 return nil;
278}
279
280@end
#define DESTROY(x)
return nil
instancetype enumeratorWithCollection:(id collection)
Definition OOWeakSet.m:256
NSEnumerator * _enumerator
Definition OOWeakSet.m:14
id copyWithZone:(NSZone *zone)
Definition OOWeakSet.m:92
void compact()
Definition OOWeakSet.m:214
instancetype set()
Definition OOWeakSet.m:48
NSString * description()
Definition OOWeakSet.m:65
void addObject:(id< OOWeakReferenceSupport > object)
Definition OOWeakSet.m:149
NSArray * allObjects()
Definition OOWeakSet.m:192
NSEnumerator * objectEnumerator()
Definition OOWeakSet.m:144
void dealloc()
Definition OOWeakSet.m:58
void addObjectsByEnumerating:(NSEnumerator *enumerator)
Definition OOWeakSet.m:167
NSMutableSet * _objects
Definition OOWeakSet.h:30
id initWithCapacity:(NSUInteger capacity)
Definition OOWeakSet.m:36
void removeAllObjects()
Definition OOWeakSet.m:209
NSUInteger count()
Definition OOWeakSet.m:129