/************************************************************************ * File: table.h * * * * Purpose: Implements hash tables. Start file for CSCI 3300 * * programming assignment. * * * * Author: Karl Abrahamson * * * * Date: September 18, 2005 * ************************************************************************/ #ifndef TABLE_H #define TABLE_H #include //--------------------------------------------------------------// // This header file describes hash tables as provided by // // table.cpp. See that file for a discussion of hash tables. // // // // NOTE: Anything that is marked PRIVATE in this header // // file must not be used in any other module. It might change // // or disappear at any time without notice. Anything that is // // not marked PRIVATE is available to all other modules. // //--------------------------------------------------------------// #define PRIVATE /**/ //--------------------------------------------------------------// // HASH_TABLE_SIZE is one larger than the maximum number of // // keys that can be stored in the hash table. // //--------------------------------------------------------------// PRIVATE #define HASH_TABLE_SIZE 100 PRIVATE struct HashCellType { char* key; char* val; }; struct HashTableType { PRIVATE int load; /* how many cells are occupied */ PRIVATE HashCellType* cells; /* the array of cells */ // When a table is created, allocate memory for it, and // set all of the key fields to NullKey. HashTableType() { int i; load = 0; cells = new HashCellType[HASH_TABLE_SIZE]; for(i = 0; i < HASH_TABLE_SIZE; i++) { cells[i].key = NULL; } } }; //--------------------------------------------------------------// // Function Prototypes // //--------------------------------------------------------------// // The following functions are exported to other modules. // //--------------------------------------------------------------// //--------------------------------------------------------------// // hashInsert(t,key,val) inserts the given key, with associated // // value val, into table t. // // // // If key is already present in the table, then hashInsert // // replaces the value associated with key by val. // // // // If there is no more room in the table, then hashInsert does // // not do the insertion -- it leaves the table unchanged. // //--------------------------------------------------------------// void hashInsert(HashTableType& tbl, char* key, char* val); //--------------------------------------------------------------// // If key is in table t, then hashLookup(t,key,val) sets // // variable val to the value associated with key in table t, // // and returns 1. // // // // If key is NOT in table t, then hashLookup(t,key,val) returns // // 0, and does not alter val. // //--------------------------------------------------------------// int hashLookup(HashTableType& tbl, char* key, char*& val); //--------------------------------------------------------------// // hashFull(t) returns 1 if table t cannot accept any more // // insertions, and 0 if table t can accept more insertions. // // If hashFull(t) returns 1, then further insertions into t // // will be ignored. // //--------------------------------------------------------------// int hashFull(HashTableType& tbl); //--------------------------------------------------------------// // hashMakeEmpty(t) removes all items from table t. // //--------------------------------------------------------------// void hashMakeEmpty(HashTableType& tbl); #endif // defined TABLE_H