80 return ((n + 1) >> 1) - 1;
83typedef NSComparisonResult (*
CompareIMP)(
id self,
SEL _cmd,
id other);
88 NSComparisonResult result;
91 compare = (
CompareIMP)[a methodForSelector:comparator];
92 result = compare(a, comparator, b);
97@interface OOPriorityQueue (Private)
99- (void)makeObjectsPerformSelector:(
SEL)selector;
101- (void)bubbleUpFrom:(NSUInteger)i;
102- (void)bubbleDownFrom:(NSUInteger)i;
107- (void)removeObjectAtIndex:(NSUInteger)i;
110- (void)appendDebugDataToString:(NSMutableString*)string index:(NSUInteger)i depth:(NSUInteger)depth;
127+ (instancetype)queueWithComparator:(
SEL)comparator
129 return [[[
self alloc] initWithComparator:comparator] autorelease];
132- (id)initWithComparator:(
SEL)comparator
134 if (comparator == NULL) {
162 return [
NSString stringWithFormat:@"<%@ %p>{count=%zu, capacity=%zu}", [
self class],
self, _count, _capacity];
166- (NSString*)debugDescription
168 NSMutableString* result =
nil;
170 result = [NSMutableString string];
171 [result appendFormat:@"<%@ %p> (count=%zu, capacity=%zu, comparator=%@)", [
self class],
self, _count, _capacity, NSStringFromSelector(_comparator)];
174 [result appendString:@"\n{\n"];
175 [
self appendDebugDataToString:result index:0 depth:0];
176 [result appendString:@"}"];
178 [result appendString:@" {}"];
184- (BOOL)isEqual:(
id)object
188 BOOL identical = YES;
192 if (![
object isKindOfClass:[
self class]])
200 selfCopy = [
self copy];
201 otherCopy = [
object copy];
219 return _count ^ [_heap[0] hash];
222- (id)copyWithZone:(NSZone*)zone
226 copy = [[
self class] allocWithZone:zone];
233 if (copy->
_heap != NULL) {
245- (void)addObject:(
id)object
252 format:@"Attempt to insert nil into OOPriorityQueue."];
257 format:@"Attempt to insert object (%@) which does not support comparator %@ into OOPriorityQueue.",
258 object, NSStringFromSelector(_comparator)];
273- (void)removeObject:(
id)object
285 for (i = 0; i <
_count; ++i) {
292- (void)removeExactObject:(
id)object
299 for (i = 0; i <
_count; ++i) {
300 if (
object ==
_heap[i]) {
331- (void)addObjects:(
id)collection
337 if (![collection respondsToSelector:
@selector(
nextObject)])
356@implementation OOPriorityQueue (Private)
358- (void)makeObjectsPerformSelector:(
SEL)selector
362 if (selector == NULL)
365 for (i = 0; i != _count; ++i) {
366 [
_heap[
i] performSelector:selector];
370- (void)bubbleUpFrom:(NSUInteger)i
380 if (
PQCompare(obj, par, _comparator) < 0) {
389- (void)bubbleDownFrom:(NSUInteger)i
391 NSUInteger end = _count - 1;
392 NSUInteger li, ri, next;
401 if (li == end ||
PQCompare(_heap[li], _heap[ri], _comparator) < 0) {
407 if (
PQCompare(_heap[next], obj, _comparator) < 0) {
409 _heap[
i] = _heap[
next];
419 id* newBuffer = NULL;
420 NSUInteger newCapacity;
422 newCapacity = _capacity * 3 / 2;
427 newBuffer = realloc(_heap, newCapacity *
sizeof(
id));
428 if (newBuffer == NULL) {
430 newCapacity = _capacity + 1;
431 newBuffer = realloc(_heap, newCapacity *
sizeof(
id));
433 if (newBuffer == NULL) {
436 format:@"Could not expand capacity of OOPriorityQueue."];
441 _capacity = newCapacity;
443 assert(_count < _capacity);
448 NSUInteger amountToRemove;
449 id* newBuffer = NULL;
450 NSUInteger newCapacity;
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) {
460 _capacity = newCapacity;
466- (void)removeObjectAtIndex:(NSUInteger)i
485 if ((_count * 2) <= _capacity)
490- (void)appendDebugDataToString:(NSMutableString*)string index:(NSUInteger)i depth:(NSUInteger)depth
499 [string appendString:@" "];
500 [string appendString:[_heap[i] description]];
501 [string appendString:@"\n"];
503 [
self appendDebugDataToString:string index:PQLeftChild(i) depth:depth + 1];
504 [
self appendDebugDataToString:string index:PQRightChild(i) depth:depth + 1];
510@implementation OOPriorityQueueEnumerator
547@implementation OOPriorityQueue (DebugGraphViz)
549static NSString* EscapedString(NSString*
string)
551 NSString* srcStrings[] = {
553 @"\\",
@"\"", @"\
'", @"\r", @"\n", @"\t", nil
555 NSString* subStrings[] = {
556 // Note: must be same order.
557 @"\\\\", @"\\\"", @"\\\'", @"\\r", @"\\n", @"\\t", nil
560 NSString **src = srcStrings, **sub = subStrings;
561 NSMutableString* mutable = nil;
562 NSString* result = nil;
564 mutable = [string mutableCopy];
565 while (*src != nil) {
566 [mutable replaceOccurrencesOfString:*src++
569 range:NSMakeRange(0, [mutable length])];
572 if ([mutable length] == [string length]) {
575 result = [[mutable copy] autorelease];
581- (NSString*)generateGraphViz
583 NSMutableString* result = nil;
586 NSString* desc = nil;
588 result = [NSMutableString string];
591 [result appendString:
592 @"// OOPriorityQueue partially ordered tree dump\n\n"
595 "\tgraph [charset=\"UTF-8\", label=\"OOPriorityQueue heap dump\", labelloc=t, labeljust=l];\n"
596 "\tnode [shape=record];\n\t\n\t"];
599 for (i = 0; i < _count; ++i) {
601 desc = [node description];
602 if ([desc length] > 70) {
603 desc = [[desc substringToIndex:64] stringByAppendingString:@"..."];
606 [result appendFormat:@"\tnode_%zu [label=\"<f0> | <f1> %@ | <f2>\"];\n", i, EscapedString(desc)];
610 for (i = 0; PQLeftChild(i) < _count; ++i) {
611 [result appendFormat:@"\tnode_%zu:f0 -> node_%zu:f1;\n", i, PQLeftChild(i)];
612 if (PQRightChild(i) < _count)
613 [result appendFormat:@"\tnode_%zu:f2 -> node_%zu:f1;\n", i, PQRightChild(i)];
616 [result appendString:@"}\n"];
621- (void)writeGraphVizToURL:(NSURL*)url
623 NSString* graphViz = nil;
626 graphViz = [self generateGraphViz];
627 data = [graphViz dataUsingEncoding:NSUTF8StringEncoding];
630 [data writeToURL:url atomically:YES];
634- (void)writeGraphVizToPath:(NSString*)path
636 [self writeGraphVizToURL:[NSURL fileURLWithPath:path]];
#define INLINE_CONST_FUNC
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
OOINLINE NSUInteger PQParent(NSUInteger n) INLINE_CONST_FUNC
NSComparisonResult(* CompareIMP)(id self, SEL _cmd, id other)
void addObject:(id object)
void removeObjectAtIndex:(NSUInteger i)
void makeObjectsPerformSelector:(SEL selector)
id initWithComparator:(SEL comparator)
NSArray * sortedObjects()
void bubbleUpFrom:(NSUInteger i)
NSEnumerator * objectEnumerator()
void bubbleDownFrom:(NSUInteger i)