60- (void) setPerformCleanup:(BOOL)flag;
65@interface OOShipGroup (Private)
67- (BOOL) resizeTo:(NSUInteger)newCapacity;
82 return [
self initWithName:nil];
86- (id) initWithName:(NSString *)name
88 if ((
self = [super init]))
91 _members = malloc(
sizeof *_members * _capacity);
105+ (instancetype) groupWithName:(NSString *)name
107 return [[[
self alloc] initWithName:name] autorelease];
111+ (instancetype) groupWithName:(NSString *)name leader:(
ShipEntity *)leader
123 for (i = 0; i < _count; i++)
125 [_members[i] release];
134- (NSString *) descriptionComponents
136 NSString *desc = [NSString stringWithFormat:@"%llu ships", (unsigned long long)_count];
137 if ([
self name] !=
nil)
139 desc = [NSString stringWithFormat:@"\"%@\", %@", [
self name], desc];
141 if ([
self leader] !=
nil)
143 desc = [NSString stringWithFormat:@"%@, leader: %@", desc, [[
self leader] shortDescription]];
155- (void) setName:(NSString *)name
162 _name = [name retain];
169 ShipEntity *result = [_leader weakRefUnderlyingObject];
172 if (result ==
nil && _leader !=
nil)
186 if (leader != [
self leader])
189 [
self addShip:leader];
195- (NSEnumerator *) objectEnumerator
201- (NSEnumerator *) mutationSafeEnumerator
203 return [[
self memberArray] objectEnumerator];
209 return [NSSet setWithArray:[
self memberArray]];
213- (NSSet *) membersExcludingLeader
215 return [NSSet setWithArray:[
self memberArrayExcludingLeader]];
219#if OOLITE_FAST_ENUMERATION
220- (NSArray *) memberArray
223 NSUInteger
count = 0;
224 NSArray *result =
nil;
226 if (_count == 0)
return [NSArray array];
228 objects = malloc(
sizeof *objects * _count);
229 for (
id ship in
self)
231 objects[count++] = ship;
234 result = [NSArray arrayWithObjects:objects count:count];
241- (NSArray *) memberArrayExcludingLeader
244 NSUInteger
count = 0;
245 NSArray *result =
nil;
248 if (_count == 0)
return [NSArray array];
249 leader =
self.leader;
251 objects = malloc(
sizeof *objects * _count);
252 for (
id ship in
self)
256 objects[count++] = ship;
260 result = [NSArray arrayWithObjects:objects count:count];
271 for (containedShip in
self)
273 if ([ship isEqual:containedShip])
282- (NSArray *) memberArray
284 return [[
self objectEnumerator] allObjects];
288- (NSArray *) memberArrayExcludingLeader
291 NSUInteger
count = 0;
292 NSArray *result =
nil;
293 NSEnumerator *shipEnum =
nil;
297 if (_count == 0)
return [NSArray array];
298 leader = [
self leader];
299 if (leader ==
nil)
return [
self memberArray];
301 objects = malloc(
sizeof *objects * _count);
302 for (shipEnum = [
self objectEnumerator]; (ship = [shipEnum nextObject]); )
306 objects[count++] = ship;
310 result = [NSArray arrayWithObjects:objects count:count];
325 while ((containedShip = [shipEnum nextObject]))
327 if ([ship isEqual:containedShip])
346 if ([
self containsShip:ship])
return YES;
349 if (_count == _capacity)
353 if (![
self resizeTo:_capacity + 1])
361 _members[_count++] = [ship weakRetain];
375 if (ship == [
self leader]) [
self setLeader:nil];
379 while ((containedShip = [shipEnum nextObject]))
381 if ([ship isEqual:containedShip])
383 index = [shipEnum
index] - 1;
384 _members[index] = _members[--_count];
405 NSEnumerator *memberEnum =
nil;
406 NSUInteger result = 0;
410 memberEnum = [
self objectEnumerator];
411 while ([memberEnum nextObject] !=
nil) result++;
414 assert(result == _count);
422 if (_count == 0)
return YES;
424 return [[
self objectEnumerator] nextObject] ==
nil;
428- (BOOL) resizeTo:(NSUInteger)newCapacity
432 if (newCapacity < _count)
return NO;
434 temp = realloc(_members, newCapacity *
sizeof *_members);
435 if (temp == NULL)
return NO;
438 _capacity = newCapacity;
445 NSUInteger newCapacity = _capacity;
451 newCapacity = _count + 1;
456 if (_capacity > _count * 2)
458 newCapacity = OORoundUpToPowerOf2_NS(_count);
463 if (newCapacity != _capacity) [
self resizeTo:newCapacity];
479 BOOL cleanupNeeded = NO;
483 [NSException raise:NSGenericException format:@"Collection <OOShipGroup: %p> was mutated while being enumerated.", group];
486 while (enumerator->
_index < group->_count)
488 result = [group->_members[enumerator->
_index] weakRefUnderlyingObject];
514#if OOLITE_FAST_ENUMERATION
515- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(
id *)stackbuf count:(NSUInteger)len
517 NSUInteger srcIndex, dstIndex = 0;
519 BOOL cleanupNeeded = NO;
521 srcIndex = state->state;
522 while (srcIndex < _count && dstIndex < len)
524 item = [_members[srcIndex] weakRefUnderlyingObject];
527 stackbuf[dstIndex++] = item;
532 _members[srcIndex] = _members[--_count];
537 if (cleanupNeeded) [
self cleanUp];
539 state->state = srcIndex;
540 state->itemsPtr = stackbuf;
541 state->mutationsPtr = &_updateCount;
553- (BOOL) suppressClangStuff
565 assert(group !=
nil);
567 if ((
self = [super init]))
569 _group = [group retain];
570 _considerCleanup = YES;
571 _updateCount = [_group updateCount];
598- (void) setPerformCleanup:(BOOL)flag
600 _considerCleanup = flag;
static id ShipGroupIterate(OOShipGroupEnumerator *enumerator)
void setPerformCleanup:(BOOL flag)
OOWeakReference ** _members
void setLeader:(ShipEntity *leader)
< NSFastEnumeration > unsigned long _updateCount
void setGroup:(OOShipGroup *group)
void setOwner:(Entity *who_owns_entity)