UXBinaryHeap
UXBinaryHeap is a min-heap: the item with the lowest priority is always
the one that comes out.
#use <UXKit> // or #import "UXBinaryHeap.xc"Overview
Section titled “Overview”UXBinaryHeap* q = new UXBinaryHeap();q.insert((Object*)repaint, 5);q.insert((Object*)quit, 1);q.insert((Object*)save, 3);
q.minimum(); // quit — peek, without removingq.removeMinimum(); // quitq.removeMinimum(); // saveinsert and removeMinimum are O(log n); minimum is O(1). Draining the
whole heap is a sort, which is the usual use when all items arrive before you
need any of them.
The priority is an integer you choose
Section titled “The priority is an integer you choose”There is no comparator callback. Every item carries an explicit i32, and
lower comes out first.
This is simpler than a callback, and the key can be any ordering you can compute:
q.insert((Object*)task, deadlineMs); // earliest deadline firstq.insert((Object*)node, distanceFromStart); // Dijkstraq.insert((Object*)hit, -score); // a MAX-heap, by negatingNegate the key for a max-heap. “Top N results” is a min-heap over -score, with
no second class needed.
Ties are not ordered
Section titled “Ties are not ordered”Two items with the same priority come out in an unspecified order, which is not insertion order. The heap is not stable.
When ties must break predictably, put the tiebreak in the key: a sequence number in the low bits, or a priority scaled up with the arrival index added. This is cheaper than a stable heap and keeps the comparison a single integer.
Empty is answered, not trapped
Section titled “Empty is answered, not trapped”UXBinaryHeap* empty = new UXBinaryHeap();empty.isEmpty(); // trueempty.minimum(); // 0 — null, not a crashempty.removeMinimum(); // 0A drain loop is preferably while (!q.isEmpty()), and a null check also works.
How it is stored
Section titled “How it is stored”A complete binary tree flattened into an Array, with
the standard arithmetic: parent (i-1)/2, children 2i+1 and 2i+2. There are
no pointers, no nodes, and no per-item allocation beyond the
UXHeapEntry that pairs an object with its
key.
Topics
Section titled “Topics”insert · minimum · removeMinimum · count · isEmpty · removeAll
insert
Section titled “insert”void insert(Object* o, i32 pri)Add with a priority. O(log n): appended, then sifted up.
Duplicates are allowed. The same object can be in the heap more than once, which lazy deletion relies on.
minimum
Section titled “minimum”Object* minimum(void)The lowest-priority item without removing it. 0 when empty. O(1).
removeMinimum
Section titled “removeMinimum”Object* removeMinimum(void)Take the lowest-priority item out and return it. 0 when empty. O(log n).
i32 count(void)How many items, counting duplicates separately.
isEmpty
Section titled “isEmpty”bool isEmpty(void)removeAll
Section titled “removeAll”void removeAll(void)Drop everything. The heap holds its items strongly, so a long-lived queue calls this to release them.
Example
Section titled “Example”heap count=4 minimum=quitdrain: quit resize save repaintmax-heap first out: score-90empty: isEmpty=1 minimum=0quit resize save repaint is priorities 1, 2, 3, 5 in order, inserted as 5, 1,
3, 2. The max-heap line uses three negated scores, so -90 is the minimum and
the highest score comes out first.
The program is website/site/examples/uxkit/collections.xc. The doc-examples
gate compiles it, and the listing above is its output.
Conforms to
Section titled “Conforms to”- A plain class (not an
Objectsubclass)
See also
Section titled “See also”UXHeapEntry: one item and its priorityUXOperationQueue: a queue of work to run, where this is a queue of things to orderUXTimerScheduler: earliest-deadline ordering applied to time