From 8b7fdca4ac08a559f61f30b2ee1c7f143f0640b9 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Sun, 28 Jan 2007 13:30:26 +0000 Subject: [PATCH] Return a default value in the const-variant of HashMap::getVal, instead of asserting out -- this way, we get less unexpected asserts, and both getVal variants behave comparably diff. The drawback is that now all HashMap instances carry one extra Value object around with them. svn-id: r25245 --- common/hashmap.h | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/common/hashmap.h b/common/hashmap.h index b13ca799801..23103232f75 100644 --- a/common/hashmap.h +++ b/common/hashmap.h @@ -104,6 +104,9 @@ public: HashFunc _hash; EqualFunc _equal; + // Default value, returned by the const getVal. + const Val _defaultVal; + #ifdef DEBUG_HASH_COLLISIONS mutable int _collisions, _lookups; #endif @@ -209,7 +212,8 @@ public: * Base constructor, creates an empty hashmap. */ template -HashMap::HashMap() { +HashMap::HashMap() + : _defaultVal() { _arrsize = nextTableSize(0); _arr = new Node *[_arrsize]; assert(_arr != NULL); @@ -229,7 +233,8 @@ HashMap::HashMap() { * to heap buffers for the internal storage. */ template -HashMap::HashMap(const HM_t& map) { +HashMap::HashMap(const HM_t& map) + : _defaultVal() { assign(map); } @@ -400,8 +405,10 @@ Val &HashMap::getVal(const Key &key) { template const Val &HashMap::getVal(const Key &key) const { uint ctr = lookup(key); - assert(_arr[ctr] != NULL); - return _arr[ctr]->_value; + if (_arr[ctr] != NULL) + return _arr[ctr]->_value; + else + return _defaultVal; } template