diff options
| author | Willem Jan Palenstijn | 2006-05-26 17:18:23 +0000 | 
|---|---|---|
| committer | Willem Jan Palenstijn | 2006-05-26 17:18:23 +0000 | 
| commit | 20c4be47a39b6d8e7bb1587a8a4630bb2e620f54 (patch) | |
| tree | d5a172294163c0f50f923dded096d3d40937bcf5 | |
| parent | da7140c3cf1df275aabd79bcd7725bf020f84643 (diff) | |
| download | scummvm-rg350-20c4be47a39b6d8e7bb1587a8a4630bb2e620f54.tar.gz scummvm-rg350-20c4be47a39b6d8e7bb1587a8a4630bb2e620f54.tar.bz2 scummvm-rg350-20c4be47a39b6d8e7bb1587a8a4630bb2e620f54.zip | |
add functions for reverse iteration of Common::List
svn-id: r22665
| -rw-r--r-- | common/list.h | 20 | ||||
| -rw-r--r-- | test/common/list.h | 43 | 
2 files changed, 63 insertions, 0 deletions
| diff --git a/common/list.h b/common/list.h index d7273295e0..38ba9b143b 100644 --- a/common/list.h +++ b/common/list.h @@ -174,6 +174,18 @@ public:  		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 @@ public:  		return iterator(_anchor->_next);  	} +	iterator		reverse_begin() { +		return iterator(_anchor->_prev); +	} +  	iterator		end() {  		return iterator(_anchor);  	} @@ -237,6 +253,10 @@ public:  		return const_iterator(_anchor->_next);  	} +	const_iterator	reverse_begin() const { +		return const_iterator(_anchor->_prev); +	} +  	const_iterator	end() const {  		return const_iterator(_anchor);  	} diff --git a/test/common/list.h b/test/common/list.h index 31ff7af399..26709b061b 100644 --- a/test/common/list.h +++ b/test/common/list.h @@ -103,4 +103,47 @@ class ListTestSuite : public CxxTest::TestSuite  		++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() ); +	}  }; | 
