-
Notifications
You must be signed in to change notification settings - Fork 10
TMaxBinaryHeap
Heap type. The values with the greatest priority are stored at the top of the heap and will be the first returned.
uses
container.binaryheap, utils.functor;
type
generic TMaxBinaryHeap<T, BinaryCompareFunctor> = class
BinaryCompareFunctor is based on utils.functor.TBinaryFunctor interface and used to compare two array items in sort and search functions.
If macro {$USE_OPTIONAL}
is defined, then all methods return a TOptionalValue wrapper, otherwise T.
uses
utils.optional;
type
TOptionalValue = {$IFDEF FPC}specialize{$ENDIF} TOptional<T>;
For non-existent values, returns a empty TOptionalValue if defined or an EHeapEmptyException is thrown.
type
{$IFNDEF USE_OPTIONAL}
EHeapEmptyException = class(Exception);
{$ENDIF}
A new min binary heap can be created by call its constructor. It is also possible to reserve memory for items by the first argument.
constructor Create (ALength : Cardinal = 0);
uses
container.binaryheap, utils.functor;
type
TIntegerMaxBinaryHeap = {$IFDEF FPC}type specialize{$ENDIF} TMaxBinaryHeap<Integer, TCompareFunctorInteger>;
var
heap : TIntegerMaxBinaryHeap;
begin
heap := TIntegerMaxBinaryHeap.Create;
FreeAndNil(heap);
end;
Insert a value into a binary heap. Return true if the request was successful, false if it was not possible to add the new entry.
function Append (AValue : T) : Boolean;
uses
container.binaryheap, utils.functor;
type
TIntegerMaxBinaryHeap = {$IFDEF FPC}type specialize{$ENDIF} TMaxBinaryHeap<Integer, TCompareFunctorInteger>;
var
heap : TIntegerMaxBinaryHeap;
begin
heap := TIntegerMaxBinaryHeap.Create;
heap.Append(1);
FreeAndNil(heap);
end;
Remove the first value from a binary heap.
function Pop : {$IFNDEF USE_OPTIONAL}T{$ELSE}TOptionalValue{$ENDIF};
If heap is empty returns empty TOptionalValue or raise EHeapEmptyException.
uses
container.binaryheap, utils.functor;
type
TIntegerMaxBinaryHeap = {$IFDEF FPC}type specialize{$ENDIF} TMaxBinaryHeap<Integer, TCompareFunctorInteger>;
var
heap : TIntegerMaxBinaryHeap;
begin
heap := TIntegerMaxBinaryHeap.Create;
writeln(heap.Pop);
FreeAndNil(heap);
end;
Find the number of values stored in a binary heap.
function NumEntries : Cardinal;
uses
container.binaryheap, utils.functor;
type
TIntegerMaxBinaryHeap = {$IFDEF FPC}type specialize{$ENDIF} TMaxBinaryHeap<Integer, TCompareFunctorInteger>;
var
heap : TIntegerMaxBinaryHeap;
begin
heap := TIntegerMaxBinaryHeap.Create;
writeln(heap.NumEntries);
FreeAndNil(heap);
end;
Return true if container is empty.
function IsEmpty : Boolean;
uses
container.binaryheap, utils.functor;
type
TIntegerMaxBinaryHeap = {$IFDEF FPC}type specialize{$ENDIF} TMaxBinaryHeap<Integer, TCompareFunctorInteger>;
var
heap : TIntegerMaxBinaryHeap;
begin
heap := TIntegerMaxBinaryHeap.Create;
if heap.IsEmpty then
;
FreeAndNil(heap);
end;