[Scummvm-cvs-logs] SF.net SVN: scummvm:[43983] scummvm/trunk

fingolfin at users.sourceforge.net fingolfin at users.sourceforge.net
Sun Sep 6 14:59:07 CEST 2009


Revision: 43983
          http://scummvm.svn.sourceforge.net/scummvm/?rev=43983&view=rev
Author:   fingolfin
Date:     2009-09-06 12:59:07 +0000 (Sun, 06 Sep 2009)

Log Message:
-----------
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).

Modified Paths:
--------------
    scummvm/trunk/common/hashmap.h
    scummvm/trunk/test/common/hashmap.h

Modified: scummvm/trunk/common/hashmap.h
===================================================================
--- scummvm/trunk/common/hashmap.h	2009-09-06 12:58:43 UTC (rev 43982)
+++ scummvm/trunk/common/hashmap.h	2009-09-06 12:59:07 UTC (rev 43983)
@@ -220,6 +220,7 @@
 
 	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 @@
 
 template<class Key, class Val, class HashFunc, class EqualFunc>
 const Val &HashMap<Key, Val, HashFunc, EqualFunc>::getVal(const Key &key) const {
+	return getVal(key, _defaultVal);
+}
+
+template<class Key, class Val, class HashFunc, class EqualFunc>
+const Val &HashMap<Key, Val, HashFunc, EqualFunc>::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<class Key, class Val, class HashFunc, class EqualFunc>

Modified: scummvm/trunk/test/common/hashmap.h
===================================================================
--- scummvm/trunk/test/common/hashmap.h	2009-09-06 12:58:43 UTC (rev 43982)
+++ scummvm/trunk/test/common/hashmap.h	2009-09-06 12:59:07 UTC (rev 43983)
@@ -86,6 +86,24 @@
 		TS_ASSERT_EQUALS(container[4], 96);
 	}
 
+	void test_lookup_with_default() {
+		Common::HashMap<int, int> container;
+		container[0] = 17;
+		container[1] = -1;
+		container[2] = 45;
+		container[3] = 12;
+		container[4] = 96;
+
+		// We take a const ref now to ensure that the map
+		// is not modified by getVal.
+		const Common::HashMap<int, int> &containerRef = container;
+
+		TS_ASSERT_EQUALS(containerRef.getVal(0), 17);
+		TS_ASSERT_EQUALS(containerRef.getVal(17), 0);
+		TS_ASSERT_EQUALS(containerRef.getVal(0, -10), 17);
+		TS_ASSERT_EQUALS(containerRef.getVal(17, -10), -10);
+	}
+
 	void test_iterator_begin_end() {
 		Common::HashMap<int, int> container;
 


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the Scummvm-git-logs mailing list