BinaryHeap
BinaryHeap is a priority queue (CFBinaryHeap in shape): a binary min-heap
whose member with the smallest priority is always first.
insert and removeMinimum are O(log n).
From 0.72.
#import "BinaryHeap.xc" // or the Foundation umbrellaOverview
Section titled “Overview”BinaryHeap* q = new BinaryHeap();q.insert(taskA, (i32)5);q.insert(taskB, (i32)2);Object* next = q.removeMinimum(); // taskBPriorities, not a comparator. Each member carries an integer priority, lower coming out first, so the caller orders by any key it likes: a deadline, a distance, or a negated score for a max-heap. Members of equal priority come out in no promised order.
Storage is two parallel arrays, the members and their priorities, in heap
order (parent (i-1)/2, children 2i+1 and 2i+2), grown by doubling, so an
insert allocates nothing until the arrays are full.
Ownership (ARC). The heap holds a strong reference to each member while it
is in the heap. removeMinimum hands its reference to the
caller.
Topics
Section titled “Topics”Adding · insert
Reading · minimum · minimumPriority · count · isEmpty
Removing · removeMinimum · removeAll
Adding
Section titled “Adding”insert
Section titled “insert”void insert(Object* o, i32 pri)Adds o with priority pri; lower comes out first. A null o is ignored.
Reading
Section titled “Reading”minimum
Section titled “minimum”Object* minimum(void)The member that would come out next, which stays in the heap; null when empty.
minimumPriority
Section titled “minimumPriority”i32 minimumPriority(void)The priority of minimum; 0 when empty.
i32 count(void)The number of members.
isEmpty
Section titled “isEmpty”bool isEmpty(void)Whether the heap has no members.
Removing
Section titled “Removing”removeMinimum
Section titled “removeMinimum”Object* removeMinimum(void)Takes out and returns the member with the smallest priority; null when empty.
removeAll
Section titled “removeAll”void removeAll(void)Empties the heap, releasing every member.