sequential_tree.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*******************************************************************************
  2. Tree Container Library: Generic container library to store data in tree-like structures.
  3. Copyright (c) 2006 Mitchel Haas
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the author be held liable for any damages arising from
  6. the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it freely,
  9. subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented;
  11. you must not claim that you wrote the original software.
  12. If you use this software in a product, an acknowledgment in the product
  13. documentation would be appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such,
  15. and must not be misrepresented as being the original software.
  16. 3. The above copyright notice and this permission notice may not be removed
  17. or altered from any source distribution.
  18. For complete documentation on this library, see http://www.datasoftsolutions.net
  19. Email questions, comments or suggestions to mhaas@datasoftsolutions.net
  20. *******************************************************************************/
  21. #pragma once
  22. #include "basic_tree.h"
  23. #include "child_iterator.h"
  24. #include "child_node_iterator.h"
  25. #include "descendant_iterator.h"
  26. #include "descendant_node_iterator.h"
  27. #include "reverse_iterator.h"
  28. #include "reverse_node_iterator.h"
  29. #include <vector>
  30. #include <algorithm>
  31. #include <stdexcept>
  32. namespace tcl
  33. {
  34. template<typename T> class sequential_tree;
  35. // overloaded comparison operations
  36. template<typename T> bool operator == (const sequential_tree<T>& lhs, const sequential_tree<T>& rhs);
  37. template<typename T> bool operator < (const sequential_tree<T>& lhs, const sequential_tree<T>& rhs);
  38. template<typename T> bool operator != (const sequential_tree<T>& lhs, const sequential_tree<T>& rhs) { return !(lhs == rhs); }
  39. template<typename T> bool operator > (const sequential_tree<T>& lhs, const sequential_tree<T>& rhs) { return rhs < lhs; }
  40. template<typename T> bool operator <= (const sequential_tree<T>& lhs, const sequential_tree<T>& rhs) { return !(rhs < lhs); }
  41. template<typename T> bool operator >= (const sequential_tree<T>& lhs, const sequential_tree<T>& rhs) { return !(lhs < rhs); }
  42. }
  43. template<typename stored_type>
  44. class tcl::sequential_tree : public tcl::basic_tree<stored_type, sequential_tree<stored_type>, std::vector<sequential_tree<stored_type>* > >
  45. {
  46. public:
  47. // typedefs
  48. typedef sequential_tree<stored_type> tree_type;
  49. typedef std::vector<tree_type*> container_type;
  50. typedef basic_tree<stored_type, tree_type, container_type > basic_tree_type;
  51. typedef typename basic_tree_type::size_type basic_size_type;
  52. using basic_tree_type::size_type;
  53. // element iterator typedefs
  54. typedef const_sequential_iterator<stored_type, tree_type, container_type> const_iterator;
  55. typedef sequential_iterator<stored_type, tree_type, container_type> iterator;
  56. typedef const_pre_order_descendant_iterator<stored_type, tree_type, container_type, tree_type> const_pre_order_iterator;
  57. typedef pre_order_descendant_iterator<stored_type, tree_type, container_type, tree_type> pre_order_iterator;
  58. typedef const_post_order_descendant_iterator<stored_type, tree_type, container_type, tree_type> const_post_order_iterator;
  59. typedef post_order_descendant_iterator<stored_type, tree_type, container_type, tree_type> post_order_iterator;
  60. typedef const_level_order_descendant_iterator<stored_type, tree_type, container_type, tree_type> const_level_order_iterator;
  61. typedef level_order_descendant_iterator<stored_type, tree_type, container_type, tree_type> level_order_iterator;
  62. typedef sequential_reverse_iterator<stored_type, tree_type, container_type> reverse_iterator;
  63. typedef const_sequential_reverse_iterator<stored_type, tree_type, container_type> const_reverse_iterator;
  64. // node iterator typedefs
  65. typedef const_sequential_node_iterator<stored_type, tree_type, container_type> const_node_iterator;
  66. typedef sequential_node_iterator<stored_type, tree_type, container_type> node_iterator;
  67. typedef const_pre_order_descendant_node_iterator<stored_type, tree_type, container_type, tree_type> const_pre_order_node_iterator;
  68. typedef pre_order_descendant_node_iterator<stored_type, tree_type, container_type, tree_type> pre_order_node_iterator;
  69. typedef const_post_order_descendant_node_iterator<stored_type, tree_type, container_type, tree_type> const_post_order_node_iterator;
  70. typedef post_order_descendant_node_iterator<stored_type, tree_type, container_type, tree_type> post_order_node_iterator;
  71. typedef const_level_order_descendant_node_iterator<stored_type, tree_type, container_type, tree_type> const_level_order_node_iterator;
  72. typedef level_order_descendant_node_iterator<stored_type, tree_type, container_type, tree_type> level_order_node_iterator;
  73. typedef sequential_reverse_node_iterator<stored_type, tree_type, container_type> reverse_node_iterator;
  74. typedef const_sequential_reverse_node_iterator<stored_type, tree_type, container_type> const_reverse_node_iterator;
  75. // constructors/destructor
  76. explicit sequential_tree(const stored_type& value = stored_type()) : basic_tree_type(value) {}
  77. explicit sequential_tree(typename basic_tree_type::size_type sz, const stored_type& value = stored_type()) : basic_tree_type(stored_type()) { insert(end(), sz, value); }
  78. sequential_tree(const tree_type& rhs); // copy constructor
  79. 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; } }
  80. ~sequential_tree() {clear(); }
  81. // assignment operator
  82. tree_type& operator = (const tree_type& rhs);
  83. // child element iterator accessors
  84. const_iterator begin() const { return const_iterator(basic_tree_type::children.begin(), this); }
  85. const_iterator end() const { return const_iterator(basic_tree_type::children.end(), this); }
  86. iterator begin() { return iterator(basic_tree_type::children.begin(), this); }
  87. iterator end() { return iterator(basic_tree_type::children.end(), this); }
  88. // child node iterator accessors
  89. const_node_iterator node_begin() const { return const_node_iterator(basic_tree_type::children.begin(), this); }
  90. const_node_iterator node_end() const { return const_node_iterator(basic_tree_type::children.end(), this); }
  91. node_iterator node_begin() { return node_iterator(basic_tree_type::children.begin(), this); }
  92. node_iterator node_end() { return node_iterator(basic_tree_type::children.end(), this); }
  93. // child element reverse iterator accessors
  94. const_reverse_iterator rbegin() const {return const_reverse_iterator(end()); }
  95. const_reverse_iterator rend() const {return const_reverse_iterator(begin()); }
  96. reverse_iterator rbegin() {return reverse_iterator(end()); }
  97. reverse_iterator rend() { return reverse_iterator(begin()); }
  98. // descendant element iterator accessors
  99. pre_order_iterator pre_order_begin() { return pre_order_iterator(this, true); }
  100. pre_order_iterator pre_order_end() { return pre_order_iterator(this, false); }
  101. const_pre_order_iterator pre_order_begin() const { return const_pre_order_iterator(this, true); }
  102. const_pre_order_iterator pre_order_end() const { return const_pre_order_iterator(this, false); }
  103. post_order_iterator post_order_begin() { return post_order_iterator(this, true); }
  104. post_order_iterator post_order_end() { return post_order_iterator(this, false); }
  105. const_post_order_iterator post_order_begin() const { return const_post_order_iterator(this, true); }
  106. const_post_order_iterator post_order_end() const { return const_post_order_iterator(this, false); }
  107. level_order_iterator level_order_begin() { return level_order_iterator(this, true); }
  108. level_order_iterator level_order_end() { return level_order_iterator(this, false); }
  109. const_level_order_iterator level_order_begin() const { return const_level_order_iterator(this, true); }
  110. const_level_order_iterator level_order_end() const { return const_level_order_iterator(this, false); }
  111. // child node reverse iterator accessors
  112. const_reverse_node_iterator node_rbegin() const {return const_reverse_node_iterator(node_end()); }
  113. const_reverse_node_iterator node_rend() const {return const_reverse_node_iterator(node_begin()); }
  114. reverse_node_iterator node_rbegin() {return reverse_node_iterator(node_end()); }
  115. reverse_node_iterator node_rend() { return reverse_node_iterator(node_begin()); }
  116. // descendant node iterator accessors
  117. pre_order_node_iterator pre_order_node_begin() { return pre_order_node_iterator(this, true); }
  118. pre_order_node_iterator pre_order_node_end() { return pre_order_node_iterator(this, false); }
  119. const_pre_order_node_iterator pre_order_node_begin() const { return const_pre_order_node_iterator(this, true); }
  120. const_pre_order_node_iterator pre_order_node_end() const { return const_pre_order_node_iterator(this, false); }
  121. post_order_node_iterator post_order_node_begin() { return post_order_node_iterator(this, true); }
  122. post_order_node_iterator post_order_node_end() { return post_order_node_iterator(this, false); }
  123. const_post_order_node_iterator post_order_node_begin() const { return const_post_order_node_iterator(this, true); }
  124. const_post_order_node_iterator post_order_node_end() const { return const_post_order_node_iterator(this, false); }
  125. level_order_node_iterator level_order_node_begin() { return level_order_node_iterator(this, true); }
  126. level_order_node_iterator level_order_node_end() { return level_order_node_iterator(this, false); }
  127. const_level_order_node_iterator level_order_node_begin() const { return const_level_order_node_iterator(this, true); }
  128. const_level_order_node_iterator level_order_node_end() const { return const_level_order_node_iterator(this, false); }
  129. // public interface
  130. typename basic_tree_type::size_type capacity() const { return basic_tree_type::children.capacity(); }
  131. void reserve(typename basic_tree_type::size_type sz) { basic_tree_type::children.reserve(sz); }
  132. tree_type& front() { return *basic_tree_type::children.front(); }
  133. tree_type& back() { return *basic_tree_type::children.back(); }
  134. const tree_type& front() const { return *basic_tree_type::children.front(); }
  135. const tree_type& back() const { return *basic_tree_type::children.back(); }
  136. void push_back(const stored_type& value);
  137. void pop_back() { iterator it = end(); erase(--it); }
  138. iterator insert(const stored_type& value);
  139. iterator insert(const tree_type& tree_obj );
  140. iterator insert(const_iterator pos, const stored_type& value);
  141. iterator insert(const const_iterator& pos, const tree_type& tree_obj);
  142. void insert(const_iterator pos, typename basic_tree_type::size_type num, const stored_type& value);
  143. #if !defined(_MSC_VER) || _MSC_VER >= 1300 // insert range not available for VC6
  144. template<typename iterator_type> void insert(const_iterator pos, iterator_type it_beg, iterator_type it_end)
  145. { while (it_beg != it_end) { pos = insert(pos, *it_beg++); ++pos; } }
  146. #endif
  147. void set(const stored_type& value) { basic_tree_type::set(value); }
  148. void set(const tree_type& tree_obj);
  149. void swap(tree_type& rhs);
  150. iterator erase(iterator it);
  151. iterator erase(iterator beg_it, iterator end_it);
  152. void clear();
  153. // subscript operators
  154. tree_type& operator [](basic_size_type index);
  155. const tree_type& operator [](basic_size_type index) const;
  156. // children sort operations
  157. template<typename T> void sort(const T& comparer) { std::sort(basic_tree_type::children.begin(), basic_tree_type::children.end(), sort_functor_deref<T>(comparer)); }
  158. void sort() { std::sort(basic_tree_type::children.begin(), basic_tree_type::children.end(), sort_deref()); }
  159. // descendant sort operations
  160. template<typename T> void sort_descendants(const T& comparer)
  161. {
  162. post_order_iterator it = post_order_begin(), it_end = post_order_end();
  163. for ( ; it != it_end; ++it )
  164. {
  165. it.node()->sort(comparer);
  166. }
  167. }
  168. void sort_descendants();
  169. // overloaded iterator arithmetic operators
  170. friend const_iterator operator +(const const_iterator& lhs, typename basic_tree_type::size_type n)
  171. { const_iterator temp(lhs); temp += n; return temp; }
  172. friend const_iterator operator +(typename basic_tree_type::size_type n, const const_iterator& rhs)
  173. { const_iterator temp(rhs); temp += n; return temp; }
  174. friend const_iterator operator -(const const_iterator& lhs, typename basic_tree_type::size_type n)
  175. { const_iterator temp(lhs); temp -= n; return temp; }
  176. friend iterator operator +(const iterator& lhs, typename basic_tree_type::size_type n)
  177. { iterator temp(lhs); temp += n; return temp; }
  178. friend iterator operator +(typename basic_tree_type::size_type n, const iterator& rhs)
  179. { iterator temp(rhs); temp += n; return temp; }
  180. friend iterator operator -(const iterator& lhs, typename basic_tree_type::size_type n)
  181. { iterator temp(lhs); temp -= n; return temp; }
  182. // overloaded node iterator arithmetic operators
  183. friend const_node_iterator operator +(const const_node_iterator& lhs, typename basic_tree_type::size_type n)
  184. { const_node_iterator temp(lhs); temp += n; return temp; }
  185. friend const_node_iterator operator +(typename basic_tree_type::size_type n, const const_node_iterator& rhs)
  186. { const_node_iterator temp(rhs); temp += n; return temp; }
  187. friend const_node_iterator operator -(const const_node_iterator& lhs, typename basic_tree_type::size_type n)
  188. { const_node_iterator temp(lhs); temp -= n; return temp; }
  189. friend node_iterator operator +(const node_iterator& lhs, typename basic_tree_type::size_type n)
  190. { node_iterator temp(lhs); temp += n; return temp; }
  191. friend node_iterator operator +(typename basic_tree_type::size_type n, const node_iterator& rhs)
  192. { node_iterator temp(rhs); temp += n; return temp; }
  193. friend node_iterator operator -(const node_iterator& lhs, typename basic_tree_type::size_type n)
  194. { node_iterator temp(lhs); temp -= n; return temp; }
  195. private:
  196. // sort() dereference functor
  197. struct sort_deref
  198. {
  199. bool operator() (const tree_type* lhs, const tree_type* rhs)
  200. {
  201. return *lhs->get() < *rhs->get();
  202. }
  203. };
  204. // sort<T>() dereference functor
  205. template<typename T>
  206. struct sort_functor_deref
  207. {
  208. explicit sort_functor_deref(const T& sort_functor_) : sort_functor(sort_functor_) {}
  209. bool operator() (const tree_type* lhs, const tree_type* rhs) const
  210. {
  211. return sort_functor(*lhs->get(), *rhs->get());
  212. }
  213. sort_functor_deref& operator = (const sort_functor_deref& rhs) { sort_functor = rhs->sort_functor; return *this; }
  214. const T& sort_functor;
  215. };
  216. // friends
  217. #if defined(_MSC_VER) && _MSC_VER < 1300
  218. friend class const_pre_order_descendant_iterator<stored_type, tree_type, container_type, tree_type>;
  219. friend class const_post_order_descendant_iterator<stored_type, tree_type, container_type, tree_type>;
  220. friend class const_level_order_descendant_iterator<stored_type, tree_type, container_type, tree_type>;
  221. friend class const_pre_order_descendant_node_iterator<stored_type, tree_type, container_type, tree_type>;
  222. friend class const_post_order_descendant_node_iterator<stored_type, tree_type, container_type, tree_type>;
  223. friend class const_level_order_descendant_node_iterator<stored_type, tree_type, container_type, tree_type>;
  224. friend bool operator == (const tree_type& lhs, const tree_type& rhs);
  225. friend bool operator < (const tree_type& lhs, const tree_type& rhs);
  226. #else
  227. template<typename T, typename U, typename V, typename W> friend class const_pre_order_descendant_iterator;
  228. template<typename T, typename U, typename V, typename W> friend class const_post_order_descendant_iterator;
  229. template<typename T, typename U, typename V, typename W> friend class const_level_order_descendant_iterator;
  230. template<typename T, typename U, typename V, typename W> friend class const_pre_order_descendant_node_iterator;
  231. template<typename T, typename U, typename V, typename W> friend class const_post_order_descendant_node_iterator;
  232. template<typename T, typename U, typename V, typename W> friend class const_level_order_descendant_node_iterator;
  233. friend bool operator ==<> (const tree_type& lhs, const tree_type& rhs);
  234. friend bool operator < <> (const tree_type& lhs, const tree_type& rhs);
  235. #endif
  236. };
  237. #include "sequential_tree.inl"