heap_node.hpp 10 KB

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