mutable_heap.hpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //
  2. //=======================================================================
  3. // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
  4. // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
  5. //
  6. // Distributed under the Boost Software License, Version 1.0. (See
  7. // accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //=======================================================================
  10. //
  11. #ifndef BOOST_GRAPH_DETAIL_MUTABLE_HEAP_H
  12. #define BOOST_GRAPH_DETAIL_MUTABLE_HEAP_H
  13. /*
  14. There are a few things wrong with this set of functions.
  15. ExternalData should be removed, it is not part of the core
  16. algorithm. It can be handled inside the tree nodes.
  17. The swap() should be replaced by assignment since its use is causing
  18. the number of memory references to double.
  19. The min_element should be replaced by a fixed length loop
  20. (fixed at d for d-heaps).
  21. The member functions of TreeNode should be changed to global
  22. functions.
  23. These functions will be replaced by those in heap_tree.h
  24. */
  25. namespace boost {
  26. template <class TreeNode, class Compare, class ExternalData>
  27. inline TreeNode up_heap(TreeNode x, const Compare& comp, ExternalData& edata) {
  28. while (x.has_parent() && comp(x, x.parent()))
  29. x.swap(x.parent(), edata);
  30. return x;
  31. }
  32. template <class TreeNode, class Compare, class ExternalData>
  33. inline TreeNode down_heap(TreeNode x, const Compare& comp, ExternalData& edata) {
  34. while (x.children().size() > 0) {
  35. typename TreeNode::children_type::iterator
  36. child_iter = std::min_element(x.children().begin(),
  37. x.children().end(),
  38. comp);
  39. if (comp(*child_iter, x))
  40. x.swap(*child_iter, edata);
  41. else
  42. break;
  43. }
  44. return x;
  45. }
  46. template <class TreeNode, class Compare, class ExternalData>
  47. inline void update_heap(TreeNode x, const Compare& comp, ExternalData& edata) {
  48. x = down_heap(x, comp, edata);
  49. (void)up_heap(x, comp, edata);
  50. }
  51. }
  52. #endif