-
Notifications
You must be signed in to change notification settings - Fork 10
TMultiHash
A multi hash table stores a set of values which can be addressed by a key. Given the key, the corresponding values can be looked up quickly.
uses
container.hashtable, container.multihash, utils.functor;
type
generic TMultiHash<K, V, KeyBinaryCompareFunctor, ValueBinaryCompareFunctor> = class
KeyBinaryCompareFunctor and ValueBinaryCompareFunctor is based on utils.functor.TBinaryFunctor interface and used to compare two keys.
MultiValue uses as internal values list.
type
TMultiValue = {$IFDEF FPC}specialize{$ENDIF} TList<T, ValueBinaryCompareFunctor>;
If macro {$USE_OPTIONAL}
is defined, then all methods return a TOptionalMultiValue wrapper, otherwise V.
uses
utils.optional;
type
TOptionalMultiValue = {$IFDEF FPC}specialize{$ENDIF} TOptional<TMultiValue>;
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 multi hash table can be created by call its constructor.
constructor Create (HashFunc : THashTableHashFunc);
HashTableFunc is a function that generate a hash key for a THashTable value.
Unit container.hashtable
contains next hash functions:
function HashPointer(location : Pointer) : Cardinal;
Generate a hash key for a pointer. The value pointed at by the pointer is not used, only the pointer itself.
function HashInteger(location : Integer) : Cardinal;
Generate a hash key for an integer. The value is used to generate the key.
function HashString(location : String) : Cardinal;
Generate a hash key from a string. This is the djb2 string hash function.
function HashStringNoCase(location : String) : Cardinal;
Generate a hash key from a string, ignoring the case of letters. This is the djb2 string hash function.
uses
container.hashtable, container.multihash, utils.functor;
type
TIntStrMultiHash = {$IFDEF FPC}type specialize{$ENDIF} TMultiHash<Integer, String, TCompareFunctorInteger, TCompareFunctorString>;
var
hash : TIntStrMultiHash;
begin
hash := TIntStrMultiHash.Create(@HashInteger);
FreeAndNil(hash);
end;
Insert a value into a multi hash table.
function Insert (Key : K; Value : V) : Boolean;
uses
container.hashtable, container.multihash, utils.functor;
type
TIntStrMultiHash = {$IFDEF FPC}type specialize{$ENDIF} TMultiHash<Integer, String, TCompareFunctorInteger, TCompareFunctorString>;
var
hash : TIntStrMultiHash;
begin
hash := TIntStrMultiHash.Create(@HashInteger);
hash.Insert(1, 'one');
hash.Insert(1, 'two');
FreeAndNil(hash);
end;
Remove a value from a multi hash table. Return false if no node with the specified key was found in the tree, true if a node with the specified key was removed.
function Remove (Key : K) : Boolean;
uses
container.hashtable, container.multihash, utils.functor;
type
TIntStrMultiHash = {$IFDEF FPC}type specialize{$ENDIF} TMultiHash<Integer, String, TCompareFunctorInteger, TCompareFunctorString>;
var
hash : TIntStrMultiHash;
begin
hash := TIntStrMultiHash.Create(@HashInteger);
hash.Remove(1);
FreeAndNil(hash);
end;
Look up a value in a multi hash table by key.
function Search (Key : K) : {$IFNDEF USE_OPTIONAL}TMultiValue{$ELSE}TOptionalMultiValue{$ENDIF};
If value not exists returns empty TOptionalMultiValue or raise EKeyNotExistsException.
uses
container.hashtable, container.multihash, utils.functor;
type
TIntStrMultiHash = {$IFDEF FPC}type specialize{$ENDIF} TMultiHash<Integer, String, TCompareFunctorInteger, TCompareFunctorString>;
var
hash : TIntStrMultiHash;
begin
hash := TIntStrMultiHash.Create(@HashInteger);
hash.Search(1);
FreeAndNil(hash);
end;
Look up a value in a multi hash table by key. Return default value if Key not exists.
function SearchDefault (Key : K; ADefault : V) : TMultiValue;
uses
container.hashtable, container.multihash, utils.functor;
type
TIntStrMultiHash = {$IFDEF FPC}type specialize{$ENDIF} TMultiHash<Integer, String, TCompareFunctorInteger, TCompareFunctorString>;
var
hash : TIntStrMultiHash;
begin
hash := TIntStrMultiHash.Create(@HashInteger);
hash.SearchDefault(1, 'none');
FreeAndNil(hash);
end;
Retrieve the number of entries in a multi hash table.
function NumEntries : Cardinal;
uses
container.hashtable, container.multihash, utils.functor;
type
TIntStrMultiHash = {$IFDEF FPC}type specialize{$ENDIF} TMultiHash<Integer, String, TCompareFunctorInteger, TCompareFunctorString>;
var
hash : TIntStrMultiHash;
begin
hash := TIntStrMultiHash.Create(@HashInteger);
writeln(hash.NumEntries);
FreeAndNil(hash);
end;
Return true if container is empty.
function IsEmpty : Boolean;
uses
container.hashtable, container.multihash, utils.functor;
type
TIntStrMultiHash = {$IFDEF FPC}type specialize{$ENDIF} TMultiHash<Integer, String, TCompareFunctorInteger, TCompareFunctorString>;
var
hash : TIntStrMultiHash;
begin
hash := TIntStrMultiHash.Create(@HashInteger);
if hash.IsEmpty then
;
FreeAndNil(hash);
end;
It is possible to iterate for TMultiHash values using in
operator. Each value would present as TMultiHash.TIterator object.
uses
container.hashtable, container.multihash, utils.functor;
type
generic TMultiHash<K, V, KeyBinaryCompareFunctor, ValueBinaryCompareFunctor> = class
type
TIterator = class({$IFDEF FPC}specialize{$ENDIF}
TForwardIterator<TKeyMultiValuePair, TIterator>)
end;
TForwardIterator is a abstract class which provide interface for iterable object that can moves to forward side.
uses
container.hashtable, container.multihash, utils.functor;
type
TIntStrMultiHash = {$IFDEF FPC}type specialize{$ENDIF} TMultiHash<Integer, String, TCompareFunctorInteger, TCompareFunctorString>;
var
hash : TIntStrMultiHash;
iterator : TIntStrMultiHash.TIterator;
begin
hash := TIntStrMultiHash.Create(@HashInteger);
for iterator in hash do
;
FreeAndNil(hash);
end;
Retrive the iterator for first entry in a TMultiHash table.
function FirstEntry : TIterator
uses
container.hashtable, container.multihash, utils.functor;
type
TIntStrMultiHash = {$IFDEF FPC}type specialize{$ENDIF} TMultiHash<Integer, String, TCompareFunctorInteger, TCompareFunctorString>;
var
hash : TIntStrMultiHash;
iterator : TIntStrMultiHash.TIterator;
begin
hash := TIntStrMultiHash.Create(@HashInteger);
iterator := hash.FirstEntry;
FreeAndNil(hash);
end;
Return true if iterator has correct value.
function HasValue : Boolean;
uses
container.hashtable, container.multihash, utils.functor;
type
TIntStrMultiHash = {$IFDEF FPC}type specialize{$ENDIF} TMultiHash<Integer, String, TCompareFunctorInteger, TCompareFunctorString>;
var
hash : TIntStrMultiHash;
iterator : TIntStrMultiHash.TIterator;
begin
hash := TIntStrHashTable.Create(@HashInteger);
iterator := hash.FirstEntry;
while iterator.HasValue do
;
FreeAndNil(hash);
end;
Retrieve the iterator for next entry in an multi hash table.
function Next : TIterator;
uses
container.hashtable, contaqiner.multihash, utils.functor;
type
TIntStrMultiHash = {$IFDEF FPC}type specialize{$ENDIF} TMultiHash<Integer, String, TCompareFunctorInteger, TCompareFunctorString>;
var
hash : TIntStrMultiHash;
iterator : TIntStrMultiHash.TIterator;
begin
hash := TIntStrMultiHash.Create(@HashInteger);
iterator := hash.FirstEntry;
while iterator.HasValue do
begin
iterator := iterator.Next;
end;
FreeAndNil(hash);
end;
Return key value.
property Key : K;
uses
container.hashtable, container.multihash, utils.functor;
type
TIntStrMultiHash = {$IFDEF FPC}type specialize{$ENDIF} TMultiHash<Integer, String, TCompareFunctorInteger, TCompareFunctorString>;
var
hash : TIntStrMultiHash;
iterator : TIntStrMultiHash.TIterator;
begin
hash := TIntStrMultiHash.Create(@HashInteger);
iterator := hash.FirstEntry;
while iterator.HasValue do
begin
writeln(iterator.Key);
iterator := iterator.Next;
end;
FreeAndNil(hash);
end;
Return value.
property Value : TMultiValue;
uses
container.hashtable, container.multihash, utils.functor;
type
TIntStrMultiHash = {$IFDEF FPC}type specialize{$ENDIF} TMultiHash<Integer, String, TCompareFunctorInteger, TCompareFunctorString>;
var
hash : TIntStrMultiHash;
iterator : TIntStrMultiHash.TIterator;
begin
hash := TIntStrMultiHash.Create(@HashInteger);
iterator := hash.FirstEntry;
while iterator.HasValue do
begin
writeln(iterator.Value.NthEntry(0).Value);
iterator := iterator.Next;
end;
FreeAndNil(hash);
end;