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


CNHashStatic -- Hash Tables with Static Capacity

SYNOPSIS

#include <CNCL/HashStatic.h>

TYPE

CN_HASHSTATIC

BASE CLASSES

CNHashTable

DERIVED CLASSES

None

RELATED CLASSES

CNHashDynamic, CNKey, CNKeyString, CNHashIterator, CNKeyInt

DESCRIPTION

CNHashStatic is a class which provides a hash table with static capacity for storing and retrieving CNCL compatible objects. Constructors:

CNHashStatic(unsigned long cap = DEFAULT_HASH_TABLE_CAPACITY);
CNHashStatic(CNParam *param);
Initializes CNHashStatic. The hash table's capacity is set to the value passed to CNHashStatic. The capacity is static, therefore, you cannot change it during the lifetime of an instance of this class.

Destructors:

~CNHashStatic();
Frees all internally allocated resources.

CNHashStatic provides the member functions required by CNCL and CNHashTable. Some member functions defined in CNHashTable and implemented in CNHashStatic 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 you try to store a key into an already full table, an error message is displayed and the program is terminated.
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, the whole table is rehashed, i.e. the positions of all entries within the hash table are recalculated and all entries are stored in a new hash table. This might lead to small time delays when handling large hash tables. 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);
Deletes the key from the actual hash table which matches the given key. After having deleted a key from the hash table, the whole table is rehashed. 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 CNHashStatic object in order to store and retrieve CNCL compatible objects.

CNHashStatic 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.