heap_node.hpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. // boost heap: heap node 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_HEAP_NODE_HPP
  9. #define BOOST_HEAP_DETAIL_HEAP_NODE_HPP
  10. #include <boost/assert.hpp>
  11. #include <boost/static_assert.hpp>
  12. #include <boost/core/allocator_access.hpp>
  13. #include <boost/intrusive/list.hpp>
  14. #include <boost/type_traits/conditional.hpp>
  15. #ifdef BOOST_HEAP_SANITYCHECKS
  16. #define BOOST_HEAP_ASSERT BOOST_ASSERT
  17. #else
  18. #define BOOST_HEAP_ASSERT(expression)
  19. #endif
  20. namespace boost {
  21. namespace heap {
  22. namespace detail {
  23. namespace bi = boost::intrusive;
  24. template <bool auto_unlink = false>
  25. struct heap_node_base:
  26. bi::list_base_hook<typename boost::conditional<auto_unlink,
  27. bi::link_mode<bi::auto_unlink>,
  28. bi::link_mode<bi::safe_link>
  29. >::type
  30. >
  31. {};
  32. typedef bi::list<heap_node_base<false> > heap_node_list;
  33. struct nop_disposer
  34. {
  35. template <typename T>
  36. void operator()(T * n)
  37. {
  38. BOOST_HEAP_ASSERT(false);
  39. }
  40. };
  41. template <typename Node, typename HeapBase>
  42. bool is_heap(const Node * n, typename HeapBase::value_compare const & cmp)
  43. {
  44. for (typename Node::const_child_iterator it = n->children.begin(); it != n->children.end(); ++it) {
  45. Node const & this_node = static_cast<Node const &>(*it);
  46. const Node * child = static_cast<const Node*>(&this_node);
  47. if (cmp(HeapBase::get_value(n->value), HeapBase::get_value(child->value)) ||
  48. !is_heap<Node, HeapBase>(child, cmp))
  49. return false;
  50. }
  51. return true;
  52. }
  53. template <typename Node>
  54. std::size_t count_nodes(const Node * n);
  55. template <typename Node, typename List>
  56. std::size_t count_list_nodes(List const & node_list)
  57. {
  58. std::size_t ret = 0;
  59. for (typename List::const_iterator it = node_list.begin(); it != node_list.end(); ++it) {
  60. const Node * child = static_cast<const Node*>(&*it);
  61. ret += count_nodes<Node>(child);
  62. }
  63. return ret;
  64. }
  65. template <typename Node>
  66. std::size_t count_nodes(const Node * n)
  67. {
  68. return 1 + count_list_nodes<Node, typename Node::child_list>(n->children);
  69. }
  70. template<class Node>
  71. void destroy_node(Node& node)
  72. {
  73. node.~Node();
  74. }
  75. /* node cloner
  76. *
  77. * Requires `Clone Constructor':
  78. * template <typename Alloc>
  79. * Node::Node(Node const &, Alloc &)
  80. *
  81. * template <typename Alloc>
  82. * Node::Node(Node const &, Alloc &, Node * parent)
  83. *
  84. * */
  85. template <typename Node,
  86. typename NodeBase,
  87. typename Alloc>
  88. struct node_cloner
  89. {
  90. node_cloner(Alloc & allocator):
  91. allocator(allocator)
  92. {}
  93. Node * operator() (NodeBase const & node)
  94. {
  95. Node * ret = allocator.allocate(1);
  96. new (ret) Node(static_cast<Node const &>(node), allocator);
  97. return ret;
  98. }
  99. Node * operator() (NodeBase const & node, Node * parent)
  100. {
  101. Node * ret = allocator.allocate(1);
  102. new (ret) Node(static_cast<Node const &>(node), allocator, parent);
  103. return ret;
  104. }
  105. private:
  106. Alloc & allocator;
  107. };
  108. /* node disposer
  109. *
  110. * Requirements:
  111. * Node::clear_subtree(Alloc &) clears the subtree via allocator
  112. *
  113. * */
  114. template <typename Node,
  115. typename NodeBase,
  116. typename Alloc>
  117. struct node_disposer
  118. {
  119. typedef typename boost::allocator_pointer<Alloc>::type node_pointer;
  120. node_disposer(Alloc & alloc):
  121. alloc_(alloc)
  122. {}
  123. void operator()(NodeBase * base)
  124. {
  125. node_pointer n = static_cast<node_pointer>(base);
  126. n->clear_subtree(alloc_);
  127. boost::heap::detail::destroy_node(*n);
  128. alloc_.deallocate(n, 1);
  129. }
  130. Alloc & alloc_;
  131. };
  132. template <typename ValueType,
  133. bool constant_time_child_size = true
  134. >
  135. struct heap_node:
  136. heap_node_base<!constant_time_child_size>
  137. {
  138. typedef heap_node_base<!constant_time_child_size> node_base;
  139. public:
  140. typedef ValueType value_type;
  141. typedef bi::list<node_base,
  142. bi::constant_time_size<constant_time_child_size> > child_list;
  143. typedef typename child_list::iterator child_iterator;
  144. typedef typename child_list::const_iterator const_child_iterator;
  145. typedef typename child_list::size_type size_type;
  146. heap_node(ValueType const & v):
  147. value(v)
  148. {}
  149. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  150. template <class... Args>
  151. heap_node(Args&&... args):
  152. value(std::forward<Args>(args)...)
  153. {}
  154. #endif
  155. /* protected: */
  156. heap_node(heap_node const & rhs):
  157. value(rhs.value)
  158. {
  159. /* we don't copy the child list, but clone it later */
  160. }
  161. public:
  162. template <typename Alloc>
  163. heap_node (heap_node const & rhs, Alloc & allocator):
  164. value(rhs.value)
  165. {
  166. children.clone_from(rhs.children, node_cloner<heap_node, node_base, Alloc>(allocator), nop_disposer());
  167. }
  168. size_type child_count(void) const
  169. {
  170. BOOST_STATIC_ASSERT(constant_time_child_size);
  171. return children.size();
  172. }
  173. void add_child(heap_node * n)
  174. {
  175. children.push_back(*n);
  176. }
  177. template <typename Alloc>
  178. void clear_subtree(Alloc & alloc)
  179. {
  180. children.clear_and_dispose(node_disposer<heap_node, node_base, Alloc>(alloc));
  181. }
  182. void swap_children(heap_node * rhs)
  183. {
  184. children.swap(rhs->children);
  185. }
  186. ValueType value;
  187. child_list children;
  188. };
  189. template <typename value_type>
  190. struct parent_pointing_heap_node:
  191. heap_node<value_type>
  192. {
  193. typedef heap_node<value_type> super_t;
  194. parent_pointing_heap_node(value_type const & v):
  195. super_t(v), parent(NULL)
  196. {}
  197. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  198. template <class... Args>
  199. parent_pointing_heap_node(Args&&... args):
  200. super_t(std::forward<Args>(args)...), parent(NULL)
  201. {}
  202. #endif
  203. template <typename Alloc>
  204. struct node_cloner
  205. {
  206. node_cloner(Alloc & allocator, parent_pointing_heap_node * parent):
  207. allocator(allocator), parent_(parent)
  208. {}
  209. parent_pointing_heap_node * operator() (typename super_t::node_base const & node)
  210. {
  211. parent_pointing_heap_node * ret = allocator.allocate(1);
  212. new (ret) parent_pointing_heap_node(static_cast<parent_pointing_heap_node const &>(node), allocator, parent_);
  213. return ret;
  214. }
  215. private:
  216. Alloc & allocator;
  217. parent_pointing_heap_node * parent_;
  218. };
  219. template <typename Alloc>
  220. parent_pointing_heap_node (parent_pointing_heap_node const & rhs, Alloc & allocator, parent_pointing_heap_node * parent):
  221. super_t(static_cast<super_t const &>(rhs)), parent(parent)
  222. {
  223. super_t::children.clone_from(rhs.children, node_cloner<Alloc>(allocator, this), nop_disposer());
  224. }
  225. void update_children(void)
  226. {
  227. typedef heap_node_list::iterator node_list_iterator;
  228. for (node_list_iterator it = super_t::children.begin(); it != super_t::children.end(); ++it) {
  229. parent_pointing_heap_node * child = static_cast<parent_pointing_heap_node*>(&*it);
  230. child->parent = this;
  231. }
  232. }
  233. void remove_from_parent(void)
  234. {
  235. BOOST_HEAP_ASSERT(parent);
  236. parent->children.erase(heap_node_list::s_iterator_to(*this));
  237. parent = NULL;
  238. }
  239. void add_child(parent_pointing_heap_node * n)
  240. {
  241. BOOST_HEAP_ASSERT(n->parent == NULL);
  242. n->parent = this;
  243. super_t::add_child(n);
  244. }
  245. parent_pointing_heap_node * get_parent(void)
  246. {
  247. return parent;
  248. }
  249. const parent_pointing_heap_node * get_parent(void) const
  250. {
  251. return parent;
  252. }
  253. parent_pointing_heap_node * parent;
  254. };
  255. template <typename value_type>
  256. struct marked_heap_node:
  257. parent_pointing_heap_node<value_type>
  258. {
  259. typedef parent_pointing_heap_node<value_type> super_t;
  260. marked_heap_node(value_type const & v):
  261. super_t(v), mark(false)
  262. {}
  263. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  264. template <class... Args>
  265. marked_heap_node(Args&&... args):
  266. super_t(std::forward<Args>(args)...), mark(false)
  267. {}
  268. #endif
  269. marked_heap_node * get_parent(void)
  270. {
  271. return static_cast<marked_heap_node*>(super_t::parent);
  272. }
  273. const marked_heap_node * get_parent(void) const
  274. {
  275. return static_cast<marked_heap_node*>(super_t::parent);
  276. }
  277. bool mark;
  278. };
  279. template <typename Node>
  280. struct cmp_by_degree
  281. {
  282. template <typename NodeBase>
  283. bool operator()(NodeBase const & left,
  284. NodeBase const & right)
  285. {
  286. return static_cast<const Node*>(&left)->child_count() < static_cast<const Node*>(&right)->child_count();
  287. }
  288. };
  289. template <typename List, typename Node, typename Cmp>
  290. Node * find_max_child(List const & list, Cmp const & cmp)
  291. {
  292. BOOST_HEAP_ASSERT(!list.empty());
  293. const Node * ret = static_cast<const Node *> (&list.front());
  294. for (typename List::const_iterator it = list.begin(); it != list.end(); ++it) {
  295. const Node * current = static_cast<const Node *> (&*it);
  296. if (cmp(ret->value, current->value))
  297. ret = current;
  298. }
  299. return const_cast<Node*>(ret);
  300. }
  301. } /* namespace detail */
  302. } /* namespace heap */
  303. } /* namespace boost */
  304. #undef BOOST_HEAP_ASSERT
  305. #endif /* BOOST_HEAP_DETAIL_HEAP_NODE_HPP */