mutable_heap.hpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. {
  27. template < class TreeNode, class Compare, class ExternalData >
  28. inline TreeNode up_heap(TreeNode x, const Compare& comp, ExternalData& edata)
  29. {
  30. while (x.has_parent() && comp(x, x.parent()))
  31. x.swap(x.parent(), edata);
  32. return x;
  33. }
  34. template < class TreeNode, class Compare, class ExternalData >
  35. inline TreeNode down_heap(TreeNode x, const Compare& comp, ExternalData& edata)
  36. {
  37. while (x.children().size() > 0)
  38. {
  39. typename TreeNode::children_type::iterator child_iter
  40. = std::min_element(x.children().begin(), x.children().end(), comp);
  41. if (comp(*child_iter, x))
  42. x.swap(*child_iter, edata);
  43. else
  44. break;
  45. }
  46. return x;
  47. }
  48. template < class TreeNode, class Compare, class ExternalData >
  49. inline void update_heap(TreeNode x, const Compare& comp, ExternalData& edata)
  50. {
  51. x = down_heap(x, comp, edata);
  52. (void)up_heap(x, comp, edata);
  53. }
  54. }
  55. #endif