00001
00002 #ifndef HASH_TABLE_HH
00003 #define HASH_TABLE_HH
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include <iostream>
00031 #include <cstring>
00032 #include "savant.hh"
00033 #include "dl_list.hh"
00034 #include "savant.hh"
00035
00047 template <class Type>
00048 class hash_table {
00049
00050 public:
00054 Type *hash_look( const char * const text ){
00055 return hash_look( text, strlen(text) );
00056 }
00057
00062 Type *hash_look( const char *const text, const int length ) {
00063 int key;
00064 int i;
00065 Type *wk;
00066
00067 key = 0;
00068
00069
00070 for (i = 0; i < length; i++) {
00071 key = (key << 3) + text[i];
00072 }
00073
00074 key = (key & 0x7fffffff) % ht_size;
00075
00076 for (wk=ht[key].first();
00077 wk!=NULL && wk->test_key( text, length );
00078 wk=ht[key].successor(wk)) {}
00079
00080 if (wk == NULL) {
00081 wk = new Type();
00082 wk->set_key( text, length );
00083 ht[key].append(wk);
00084 }
00085
00086 return wk;
00087 }
00088
00095 hash_table(int table_size = 4093) : ht_size(table_size) {
00096 ht = new dl_list<Type>[ht_size];
00097 }
00098
00099 ~hash_table(){
00100 delete [] ht;
00101 };
00102
00106 void reset(){
00107 int i;
00108 for(i = 0 ; i < ht_size; i++){
00109 ht[i].destroy_containers();
00110 }
00111 }
00112
00117 const int get_size() const {
00118 return ht_size;
00119 }
00120
00125 dl_list<Type>* get_entry(int entry_number) const {
00126 ASSERT ( (entry_number >= 0) && (entry_number <= ht_size) );
00127 return (ht + entry_number);
00128 }
00129
00135 dl_list<Type> *convert_to_list() const {
00136 dl_list<Type> *retval = new dl_list<Type>;
00137 int i;
00138 for( i = 0; i < ht_size; i++ ){
00139 Type *current = ht[i].first();
00140 while( current != NULL ){
00141 retval->append( current );
00142 current = ht[i].successor( current );
00143 }
00144 }
00145 return retval;
00146 }
00147
00148 private:
00149 const int ht_size;
00150 dl_list<Type> *ht;
00151 };
00152
00153
00154
00155
00156
00157
00158
00159 #endif