Main Page | Modules | Alphabetical List | Data Structures | File List | Data Fields | Related Pages

Hashtables


Detailed Description

Implementation of hashtables.

A hashtable stores an arbitrary number of objects that are accessed using an arbitrary key. The key is converted to an integer value, so so-called hash value, by the hash function. A hash function should be fast and should map the keys to integers in a highly irregular but consistent way, i.~e., the keys should be distributed evenly over the whole set of integers. Ideally, objects can then be accessed in constant time based on their key.

Data Structures

Defines

Functions


Function Documentation

Boolean hashContainsKey Hashtable  ht,
Pointer  key
 

checks if the hashtable contains the specified key.

Parameters:
ht the hashtable in which the specified key has to be checked.
key the key whose existance in the hashtable is to be checked.
Returns:
TRUE if the an object under the key exists and FALSE otherwise.
Definition at line 346 of file hashtable.c.

Boolean hashContainsValue Hashtable  ht,
Pointer  value
 

checks if the hashtable contains a specific value.

Objects are compared by the standard C operator == and this method is Expensive!!!

Parameters:
ht the hashtable in which the specific value has to be checked.
value the value whose existance in the hashtable has to be checked.
Returns:
TRUE if the object is contained in the hashtable and FALSE otherwise.
Definition at line 373 of file hashtable.c.

void hashDelete Hashtable  ht  ) 
 

deletes the hashtable, but can't free the memory for the content.

Parameters:
ht the hashtable that has to be deleted.
Definition at line 396 of file hashtable.c.

void hashForEach Hashtable  ht,
VoidFunction *  f
 

calls the function `f(key,value)' for each item in the hashtable.

Parameters:
ht the hashtable in which the function 'f' has to be called.
f the function that has to be called for every object in the hashtable ht.
Definition at line 417 of file hashtable.c.

Referenced by strFinalize().

void hashForEachFree Hashtable  ht,
VoidFunction *  f
 

calls the function `f(key, value)' for each item in the Hashtable.

deletes the hashtable and hashtable becomes inaccessible

Parameters:
ht the hashtable in which the function 'f' has to be called.
f the function that has to be called for every object in the hashtable ht.
Definition at line 455 of file hashtable.c.

Referenced by strFinalize().

void hashForEachFreeValue Hashtable  ht,
VoidFunction *  f
 

List hashForEachFree(), but frees only the embedded value.

F is applied to the value only and must be a unariy void function. Definition at line 475 of file hashtable.c.

void hashForEachWithData Hashtable  ht,
VoidFunction *  f,
Pointer  clientData
 

calls the function `f(element,data)' for each object in the hashtable.

Parameters:
ht the hashtable in which the function 'f' has to be called.
f the function that has to be called in the hashtable ht.
clientData the data in the function 'f(element,data)'that is called in the hashtable ht.
Definition at line 435 of file hashtable.c.

Pointer hashGet Hashtable  ht,
Pointer  key
 

retrieves value associated with the key.

Parameters:
ht the hashtable from which the value at the specified key has to be retrieved.
key the key at which the value of the hashtable has to be retrieved.
Returns:
the object that(or NULL) that is stored under the key in the hashtable.
Definition at line 222 of file hashtable.c.

Referenced by _strLookup().

Pointer* hashGetPointerToValue Hashtable  ht,
Pointer  key
 

retrieves value associated with the key

Parameters:
ht the hashtable from which the value at the specified key has to be retrieved.
key the key at which the value of the hashtable has to be retrieved.
Returns:
the object that(or NULL) that is stored under the key in the hashtable.
Definition at line 250 of file hashtable.c.

Boolean hashIsEmpty Hashtable  ht  ) 
 

checks if the hashtable is empty.

Parameters:
ht the hash table whose emptiness is checked.
Returns:
TRUE if the hashtable is empty and False otherwise.
Definition at line 329 of file hashtable.c.

void hashIteratorDelete HashIterator  hi  ) 
 

deletes and frees hash iterator object

Parameters:
hi the hash iterator object that has to be deleted.
Definition at line 603 of file hashtable.c.

HashIterator hashIteratorNew Hashtable  ht  ) 
 

returns a new hash iterator object.

Iterators allow to loop through all the elements of a container. However the behaviour is undefined if the container changes while the iterator is still looping.

 hi = hashIteratorNew(ht);
  while (NULL != (key = hashIteratorNextKey(hi))) {
    do something with key;
  }
  hashIteratorDelete(hi); 

Parameters:
ht the hashtable for which the new iterator object has to be returned.
Returns:
the new hash iterator object for the hashtable ht.
Definition at line 528 of file hashtable.c.

Pointer hashIteratorNextKey HashIterator  hi  ) 
 

returns the next key of a hash-iterator.

Hash iterator points to the following entry afterwards

Parameters:
hi the hash iterator whose next key has to be returned.
Returns:
the next key of the hash iterator hi
Definition at line 552 of file hashtable.c.

Pointer hashIteratorNextValue HashIterator  hi  ) 
 

returns the next value of a hash-iterator.

Hash iterator points to the following entry afterwards.

Parameters:
hi the hash iterator whose next value has to be returned.
Returns:
the next value of a hash-iterator hi.
Definition at line 577 of file hashtable.c.

List hashListOfKeys Hashtable  ht  ) 
 

retrieves a list of keys of all objects in the hashtable.

Parameters:
ht the hashtable from which the list of keys have to be retrieved.
Returns:
the list of the keys of all objects in the hashtable.
Definition at line 498 of file hashtable.c.

References listPrependElement().

Referenced by strFinalize().

Hashtable hashNew int  capacity,
double  loadFactor,
IntFunction *  hashFunction,
IntFunction *  keyEqualFunction
 

creates a new hashtable with an initial capacity of c.

whenever the number of stored objects exceeds loadFactor times the current capacity, the hashtable is automatically resized.

Parameters:
capacity specifies the capacity of the new hashtable to be created.
loadFactor it is a number that should be chosen between 0.5 and 0.9.
hashFunction it is that function which is called with a key as the only parameter and returns the hash value of * that key.
keyEqualFunction It is that function which is called with two keys as parameters.It returns 1 if the keys are to be considered equal and 0 otherwise.
Returns:
the new hashtable with a capacity of c.
Definition at line 136 of file hashtable.c.

References primeNext().

Referenced by strInitialize().

Pointer hashRemove Hashtable  ht,
Pointer  key
 

removes key/value pair from hashtable

Parameters:
ht the hashtable from which the key/value pair is to be removed.
key this shows the value that has to be deleted in the hashtable ht.
Returns:
the object (or NULL) that is stored under the key in the hashtable ht.
Definition at line 277 of file hashtable.c.

Referenced by strDelete().

Pointer hashSet Hashtable  ht,
Pointer  key,
Pointer  value
 

adds the object value with the key key in the hashtable.

rehashes the hashtable if necessary.

Parameters:
ht the hashtable in which the object value has to be added with the key.
key the key value that has to be added with the value
value the object whose value has to be added to the key.
Returns:
the old object that was stored at that key or NULL
Definition at line 172 of file hashtable.c.

References rehashHashtable().

Referenced by strRegister().

int hashSize Hashtable  ht  ) 
 

retrieves the size of the hashtable

Parameters:
ht the hashtable whose size has to be retrieved.
Returns:
the number of objects that are currently stored in the hashtable.
Definition at line 313 of file hashtable.c.

Referenced by strFinalize(), and strStoreSize().

int hashStringEqualFunction char *  s,
char *  t
 

is an example equality function for C strings that can be used in hashNew.

Parameters:
s the first string that is used in the string comparison function.
t the second string that is used in the string comparison function.
Returns:
0 if they are equal and 1 if not.
Definition at line 649 of file hashtable.c.

Referenced by strInitialize().

int hashStringHashFunction char *  s  ) 
 

is an example has function for C strings that can be used in hashNew.

A bit rotating function by Knuth is used here. TODO:

  • try different hash functions
  • strlen should go out - supply length of key.

Parameters:
s the string on which the hashStringHashFunction is performed.
Returns:
the integer representation of the string.
Definition at line 620 of file hashtable.c.

Referenced by strInitialize().

void rehashHashtable Hashtable  ht  ) 
 

rehashes the hashtable.

doubles the capacity and this enlarges the space.

Parameters:
ht the hash table that is to be resized
Returns:
the new resized hashtable.
Definition at line 86 of file hashtable.c.

References primeNext().

Referenced by hashSet().


BLAH 0.95 (20 Oct 2004)