Oolite
Loading...
Searching...
No Matches
OOPriorityQueue Class Reference

#include <OOPriorityQueue.h>

Inheritance diagram for OOPriorityQueue:
Collaboration diagram for OOPriorityQueue:

Instance Methods

(id) - initWithComparator:
(void) - addObject:
(void) - removeObject:
(void) - removeExactObject:
(NSUInteger) - count
(id) - nextObject
(id) - peekAtNextObject
(void) - removeNextObject
(void) - addObjects:
(NSArray *) - sortedObjects
(NSEnumerator *) - objectEnumerator
(id) - init [implementation]
(void) - dealloc [implementation]
(NSString *) - description [implementation]
(BOOL) - isEqual: [implementation]
(NSUInteger) - hash [implementation]
(id) - copyWithZone: [implementation]
(void) - makeObjectsPerformSelector: [implementation]
(void) - bubbleUpFrom: [implementation]
(void) - bubbleDownFrom: [implementation]
(void) - growBuffer [implementation]
(void) - shrinkBuffer [implementation]
(void) - removeObjectAtIndex: [implementation]

Class Methods

(instancetype) + queueWithComparator:

Private Attributes

SEL _comparator
OO_PQ_STRONG id * _heap
NSUInteger _count
NSUInteger _capacity

Detailed Description

Definition at line 51 of file OOPriorityQueue.h.

Method Documentation

◆ addObject:

- (void) addObject: (id) object

Definition at line 245 of file OOPriorityQueue.m.

245 :(id)object
246{
247 NSUInteger i;
248
249 // Validate object
250 if (object == nil) {
251 [NSException raise:NSInvalidArgumentException
252 format:@"Attempt to insert nil into OOPriorityQueue."];
253 }
254
255 if (![object respondsToSelector:_comparator]) {
256 [NSException raise:NSInvalidArgumentException
257 format:@"Attempt to insert object (%@) which does not support comparator %@ into OOPriorityQueue.",
258 object, NSStringFromSelector(_comparator)];
259 }
260
261 // Ensure there is sufficent space.
262 if (_count == _capacity)
263 [self growBuffer];
264
265 // insert object at end of buffer.
266 i = _count++;
267 _heap[i] = object;
268
269 [self bubbleUpFrom:i];
270 [object retain];
271}
return nil
OO_PQ_STRONG id * _heap
NSUInteger _capacity

References _capacity, _comparator, _count, _heap, bubbleUpFrom:, growBuffer, and nil.

Referenced by addObjects:, and OOScriptTimer::scheduleTimer.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ addObjects:

- (void) addObjects: (id) collection

Definition at line 331 of file OOPriorityQueue.m.

331 :(id)collection
332{
333 id value = nil;
334
335 if ([collection respondsToSelector:@selector(objectEnumerator)])
336 collection = [collection objectEnumerator];
337 if (![collection respondsToSelector:@selector(nextObject)])
338 return;
339
340 while ((value = [collection nextObject]))
341 [self addObject:value];
342}
NSEnumerator * objectEnumerator()

References addObject:, nextObject, nil, and objectEnumerator.

Referenced by OOScriptTimer::updateTimers.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ bubbleDownFrom:

- (void) bubbleDownFrom: (NSUInteger) i
implementation

Provided by category OOPriorityQueue(Private).

Definition at line 389 of file OOPriorityQueue.m.

389 :(NSUInteger)i
390{
391 NSUInteger end = _count - 1;
392 NSUInteger li, ri, next;
393 id obj = nil;
394
395 obj = _heap[i];
396 while (PQLeftChild(i) <= end) {
397 li = PQLeftChild(i);
398 ri = PQRightChild(i);
399
400 // If left child has lower priority than right child, or there is only one child...
401 if (li == end || PQCompare(_heap[li], _heap[ri], _comparator) < 0) {
402 next = li;
403 } else {
404 next = ri;
405 }
406
407 if (PQCompare(_heap[next], obj, _comparator) < 0) {
408 // Exchange parent with lowest-priority child
409 _heap[i] = _heap[next];
410 _heap[next] = obj;
411 i = next;
412 } else
413 break;
414 }
415}
OOINLINE NSComparisonResult PQCompare(id a, id b, SEL comparator)
OOINLINE NSUInteger PQLeftChild(NSUInteger n) INLINE_CONST_FUNC
OOINLINE NSUInteger PQRightChild(NSUInteger n) INLINE_CONST_FUNC

Referenced by OOPriorityQueue(Private)::removeObjectAtIndex:.

Here is the caller graph for this function:

◆ bubbleUpFrom:

- (void) bubbleUpFrom: (NSUInteger) i
implementation

Provided by category OOPriorityQueue(Private).

Definition at line 370 of file OOPriorityQueue.m.

370 :(NSUInteger)i
371{
372 NSUInteger pi;
373 id obj = nil, par = nil;
374
375 while (0 < i) {
376 pi = PQParent(i);
377 obj = _heap[i];
378 par = _heap[pi];
379
380 if (PQCompare(obj, par, _comparator) < 0) {
381 _heap[i] = par;
382 _heap[pi] = obj;
383 i = pi;
384 } else
385 break;
386 }
387}
OOINLINE NSUInteger PQParent(NSUInteger n) INLINE_CONST_FUNC

Referenced by addObject:.

Here is the caller graph for this function:

◆ copyWithZone:

- (id) copyWithZone: (NSZone*) zone
implementation

Definition at line 222 of file OOPriorityQueue.m.

222 :(NSZone*)zone
223{
224 OOPriorityQueue* copy = nil;
225
226 copy = [[self class] allocWithZone:zone];
227 if (copy != nil) {
228 copy->_comparator = _comparator;
229 copy->_count = _count;
230 copy->_capacity = _count;
231
232 copy->_heap = malloc(_count * sizeof(id));
233 if (copy->_heap != NULL) {
234 memcpy(copy->_heap, _heap, _count * sizeof(id));
235 [copy makeObjectsPerformSelector:@selector(retain)];
236 } else if (_count != 0) {
237 [copy release];
238 copy = nil;
239 }
240 }
241
242 return copy;
243}
void makeObjectsPerformSelector:(SEL selector)

References _capacity, _comparator, _count, _heap, makeObjectsPerformSelector:, and nil.

Here is the call graph for this function:

◆ count

- (NSUInteger) count

Definition at line 306 of file OOPriorityQueue.m.

307{
308 return _count;
309}

References _count, and count.

Referenced by count, and isEqual:.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ dealloc

- (void) dealloc
implementation

Definition at line 152 of file OOPriorityQueue.m.

153{
154 [self makeObjectsPerformSelector:@selector(release)];
155 free(_heap);
156
157 [super dealloc];
158}

References _heap, dealloc, and makeObjectsPerformSelector:.

Referenced by dealloc.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ description

- (NSString *) description
implementation

Definition at line 160 of file OOPriorityQueue.m.

161{
162 return [NSString stringWithFormat:@"<%@ %p>{count=%zu, capacity=%zu}", [self class], self, _count, _capacity];
163}

References description.

Referenced by description.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ growBuffer

- (void) growBuffer
implementation

Provided by category OOPriorityQueue(Private).

Definition at line 417 of file OOPriorityQueue.m.

418{
419 id* newBuffer = NULL;
420 NSUInteger newCapacity;
421
422 newCapacity = _capacity * 3 / 2;
423 if (newCapacity < kMinCapacity)
424 newCapacity = kMinCapacity;
425
426 // Note: realloc(NULL, size) with non-zero size is equivalent to malloc(size), so this is OK starting from a NULL buffer.
427 newBuffer = realloc(_heap, newCapacity * sizeof(id));
428 if (newBuffer == NULL) {
429 // Attempt to grow by just one waffer-thin slot.
430 newCapacity = _capacity + 1;
431 newBuffer = realloc(_heap, newCapacity * sizeof(id));
432
433 if (newBuffer == NULL) {
434 // Failed to grow.
435 [NSException raise:NSMallocException
436 format:@"Could not expand capacity of OOPriorityQueue."];
437 }
438 }
439
440 _heap = newBuffer;
441 _capacity = newCapacity;
442
443 assert(_count < _capacity);
444}
@ kMinCapacity

Referenced by addObject:.

Here is the caller graph for this function:

◆ hash

- (NSUInteger) hash
implementation

Definition at line 215 of file OOPriorityQueue.m.

216{
217 if (_count == 0)
218 return NSNotFound;
219 return _count ^ [_heap[0] hash];
220}

References _count, and hash.

Referenced by hash.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ init

- (id) init
implementation

Definition at line 147 of file OOPriorityQueue.m.

148{
149 return [self initWithComparator:@selector(compare:)];
150}

References init, and initWithComparator:.

Referenced by init.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ initWithComparator:

- (id) initWithComparator: (SEL) comparator

Definition at line 132 of file OOPriorityQueue.m.

132 :(SEL)comparator
133{
134 if (comparator == NULL) {
135 [self release];
136 return nil;
137 }
138
139 self = [super init];
140 if (self != nil) {
141 _comparator = comparator;
142 }
143
144 return self;
145}

References _comparator, and nil.

Referenced by init.

Here is the caller graph for this function:

◆ isEqual:

- (BOOL) isEqual: (id) object
implementation

Definition at line 184 of file OOPriorityQueue.m.

184 :(id)object
185{
186 NSUInteger i;
187 OOPriorityQueue *selfCopy = nil, *otherCopy = nil;
188 BOOL identical = YES;
189
190 if (object == self)
191 return YES;
192 if (![object isKindOfClass:[self class]])
193 return NO;
194
195 if (_count != [object count])
196 return NO;
197 if (_count == 0)
198 return YES;
199
200 selfCopy = [self copy];
201 otherCopy = [object copy];
202 i = _count;
203 while (i--) {
204 if (![[selfCopy nextObject] isEqual:[otherCopy nextObject]]) {
205 identical = NO;
206 break;
207 }
208 }
209 [selfCopy release];
210 [otherCopy release];
211
212 return identical;
213}

References _count, count, nextObject, and nil.

Here is the call graph for this function:

◆ makeObjectsPerformSelector:

- (void) makeObjectsPerformSelector: (SEL) selector
implementation

Provided by category OOPriorityQueue(Private).

Definition at line 358 of file OOPriorityQueue.m.

358 :(SEL)selector
359{
360 NSUInteger i;
361
362 if (selector == NULL)
363 return;
364
365 for (i = 0; i != _count; ++i) {
366 [_heap[i] performSelector:selector];
367 }
368}

Referenced by copyWithZone:, and dealloc.

Here is the caller graph for this function:

◆ nextObject

- (id) nextObject

Definition at line 311 of file OOPriorityQueue.m.

312{
313 id result = [self peekAtNextObject];
314 [self removeNextObject];
315 return result;
316}

References nextObject, peekAtNextObject, and removeNextObject.

Referenced by addObjects:, isEqual:, nextObject, and OOPriorityQueueEnumerator::nextObject.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ objectEnumerator

- (NSEnumerator *) objectEnumerator

Definition at line 349 of file OOPriorityQueue.m.

350{
351 return [[[OOPriorityQueueEnumerator alloc] initWithPriorityQueue:self] autorelease];
352}

References objectEnumerator.

Referenced by addObjects:, objectEnumerator, and sortedObjects.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ peekAtNextObject

- (id) peekAtNextObject

Definition at line 318 of file OOPriorityQueue.m.

319{
320 if (_count == 0)
321 return nil;
322 // return [[_heap[0] retain] autorelease];
323 return _heap[0];
324}

References _count, _heap, nil, and peekAtNextObject.

Referenced by nextObject, peekAtNextObject, and OOScriptTimer::updateTimers.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ queueWithComparator:

+ (instancetype) queueWithComparator: (SEL) comparator

Definition at line 127 of file OOPriorityQueue.m.

127 :(SEL)comparator
128{
129 return [[[self alloc] initWithComparator:comparator] autorelease];
130}

◆ removeExactObject:

- (void) removeExactObject: (id) object

Definition at line 292 of file OOPriorityQueue.m.

292 :(id)object
293{
294 NSUInteger i;
295
296 if (object == nil)
297 return;
298
299 for (i = 0; i < _count; ++i) {
300 if (object == _heap[i]) {
301 [self removeObjectAtIndex:i];
302 }
303 }
304}

References _count, _heap, nil, and removeObjectAtIndex:.

Referenced by OOScriptTimer::unscheduleTimer.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ removeNextObject

- (void) removeNextObject

Definition at line 326 of file OOPriorityQueue.m.

327{
328 [self removeObjectAtIndex:0];
329}

References removeNextObject, and removeObjectAtIndex:.

Referenced by nextObject, removeNextObject, and OOScriptTimer::updateTimers.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ removeObject:

- (void) removeObject: (id) object

Definition at line 273 of file OOPriorityQueue.m.

273 :(id)object
274{
275 NSUInteger i;
276
277 /* Perform linear search for object (using comparator). A depth-first
278 search could skip leaves of lower priority, but I don't expect this to
279 be called very often.
280 */
281
282 if (object == nil)
283 return;
284
285 for (i = 0; i < _count; ++i) {
286 if (PQCompare(object, _heap[i], _comparator) == 0) {
287 [self removeObjectAtIndex:i];
288 }
289 }
290}

References _comparator, _count, _heap, nil, PQCompare(), and removeObjectAtIndex:.

Here is the call graph for this function:

◆ removeObjectAtIndex:

- (void) removeObjectAtIndex: (NSUInteger) i
implementation

Provided by category OOPriorityQueue(Private).

Definition at line 466 of file OOPriorityQueue.m.

466 :(NSUInteger)i
467{
468 id object = nil;
469
470 if (_count <= i)
471 return;
472
473 object = _heap[i];
474 if (i < --_count) {
475 // Overwrite object with last object in array
476 _heap[i] = _heap[_count];
477
478 // Push previously-last object down until tree is partially ordered.
479 [self bubbleDownFrom:i];
480 } else {
481 // Special case: removing last (or only) object. No bubbling needed.
482 }
483
484 [object autorelease];
485 if ((_count * 2) <= _capacity)
486 [self shrinkBuffer];
487}

Referenced by removeExactObject:, removeNextObject, and removeObject:.

Here is the caller graph for this function:

◆ shrinkBuffer

- (void) shrinkBuffer
implementation

Provided by category OOPriorityQueue(Private).

Definition at line 446 of file OOPriorityQueue.m.

447{
448 NSUInteger amountToRemove;
449 id* newBuffer = NULL;
450 NSUInteger newCapacity;
451
452 if (kMinCapacity < _capacity) {
453 // Remove two thirds of free space, if at least three slots are free.
454 amountToRemove = (_capacity - _count) * 2 / 3;
455 if (2 < amountToRemove) {
456 newCapacity = _capacity - amountToRemove;
457 newBuffer = realloc(_heap, newCapacity * sizeof(id));
458 if (newBuffer != NULL) {
459 _heap = newBuffer;
460 _capacity = newCapacity;
461 }
462 }
463 }
464}

Referenced by OOPriorityQueue(Private)::removeObjectAtIndex:.

Here is the caller graph for this function:

◆ sortedObjects

- (NSArray *) sortedObjects

Definition at line 344 of file OOPriorityQueue.m.

345{
346 return [[self objectEnumerator] allObjects];
347}

References objectEnumerator, and sortedObjects.

Referenced by OOScriptTimer::noteGameReset, and sortedObjects.

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ _capacity

- (NSUInteger) _capacity
private

Definition at line 56 of file OOPriorityQueue.h.

Referenced by addObject:, and copyWithZone:.

◆ _comparator

- (SEL) _comparator
private

Definition at line 53 of file OOPriorityQueue.h.

Referenced by addObject:, copyWithZone:, initWithComparator:, and removeObject:.

◆ _count

- (NSUInteger) _count
private

◆ _heap

- (OO_PQ_STRONG id*) _heap
private

The documentation for this class was generated from the following files: