-
Notifications
You must be signed in to change notification settings - Fork 10
TTrie
A trie is a data structure which provides fast mappings from strings to values.
uses
container.trie;
type
generic TTrie<V> = class
If macro {$USE_OPTIONAL}
is defined, then all methods return a TOptionalValue wrapper, otherwise V.
uses
utils.optional;
type
TOptionalValue = {$IFDEF FPC}specialize{$ENDIF} TOptional<V>;
For non-existent values, returns a empty TOptionalValue if defined or an EKeyNotExistsException is thrown.
type
{$IFNDEF USE_OPTIONAL}
EKeyNotExistsException = class(Exception);
{$ENDIF}
A new trie can be created by call its constructor.
constructor Create;
uses
container.trie;
type
TIntegerTrie = {$IFDEF FPC}type specialize{$ENDIF} TTrie<Integer>;
var
trie : TIntegerTrie;
begin
trie := TIntegerTrie.Create;
FreeAndNil(trie);
end;
There are several methods to insert data to the trie.
Insert a new key-value pair into a trie. The key is a NUL-terminated string. Return true if the value was inserted successfully, or false if it was not possible to append new entry.
function Insert (Key : AnsiString; Value : V) : Boolean;
uses
container.trie;
type
TIntegerTrie = {$IFDEF FPC}type specialize{$ENDIF} TTrie<Integer>;
var
trie : TIntegerTrie;
begin
trie := TIntegerTrie.Create;
trie.Insert('test', 123);
FreeAndNil(trie);
end;
Insert a new key-value pair into a trie. The key is a sequence of bytes. Return true if the value was inserted successfully, or false if it was not possible to append new entry.
function InsertBinary (Key : PByte; KeyLength : Integer; Value : V) : Boolean;
uses
container.trie;
type
TIntegerTrie = {$IFDEF FPC}type specialize{$ENDIF} TTrie<Integer>;
var
trie : TIntegerTrie;
data : array [0 .. 10] of byte;
begin
trie := TIntegerTrie.Create;
trie.Insert(PByte(@data), 10, 123);
FreeAndNil(trie);
end;
The methods to remove data from the trie.
Remove an entry from a trie. Return true if the request was successful, false if it was not possible to find the value.
function Remove (Key : AnsiString) : Boolean;
uses
container.trie;
type
TIntegerTrie = {$IFDEF FPC}type specialize{$ENDIF} TTrie<Integer>;
var
trie : TIntegerTrie;
begin
trie := TIntegerTrie.Create;
trie.Remove('test');
FreeAndNil(trie);
end;
Remove an entry from a trie. The key is a sequence of bytes.
function RemoveBinary (Key : PByte; KeyLength : Integer) : Boolean;
uses
container.trie;
type
TIntegerTrie = {$IFDEF FPC}type specialize{$ENDIF} TTrie<Integer>;
var
trie : TIntegerTrie;
data : array [0 .. 10] of byte;
begin
trie := TIntegerTrie.Create;
trie.Remove(PByte(@data), 10);
FreeAndNil(trie);
end;
Look up a value from its key in a trie.
function Search (Key : AnsiString) : {$IFNDEF USE_OPTIONAL}V
{$ELSE}TOptionalValue{$ENDIF};
If value not exists returns empty TOptionalValue or raise EKeyNotExistsException.
uses
container.trie;
type
TIntegerTrie = {$IFDEF FPC}type specialize{$ENDIF} TTrie<Integer>;
var
trie : TIntegerTrie;
begin
trie := TIntegerTrie.Create;
writeln(trie.Search('test'));
FreeAndNil(trie);
end;
Look up a value from its key in a trie. Return default value if Key not exists.
function SearchDefault (Key : AnsiString; ADefault : V) : V;
uses
container.trie;
type
TIntegerTrie = {$IFDEF FPC}type specialize{$ENDIF} TTrie<Integer>;
var
trie : TIntegerTrie;
begin
trie := TIntegerTrie.Create;
writeln(trie.SearchDefault('test', -1));
FreeAndNil(trie);
end;
Look up a value from its key in a trie. The key is a sequence of bytes.
function SearchBinary (Key : PByte; KeyLength : Integer) :
{$IFNDEF USE_OPTIONAL}V{$ELSE}TOptionalValue{$ENDIF};
uses
container.trie;
type
TIntegerTrie = {$IFDEF FPC}type specialize{$ENDIF} TTrie<Integer>;
var
trie : TIntegerTrie;
data : array [0 .. 10] of byte;
begin
trie := TIntegerTrie.Create;
writeln(trie.SearchBinary(PByte(@data), 10));
FreeAndNil(trie);
end;
Look up a value from its key in a trie. The key is a sequence of bytes. Return default value if Key not exists.
function SearchBinaryDefault (Key : PByte; KeyLength : Integer; ADefault :
V) : V;
uses
container.trie;
type
TIntegerTrie = {$IFDEF FPC}type specialize{$ENDIF} TTrie<Integer>;
var
trie : TIntegerTrie;
data : array [0 .. 10] of byte;
begin
trie := TIntegerTrie.Create;
writeln(trie.SearchBinaryDefault(PByte(@data), 10, -1));
FreeAndNil(trie);
end;
Return the number of entries in a trie.
function NumEntries : Cardinal;
uses
container.trie;
type
TIntegerTrie = {$IFDEF FPC}type specialize{$ENDIF} TTrie<Integer>;
var
trie : TIntegerTrie;
begin
trie := TIntegerTrie.Create;
writeln(trie.NumEntries);
FreeAndNil(trie);
end;
Return true if container is empty.
function IsEmpty : Boolean;
uses
container.trie;
type
TIntegerTrie = {$IFDEF FPC}type specialize{$ENDIF} TTrie<Integer>;
var
trie : TIntegerTrie;
begin
trie := TIntegerTrie.Create;
if trie.IsEmpty then
;
FreeAndNil(trie);
end;