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