[Scummvm-cvs-logs] SF.net SVN: scummvm: [25245] scummvm/trunk/common/hashmap.h

fingolfin at users.sourceforge.net fingolfin at users.sourceforge.net
Sun Jan 28 14:30:27 CET 2007


Revision: 25245
          http://scummvm.svn.sourceforge.net/scummvm/?rev=25245&view=rev
Author:   fingolfin
Date:     2007-01-28 05:30:26 -0800 (Sun, 28 Jan 2007)

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

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

Modified: scummvm/trunk/common/hashmap.h
===================================================================
--- scummvm/trunk/common/hashmap.h	2007-01-28 13:19:17 UTC (rev 25244)
+++ scummvm/trunk/common/hashmap.h	2007-01-28 13:30:26 UTC (rev 25245)
@@ -104,6 +104,9 @@
 	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 @@
  * 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 @@
  * 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 @@
 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>


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