00001 #ifndef SL_HASH_TABLE_HPP 00002 #define SL_HASH_TABLE_HPP 00003 /* 00004 * Hashtable object adapated from Java sources. 00005 * Autor : Alexandre Sauve 00006 */ 00007 00008 #include <boost/shared_ptr.hpp> 00009 00010 00072 class SLHashTable { 00073 public: 00074 class Element; 00075 class NotFoundException; 00076 00080 typedef unsigned long HashCode; 00081 00089 static HashCode computeHashCode(const char *key, unsigned long size); 00090 00094 SLHashTable(unsigned long DEFAULT_LENGTH = 32); 00095 00100 void put(const Element &key, const Element &value); 00101 00106 Element &get(const Element &key) const 00107 throw(SLHashTable::NotFoundException); 00108 00114 void remove(const Element &key); 00115 00116 00117 00118 //char ** (*getKeysList)(Hashtable *this); 00119 00120 00121 protected: 00125 void rehash(); 00126 00128 static const unsigned long DEFAULT_LENGTH = 32; 00129 00131 static const float DEFAULT_LOAD_FACTOR = 0.75; 00132 00134 class Data; 00135 00137 boost::shared_ptr<Data> _shared_ptr; 00138 }; // class SLHashTable 00139 00140 00141 00142 00148 class SLHashTable::Element { 00149 public: 00153 virtual HashCode getHashCode() const = 0; 00154 00161 virtual Element *clone() const = 0; 00162 00166 virtual bool operator==(const Element &other) const = 0; 00167 00172 virtual ~Element() { 00173 // A dummy destructor is provided here for classes that do not 00174 // have one (frequent when boost::shared_ptr is used). 00175 } 00176 }; 00177 00178 00182 class SLHashTable::NotFoundException : public SLNotFoundException { 00184 NotFoundException(const SLString &message) 00185 : 00186 SLNotFoundException(message) 00187 { } 00188 }; 00189 00190 00191 00192 #endif 00193