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

sev at users.sourceforge.net sev at users.sourceforge.net
Fri Mar 31 15:50:02 CEST 2006


Revision: 21520
Author:   sev
Date:     2006-03-31 15:49:08 -0800 (Fri, 31 Mar 2006)
ViewCVS:  http://svn.sourceforge.net/scummvm/?rev=21520&view=rev

Log Message:
-----------
Add size() methods to Map and HashMap classes

Modified Paths:
--------------
    scummvm/trunk/common/hashmap.h
    scummvm/trunk/common/map.h
Modified: scummvm/trunk/common/hashmap.h
===================================================================
--- scummvm/trunk/common/hashmap.h	2006-03-31 23:10:24 UTC (rev 21519)
+++ scummvm/trunk/common/hashmap.h	2006-03-31 23:49:08 UTC (rev 21520)
@@ -168,6 +168,8 @@
 
 	size_t erase(const Key &key);
 
+	uint size() { return _nele; }
+
 	const_iterator	begin() const {
 		// Find and return the first non-empty entry
 		for (uint ctr = 0; ctr < _arrsize; ++ctr) {

Modified: scummvm/trunk/common/map.h
===================================================================
--- scummvm/trunk/common/map.h	2006-03-31 23:10:24 UTC (rev 21519)
+++ scummvm/trunk/common/map.h	2006-03-31 23:49:08 UTC (rev 21520)
@@ -60,6 +60,8 @@
 
 	Comparator _cmp;
 
+	uint _size;
+
 private:
 	Map<Key, Value, Comparator>(const Map<Key, Value, Comparator> &map);
 	Map<Key, Value, Comparator> &operator =(const Map<Key, Value, Comparator> &map);
@@ -104,7 +106,7 @@
 	};
 
 public:
-	Map<Key, Value, Comparator>() : _root(0) {
+	Map<Key, Value, Comparator>() : _root(0), _size(0) {
 		_header = new Node();
 		_header->_right = _header->_left = _header;
 	}
@@ -118,6 +120,7 @@
 		clearNodes(_root);
 		delete _header;
 		_root = _header = 0;
+		_size = 0;
 	}
 
 	/*
@@ -126,10 +129,12 @@
 	 */
 	Value &operator [](const Key &key) {
 		Node *node;
-		if (!_root)
+		if (!_root) {
 			node = _root = new Node(key, _header);
-		else
+			_size++;
+		} else {
 			node = findOrCreateNode(_root, key);
+		}
 		return node->_value;
 	}
 
@@ -146,12 +151,15 @@
 	void clear() {
 		clearNodes(_root);
 		_root = 0;
+		_size = 0;
 	}
 
 	bool empty() const {
 		return (_root == 0);
 	}
 
+	uint size() const { return _size; }
+
 	size_t erase(const Key &key) {
 		// TODO - implement efficiently. Indeed, maybe switch to using red-black trees?
 		// For now, just a lame, bad remove algorithm. Rule: don't remove elements
@@ -259,6 +267,7 @@
 				return node;
 		}
 		node = new Node(key, prevNode);
+		_size++;
 		if (left) {
 			prevNode->_left = node;
 		} else {


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