Integerhash.pas

This unit includes an implementation of hash table mapping objects to integers. 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.


tIntHashIterator = class(tHashTableIterator)
public
function getKey : integer; reintroduce;
property key : integer read getKey;

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

TIntegerHash = class(TObject)
protected
procedure fSetValue(key : integer; value : tObject); virtual;
public
function getIterator : tIntHashIterator; virtual;
function containsKey(key : integer) : boolean; virtual;
function containsValue(value : tObject) : boolean; virtual;
function getValue(key : integer) : tObject; virtual;
function setValue(key : integer; value : tObject) : boolean; virtual;
function remove(key : integer) : tObject; virtual;
function getCount : integer; virtual;
property values[key : integer] : tObject read getValue write fsetValue;
property count : integer read getCount;
{$IFNDEF FPC}
constructor create(initialcapacity : integer = 10);
{$ELSE FPC}
constructor create;
constructor create(initialcapacity : integer);
{$ENDIF FPC}
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 TIntHashIterator

containsKey - is the given integer already a key in the hash table.

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.

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