Home · Pages · Index · Overviews

Hash_Table Class Reference

A classic hash table that holds a set of strings and maps them to consecutive integer IDs. More...

 #include <hash.h>

Routines

Hash_Table * New_Hash_TableG (int size)

void Clear_Hash_Table (Hash_Table *table M)
void Print_Hash_Table (FILE *file, Hash_Table *table)

int Hash_Lookup (Hash_Table *table, string entry)
int Hash_Add (Hash_Table *table M, string entry)

int Get_Hash_Size (Hash_Table *table)
string Get_Hash_String (Hash_Table *table, int i)
void ** Get_Hash_User_Hook (Hash_Table *table, int i)

Detailed Description

A Hash_Table is a classic data structure for efficiently mapping a set of N strings in a 1-1 fashion to integer IDs in the range [0,N-1]. A Hash_Table object is generated by New_Hash_Table which creates an empty hash table that initially has room for specified number of entries. The table is enlarged as necessary and there is conceptually no limit on how many strings it can contain. The table can be emptied of all entries with Clear_Hash_Table and a debug print out of its contents produced with Print_Hash_Table. Entries are looked up with Hash_Lookup and added to the table with Hash_Add. The current number of entries in the table is returned by Get_Hash_Size and the string associated with a given ID is returned by Get_Hash_String. As a convenience each entry also has a void * handle, and a pointer to this field for an entry with a given ID is return by Get_Hash_User_Hook.


Routine Documentation

Hash_Table * New_Hash_TableG (int size)

Generates a Hash_Table object that is initially empty but is large enough, without requiring memory reallocation, to hold up to size entries. Each entry is a '\0'-terminated character string. The hash table effectively assigns unique IDs starting at 0 to each entry that has been placed in the table. In addition, each entry has a void * "handle" via which a user can associate any information they wish.

void Clear_Hash_Table (Hash_Table *table M)

Reset table by removing all the entries within it.

void Print_Hash_Table (FILE *file, Hash_Table *table)

Print an ascii representation of the contents of hash on output file.

int Hash_Lookup (Hash_Table *table, string entry)

Return the integer ID for the string entry if it is in hash, or -1 if it is not in the table.

int Hash_Add (Hash_Table *table M, string entry)

Add the string entry to the table. If it was already in the table return -1, otherwise return the ID assigned to the newly added entry.

int Get_Hash_Size (Hash_Table *table)

Return the number of entries in the table hash.

string Get_Hash_String (Hash_Table *table, int i)

Return a pointer to the string for the entry in table hash that has ID i.

void ** Get_Hash_User_Hook (Hash_Table *table, int i)

Return a pointer to the void * handle for the entry with ID i.