Click here to Skip to main content
15,886,518 members
Articles / Desktop Programming / MFC

Tree Container Library

Rate me:
Please Sign up or sign in to vote.
4.85/5 (29 votes)
22 Aug 2007Zlib10 min read 228.5K   7.2K   111  
A generic template class library for storing data in a tree-like structure.
/*******************************************************************************
Tree Container Library: Generic container library to store data in tree-like structures.
Copyright (c) 2006  Mitchel Haas

This software is provided 'as-is', without any express or implied warranty. 
In no event will the author be held liable for any damages arising from 
the use of this software.

Permission is granted to anyone to use this software for any purpose, 
including commercial applications, and to alter it and redistribute it freely, 
subject to the following restrictions:

1.	The origin of this software must not be misrepresented; 
you must not claim that you wrote the original software. 
If you use this software in a product, an acknowledgment in the product 
documentation would be appreciated but is not required.

2.	Altered source versions must be plainly marked as such, 
and must not be misrepresented as being the original software.

3.	The above copyright notice and this permission notice may not be removed 
or altered from any source distribution.

For complete documentation on this library, see http://www.datasoftsolutions.net
Email questions, comments or suggestions to mhaas@datasoftsolutions.net
*******************************************************************************/
#pragma once
#include "child_node_iterator.h"
#include <iterator>

namespace tcl 
{
	template<typename stored_type, typename tree_type, typename container_type> class associative_reverse_node_iterator;

	template<typename T, typename U, typename V> class const_associative_reverse_node_iterator;
	template<typename T, typename U, typename V> class associative_reverse_node_iterator;
	template<typename T, typename U, typename V> class const_sequential_reverse_node_iterator;
	template<typename T, typename U, typename V> class sequential_reverse_node_iterator;
}


template<typename stored_type, typename tree_type, typename container_type>
class tcl::const_associative_reverse_node_iterator : public tcl::const_associative_node_iterator<stored_type, tree_type, container_type>
{
	typedef const_associative_node_iterator<stored_type, tree_type, container_type> associative_iterator_type;
public:
	const_associative_reverse_node_iterator() : associative_iterator_type() {}
	explicit const_associative_reverse_node_iterator(const associative_iterator_type& _it) : associative_iterator_type(_it) {}
	const_associative_reverse_node_iterator(associative_reverse_node_iterator<stored_type, tree_type, container_type>& _it) : associative_iterator_type(_it.it) {}

	const tree_type& operator*() { associative_iterator_type tmp(*this);  return (*--tmp); }
	const tree_type* operator->() { associative_iterator_type tmp(*this); --tmp; return tmp.operator ->(); }
	const_associative_reverse_node_iterator& operator ++() { associative_iterator_type::operator --(); return *this; }
	const_associative_reverse_node_iterator operator ++(int) { const_associative_reverse_node_iterator old(*this); ++*this; return old; }
	const_associative_reverse_node_iterator& operator --() { associative_iterator_type::operator ++(); return *this; }
	const_associative_reverse_node_iterator operator --(int) { const_associative_reverse_node_iterator old(*this); --*this; return old; }

	associative_iterator_type base() { return associative_iterator_type(*this); }
};

template<typename stored_type, typename tree_type, typename container_type>
class tcl::associative_reverse_node_iterator : public tcl::const_associative_reverse_node_iterator<stored_type, tree_type, container_type>
{
	typedef associative_node_iterator<stored_type, tree_type, container_type> associative_iterator_type;
	typedef const_associative_node_iterator<stored_type, tree_type, container_type> const_associative_iterator_type;
	typedef const_associative_reverse_node_iterator<stored_type, tree_type, container_type> base_reverse_iterator_type;
public:
	associative_reverse_node_iterator() : base_reverse_iterator_type() {}
	explicit associative_reverse_node_iterator(const associative_iterator_type& _it) : const_associative_reverse_node_iterator<stored_type, tree_type, container_type>(_it) {}

	tree_type& operator*() {return const_cast<tree_type&>(base_reverse_iterator_type::operator*()); }
	tree_type* operator->() { return const_cast<tree_type*>(base_reverse_iterator_type::operator->()); }
	associative_reverse_node_iterator& operator ++() { base_reverse_iterator_type::operator ++(); return *this; }
	associative_reverse_node_iterator operator ++(int) { associative_reverse_node_iterator old(*this); ++*this; return old; }
	associative_reverse_node_iterator& operator --() { base_reverse_iterator_type::operator --(); return *this; }
	associative_reverse_node_iterator operator --(int) { associative_reverse_node_iterator old(*this); --*this; return old; }

	associative_iterator_type base() { associative_iterator_type _it; _it.it = const_associative_iterator_type::it; _it.pIt_parent = const_associative_iterator_type::pIt_parent; return _it; }
};


template<typename stored_type, typename tree_type, typename container_type>
class tcl::const_sequential_reverse_node_iterator : public tcl::const_sequential_node_iterator<stored_type, tree_type, container_type>
{
	typedef const_sequential_node_iterator<stored_type, tree_type, container_type> sequential_iterator_type;
public:
	const_sequential_reverse_node_iterator() : sequential_iterator_type() {}
	explicit const_sequential_reverse_node_iterator(const sequential_iterator_type& _it) : sequential_iterator_type(_it) {}

	const tree_type& operator*() { sequential_iterator_type tmp(*this);  return (*--tmp); }
	const tree_type* operator->() { sequential_iterator_type tmp(*this); --tmp; return tmp.operator ->(); }
	const_sequential_reverse_node_iterator& operator ++() { sequential_iterator_type::operator --(); return *this; }
	const_sequential_reverse_node_iterator operator ++(int) { const_sequential_reverse_node_iterator old(*this); ++*this; return old; }
	const_sequential_reverse_node_iterator& operator --() { sequential_iterator_type::operator ++(); return *this; }
	const_sequential_reverse_node_iterator operator --(int) { const_sequential_reverse_node_iterator old(*this); --*this; return old; }

	sequential_iterator_type base() { return sequential_iterator_type(*this); }
};

template<typename stored_type, typename tree_type, typename container_type>
class tcl::sequential_reverse_node_iterator : public tcl::const_sequential_reverse_node_iterator<stored_type, tree_type, container_type>
{
	typedef sequential_node_iterator<stored_type, tree_type, container_type> sequential_iterator_type;
	typedef const_sequential_node_iterator<stored_type, tree_type, container_type> const_sequential_iterator_type;
	typedef const_sequential_reverse_node_iterator<stored_type, tree_type, container_type> base_reverse_iterator_type;
public:
	sequential_reverse_node_iterator() : base_reverse_iterator_type() {}
	explicit sequential_reverse_node_iterator(const sequential_iterator_type& _it) : const_sequential_reverse_node_iterator<stored_type, tree_type, container_type>(_it) {}

	tree_type& operator*() { return const_cast<tree_type&>(base_reverse_iterator_type::operator*()); }
	tree_type* operator->() { return const_cast<tree_type*>(base_reverse_iterator_type::operator->()); }
	sequential_reverse_node_iterator& operator ++() { base_reverse_iterator_type::operator ++(); return *this; }
	sequential_reverse_node_iterator operator ++(int) { sequential_reverse_node_iterator old(*this); ++*this; return old; }
	sequential_reverse_node_iterator& operator --() { base_reverse_iterator_type::operator --(); return *this; }
	sequential_reverse_node_iterator operator --(int) { sequential_reverse_node_iterator old(*this); --*this; return old; }

	sequential_iterator_type base() { sequential_iterator_type _it; _it.it = const_sequential_iterator_type::it; _it.pIt_parent = const_sequential_iterator_type::pIt_parent; return _it; }
};

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The zlib/libpng License



Comments and Discussions