Go to the first, previous, next, last section, table of contents.


CNHashList -- Hash Tables with List Collision Management

SYNOPSIS

#include <CNCL/HashList.h>

TYPE

CN_HASHLIST

BASE CLASSES

CNHashTable

DERIVED CLASSES

None

RELATED CLASSES

CNHashDynamic, CNKey, CNKeyString, CNKeyInt

CHHashIterator does NOT work for CNHashList

DESCRIPTION

CNHashList is a class which provides a hash table with unrestricted capacity. Contrary to CNHashStatic the keys occuring more than once are stored in lists, i.e. the capacity of the hash table does not restrict the number of keys, that may be stored. Nontheless the capacity should be large enough for the number of keys to be stored in. Constructors:

CNHashList(unsigned long cap = DEFAULT_HASH_TABLE_CAPACITY);
CNHashList(CNParam *param);
Initializes CNHashList. The hash table's capacity is set to the value passed to CNHashList. The capacity is static. Nontheless, you can store more keys since they are stored in lists.

Destructors:

~CNHashList();
Frees all internally allocated resources.

CNHashList provides the member functions required by CNCL and CNHashTable. Some member functions defined in CNHashTable and implemented in CNHashList demand further explanation:

void store_key(CNKey *k);
Stores a key into the homogenous hash table. Only keys of the same type may be stored into the same table. The methods get_key() and get_object() should detect it. If a hashvalue occures the second time it will be stored in a list. Therefore the hash table will never be full unless the memory is exhausted.
bool delete_key(CNKey *k);
Deletes the key from the actual hash table which matches the given key. After having deleted a key from the hash table, it is not necessary to rehash the table. This method does not free the memory allocated for the keys stored in the hash table. If the supplied key does not match any in the table, FALSE is returned, otherwise TRUE.
bool delete_key_absolutely(CNKey *k);
The same as above. This method does free the memory allocated for the keys stored in the hash table. If the supplied key does not match any in the table, FALSE is returned, otherwise TRUE.

The following example shows how to use a CNHashList object in order to store and retrieve CNCL compatible objects.

CNHashList tab(200);
CNKeyString ks("Test", NIL);
CNObject *obj = &ks;

tab.store_key(new CNKeyString("Jabba", obj));
tab.store_key(new CNKeyString("Dabba"));
tab.store_key(new CNKeyString("Dooo"));

if (tab.get_object(CNKeyString("Jabba")) != obj)
   cout << "strange behaviour\n";
else
   cout << "found obj\n";

tab.store_key(new CNKeyInt(10, obj)); // error, key of different type

tab.reset_absolutely();

tab.store_key(new CNKeyInt(10, new CNObject)); // okay

// free memory of all keys and their objects
tab.reset_absolutely_w_obj();


Go to the first, previous, next, last section, table of contents.