constructor that gets content from arbitrary range
constructor that gets content from arbitrary range
Remove the first element (i.e. the smallest) from the queue.
removes all elements but keeps allocated memory
Remove the first element (i.e. the smallest) from the queue.
Add an element to the queue.
Add multiple elements to the queue.
clear the heap and return all elements (unordered!)
Allocate memory for s elements. Does nothing if s < length.
basic usage
// custom predicate turns a min-heap into a max-heap PriorityQueue!int q; q.push(7); q.push(3); q.push(5); q.push(3); assert(q.length == 4); // note that duplicates are kept assert(q.pop == 3); assert(q.pop == 3); assert(q.pop == 5); assert(q.pop == 7); assert(q.empty);
custom predicate (without state)
// custom predicate turns a min-heap into a max-heap PriorityQueue!(int, "a > b") q; q.push(7); q.push([9,2,8,3,4,1,6,5,8,0]); assert(q.pop == 9); assert(q.pop == 8); assert(q.pop == 8); assert(q.pop == 7); q.push(18); assert(q.pop == 18); assert(q.pop == 6); assert(q.pop == 5); q.clear; assert(q.empty);
custom predicate (with state)
// sometimes, you need a custom predicate/comparator that contains state. // For example this int-comparator puts one selected item first, followed // by all other integers in their usual order. struct Cmp { int priority; @disable this(); this(int p) { this.priority = p; } // this is understood as a comparison 'a < b'. bool opCall(int a, int b) { if(b == priority) return false; if(a == priority) return true; return a < b; } } // the constructor now takes an instance of the comparator auto q = PriorityQueue!(int, Cmp)(Cmp(3)); q.push([2,3,4,1,5,0]); assert(q.pop == 3); assert(q.pop == 0); assert(q.pop == 1); assert(q.pop == 2); assert(q.pop == 4); assert(q.pop == 5);
Priority queue which allows fast access to the smallest element. Implemented as binary heap embedded into a jive.Array.