hash_table.hh

Go to the documentation of this file.
00001 
00002 #ifndef HASH_TABLE_HH
00003 #define HASH_TABLE_HH
00004 
00005 // Copyright (c) 1993-1999 The University of Cincinnati.
00006 // All rights reserved. 
00007 
00008 // UC MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF 
00009 // THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
00010 // TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
00011 // PARTICULAR PURPOSE, OR NON-INFRINGEMENT.  UC SHALL NOT BE LIABLE
00012 // FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
00013 // RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
00014 // DERIVATIVES.
00015 
00016 // UC MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
00017 // THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
00018 // TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
00019 // PARTICULAR PURPOSE, OR NON-INFRINGEMENT.  UC SHALL NOT BE LIABLE
00020 // FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
00021 // RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
00022 // DERIVATIVES.
00023 
00024 // Authors: Philip A. Wilsey    phil.wilsey@uc.edu
00025 //          Dale E. Martin      dmartin@cliftonlabs.com
00026 //          Timothy J. McBrayer tmcbraye@ece.uc.edu
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     // calculate a hash value...
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 // template <class Type>
00154 // inline
00155 // Type *
00156 // hash_table<Type>::hash_look(){return NULL;}
00157 
00158 
00159 #endif

Generated on Fri Mar 31 11:04:13 2006 for Savant by  doxygen 1.4.6