aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMax Horn2007-01-28 13:30:26 +0000
committerMax Horn2007-01-28 13:30:26 +0000
commit8b7fdca4ac08a559f61f30b2ee1c7f143f0640b9 (patch)
tree8cc916af88851e928dcccabd4801ac8f19275aa8
parentb13e7ce8ecf853809b98abf3b455a0093f26b202 (diff)
downloadscummvm-rg350-8b7fdca4ac08a559f61f30b2ee1c7f143f0640b9.tar.gz
scummvm-rg350-8b7fdca4ac08a559f61f30b2ee1c7f143f0640b9.tar.bz2
scummvm-rg350-8b7fdca4ac08a559f61f30b2ee1c7f143f0640b9.zip
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
-rw-r--r--common/hashmap.h15
1 files changed, 11 insertions, 4 deletions
diff --git a/common/hashmap.h b/common/hashmap.h
index b13ca79980..23103232f7 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 <class Key, class Val, class HashFunc, class EqualFunc>
-HashMap<Key, Val, HashFunc, EqualFunc>::HashMap() {
+HashMap<Key, Val, HashFunc, EqualFunc>::HashMap()
+ : _defaultVal() {
_arrsize = nextTableSize(0);
_arr = new Node *[_arrsize];
assert(_arr != NULL);
@@ -229,7 +233,8 @@ HashMap<Key, Val, HashFunc, EqualFunc>::HashMap() {
* to heap buffers for the internal storage.
*/
template <class Key, class Val, class HashFunc, class EqualFunc>
-HashMap<Key, Val, HashFunc, EqualFunc>::HashMap(const HM_t& map) {
+HashMap<Key, Val, HashFunc, EqualFunc>::HashMap(const HM_t& map)
+ : _defaultVal() {
assign(map);
}
@@ -400,8 +405,10 @@ Val &HashMap<Key, Val, HashFunc, EqualFunc>::getVal(const Key &key) {
template <class Key, class Val, class HashFunc, class EqualFunc>
const Val &HashMap<Key, Val, HashFunc, EqualFunc>::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 <class Key, class Val, class HashFunc, class EqualFunc>