diff options
author | Max Horn | 2007-01-21 00:06:50 +0000 |
---|---|---|
committer | Max Horn | 2007-01-21 00:06:50 +0000 |
commit | 12476efb587097abb04657a1d601564718e99c97 (patch) | |
tree | 2e0b5154d23f865230671c283933efb72abdaf50 | |
parent | cd8a5f3a98287fe7366db100c2fb45ff986e2d1b (diff) | |
download | scummvm-rg350-12476efb587097abb04657a1d601564718e99c97.tar.gz scummvm-rg350-12476efb587097abb04657a1d601564718e99c97.tar.bz2 scummvm-rg350-12476efb587097abb04657a1d601564718e99c97.zip |
Added some new HashMap methods: lookupAndCreateIfMissing (internal only), setVal and getVal (which actually is just the old queryVal renamed for consistency)
svn-id: r25135
-rw-r--r-- | common/hashmap.h | 38 |
1 files changed, 28 insertions, 10 deletions
diff --git a/common/hashmap.h b/common/hashmap.h index 91bc1347e7..c859239606 100644 --- a/common/hashmap.h +++ b/common/hashmap.h @@ -110,6 +110,7 @@ public: void assign(const HM_t& map); int lookup(const Key &key) const; + int lookupAndCreateIfMissing(const Key &key); void expand_array(uint newsize); public: @@ -164,7 +165,9 @@ public: Val &operator [](const Key &key); const Val &operator [](const Key &key) const; - const Val &queryVal(const Key &key) const; + + const Val &getVal(const Key &key) const; + void setVal(const Key &key, const Val &val); void clear(bool shrinkArray = 0); @@ -352,13 +355,7 @@ int HashMap<Key, Val, HashFunc, EqualFunc>::lookup(const Key &key) const { } template <class Key, class Val, class HashFunc, class EqualFunc> -bool HashMap<Key, Val, HashFunc, EqualFunc>::contains(const Key &key) const { - uint ctr = lookup(key); - return (_arr[ctr] != NULL); -} - -template <class Key, class Val, class HashFunc, class EqualFunc> -Val &HashMap<Key, Val, HashFunc, EqualFunc>::operator [](const Key &key) { +int HashMap<Key, Val, HashFunc, EqualFunc>::lookupAndCreateIfMissing(const Key &key) { uint ctr = lookup(key); if (_arr[ctr] == NULL) { @@ -372,22 +369,43 @@ Val &HashMap<Key, Val, HashFunc, EqualFunc>::operator [](const Key &key) { } } + return ctr; +} + + +template <class Key, class Val, class HashFunc, class EqualFunc> +bool HashMap<Key, Val, HashFunc, EqualFunc>::contains(const Key &key) const { + uint ctr = lookup(key); + return (_arr[ctr] != NULL); +} + +template <class Key, class Val, class HashFunc, class EqualFunc> +Val &HashMap<Key, Val, HashFunc, EqualFunc>::operator [](const Key &key) { + uint ctr = lookupAndCreateIfMissing(key); + assert(_arr[ctr] != NULL); return _arr[ctr]->_value; } template <class Key, class Val, class HashFunc, class EqualFunc> const Val &HashMap<Key, Val, HashFunc, EqualFunc>::operator [](const Key &key) const { - return queryVal(key); + return getVal(key); } template <class Key, class Val, class HashFunc, class EqualFunc> -const Val &HashMap<Key, Val, HashFunc, EqualFunc>::queryVal(const Key &key) const { +const Val &HashMap<Key, Val, HashFunc, EqualFunc>::getVal(const Key &key) const { uint ctr = lookup(key); assert(_arr[ctr] != NULL); return _arr[ctr]->_value; } template <class Key, class Val, class HashFunc, class EqualFunc> +void HashMap<Key, Val, HashFunc, EqualFunc>::setVal(const Key &key, const Val &val) { + uint ctr = lookupAndCreateIfMissing(key); + assert(_arr[ctr] != NULL); + _arr[ctr]->_value = val; +} + +template <class Key, class Val, class HashFunc, class EqualFunc> size_t HashMap<Key, Val, HashFunc, EqualFunc>::erase(const Key &key) { // This is based on code in the Wikipedia article on Hash tables. uint i = lookup(key); |