diff options
author | Bertrand Augereau | 2008-03-29 23:04:10 +0000 |
---|---|---|
committer | Bertrand Augereau | 2008-03-29 23:04:10 +0000 |
commit | 7b295995dc0d42c2f3297791b04316d2fb2b9813 (patch) | |
tree | 68f09c7454121dd7f664eb3f72238c0778dfb8af /common | |
parent | e93a1f0c32736b58e622c7907b83cefa0dff7ea1 (diff) | |
download | scummvm-rg350-7b295995dc0d42c2f3297791b04316d2fb2b9813.tar.gz scummvm-rg350-7b295995dc0d42c2f3297791b04316d2fb2b9813.tar.bz2 scummvm-rg350-7b295995dc0d42c2f3297791b04316d2fb2b9813.zip |
Centralized the way the hashmaps allocate and free nodes (in order to instrument and maybe use a pool allocator later)
svn-id: r31305
Diffstat (limited to 'common')
-rw-r--r-- | common/hashmap.h | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/common/hashmap.h b/common/hashmap.h index d4e418e1d5..9d5b6cbf6e 100644 --- a/common/hashmap.h +++ b/common/hashmap.h @@ -98,6 +98,16 @@ public: Node(const Key &key) : _key(key), _value() {} }; + Node* allocNode(const Key& key) + { + return new Node(key); + } + + void freeNode(Node* node) + { + delete node; + } + Node **_arr; // hashtable of size arrsize. uint _arrsize, _nele; @@ -353,7 +363,7 @@ template <class Key, class Val, class HashFunc, class EqualFunc> HashMap<Key, Val, HashFunc, EqualFunc>::~HashMap() { for (uint ctr = 0; ctr < _arrsize; ++ctr) if (_arr[ctr] != NULL) - delete _arr[ctr]; + freeNode(_arr[ctr]); delete[] _arr; } @@ -376,7 +386,7 @@ void HashMap<Key, Val, HashFunc, EqualFunc>::assign(const HM_t& map) { _nele = 0; for (uint ctr = 0; ctr < _arrsize; ++ctr) { if (map._arr[ctr] != NULL) { - _arr[ctr] = new Node(*map._arr[ctr]); + _arr[ctr] = allocNode(map._arr[ctr]->_key); _nele++; } } @@ -389,7 +399,7 @@ template <class Key, class Val, class HashFunc, class EqualFunc> void HashMap<Key, Val, HashFunc, EqualFunc>::clear(bool shrinkArray) { for (uint ctr = 0; ctr < _arrsize; ++ctr) { if (_arr[ctr] != NULL) { - delete _arr[ctr]; + freeNode(_arr[ctr]); _arr[ctr] = NULL; } } @@ -476,7 +486,7 @@ int HashMap<Key, Val, HashFunc, EqualFunc>::lookupAndCreateIfMissing(const Key & uint ctr = lookup(key); if (_arr[ctr] == NULL) { - _arr[ctr] = new Node(key); + _arr[ctr] = allocNode(key); _nele++; // Keep the load factor below 75%. @@ -538,7 +548,7 @@ void HashMap<Key, Val, HashFunc, EqualFunc>::erase(const Key &key) { // If we remove a key, we must check all subsequent keys and possibly // reinsert them. uint j = i; - delete _arr[i]; + freeNode(_arr[i]); _arr[i] = NULL; while (true) { // Look at the next table slot |