HashTable

The HashTable class has a standard iterator called HashTableIter, where the next method and ++ operator return a pointer to class HashEntry. This class has a const char* key() method that returns the key for the entry, and a Pointer value() method that returns a pointer to the entry.

Sophisticated users will often want to derive new classes from HashTable. The reason is that the methods that look up data in the table can be defined to return pointers of the appropriate type. Moreover, the deallocation of memory when an entry is deleted or the table itself is deleted can be automated. TextTable is a good example of such a derived class. This is not possible with the generic HashTable class, because the Pointer type does not give enough information to know what destructor to invoke. Thus, when using the generic HashTable class, the programmer should explicitly delete the objects pointed to by the Pointer if they were dynamically created and are no longer needed.

Class HashTable

Method

Parameter

Description

void clear ()

 

empty the table

virtual void cleanup (...)

 

does nothing; in derived classes it might

 

Pointer p

 

int hasKey (...)

 

return 1 if the given key is in the table, 0 otherwise

 

const char* key

 

void insert (...)

 

insert an entry; any previous entry with the same key is replaced and the cleanup method is called so that in derived classes its memory can be deallocated

 

const char* key

 

 

Pointer data

 

Pointer lookup (...)

 

loop up an entry; in a derived class, this could be overloaded to return a pointer of a more specific type

 

const char* key

 

int remove (...)

 

remove the entry with the given key from the table; note that the object pointed to by the entry is not deallocated

 

const char* key

 

int size ()

 

return the number of entries in the hash table