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

fingolfin at users.sourceforge.net fingolfin at users.sourceforge.net
Fri Mar 31 14:18:01 CEST 2006


Revision: 21516
Author:   fingolfin
Date:     2006-03-31 14:17:06 -0800 (Fri, 31 Mar 2006)
ViewCVS:  http://svn.sourceforge.net/scummvm/?rev=21516&view=rev

Log Message:
-----------
Modified our Map class to use a 'Less' function instead of a 'strcmp'-like comparator functor, to match the STL map template

Modified Paths:
--------------
    scummvm/trunk/common/map.h
Modified: scummvm/trunk/common/map.h
===================================================================
--- scummvm/trunk/common/map.h	2006-03-31 22:15:42 UTC (rev 21515)
+++ scummvm/trunk/common/map.h	2006-03-31 22:17:06 UTC (rev 21516)
@@ -23,21 +23,11 @@
 #define COMMON_MAP_H
 
 #include "common/scummsys.h"
+#include "common/func.h"
 
 namespace Common {
 
-/**
- * Default comparison functor: compares to objects using their </==/> operators.
- * Comparison functors ('comparators') are used by the Map template to
- * compare keys. A non-standard comparator might e.g. be implemented to
- * compare strings, ignoring the case.
- */
-template <class T>
-struct DefaultComparator {
-  int operator()(const T& x, const T& y) const { return (x < y) ? -1 : (y < x) ? +1 : 0; }
-};
 
-
 /**
  * Template based map (aka dictionary) class which uniquely maps elements of
  * class Key to elements of class Value.
@@ -49,7 +39,7 @@
  * @todo Having unit tests for this class would be very desirable. There are a
  *       big number of things which can go wrong in this code.
  */
-template <class Key, class Value, class Comparator = DefaultComparator<Key> >
+template <class Key, class Value, class Comparator = Less<Key> >
 class Map {
 protected:
 	struct BaseNode {
@@ -68,6 +58,8 @@
 	Node *_root;
 	Node *_header;
 
+	Comparator _cmp;
+
 private:
 	Map<Key, Value, Comparator>(const Map<Key, Value, Comparator> &map);
 	Map<Key, Value, Comparator> &operator =(const Map<Key, Value, Comparator> &map);
@@ -84,10 +76,11 @@
 
 		const BaseNode &operator *() const { assert(_node != 0); return *_node; }
 		const BaseNode *operator->() const { assert(_node != 0); return _node; }
-		bool operator !=(const const_iterator &iter) const { return _node != iter._node; }
-		void operator ++() {
+		bool operator ==(const const_iterator &iter) const { return _node == iter._node; }
+		bool operator !=(const const_iterator &iter) const { return !(*this == iter); }
+		const_iterator operator ++() {
 			if (!_node)
-				return;
+				return *this;
 			if (_node->_right) {
 				_node = _node->_right;
 				while (_node->_left)
@@ -105,6 +98,8 @@
 
 			if (_node->_parent == 0)
 				_node = 0;
+			
+			return *this;
 		}
 	};
 
@@ -238,35 +233,30 @@
 
 	/** Find and return the node matching the given key, if any. */
 	Node *findNode(Node *node, const Key &key) const {
-		Comparator cmp;
 		while (node) {
-			int val = cmp(key,node->_key);
-			if (val == 0)
-				return node;
-			else if (val < 0)
+			if (_cmp(key, node->_key))
 				node = node->_left;
-			else
+			else if (_cmp(node->_key, key))
 				node = node->_right;
+			else
+				return node;
 		}
 		return 0;
 	}
 
 	Node *findOrCreateNode(Node *node, const Key &key) {
-		Comparator cmp;
 		Node *prevNode = 0;
 		bool left = true;
 		while (node) {
-			int val = cmp(key, node->_key);
 			prevNode = node;
-			if (val == 0) {
-				return node;
-			} else if (val < 0) {
+			if (_cmp(key, node->_key)) {
 				left = true;
 				node = node->_left;
-			} else {
+			} else if (_cmp(node->_key, key)) {
 				left = false;
 				node = node->_right;
-			}
+			} else
+				return node;
 		}
 		node = new Node(key, prevNode);
 		if (left) {


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