tree_iterator.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. // boost heap: node tree iterator helper classes
  2. //
  3. // Copyright (C) 2010 Tim Blechmann
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. #ifndef BOOST_HEAP_DETAIL_TREE_ITERATOR_HPP
  9. #define BOOST_HEAP_DETAIL_TREE_ITERATOR_HPP
  10. #include <functional>
  11. #include <vector>
  12. #include <boost/core/allocator_access.hpp>
  13. #include <boost/iterator/iterator_adaptor.hpp>
  14. #include <boost/type_traits/conditional.hpp>
  15. #include <queue>
  16. namespace boost {
  17. namespace heap {
  18. namespace detail {
  19. template<typename type>
  20. struct identity
  21. {
  22. type& operator()(type& x) const BOOST_NOEXCEPT
  23. { return x; }
  24. const type& operator()(const type& x) const BOOST_NOEXCEPT
  25. { return x; }
  26. };
  27. template<typename Node>
  28. struct dereferencer
  29. {
  30. template <typename Iterator>
  31. Node * operator()(Iterator const & it)
  32. {
  33. return static_cast<Node *>(*it);
  34. }
  35. };
  36. template<typename Node>
  37. struct pointer_to_reference
  38. {
  39. template <typename Iterator>
  40. const Node * operator()(Iterator const & it)
  41. {
  42. return static_cast<const Node *>(&*it);
  43. }
  44. };
  45. template <typename HandleType,
  46. typename Alloc,
  47. typename ValueCompare
  48. >
  49. struct unordered_tree_iterator_storage
  50. {
  51. unordered_tree_iterator_storage(ValueCompare const & cmp)
  52. {}
  53. void push(HandleType h)
  54. {
  55. data_.push_back(h);
  56. }
  57. HandleType const & top(void)
  58. {
  59. return data_.back();
  60. }
  61. void pop(void)
  62. {
  63. data_.pop_back();
  64. }
  65. bool empty(void) const
  66. {
  67. return data_.empty();
  68. }
  69. std::vector<HandleType, typename boost::allocator_rebind<Alloc, HandleType>::type> data_;
  70. };
  71. template <typename ValueType,
  72. typename HandleType,
  73. typename Alloc,
  74. typename ValueCompare,
  75. typename ValueExtractor
  76. >
  77. struct ordered_tree_iterator_storage:
  78. ValueExtractor
  79. {
  80. struct compare_values_by_handle:
  81. ValueExtractor,
  82. ValueCompare
  83. {
  84. compare_values_by_handle(ValueCompare const & cmp):
  85. ValueCompare(cmp)
  86. {}
  87. bool operator()(HandleType const & lhs, HandleType const & rhs) const
  88. {
  89. ValueType const & lhs_value = ValueExtractor::operator()(lhs->value);
  90. ValueType const & rhs_value = ValueExtractor::operator()(rhs->value);
  91. return ValueCompare::operator()(lhs_value, rhs_value);
  92. }
  93. };
  94. ordered_tree_iterator_storage(ValueCompare const & cmp):
  95. data_(compare_values_by_handle(cmp))
  96. {}
  97. void push(HandleType h)
  98. {
  99. data_.push(h);
  100. }
  101. void pop(void)
  102. {
  103. data_.pop();
  104. }
  105. HandleType const & top(void)
  106. {
  107. return data_.top();
  108. }
  109. bool empty(void) const BOOST_NOEXCEPT
  110. {
  111. return data_.empty();
  112. }
  113. std::priority_queue<HandleType,
  114. std::vector<HandleType, typename boost::allocator_rebind<Alloc, HandleType>::type>,
  115. compare_values_by_handle> data_;
  116. };
  117. /* tree iterator helper class
  118. *
  119. * Requirements:
  120. * Node provides child_iterator
  121. * ValueExtractor can convert Node->value to ValueType
  122. *
  123. * */
  124. template <typename Node,
  125. typename ValueType,
  126. typename Alloc = std::allocator<Node>,
  127. typename ValueExtractor = identity<typename Node::value_type>,
  128. typename PointerExtractor = dereferencer<Node>,
  129. bool check_null_pointer = false,
  130. bool ordered_iterator = false,
  131. typename ValueCompare = std::less<ValueType>
  132. >
  133. class tree_iterator:
  134. public boost::iterator_adaptor<tree_iterator<Node,
  135. ValueType,
  136. Alloc,
  137. ValueExtractor,
  138. PointerExtractor,
  139. check_null_pointer,
  140. ordered_iterator,
  141. ValueCompare
  142. >,
  143. const Node *,
  144. ValueType,
  145. boost::forward_traversal_tag
  146. >,
  147. ValueExtractor,
  148. PointerExtractor
  149. {
  150. typedef boost::iterator_adaptor<tree_iterator<Node,
  151. ValueType,
  152. Alloc,
  153. ValueExtractor,
  154. PointerExtractor,
  155. check_null_pointer,
  156. ordered_iterator,
  157. ValueCompare
  158. >,
  159. const Node *,
  160. ValueType,
  161. boost::forward_traversal_tag
  162. > adaptor_type;
  163. friend class boost::iterator_core_access;
  164. typedef typename boost::conditional< ordered_iterator,
  165. ordered_tree_iterator_storage<ValueType, const Node*, Alloc, ValueCompare, ValueExtractor>,
  166. unordered_tree_iterator_storage<const Node*, Alloc, ValueCompare>
  167. >::type
  168. unvisited_node_container;
  169. public:
  170. tree_iterator(void):
  171. adaptor_type(0), unvisited_nodes(ValueCompare())
  172. {}
  173. tree_iterator(ValueCompare const & cmp):
  174. adaptor_type(0), unvisited_nodes(cmp)
  175. {}
  176. tree_iterator(const Node * it, ValueCompare const & cmp):
  177. adaptor_type(it), unvisited_nodes(cmp)
  178. {
  179. if (it)
  180. discover_nodes(it);
  181. }
  182. /* fills the iterator from a list of possible top nodes */
  183. template <typename NodePointerIterator>
  184. tree_iterator(NodePointerIterator begin, NodePointerIterator end, const Node * top_node, ValueCompare const & cmp):
  185. adaptor_type(0), unvisited_nodes(cmp)
  186. {
  187. BOOST_STATIC_ASSERT(ordered_iterator);
  188. if (begin == end)
  189. return;
  190. adaptor_type::base_reference() = top_node;
  191. discover_nodes(top_node);
  192. for (NodePointerIterator it = begin; it != end; ++it) {
  193. const Node * current_node = static_cast<const Node*>(&*it);
  194. if (current_node != top_node)
  195. unvisited_nodes.push(current_node);
  196. }
  197. }
  198. bool operator!=(tree_iterator const & rhs) const
  199. {
  200. return adaptor_type::base() != rhs.base();
  201. }
  202. bool operator==(tree_iterator const & rhs) const
  203. {
  204. return !operator!=(rhs);
  205. }
  206. const Node * get_node() const
  207. {
  208. return adaptor_type::base_reference();
  209. }
  210. private:
  211. void increment(void)
  212. {
  213. if (unvisited_nodes.empty())
  214. adaptor_type::base_reference() = 0;
  215. else {
  216. const Node * next = unvisited_nodes.top();
  217. unvisited_nodes.pop();
  218. discover_nodes(next);
  219. adaptor_type::base_reference() = next;
  220. }
  221. }
  222. ValueType const & dereference() const
  223. {
  224. return ValueExtractor::operator()(adaptor_type::base_reference()->value);
  225. }
  226. void discover_nodes(const Node * n)
  227. {
  228. for (typename Node::const_child_iterator it = n->children.begin(); it != n->children.end(); ++it) {
  229. const Node * n = PointerExtractor::operator()(it);
  230. if (check_null_pointer && n == NULL)
  231. continue;
  232. unvisited_nodes.push(n);
  233. }
  234. }
  235. unvisited_node_container unvisited_nodes;
  236. };
  237. template <typename Node, typename NodeList>
  238. struct list_iterator_converter
  239. {
  240. typename NodeList::const_iterator operator()(const Node * node)
  241. {
  242. return NodeList::s_iterator_to(*node);
  243. }
  244. Node * operator()(typename NodeList::const_iterator it)
  245. {
  246. return const_cast<Node*>(static_cast<const Node*>(&*it));
  247. }
  248. };
  249. template <typename Node,
  250. typename NodeIterator,
  251. typename ValueType,
  252. typename ValueExtractor = identity<typename Node::value_type>,
  253. typename IteratorCoverter = identity<NodeIterator>
  254. >
  255. class recursive_tree_iterator:
  256. public boost::iterator_adaptor<recursive_tree_iterator<Node,
  257. NodeIterator,
  258. ValueType,
  259. ValueExtractor,
  260. IteratorCoverter
  261. >,
  262. NodeIterator,
  263. ValueType const,
  264. boost::bidirectional_traversal_tag>,
  265. ValueExtractor, IteratorCoverter
  266. {
  267. typedef boost::iterator_adaptor<recursive_tree_iterator<Node,
  268. NodeIterator,
  269. ValueType,
  270. ValueExtractor,
  271. IteratorCoverter
  272. >,
  273. NodeIterator,
  274. ValueType const,
  275. boost::bidirectional_traversal_tag> adaptor_type;
  276. friend class boost::iterator_core_access;
  277. public:
  278. recursive_tree_iterator(void):
  279. adaptor_type(0)
  280. {}
  281. explicit recursive_tree_iterator(NodeIterator const & it):
  282. adaptor_type(it)
  283. {}
  284. void increment(void)
  285. {
  286. NodeIterator next = adaptor_type::base_reference();
  287. const Node * n = get_node(next);
  288. if (n->children.empty()) {
  289. const Node * parent = get_node(next)->get_parent();
  290. ++next;
  291. while (true) {
  292. if (parent == NULL || next != parent->children.end())
  293. break;
  294. next = IteratorCoverter::operator()(parent);
  295. parent = get_node(next)->get_parent();
  296. ++next;
  297. }
  298. } else
  299. next = n->children.begin();
  300. adaptor_type::base_reference() = next;
  301. return;
  302. }
  303. ValueType const & dereference() const
  304. {
  305. return ValueExtractor::operator()(get_node(adaptor_type::base_reference())->value);
  306. }
  307. static const Node * get_node(NodeIterator const & it)
  308. {
  309. return static_cast<const Node *>(&*it);
  310. }
  311. const Node * get_node() const
  312. {
  313. return get_node(adaptor_type::base_reference());
  314. }
  315. };
  316. } /* namespace detail */
  317. } /* namespace heap */
  318. } /* namespace boost */
  319. #endif /* BOOST_HEAP_DETAIL_TREE_ITERATOR_HPP */