Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

HashTable Class Template Reference

The following class defines a "hash table" -- allowing quick lookup according to the hash function defined for the items being put into the table. More...

#include <hash.h>

List of all members.

Public Methods

 HashTable (Key(*get)(T x), unsigned(*hFunc)(Key x))
 initialize a hash table. More...

 ~HashTable ()
 deallocate a hash table. More...

void Insert (T item)
 Put item into hash table. More...

Remove (Key key)
 Remove item from hash table. More...

bool Find (Key key, T *itemPtr) const
 Find an item from its key. More...

bool IsInTable (Key key)
 Is the item in the table? More...

bool IsEmpty ()
 does the table have anything in it. More...

void Apply (void(*f)(T)) const
 apply function to all elements in table. More...

void SanityCheck () const
 is this still a legal hash table? More...

void SelfTest (T *p, int numItems)
 is the module working? More...


Private Types

typedef List<T>* Bucket

Private Methods

void InitBuckets (int size)
void DeleteBuckets (Bucket *table, int size)
 deallocate bucket array. More...

int HashValue (Key key) const
 which bucket does the key hash to? More...

void ReHash ()
 expand the hash table. More...

bool FindInBucket (int bucket, Key key, T *itemPtr) const
 find item in bucket. More...

int FindNextFullBucket (int start) const
 find next full bucket starting from this one. More...


Private Attributes

Bucketbuckets
 the array of hash buckets. More...

int numBuckets
 the number of buckets. More...

int numItems
 the number of items in the table. More...

Key (* getKey )(T x)
 get Key from value. More...

unsigned (* hash )(Key x)
 the hash function. More...

friend HashIterator<Key,T>


Detailed Description

template<class Key, class T> class HashTable

The following class defines a "hash table" -- allowing quick lookup according to the hash function defined for the items being put into the table.

Definition at line 40 of file hash.h.


Member Typedef Documentation

template<class Key, class T>
typedef List< T > * HashTable<Key, T>::Bucket<T> [private]
 

Definition at line 65 of file hash.h.


Constructor & Destructor Documentation

template<class Key, class T>
HashTable<Key, T>::HashTable<Key, T> ( Key(* get)(T x),
unsigned(* hFunc)(Key x) )
 

initialize a hash table.

HashTable<Key,T>::HashTable Initialize a hash table, empty to start with. Elements can now be added to the table.

Definition at line 29 of file hash.cc.

template<class Key, class T>
HashTable<Key, T>::~HashTable<Key, T> ( )
 

deallocate a hash table.

HashTable<T>~HashTable Prepare a hash table for deallocation.

Definition at line 60 of file hash.cc.


Member Function Documentation

template<class Key, class T>
void HashTable<Key, T>::Apply ( void(* f)(T) ) const
 

apply function to all elements in table.

HashTable<Key,T>::Apply Apply function to every item in the hash table.

"func" -- the function to apply

Definition at line 235 of file hash.cc.

template<class Key, class T>
void HashTable<Key, T>::DeleteBuckets ( Bucket * table,
int size ) [private]
 

deallocate bucket array.

Referenced by ReHash(), and ~HashTable().

template<class Key, class T>
bool HashTable<Key, T>::Find ( Key key,
T * itemPtr ) const
 

Find an item from its key.

HashTable<Key,T>::Find Find an item from the hash table.

Returns: The item or NULL if not found.

Definition at line 193 of file hash.cc.

Referenced by IsInTable().

template<class Key, class T>
bool HashTable<Key, T>::FindInBucket ( int bucket,
Key key,
T * itemPtr ) const [private]
 

find item in bucket.

HashTable<Key,T>::FindInBucket Find an item in a hash table bucket, from it's key

"bucket" -- the list storing the item, if it's in the table "key" -- the key uniquely identifying the item

Returns: Whether item is found, and if found, the item.

Definition at line 168 of file hash.cc.

Referenced by Find(), and Remove().

template<class Key, class T>
int HashTable<Key, T>::FindNextFullBucket ( int start ) const [private]
 

find next full bucket starting from this one.

HashTable<Key,T>::FindNextFullBucket Find the next bucket in the hash table that has any items in it.

"bucket" -- where to start looking for full buckets

Definition at line 251 of file hash.cc.

Referenced by HashIterator::HashIterator(), and HashIterator::Next().

template<class Key, class T>
int HashTable<Key, T>::HashValue ( Key key ) const [private]
 

which bucket does the key hash to?

HashTable<Key,T>::HashValue Return hash table bucket that would contain key.

Definition at line 89 of file hash.cc.

Referenced by Find(), Insert(), ReHash(), Remove(), and SanityCheck().

template<class Key, class T>
void HashTable<Key, T>::InitBuckets ( int size ) [private]
 

HashTable<Key,T>::InitBuckets Initialize the bucket array for a hash table. Called by the constructor and by ReHash().

Definition at line 45 of file hash.cc.

Referenced by ReHash().

template<class Key, class T>
void HashTable<Key, T>::Insert ( T item )
 

Put item into hash table.

HashTable<Key,T>::Insert Put an item into the hashtable.

Resize the table if the # of elements / # of buckets is too big. Then allocate a HashElement to keep track of the key, item pair, and add it to the right bucket.

"key" is the key we'll use to find this item. "item" is the thing to put in the table.

Definition at line 110 of file hash.cc.

Referenced by SelfTest().

template<class Key, class T>
bool HashTable<Key, T>::IsEmpty ( ) [inline]
 

does the table have anything in it.

Definition at line 54 of file hash.h.

Referenced by SelfTest(), and ~HashTable().

template<class Key, class T>
bool HashTable<Key, T>::IsInTable ( Key key ) [inline]
 

Is the item in the table?

Definition at line 51 of file hash.h.

Referenced by Insert(), Remove(), and SelfTest().

template<class Key, class T>
void HashTable<Key, T>::ReHash ( ) [private]
 

expand the hash table.

HashTable<Key,T>::ReHash Increase the size of the hashtable, by (i) making a new table (ii) moving all the elements into the new table (iii) deleting the old table

Definition at line 136 of file hash.cc.

Referenced by Insert().

template<class Key, class T>
T HashTable<Key, T>::Remove ( Key key )
 

Remove item from hash table.

HashTable<Key,T>::Remove Remove an item from the hash table. The item must be in the table.

Returns: The removed item.

Definition at line 210 of file hash.cc.

Referenced by SelfTest().

template<class Key, class T>
void HashTable<Key, T>::SanityCheck ( ) const
 

is this still a legal hash table?

HashTable<Key,T>::SanityCheck Test whether this is still a legal hash table.

Tests: are all the buckets legal? does the table have the right # of elements? do all the elements hash to where they are stored?

Definition at line 272 of file hash.cc.

Referenced by ReHash(), and SelfTest().

template<class Key, class T>
void HashTable<Key, T>::SelfTest ( T * p,
int numItems )
 

is the module working?

HashTable<Key,T>::SelfTest Test whether this module is working.

Definition at line 297 of file hash.cc.

Referenced by LibSelfTest().


Member Data Documentation

template<class Key, class T>
friend HashTable<Key, T>::HashIterator<Key,T> [private]
 

Definition at line 88 of file hash.h.

template<class Key, class T>
Bucket * HashTable<Key, T>::buckets [private]
 

the array of hash buckets.

Definition at line 67 of file hash.h.

template<class Key, class T>
Key(* HashTable<Key, T>::getKey)(T x) [private]
 

get Key from value.

Referenced by FindInBucket(), Insert(), ReHash(), SanityCheck(), and SelfTest().

template<class Key, class T>
unsigned(* HashTable<Key, T>::hash)(Key x) [private]
 

the hash function.

template<class Key, class T>
int HashTable<Key, T>::numBuckets [private]
 

the number of buckets.

Definition at line 68 of file hash.h.

template<class Key, class T>
int HashTable<Key, T>::numItems [private]
 

the number of items in the table.

Definition at line 69 of file hash.h.


The documentation for this class was generated from the following files:
Generated at Wed Jul 4 11:32:23 2001 for Nachos by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001