Oolite
Loading...
Searching...
No Matches
OOPriorityQueue(Private) Category Reference

Instance Methods

(void) - makeObjectsPerformSelector:
(void) - bubbleUpFrom:
(void) - bubbleDownFrom:
(void) - growBuffer
(void) - shrinkBuffer
(void) - removeObjectAtIndex:

Detailed Description

Definition at line 97 of file OOPriorityQueue.m.

Method Documentation

◆ bubbleDownFrom:

- (void) bubbleDownFrom: (NSUInteger) i

Extends class OOPriorityQueue.

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
return nil

References nil, PQCompare(), PQLeftChild(), and PQRightChild().

Here is the call graph for this function:

◆ bubbleUpFrom:

- (void) bubbleUpFrom: (NSUInteger) i

Extends class OOPriorityQueue.

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

References nil, PQCompare(), and PQParent().

Here is the call graph for this function:

◆ growBuffer

- (void) growBuffer

Extends class OOPriorityQueue.

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

References growBuffer, and kMinCapacity.

Referenced by growBuffer.

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

◆ makeObjectsPerformSelector:

- (void) makeObjectsPerformSelector: (SEL) selector

Extends class OOPriorityQueue.

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}

◆ removeObjectAtIndex:

- (void) removeObjectAtIndex: (NSUInteger) i

Extends class OOPriorityQueue.

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}

References OOPriorityQueue::bubbleDownFrom:, nil, and OOPriorityQueue::shrinkBuffer.

Here is the call graph for this function:

◆ shrinkBuffer

- (void) shrinkBuffer

Extends class OOPriorityQueue.

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}

References kMinCapacity, and shrinkBuffer.

Referenced by shrinkBuffer.

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

The documentation for this category was generated from the following file: