Skip to content

TMinBinaryHeap

Ivan Semenkov edited this page Feb 11, 2021 · 2 revisions

Table of contents

About

Heap type. The values with the lowest priority are stored at the top of the heap and will be the first returned.

uses
  container.binaryheap, utils.functor;
  
type
  generic TMinBinaryHeap<T, BinaryCompareFunctor> = class

BinaryCompareFunctor is based on utils.functor.TBinaryFunctor interface and used to compare two array items in sort and search functions.

TOptionalValue

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}

Create

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);
Example
uses
  container.binaryheap, utils.functor;
  
type
  TIntegerMinBinaryHeap = {$IFDEF FPC}type specialize{$ENDIF} TMinBinaryHeap<Integer, 
    TCompareFunctorInteger>;

var
  heap : TIntegerMinBinaryHeap;
  
begin
  heap := TIntegerMinBinaryHeap.Create;

  FreeAndNil(heap);
end;

Append

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;
Example
uses
  container.binaryheap, utils.functor;
  
type
  TIntegerMinBinaryHeap = {$IFDEF FPC}type specialize{$ENDIF} TMinBinaryHeap<Integer, 
    TCompareFunctorInteger>;

var
  heap : TIntegerMinBinaryHeap;
  
begin
  heap := TIntegerMinBinaryHeap.Create;
  heap.Append(1);

  FreeAndNil(heap);
end;

Pop

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.

Example
uses
  container.binaryheap, utils.functor;
  
type
  TIntegerMinBinaryHeap = {$IFDEF FPC}type specialize{$ENDIF} TMinBinaryHeap<Integer, 
    TCompareFunctorInteger>;

var
  heap : TIntegerMinBinaryHeap;
  
begin
  heap := TIntegerMinBinaryHeap.Create;
  writeln(heap.Pop);

  FreeAndNil(heap);
end;

NumEntries

Find the number of values stored in a binary heap.

function NumEntries : Cardinal;
Example
uses
  container.binaryheap, utils.functor;
  
type
  TIntegerMinBinaryHeap = {$IFDEF FPC}type specialize{$ENDIF} TMinBinaryHeap<Integer, 
    TCompareFunctorInteger>;

var
  heap : TIntegerMinBinaryHeap;
  
begin
  heap := TIntegerMinBinaryHeap.Create;
  writeln(heap.NumEntries);

  FreeAndNil(heap);
end;

IsEmpty

Return true if container is empty.

function IsEmpty : Boolean;
Example
uses
  container.binaryheap, utils.functor;
  
type
  TIntegerMinBinaryHeap = {$IFDEF FPC}type specialize{$ENDIF} TMinBinaryHeap<Integer, 
    TCompareFunctorInteger>;

var
  heap : TIntegerMinBinaryHeap;
  
begin
  heap := TIntegerMinBinaryHeap.Create;
  if heap.IsEmpty then
    ;

  FreeAndNil(heap);
end;
Clone this wiki locally