GEL
2
GEL is a library for Geometry and Linear Algebra
|
Hashtable class template. More...
#include <HashTable.h>
Classes | |
struct | EntryType |
Public Member Functions | |
HashTable () | |
Construct an empty hash table. | |
~HashTable () | |
Destruct it. | |
bool | create_entry (const KEY_T &, const VAL_T &, int=0) |
bool | create_entry (const KEY_T &kv, VAL_T *&val, int incr=0) |
bool | remove_entry (const KEY_T &) |
bool | find_entry (const KEY_T &, VAL_T &) const |
bool | decr_reference (const KEY_T &, VAL_T &, int=1) |
bool | change_entry (const KEY_T &, const VAL_T &) |
void | print () const |
Print various statistics for the table. | |
bool | get_first (int &idx, KEY_T &, VAL_T &val, int &) const |
bool | get_next (int &idx, KEY_T &, VAL_T &val, int &) const |
int | integrate () const |
Returns the sum of the reference counts that are > 0. | |
int | no_items () const |
Returns no of items in table. | |
bool | empty () const |
Returns true if number of items==0. | |
int | total_size () const |
Returns total size of table (in bytes) | |
Static Public Member Functions | |
static int | entry_size () |
Returns the full size of an entry (key + value + ref.count). |
Hashtable class template.
This class template has taken for too long to get right. That is because it is flexible. It can be used either as a simple (key, value) database or as a reference counted database.
There are two template arguments: KEY_T is the hash key type. This type must provide operators == and != and a hash function which takes an integer denoting the table size and returns another integer - the table argument.
VAL_T is the value type. There are few restrictions on this type but it must have default and copy constructors and operator=. The HashTable template is intended for use with simple types.
bool Util::HashTable< KEY_T, VAL_T >::change_entry | ( | const KEY_T & | kv, |
const VAL_T & | val | ||
) |
Change Entry. Does not affect reference count. Returns true if the entry exists - false otherwise.
bool Util::HashTable< KEY_T, VAL_T >::create_entry | ( | const KEY_T & | kv, |
const VAL_T & | val, | ||
int | incr = 0 |
||
) |
Create entry in hash table. The first argument is the key, and the second is the value to be inserted. The third argument is the amount that the reference count should be increased.
If third argument is zero: The key,value pair is inserted if key is not found. In either case the reference count is set to 1. function returns true if key was not found (or reference count=0 which counts as key begin absent)
If third argument is non-zero: Behaviour is as above except that the reference count is incremented or set to value of third argument depending on whether the key was already present.
bool Util::HashTable< KEY_T, VAL_T >::decr_reference | ( | const KEY_T & | kv, |
VAL_T & | val, | ||
int | decr = 1 |
||
) |
Decrease reference count. THe key is first argument, the value is returned in second argument, and the third argument contains the amount of decrease, 1 is default. The function returns true if the key was found and reference non-zero. False otherwise.
if the reference count is zero after being decreased, the function may decrease the size of the table.
bool Util::HashTable< KEY_T, VAL_T >::find_entry | ( | const KEY_T & | kv, |
VAL_T & | val | ||
) | const |
Find entry in database. The second argument is set to the value that corresponds to the key passed as first argument. The function returns true if the key was found (and reference non-zero)
bool Util::HashTable< KEY_T, VAL_T >::get_first | ( | int & | idx, |
KEY_T & | key, | ||
VAL_T & | val, | ||
int & | r | ||
) | const |
Get the first entry in HashTable. All arguments are passed by reference. The first argument is the index. Upon return the first argument will contain the first valid index in the table. This index should be passed to the get_next function. (see below). Subsequent arguments are the key, the value and the reference count. These are set to the values that correspond to the index. The function returns false if there are no valid indices - i.e. the table is empty
bool Util::HashTable< KEY_T, VAL_T >::get_next | ( | int & | idx, |
KEY_T & | key, | ||
VAL_T & | val, | ||
int & | r | ||
) | const |
Get the next entry in the HashTable. All arguments are passed by reference. This first argument is a valid index to an element in the table (from a previous call to either this function or get_first) get_next finds the next valid entry and returns key, value and reference count in the next three arguments. get_next returns false if there is no next valid index.
bool Util::HashTable< KEY_T, VAL_T >::remove_entry | ( | const KEY_T & | kv | ) |
Removes an entry from database. This function returns false if the key was not found. Otherwise the reference count is set to zero and the function returns true having first reduced the size of the table, if the size was below threshold after removal.