| 1 | /* $Header: d:/cvsroot/tads/tads3/vmhash.h,v 1.3 1999/07/11 00:46:59 MJRoberts Exp $ */ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (c) 1997, 2002 Michael J. Roberts. All Rights Reserved. |
| 5 | * |
| 6 | * Please see the accompanying license file, LICENSE.TXT, for information |
| 7 | * on using and copying this software. |
| 8 | */ |
| 9 | /* |
| 10 | Name |
| 11 | vmhash.h - hash table implementation |
| 12 | Function |
| 13 | |
| 14 | Notes |
| 15 | |
| 16 | Modified |
| 17 | 10/25/97 MJRoberts - Creation |
| 18 | */ |
| 19 | |
| 20 | #ifndef VMHASH_H |
| 21 | #define VMHASH_H |
| 22 | |
| 23 | #ifndef STD_H |
| 24 | #include "t3std.h" |
| 25 | #endif |
| 26 | |
| 27 | |
| 28 | /* ------------------------------------------------------------------------ */ |
| 29 | /* |
| 30 | * Hash function interface class. Hash table clients must implement an |
| 31 | * appropriate hash function to use with the hash table; this abstract |
| 32 | * class provides the necessary interface. |
| 33 | */ |
| 34 | class CVmHashFunc |
| 35 | { |
| 36 | public: |
| 37 | virtual ~CVmHashFunc() { } |
| 38 | virtual unsigned int compute_hash(const char *str, size_t len) const = 0; |
| 39 | }; |
| 40 | |
| 41 | |
| 42 | /* ------------------------------------------------------------------------ */ |
| 43 | /* |
| 44 | * Hash table symbol entry. This is an abstract class; subclasses must |
| 45 | * provide a symbol-matching method. |
| 46 | */ |
| 47 | class CVmHashEntry |
| 48 | { |
| 49 | public: |
| 50 | /* |
| 51 | * Construct the hash entry. 'copy' indicates whether we should |
| 52 | * make a private copy of the value; if not, the caller must keep |
| 53 | * the original string around as long as this hash entry is around. |
| 54 | * If 'copy' is true, we'll make a private copy of the string |
| 55 | * immediately, so the caller need not keep it around after |
| 56 | * constructing the entry. |
| 57 | */ |
| 58 | CVmHashEntry(const char *str, size_t len, int copy); |
| 59 | virtual ~CVmHashEntry(); |
| 60 | |
| 61 | /* determine if this entry matches a given string */ |
| 62 | virtual int matches(const char *str, size_t len) const = 0; |
| 63 | |
| 64 | /* list link */ |
| 65 | CVmHashEntry *nxt_; |
| 66 | |
| 67 | /* get the string pointer and length */ |
| 68 | const char *getstr() const { return str_; } |
| 69 | size_t getlen() const { return len_; } |
| 70 | |
| 71 | protected: |
| 72 | const char *str_; |
| 73 | size_t len_; |
| 74 | int copy_ : 1; |
| 75 | }; |
| 76 | |
| 77 | |
| 78 | /* ------------------------------------------------------------------------ */ |
| 79 | /* |
| 80 | * Hash table |
| 81 | */ |
| 82 | class CVmHashTable |
| 83 | { |
| 84 | public: |
| 85 | /* |
| 86 | * Construct a hash table. If own_hash_func is true, the hash table |
| 87 | * object takes ownership of the hash function object, so the hash |
| 88 | * table object will delete the hash function object when the table |
| 89 | * is deleted. |
| 90 | */ |
| 91 | CVmHashTable(int hash_table_size, CVmHashFunc *hash_function, |
| 92 | int own_hash_func) |
| 93 | { |
| 94 | /* initialize, allocating our own hash array */ |
| 95 | init(hash_table_size, hash_function, own_hash_func, 0); |
| 96 | } |
| 97 | |
| 98 | /* |
| 99 | * Construct a hash table, using memory allocated and owned by the |
| 100 | * caller for the hash array. |
| 101 | */ |
| 102 | CVmHashTable(int hash_table_size, CVmHashFunc *hash_function, |
| 103 | int own_hash_func, CVmHashEntry **hash_array) |
| 104 | { |
| 105 | /* initialize, using the provided hash array */ |
| 106 | init(hash_table_size, hash_function, own_hash_func, hash_array); |
| 107 | } |
| 108 | |
| 109 | /* delete the hash table */ |
| 110 | ~CVmHashTable(); |
| 111 | |
| 112 | /* |
| 113 | * Add a symbol. If 'copy' is true, it means that we need to make |
| 114 | * a private copy of the string; otherwise, the caller must ensure |
| 115 | * that the string remains valid as long as the hash table entry |
| 116 | * remains valid, since we'll just store a pointer to the original |
| 117 | * string. IMPORTANT: the hash table takes over ownership of the |
| 118 | * hash table entry; the hash table will delete this object when the |
| 119 | * hash table is deleted, so the client must not delete the entry |
| 120 | * once it's been added to the table. |
| 121 | */ |
| 122 | void add(CVmHashEntry *entry); |
| 123 | |
| 124 | /* |
| 125 | * Remove an object from the cache. This routine does not delete |
| 126 | * the object. |
| 127 | */ |
| 128 | void remove(CVmHashEntry *entry); |
| 129 | |
| 130 | /* |
| 131 | * Delete all entries in the table |
| 132 | */ |
| 133 | void delete_all_entries(); |
| 134 | |
| 135 | /* |
| 136 | * Find an entry in the table matching the given string |
| 137 | */ |
| 138 | CVmHashEntry *find(const char *str, size_t len) const; |
| 139 | |
| 140 | /* enumerate all entries that match the given string's hash value */ |
| 141 | void enum_hash_matches(const char *str, size_t len, |
| 142 | void (*cb)(void *cbctx, CVmHashEntry *entry), |
| 143 | void *cbctx); |
| 144 | |
| 145 | /* enumerate all entries with the given hash value */ |
| 146 | void enum_hash_matches(uint hash, |
| 147 | void (*cb)(void *cbctx, CVmHashEntry *entry), |
| 148 | void *cbctx); |
| 149 | |
| 150 | /* |
| 151 | * Find an entry that matches the longest leading substring of the |
| 152 | * given string. (For this routine, we find a match where the |
| 153 | * dictionary word is SHORTER than the given word.) |
| 154 | */ |
| 155 | CVmHashEntry *find_leading_substr(const char *str, size_t len); |
| 156 | |
| 157 | /* |
| 158 | * Enumerate all entries, invoking a callback for each entry in the |
| 159 | * table |
| 160 | */ |
| 161 | void enum_entries(void (*func)(void *ctx, class CVmHashEntry *entry), |
| 162 | void *ctx); |
| 163 | |
| 164 | /* |
| 165 | * Enumerate all entries safely. This does the same thing as |
| 166 | * enum_entries(), but this version is safe to use regardless of any |
| 167 | * changes to the table that the callback makes. |
| 168 | */ |
| 169 | void safe_enum_entries(void (*func)(void *ctx, class CVmHashEntry *entry), |
| 170 | void *ctx); |
| 171 | |
| 172 | /* |
| 173 | * Move all of the entries in this hash table to a new hash table. |
| 174 | * After this is finished, this hash table will be empty, and the new |
| 175 | * hash table will be populated with all of the entries that were |
| 176 | * formerly in this table. We don't reallocate any of the entries - |
| 177 | * they simply are unlinked from this table and moved into the new |
| 178 | * table. This can be used to rebuild a hash table with a new bucket |
| 179 | * count or hash function. |
| 180 | */ |
| 181 | void move_entries_to(CVmHashTable *new_tab); |
| 182 | |
| 183 | /* dump information on the hash table to stderr for debugging */ |
| 184 | void debug_dump() const; |
| 185 | |
| 186 | /* compute the hash value for an entry/a string */ |
| 187 | unsigned int compute_hash(CVmHashEntry *entry) const; |
| 188 | unsigned int compute_hash(const char *str, size_t len) const; |
| 189 | |
| 190 | private: |
| 191 | /* adjust a hash to the table size */ |
| 192 | unsigned int adjust_hash(unsigned int hash) const |
| 193 | { return hash & (table_size_ - 1); } |
| 194 | |
| 195 | /* initialize */ |
| 196 | void init(int hash_table_size, CVmHashFunc *hash_function, |
| 197 | int own_hash_func, CVmHashEntry **hash_array); |
| 198 | |
| 199 | /* internal service routine for checking hash table sizes for validity */ |
| 200 | int is_power_of_two(int n); |
| 201 | |
| 202 | /* the table of hash entries */ |
| 203 | CVmHashEntry **table_; |
| 204 | size_t table_size_; |
| 205 | |
| 206 | /* hash function */ |
| 207 | CVmHashFunc *hash_function_; |
| 208 | |
| 209 | /* flag: I own the hash function and must delete it when done */ |
| 210 | unsigned int own_hash_func_ : 1; |
| 211 | |
| 212 | /* flag: I own the hash table array and must delete it when done */ |
| 213 | unsigned int own_hash_table_ : 1; |
| 214 | }; |
| 215 | |
| 216 | /* ------------------------------------------------------------------------ */ |
| 217 | /* |
| 218 | * Simple case-insensitive hash function |
| 219 | */ |
| 220 | class CVmHashFuncCI: public CVmHashFunc |
| 221 | { |
| 222 | public: |
| 223 | unsigned int compute_hash(const char *str, size_t len) const; |
| 224 | }; |
| 225 | |
| 226 | /* ------------------------------------------------------------------------ */ |
| 227 | /* |
| 228 | * Simple case-sensitive hash function implementation |
| 229 | */ |
| 230 | class CVmHashFuncCS: public CVmHashFunc |
| 231 | { |
| 232 | public: |
| 233 | unsigned int compute_hash(const char *str, size_t len) const; |
| 234 | }; |
| 235 | |
| 236 | /* ------------------------------------------------------------------------ */ |
| 237 | /* |
| 238 | * Concrete subclass of CVmHashEntry providing a case-insensitive |
| 239 | * symbol match implementation |
| 240 | */ |
| 241 | class CVmHashEntryCI: public CVmHashEntry |
| 242 | { |
| 243 | public: |
| 244 | CVmHashEntryCI(const char *str, size_t len, int copy) |
| 245 | : CVmHashEntry(str, len, copy) { } |
| 246 | |
| 247 | virtual int matches(const char *str, size_t len) const; |
| 248 | }; |
| 249 | |
| 250 | /* ------------------------------------------------------------------------ */ |
| 251 | /* |
| 252 | * Concrete subclass of CVmHashEntry providing a case-sensitive symbol |
| 253 | * match implementation |
| 254 | */ |
| 255 | class CVmHashEntryCS: public CVmHashEntry |
| 256 | { |
| 257 | public: |
| 258 | CVmHashEntryCS(const char *str, size_t len, int copy) |
| 259 | : CVmHashEntry(str, len, copy) { } |
| 260 | |
| 261 | virtual int matches(const char *str, size_t len) const; |
| 262 | }; |
| 263 | |
| 264 | |
| 265 | #endif /* VMHASH_H */ |