Skip to content

Instantly share code, notes, and snippets.

@jorbsd
Created November 25, 2009 03:54
Show Gist options
  • Save jorbsd/242456 to your computer and use it in GitHub Desktop.
Save jorbsd/242456 to your computer and use it in GitHub Desktop.
// gcc -o heap2 heap2.m -framework Foundation && ./heap2
#import <Foundation/Foundation.h>
CFTypeRef JBBRetainCallBack(CFAllocatorRef allocator, const void *obj) {
return [(id)obj retain];
}
void JBBReleaseCallBack(CFAllocatorRef allocator, const void *obj) {
[(id)obj release];
}
CFComparisonResult JBBCompareCallBack(const void *lhs, const void *rhs, void *info) {
return [(id)lhs compare:(id)rhs];
}
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
CFBinaryHeapCallBacks ourCallbacks = {
.version = 0,
.copyDescription = CFCopyDescription,
.retain = JBBRetainCallBack,
.release = JBBReleaseCallBack,
.compare = JBBCompareCallBack,
};
CFBinaryHeapRef heap = CFBinaryHeapCreate(NULL, 0, &ourCallbacks, NULL);
CFBinaryHeapAddValue(heap, CFSTR("Jane"));
CFBinaryHeapAddValue(heap, @"Joe");
id minValue = nil;
BOOL hasMin = CFBinaryHeapGetMinimumIfPresent(heap, (const void **)&minValue);
[minValue retain];
NSLog(@"hasMin = %i, minValue = %@", hasMin, minValue);
[minValue release];
CFShow(heap);
CFRelease(heap);
[pool drain];
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment