⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sequential_tree.h

📁 C++ STL 中没有树形容器是最大的遗憾! 还好高手总是能及时出现
💻 H
📖 第 1 页 / 共 2 页
字号:
/*******************************************************************************
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 "basic_tree.h"
#include "child_iterator.h"
#include "child_node_iterator.h"
#include "descendant_iterator.h"
#include "descendant_node_iterator.h"
#include "reverse_iterator.h"
#include "reverse_node_iterator.h"
#include <vector>
#include <algorithm>
#include <stdexcept>

namespace tcl 
{
	template<typename T> class sequential_tree;
}

template<typename stored_type>
class tcl::sequential_tree : public tcl::basic_tree<stored_type, sequential_tree<stored_type>, std::vector<sequential_tree<stored_type>* > >
{
public:
	// typedefs
	typedef sequential_tree<stored_type> tree_type;
	typedef sequential_tree<stored_type> sequential_tree_type;
	typedef std::vector<sequential_tree<stored_type>*> container_type;
	typedef basic_tree<stored_type, tree_type, container_type > basic_tree_type;
	friend class const_pre_order_descendant_iterator<stored_type, tree_type, container_type, sequential_tree_type>;
	friend class const_post_order_descendant_iterator<stored_type, tree_type, container_type, sequential_tree_type>;
	friend class const_level_order_descendant_iterator<stored_type, tree_type, container_type, sequential_tree_type>;
	friend class const_pre_order_descendant_node_iterator<stored_type, tree_type, container_type, sequential_tree_type>;
	friend class const_post_order_descendant_node_iterator<stored_type, tree_type, container_type, sequential_tree_type>;
	friend class const_level_order_descendant_node_iterator<stored_type, tree_type, container_type, sequential_tree_type>;
	// element iterator typedefs
	typedef const_sequential_iterator<stored_type, tree_type, container_type>										const_iterator;
	typedef sequential_iterator<stored_type, tree_type, container_type>												iterator;
	typedef const_pre_order_descendant_iterator<stored_type, tree_type, container_type, sequential_tree_type>		const_pre_order_iterator;
	typedef pre_order_descendant_iterator<stored_type, tree_type, container_type, sequential_tree_type>				pre_order_iterator;
	typedef const_post_order_descendant_iterator<stored_type, tree_type, container_type, sequential_tree_type>		const_post_order_iterator;
	typedef post_order_descendant_iterator<stored_type, tree_type, container_type, sequential_tree_type>			post_order_iterator;
	typedef const_level_order_descendant_iterator<stored_type, tree_type, container_type, sequential_tree_type>		const_level_order_iterator;
	typedef level_order_descendant_iterator<stored_type, tree_type, container_type, sequential_tree_type>			level_order_iterator;
	typedef sequential_reverse_iterator<stored_type, tree_type, container_type>										reverse_iterator;
	typedef const_sequential_reverse_iterator<stored_type, tree_type, container_type>								const_reverse_iterator;

	// node iterator typedefs
	typedef const_sequential_node_iterator<stored_type, tree_type, container_type>									const_node_iterator;
	typedef sequential_node_iterator<stored_type, tree_type, container_type>										node_iterator;
	typedef const_pre_order_descendant_node_iterator<stored_type, tree_type, container_type, sequential_tree_type>	const_pre_order_node_iterator;
	typedef pre_order_descendant_node_iterator<stored_type, tree_type, container_type, sequential_tree_type>		pre_order_node_iterator;
	typedef const_post_order_descendant_node_iterator<stored_type, tree_type, container_type, sequential_tree_type>	const_post_order_node_iterator;
	typedef post_order_descendant_node_iterator<stored_type, tree_type, container_type, sequential_tree_type>		post_order_node_iterator;
	typedef const_level_order_descendant_node_iterator<stored_type, tree_type, container_type, sequential_tree_type> const_level_order_node_iterator;
	typedef level_order_descendant_node_iterator<stored_type, tree_type, container_type, sequential_tree_type>		level_order_node_iterator;
	typedef sequential_reverse_node_iterator<stored_type, tree_type, container_type>								reverse_node_iterator;
	typedef const_sequential_reverse_node_iterator<stored_type, tree_type, container_type>							const_reverse_node_iterator;

	typedef size_t size_type;
	// constructors/destructor
	explicit sequential_tree(const stored_type& value = stored_type()) : basic_tree_type(value) {}
	explicit sequential_tree(size_type sz, const stored_type& value = stored_type()) : basic_tree_type(stored_type()) { insert(end(), sz, value); }
	sequential_tree(const sequential_tree<stored_type>& rhs);  // copy constructor
	template<typename iterator_type> sequential_tree(iterator_type it_beg, iterator_type it_end, const stored_type& value = stored_type()) : basic_tree_type(value) { while (it_beg != it_end) { insert(*it_beg); ++it_beg; } }
	~sequential_tree() {clear(); }

	// assignment operator
	tree_type& operator = (const tree_type& rhs);

	// child element iterator accessors
	const_iterator begin() const { return const_iterator(basic_tree_type::children.begin(), this); }
	const_iterator end() const { return const_iterator(basic_tree_type::children.end(), this); }
	iterator begin() { return iterator(basic_tree_type::children.begin(), this); }
	iterator end() { return iterator(basic_tree_type::children.end(), this); }

	// child node iterator accessors
	const_node_iterator node_begin() const { return const_node_iterator(basic_tree_type::children.begin(), this); }
	const_node_iterator node_end() const { return const_node_iterator(basic_tree_type::children.end(), this); }
	node_iterator node_begin() { return node_iterator(basic_tree_type::children.begin(), this); }
	node_iterator node_end() { return node_iterator(basic_tree_type::children.end(), this); }
	

	// child element reverse iterator accessors
	const_reverse_iterator rbegin() const {return const_reverse_iterator(end()); }
	const_reverse_iterator rend() const {return const_reverse_iterator(begin()); }
	reverse_iterator rbegin() {return reverse_iterator(end()); }
	reverse_iterator rend() { return reverse_iterator(begin()); }

	// descendant element iterator accessors
	post_order_iterator post_order_begin() { post_order_iterator it(this); return it; }
	post_order_iterator post_order_end() { iterator it = end(); return post_order_iterator(it, this); }
	const_post_order_iterator post_order_begin() const { const_post_order_iterator it(this); return it; }
	const_post_order_iterator post_order_end() const { const_iterator it = end(); return const_post_order_iterator(it, this); }
	pre_order_iterator pre_order_begin() { iterator it = begin(); return pre_order_iterator(it, this); }
	pre_order_iterator pre_order_end() { iterator it = end(); return pre_order_iterator(it, this); }
	const_pre_order_iterator pre_order_begin() const { const_iterator it = begin(); return const_pre_order_iterator(it, this); }
	const_pre_order_iterator pre_order_end() const { const_iterator it = end(); return const_pre_order_iterator(it, this); }
	level_order_iterator level_order_begin() { iterator it = begin(); return level_order_iterator(it, this); }
	level_order_iterator level_order_end() { iterator it = end(); return level_order_iterator(it, this); }
	const_level_order_iterator level_order_begin() const { const_iterator it = begin(); return const_level_order_iterator(it, this); }
	const_level_order_iterator level_order_end() const { const_iterator it = end(); return const_level_order_iterator(it, this); }

	// child node reverse iterator accessors
	const_reverse_node_iterator node_rbegin() const {return const_reverse_node_iterator(node_end()); }
	const_reverse_node_iterator node_rend() const {return const_reverse_node_iterator(node_begin()); }
	reverse_node_iterator node_rbegin() {return reverse_node_iterator(node_end()); }
	reverse_node_iterator node_rend() { return reverse_node_iterator(node_begin()); }

	// descendant node iterator accessors
	pre_order_node_iterator pre_order_node_begin() { node_iterator it = node_begin(); return pre_order_node_iterator(it, this); }
	pre_order_node_iterator pre_order_node_end() { node_iterator it = node_end(); return pre_order_node_iterator(it, this); }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -