Skip to content
This repository was archived by the owner on Feb 24, 2025. It is now read-only.

TMaxBinaryHeap

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

Table of contents

About

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.

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
  TIntegerMaxBinaryHeap = {$IFDEF FPC}type specialize{$ENDIF} TMaxBinaryHeap<Integer, TCompareFunctorInteger>;

var
  heap : TIntegerMaxBinaryHeap;
  
begin
  heap := TIntegerMaxBinaryHeap.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
  TIntegerMaxBinaryHeap = {$IFDEF FPC}type specialize{$ENDIF} TMaxBinaryHeap<Integer, TCompareFunctorInteger>;

var
  heap : TIntegerMaxBinaryHeap;
  
begin
  heap := TIntegerMaxBinaryHeap.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
  TIntegerMaxBinaryHeap = {$IFDEF FPC}type specialize{$ENDIF} TMaxBinaryHeap<Integer, TCompareFunctorInteger>;

var
  heap : TIntegerMaxBinaryHeap;
  
begin
  heap := TIntegerMaxBinaryHeap.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
  TIntegerMaxBinaryHeap = {$IFDEF FPC}type specialize{$ENDIF} TMaxBinaryHeap<Integer, TCompareFunctorInteger>;

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

  FreeAndNil(heap);
end;

IsEmpty

Return true if container is empty.

function IsEmpty : Boolean;
Example
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;
Clone this wiki locally