From 7b295995dc0d42c2f3297791b04316d2fb2b9813 Mon Sep 17 00:00:00 2001 From: Bertrand Augereau Date: Sat, 29 Mar 2008 23:04:10 +0000 Subject: Centralized the way the hashmaps allocate and free nodes (in order to instrument and maybe use a pool allocator later) svn-id: r31305 --- common/hashmap.h | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) (limited to 'common') 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 HashMap::~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::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 void HashMap::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::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::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 -- cgit v1.2.3