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

lordhoto at users.sourceforge.net lordhoto at users.sourceforge.net
Sun Jan 30 18:28:35 CET 2011


Revision: 55661
          http://scummvm.svn.sourceforge.net/scummvm/?rev=55661&view=rev
Author:   lordhoto
Date:     2011-01-30 17:28:35 +0000 (Sun, 30 Jan 2011)

Log Message:
-----------
COMMON: Add an erase method which takes an iterator to HashMap.

Currently there is no iterator returned from this method, to have some
similarity to associative containers of the STL.

I also "added" one unit test for this method, which is basically just
a copy of the HashMap::erase(const Key &) test with the required adaptions.

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

Modified: scummvm/trunk/common/hashmap.h
===================================================================
--- scummvm/trunk/common/hashmap.h	2011-01-30 16:50:55 UTC (rev 55660)
+++ scummvm/trunk/common/hashmap.h	2011-01-30 17:28:35 UTC (rev 55661)
@@ -240,6 +240,7 @@
 
 	void clear(bool shrinkArray = 0);
 
+	void erase(iterator entry);
 	void erase(const Key &key);
 
 	uint size() const { return _size; }
@@ -591,6 +592,23 @@
 }
 
 template<class Key, class Val, class HashFunc, class EqualFunc>
+void HashMap<Key, Val, HashFunc, EqualFunc>::erase(iterator entry) {
+	// Check whether we have a valid iterator
+	assert(entry._hashmap == this);
+	const uint ctr = entry._idx;
+	assert(ctr <= _mask);
+	Node * const node = _storage[ctr];
+	assert(node != NULL);
+	assert(node != HASHMAP_DUMMY_NODE);
+
+	// If we remove a key, we replace it with a dummy node.
+	freeNode(node);
+	_storage[ctr] = HASHMAP_DUMMY_NODE;
+	_size--;
+	_deleted++;
+}
+
+template<class Key, class Val, class HashFunc, class EqualFunc>
 void HashMap<Key, Val, HashFunc, EqualFunc>::erase(const Key &key) {
 
 	uint ctr = lookup(key);

Modified: scummvm/trunk/test/common/hashmap.h
===================================================================
--- scummvm/trunk/test/common/hashmap.h	2011-01-30 16:50:55 UTC (rev 55660)
+++ scummvm/trunk/test/common/hashmap.h	2011-01-30 17:28:35 UTC (rev 55661)
@@ -71,6 +71,35 @@
 		TS_ASSERT(container.empty());
 	}
 
+	void test_add_remove_iterator() {
+		Common::HashMap<int, int> container;
+		container[0] = 17;
+		container[1] = 33;
+		container[2] = 45;
+		container[3] = 12;
+		container[4] = 96;
+		TS_ASSERT(container.contains(1));
+		container.erase(container.find(1));
+		TS_ASSERT(!container.contains(1));
+		container[1] = 42;
+		TS_ASSERT(container.contains(1));
+		container.erase(container.find(0));
+		TS_ASSERT(!container.empty());
+		container.erase(container.find(1));
+		TS_ASSERT(!container.empty());
+		container.erase(container.find(2));
+		TS_ASSERT(!container.empty());
+		container.erase(container.find(3));
+		TS_ASSERT(!container.empty());
+		container.erase(container.find(4));
+		TS_ASSERT(container.empty());
+		container[1] = 33;
+		TS_ASSERT(container.contains(1));
+		TS_ASSERT(!container.empty());
+		container.erase(container.find(1));
+		TS_ASSERT(container.empty());
+	}
+
 	void test_lookup() {
 		Common::HashMap<int, int> container;
 		container[0] = 17;


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