child_iterator.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 <set>
  23. #include <stack>
  24. #include <queue>
  25. #include <algorithm>
  26. #include <iterator>
  27. namespace tcl
  28. {
  29. // forward declaration
  30. template<typename T, typename U, typename V> class basic_tree;
  31. template<typename T> class sequential_tree;
  32. template<typename T, typename U, typename V> class associative_tree;
  33. template<typename T, typename U, typename V> class associative_reverse_iterator;
  34. template<typename T, typename U, typename V> class sequential_reverse_iterator;
  35. template<typename T, typename U, typename V, typename W> class const_pre_order_descendant_iterator;
  36. template<typename T, typename U, typename V, typename W> class const_post_order_descendant_iterator;
  37. template<typename T, typename U, typename V> class const_associative_iterator;
  38. template<typename T, typename U, typename V> class associative_iterator;
  39. template<typename T, typename U, typename V> class const_sequential_iterator;
  40. template<typename T, typename U, typename V> class sequential_iterator;
  41. // comparison operators
  42. template<typename T, typename U, typename V> bool operator == (const const_associative_iterator<T, U, V>& lhs, const const_associative_iterator<T, U, V>& rhs) { return lhs.pParent == rhs.pParent && lhs.it == rhs.it; }
  43. template<typename T, typename U, typename V> bool operator != (const const_associative_iterator<T, U, V>& lhs, const const_associative_iterator<T, U, V>& rhs) { return !(lhs == rhs); }
  44. template<typename T, typename U, typename V> bool operator == (const const_sequential_iterator<T, U, V>& lhs, const const_sequential_iterator<T, U, V>& rhs ) { return lhs.pParent == rhs.pParent && lhs.it == rhs.it; }
  45. template<typename T, typename U, typename V> bool operator != (const const_sequential_iterator<T, U, V>& lhs, const const_sequential_iterator<T, U, V>& rhs ) { return !(lhs == rhs); }
  46. template<typename T, typename U, typename V> bool operator < (const const_sequential_iterator<T, U, V>& lhs, const const_sequential_iterator<T, U, V>& rhs) { return lhs.it < rhs.it && lhs.pParent == rhs.pParent; }
  47. template<typename T, typename U, typename V> bool operator <= (const const_sequential_iterator<T, U, V>& lhs, const const_sequential_iterator<T, U, V>& rhs) { return lhs < rhs || lhs == rhs; }
  48. template<typename T, typename U, typename V> bool operator > (const const_sequential_iterator<T, U, V>& lhs, const const_sequential_iterator<T, U, V>& rhs) { return !(lhs <= rhs); }
  49. template<typename T, typename U, typename V> bool operator >= (const const_sequential_iterator<T, U, V>& lhs, const const_sequential_iterator<T, U, V>& rhs) { return !(lhs < rhs); }
  50. }
  51. /************************************************************************/
  52. /* associative tree child iterators */
  53. /************************************************************************/
  54. template<typename stored_type, typename tree_type, typename container_type>
  55. class tcl::const_associative_iterator : public std::iterator<std::bidirectional_iterator_tag, stored_type>
  56. {
  57. public:
  58. // constructors/destructor
  59. const_associative_iterator() : pParent(0) {}
  60. virtual ~const_associative_iterator() {}
  61. protected:
  62. explicit const_associative_iterator(typename container_type::const_iterator iter, const associative_tree<stored_type, tree_type, container_type>* pCalled_node) : it(iter), pParent(pCalled_node) {}
  63. // copy constructor, and assignment operator will be compiler generated correctly
  64. public:
  65. // overloaded operators
  66. const stored_type& operator*() const { return *(*it)->get(); }
  67. const stored_type* operator->() const { return (*it)->get(); }
  68. const_associative_iterator& operator ++() { ++it; return *this; }
  69. const_associative_iterator operator ++(int) { const_associative_iterator old(*this); ++*this; return old; }
  70. const_associative_iterator& operator --() { --it; return *this; }
  71. const_associative_iterator operator --(int) { const_associative_iterator old(*this); --*this; return old; }
  72. // public interface
  73. const tree_type* node() const { return *it; }
  74. // data
  75. protected:
  76. typename container_type::const_iterator it;
  77. const associative_tree<stored_type, tree_type, container_type>* pParent;
  78. // friends
  79. #if defined(_MSC_VER) && _MSC_VER < 1300
  80. friend class basic_tree<stored_type, tree_type, container_type>;
  81. friend class associative_tree<stored_type, tree_type, container_type>;
  82. friend class const_pre_order_descendant_iterator<stored_type, tree_type, container_type, associative_tree<stored_type, tree_type, container_type> >;
  83. friend class const_post_order_descendant_iterator<stored_type, tree_type, container_type, associative_tree<stored_type, tree_type, container_type> >;
  84. friend bool operator == (const const_associative_iterator& lhs, const const_associative_iterator& rhs);
  85. #else
  86. template<typename T, typename U, typename V> friend class basic_tree;
  87. template<typename T, typename U, typename V> friend class associative_tree;
  88. template<typename T, typename U, typename V, typename W> friend class const_pre_order_descendant_iterator;
  89. template<typename T, typename U, typename V, typename W> friend class const_post_order_descendant_iterator;
  90. friend bool operator ==<> (const const_associative_iterator& lhs, const const_associative_iterator& rhs);
  91. #endif
  92. };
  93. template<typename stored_type, typename tree_type, typename container_type>
  94. class tcl::associative_iterator : public tcl::const_associative_iterator<stored_type, tree_type, container_type>
  95. {
  96. private:
  97. public:
  98. // constructors/destructor
  99. associative_iterator() : const_associative_iterator<stored_type, tree_type, container_type>() {}
  100. private:
  101. explicit associative_iterator(typename container_type::iterator iter, associative_tree<stored_type, tree_type, container_type>* pCalled_node) : const_associative_iterator<stored_type, tree_type, container_type>(iter, pCalled_node) {}
  102. // destructor, copy constructor, and assignment operator will be compiler generated correctly
  103. public:
  104. // overloaded operators
  105. stored_type& operator*() const { return *(*it)->get(); }
  106. stored_type* operator->() const { return (*it)->get(); }
  107. associative_iterator& operator ++() { ++it; return *this; }
  108. associative_iterator operator ++(int) { associative_iterator old(*this); ++*this; return old; }
  109. associative_iterator& operator --() { --it; return *this; }
  110. associative_iterator operator --(int) { associative_iterator old(*this); --*this; return old; }
  111. tree_type* node() const { return *it; }
  112. // friends
  113. using const_associative_iterator<stored_type, tree_type, container_type>::it;
  114. #if defined(_MSC_VER) && _MSC_VER < 1300
  115. friend class associative_tree<stored_type, tree_type, container_type>;
  116. friend class associative_reverse_iterator<stored_type, tree_type, container_type>;
  117. #else
  118. template<typename T, typename U, typename V> friend class associative_tree;
  119. template<typename T, typename U, typename V> friend class associative_reverse_iterator;
  120. #endif
  121. };
  122. /************************************************************************/
  123. /* sequential tree child iterators */
  124. /************************************************************************/
  125. template<typename stored_type, typename tree_type, typename container_type>
  126. class tcl::const_sequential_iterator : public std::iterator<std::random_access_iterator_tag, stored_type>
  127. {
  128. public:
  129. // constructors/destructor
  130. const_sequential_iterator() : pParent(0) {}
  131. virtual ~const_sequential_iterator() {}
  132. protected:
  133. explicit const_sequential_iterator(typename container_type::const_iterator iter, const tree_type* pCalled_node) : it(iter), pParent(pCalled_node) {}
  134. // copy constructor, and assignment operator will be compiler generated correctly
  135. public:
  136. // typedefs
  137. typedef size_t size_type;
  138. #if defined(_MSC_VER) && _MSC_VER < 1300
  139. typedef std::iterator_traits<std::iterator<std::random_access_iterator_tag, stored_type> >::distance_type difference_type;
  140. #else
  141. typedef typename std::iterator_traits<const_sequential_iterator>::difference_type difference_type;
  142. #endif
  143. // overloaded operators
  144. const stored_type& operator*() const { return *(*it)->get(); }
  145. const stored_type* operator->() const { return (*it)->get(); }
  146. const_sequential_iterator& operator ++() { ++it; return *this; }
  147. const_sequential_iterator operator ++(int) { const_sequential_iterator old(*this); ++*this; return old; }
  148. const_sequential_iterator& operator --() { --it; return *this; }
  149. const_sequential_iterator operator --(int) { const_sequential_iterator old(*this); --*this; return old; }
  150. const_sequential_iterator& operator +=(difference_type n) { it += n; return *this; }
  151. const_sequential_iterator& operator -=(difference_type n) { it -= n; return *this; }
  152. difference_type operator -(const const_sequential_iterator& rhs) const { return it - rhs.it; }
  153. const tree_type* node() const { return *it; }
  154. // data
  155. protected:
  156. typename container_type::const_iterator it;
  157. const tree_type* pParent;
  158. // friends
  159. #if defined(_MSC_VER) && _MSC_VER < 1300
  160. friend class sequential_tree<stored_type>;
  161. friend class const_pre_order_descendant_iterator<stored_type, tree_type, container_type, sequential_tree<stored_type> >;
  162. friend class const_post_order_descendant_iterator<stored_type, tree_type, container_type, sequential_tree<stored_type> >;
  163. friend bool operator == (const const_sequential_iterator& lhs, const const_sequential_iterator& rhs );
  164. friend bool operator < (const const_sequential_iterator& lhs, const const_sequential_iterator& rhs);
  165. #else
  166. friend bool operator ==<> (const const_sequential_iterator& lhs, const const_sequential_iterator& rhs );
  167. friend bool operator < <> (const const_sequential_iterator& lhs, const const_sequential_iterator& rhs);
  168. template<typename T> friend class sequential_tree;
  169. template<typename T, typename U, typename V, typename W> friend class const_pre_order_descendant_iterator;
  170. template<typename T, typename U, typename V, typename W> friend class const_post_order_descendant_iterator;
  171. #endif
  172. };
  173. template<typename stored_type, typename tree_type, typename container_type>
  174. class tcl::sequential_iterator : public tcl::const_sequential_iterator<stored_type, tree_type, container_type>
  175. {
  176. public:
  177. // constructors/destructor
  178. sequential_iterator() : const_sequential_iterator<stored_type, tree_type, container_type>() {}
  179. private:
  180. explicit sequential_iterator(const typename container_type::iterator& iter, const tree_type* const pCalled_node) : const_sequential_iterator<stored_type, tree_type, container_type>(iter, pCalled_node) {}
  181. // destructor, copy constructor, and assignment operator will be compiler generated correctly
  182. public:
  183. // overloaded operators
  184. stored_type& operator*() const { return *(*it)->get(); }
  185. stored_type* operator->() const { return (*it)->get(); }
  186. sequential_iterator& operator ++() { ++it; return *this; }
  187. sequential_iterator operator ++(int) { sequential_iterator old(*this); ++*this; return old; }
  188. sequential_iterator& operator --() { --it; return *this; }
  189. sequential_iterator operator --(int) { sequential_iterator old(*this); --*this; return old; }
  190. tree_type* node() const { return *it; }
  191. // friends
  192. using const_sequential_iterator<stored_type, tree_type, container_type>::it;
  193. #if defined(_MSC_VER) && _MSC_VER < 1300
  194. friend class sequential_tree<stored_type>;
  195. friend class sequential_reverse_iterator<stored_type, tree_type, container_type>;
  196. #else
  197. template<typename T> friend class sequential_tree;
  198. template<typename T, typename U, typename V> friend class sequential_reverse_iterator;
  199. #endif
  200. };