diff options
Diffstat (limited to 'emulator/qtools/hash_table.h')
-rw-r--r-- | emulator/qtools/hash_table.h | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/emulator/qtools/hash_table.h b/emulator/qtools/hash_table.h index 45786ec..4ea9ed5 100644 --- a/emulator/qtools/hash_table.h +++ b/emulator/qtools/hash_table.h @@ -21,6 +21,7 @@ class HashTable { typedef T value_type; void Update(const char *key, T value); + bool Remove(const char *key); T Find(const char *key); entry_type* GetFirst(); entry_type* GetNext(); @@ -121,6 +122,31 @@ void HashTable<T>::Update(const char *key, T value) } template<class T> +bool HashTable<T>::Remove(const char *key) +{ + // Hash the key to get the table position + int len = strlen(key); + int pos = HashFunction(key) & mask_; + + // Search the chain for a matching key and keep track of the previous + // element in the chain. + entry_type *prev = NULL; + for (entry_type *ptr = table_[pos]; ptr; prev = ptr, ptr = ptr->next) { + if (strcmp(ptr->key, key) == 0) { + if (prev == NULL) { + table_[pos] = ptr->next; + } else { + prev->next = ptr->next; + } + delete ptr->key; + delete ptr; + return true; + } + } + return false; +} + +template<class T> typename HashTable<T>::value_type HashTable<T>::Find(const char *key) { // Hash the key to get the table position |