PriorityQueue

Priority queue which allows fast access to the smallest element. Implemented as binary heap embedded into a jive.Array.

Constructors

this
this()
Undocumented in source.
this
this(_pred p)
Undocumented in source.
this
this(_pred p, Stuff data)

constructor that gets content from arbitrary range

this
this(Stuff data)

constructor that gets content from arbitrary range

Members

Aliases

popFront
alias popFront = pop

Remove the first element (i.e. the smallest) from the queue.

pushBack
alias pushBack = push
Undocumented in source.

Functions

clear
void clear()

removes all elements but keeps allocated memory

empty
bool empty()
front
inout(V) front()
length
size_t length()
pop
V pop()

Remove the first element (i.e. the smallest) from the queue.

push
void push(V value)

Add an element to the queue.

push
void push(Stuff data)

Add multiple elements to the queue.

release
Array!V release()

clear the heap and return all elements (unordered!)

reserve
void reserve(size_t s)

Allocate memory for s elements. Does nothing if s < length.

Mixins

__anonymous
mixin PredicateHelper!(_pred, V)
Undocumented in source.

Examples

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);

Meta