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

wjpalenstijn at users.sourceforge.net wjpalenstijn at users.sourceforge.net
Fri May 26 10:19:02 CEST 2006


Revision: 22665
Author:   wjpalenstijn
Date:     2006-05-26 10:18:23 -0700 (Fri, 26 May 2006)
ViewCVS:  http://svn.sourceforge.net/scummvm/?rev=22665&view=rev

Log Message:
-----------
add functions for reverse iteration of Common::List

Modified Paths:
--------------
    scummvm/trunk/common/list.h
    scummvm/trunk/test/common/list.h
Modified: scummvm/trunk/common/list.h
===================================================================
--- scummvm/trunk/common/list.h	2006-05-26 16:02:37 UTC (rev 22664)
+++ scummvm/trunk/common/list.h	2006-05-26 17:18:23 UTC (rev 22665)
@@ -174,6 +174,18 @@
 		return iterator(next);
 	}
 
+	iterator reverse_erase(iterator pos) {
+		assert(pos != end());
+
+		NodeBase *next = pos._node->_next;
+		NodeBase *prev = pos._node->_prev;
+		Node<T> *node = static_cast<Node<T> *>(pos._node);
+		prev->_next = next;
+		next->_prev = prev;
+		delete node;
+		return iterator(prev);
+	}
+
 	iterator erase(iterator first, iterator last) {
 		while (first != last)
 			erase(first++);
@@ -229,6 +241,10 @@
 		return iterator(_anchor->_next);
 	}
 
+	iterator		reverse_begin() {
+		return iterator(_anchor->_prev);
+	}
+
 	iterator		end() {
 		return iterator(_anchor);
 	}
@@ -237,6 +253,10 @@
 		return const_iterator(_anchor->_next);
 	}
 
+	const_iterator	reverse_begin() const {
+		return const_iterator(_anchor->_prev);
+	}
+
 	const_iterator	end() const {
 		return const_iterator(_anchor);
 	}

Modified: scummvm/trunk/test/common/list.h
===================================================================
--- scummvm/trunk/test/common/list.h	2006-05-26 16:02:37 UTC (rev 22664)
+++ scummvm/trunk/test/common/list.h	2006-05-26 17:18:23 UTC (rev 22665)
@@ -103,4 +103,47 @@
 		++iter;
 		TS_ASSERT( iter == container.end() );
 	}
+
+	void test_reverse( void )
+	{
+		Common::List<int> container;
+		Common::List<int>::iterator iter;
+
+		// Fill the container with some random data
+		container.push_back(17);
+		container.push_back(33);
+		container.push_back(-11);
+
+		iter = container.reverse_begin();
+		TS_ASSERT( iter != container.end() );
+
+
+		TS_ASSERT( *iter == -11 );
+		--iter;
+		TS_ASSERT( iter != container.end() );
+
+		TS_ASSERT( *iter == 33 );
+		--iter;
+		TS_ASSERT( iter != container.end() );
+
+		TS_ASSERT( *iter == 17 );
+		--iter;
+		TS_ASSERT( iter == container.end() );
+
+		iter = container.reverse_begin();
+
+		iter = container.reverse_erase(iter);
+		TS_ASSERT( iter != container.end() );
+		TS_ASSERT( *iter == 33 );
+
+		iter = container.reverse_erase(iter);
+		TS_ASSERT( iter != container.end() );
+		TS_ASSERT( *iter == 17 );
+
+		iter = container.reverse_erase(iter);
+		TS_ASSERT( iter == container.end() );
+
+		TS_ASSERT( container.begin() == container.end() );
+		TS_ASSERT( container.reverse_begin() == container.end() );
+	}
 };


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