From ae16d496b9f2b245167a8af3fdab3e124364047d Mon Sep 17 00:00:00 2001 From: Max Horn Date: Sun, 6 Sep 2009 12:59:07 +0000 Subject: COMMON: HashMap::getVal now allows specifying a default value. A new variant of HashMap::getVal with a second 'default value' parameter has been added. This helps avoid many contains() + getVal() combos (which incur double lookup penalty), and is much lighter than using find() (which has to create an iterator). svn-id: r43983 --- common/hashmap.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'common/hashmap.h') diff --git a/common/hashmap.h b/common/hashmap.h index 2a50bff07a..25381b92a7 100644 --- a/common/hashmap.h +++ b/common/hashmap.h @@ -220,6 +220,7 @@ public: Val &getVal(const Key &key); const Val &getVal(const Key &key) const; + const Val &getVal(const Key &key, const Val &defaultVal) const; void setVal(const Key &key, const Val &val); void clear(bool shrinkArray = 0); @@ -555,11 +556,16 @@ Val &HashMap::getVal(const Key &key) { template const Val &HashMap::getVal(const Key &key) const { + return getVal(key, _defaultVal); +} + +template +const Val &HashMap::getVal(const Key &key, const Val &defaultVal) const { uint ctr = lookup(key); if (_storage[ctr] != NULL) return _storage[ctr]->_value; else - return _defaultVal; + return defaultVal; } template -- cgit v1.2.3