This unit includes an implementation of hash table mapping objects to strings. It uses a THashTable as the underlying hash table.

The unit can be compiled by Delphi/Kylix or by freepascal. When compiling with FreePascal, use -S2 directive (needed to compile hashtable.pas). The compiler may complain about reintroduce directive but will take it.

tStrHashIterator = class(tHashTableIterator)
function getKey : string; reintroduce;
property key : string read getKey;

tStrHashIterator reimplements getKey method and key property to deal directly with strings instead of TComparable. Otherwise the functionality of THashTableIterator remains intact.

TStringHash = class(TObject)
procedure fSetValue(key : string; value : tObject); virtual;
function getIterator : tStrHashIterator; virtual;
function containsKey(key : string) : boolean; virtual;
function containsValue(value : tObject) : boolean; virtual;
function getValue(key : string) : tObject; virtual;
function setValue(key : string; value : tObject) : boolean; virtual;
function remove(key : string) : tObject; virtual;
function getCount : integer; virtual;
property values[key : string] : tObject read getValue write fsetValue;
property count : integer read getCount;
constructor create(caseSensitive : boolean = false; initialcapacity : integer = 10);
constructor create;
constructor create(caseSensitive : boolean);
constructor create(caseSensitive : boolean; initialcapacity : integer);
destructor destroy; override;
procedure clear; virtual;
procedure deleteAll; virtual;

This class encapsulates a THashTable to handle the hash. It may as well have been derived from it :). Maybe it will, in some future version

fSetValue - a wrapper around function setValue

getIterator - creates and returns a new TStrHashIterator

containsKey - is the given string already a key in the hash table. If the hash table created with caseSensitive parameter set to true, the search is case-sensitive, otherwise it is not

containsValue - is the given value in the table? This method only works if the value is a TComparable, otherwise it returns false.

getValue - get the value corresponding to the given key from the table. If the key is not in the table, returns nil

setValue - add a (key, value) pair to the hash table. If they key is new to the hash table, returns true, if not, returns false. NOTE: If the value is being replaced, this function does not free it. It is your responsibility

remove - removes the (key, value) pair from the table, and returns the value. If the key was not part of the table, returns nil

getCount - returns the number of entries in the hash table

values - read / write access to (key, value) pairs of the hash table via getValue and setValue

count - access to the number of entries in the hash table via getCount

create - creates the hash table. The Delphi version has default parameter values, while freePascal version overloads the constructor to provide the same functionality. Pass true as first parameter if you want key matching to be case-sensitive

destroy - clears and frees the hash table but does not free the values

clear - clears the hash table but does not free the values

deleteAll - clears the hash table and deletes the values