With a priority queue, everything is roughly the same as with any other generic container.
First, you need to specify the type of element that the container will be specialized for, given that the priority queue also needs a way to compare the priorities of the elements in order to work properly.
Let, for example, this be the very first way: the TCmpRel functor(see line 59):
program pq_test;
{$mode delphi}
uses
SysUtils, LgUtils, LgPriorityQueue, LgMiscUtils;
type
TPqEl = record
Data,
Prio: Integer;
constructor Make(aData, aPrio: Integer);
class function Less(const L, R: TPqEl): Boolean; static; inline; /////
end;
constructor TPqEl.Make(aData, aPrio: Integer);
begin
Data := aData;
Prio := aPrio;
end;
class function TPqEl.Less(const L, R: TPqEl): Boolean;
begin
Result := L.Prio < R.Prio;
end;
var
pq: TGBinHeap<TPqEl>;
e: TPqEl;
begin
//you can create an empty queue
//pq := TGBinHeap<TPqEl>.Create;
//or not empty
pq := TGBinHeap<TPqEl>.Create(
[TPqEl.Make(70, 1), TPqEl.Make(60, 2), TPqEl.Make(50, 3), TPqEl.Make(40, 4)]
);
//check that the queue is not empty and look at the top element
if pq.NonEmpty then
WriteLn('Top element: ', Stringify<TPqEl>(pq.Peek));
//you can add elements one by one
pq.Enqueue(TPqEl.Make(30, 5));
pq.Enqueue(TPqEl.Make(20, 0));
//or several at once
pq.EnqueueAll([TPqEl.Make(10, 6), TPqEl.Make(0, -1)]);
//dequeue element
e := pq.Dequeue;
WriteLn('Element: ', Stringify<TPqEl>(e));
//dequeue all elements
while pq.TryDequeue(e) do
WriteLn('Dequeued: ', Stringify<TPqEl>(e));
WriteLn('Remaining in queue: ', pq.Count);
pq.Free;
end.