00001 #ifndef HASH_TABLE_CLASS
00002 #define HASH_TABLE_CLASS
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "data_structure_dll.h"
00019
00020 #include <basis/object_base.h>
00021
00022
00023 template <class key, class contents> class internal_hash_array;
00024
00026
00032 class DATA_STRUCTURE_CLASS_STYLE hashing_algorithm : public virtual object_base
00033 {
00034 public:
00035 virtual ~hashing_algorithm() {}
00036
00037 IMPLEMENT_CLASS_NAME("hashing_algorithm");
00038
00039 virtual u_int hash(const void *key_data, int key_length) const = 0;
00041
00047 virtual hashing_algorithm *clone() const = 0;
00049 };
00050
00052
00054
00061 template <class key_type, class contents>
00062
00063
00064
00065 class hash_table
00066 {
00067 public:
00068 hash_table(const hashing_algorithm &hasher, int max_table_bits);
00070
00084 ~hash_table();
00086
00087 static const char *class_name() { return "hash_table"; }
00088
00089 int max_bits() const { return _max_bits; }
00090
00091 void rehash(int max_bits);
00093
00095 enum outcomes {
00096 IS_NEW = common::IS_NEW,
00097 EXISTING = common::EXISTING
00098 };
00099
00100 int elements() const;
00102
00104 outcome add(const key_type &key, contents *to_store);
00106
00114 outcome fast_dangerous_add(const key_type &key, contents *to_store);
00116
00119 bool find(const key_type &key, contents * &item_found) const;
00121
00125 inline contents *find(const key_type &key) const
00126 { contents *c = NIL; return find(key, c)? c : NIL; }
00128
00129 contents *acquire(const key_type &key);
00131
00135 bool zap(const key_type &key);
00137
00139 void reset();
00141
00142 typedef bool apply_function(const key_type &key, contents ¤t,
00143 void *data_link);
00145
00150 void apply(apply_function *to_apply, void *data_link);
00152
00158 outcome add(key_type *key, contents *to_store, bool check_dupes = true);
00160
00167 bool verify() const;
00169
00171 private:
00172 int _max_bits;
00173 hashing_algorithm *_hasher;
00174 internal_hash_array<key_type, contents> *_table;
00175 int _last_iter;
00177
00178 hash_table(const hash_table &to_copy);
00180 hash_table &operator =(const hash_table &to_copy);
00182
00183 public:
00184 internal_hash_array<key_type, contents> &table_access() const;
00186 };
00187
00189
00191
00197 template <class key_type, class contents>
00198 void copy_hash_table(hash_table<key_type, contents> &target,
00199 const hash_table<key_type, contents> &source);
00200
00201 #endif
00202