sift_down.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef _LIBCPP___ALGORITHM_SIFT_DOWN_H
  9. #define _LIBCPP___ALGORITHM_SIFT_DOWN_H
  10. #include <__assert>
  11. #include <__config>
  12. #include <__iterator/iterator_traits.h>
  13. #include <__utility/move.h>
  14. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  15. # pragma GCC system_header
  16. #endif
  17. _LIBCPP_BEGIN_NAMESPACE_STD
  18. template <class _Compare, class _RandomAccessIterator>
  19. _LIBCPP_CONSTEXPR_AFTER_CXX11 void
  20. __sift_down(_RandomAccessIterator __first, _Compare __comp,
  21. typename iterator_traits<_RandomAccessIterator>::difference_type __len,
  22. _RandomAccessIterator __start)
  23. {
  24. typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
  25. typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
  26. // left-child of __start is at 2 * __start + 1
  27. // right-child of __start is at 2 * __start + 2
  28. difference_type __child = __start - __first;
  29. if (__len < 2 || (__len - 2) / 2 < __child)
  30. return;
  31. __child = 2 * __child + 1;
  32. _RandomAccessIterator __child_i = __first + __child;
  33. if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + difference_type(1)))) {
  34. // right-child exists and is greater than left-child
  35. ++__child_i;
  36. ++__child;
  37. }
  38. // check if we are in heap-order
  39. if (__comp(*__child_i, *__start))
  40. // we are, __start is larger than its largest child
  41. return;
  42. value_type __top(_VSTD::move(*__start));
  43. do
  44. {
  45. // we are not in heap-order, swap the parent with its largest child
  46. *__start = _VSTD::move(*__child_i);
  47. __start = __child_i;
  48. if ((__len - 2) / 2 < __child)
  49. break;
  50. // recompute the child based off of the updated parent
  51. __child = 2 * __child + 1;
  52. __child_i = __first + __child;
  53. if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + difference_type(1)))) {
  54. // right-child exists and is greater than left-child
  55. ++__child_i;
  56. ++__child;
  57. }
  58. // check if we are in heap-order
  59. } while (!__comp(*__child_i, __top));
  60. *__start = _VSTD::move(__top);
  61. }
  62. template <class _Compare, class _RandomAccessIterator>
  63. _LIBCPP_CONSTEXPR_AFTER_CXX11 _RandomAccessIterator
  64. __floyd_sift_down(_RandomAccessIterator __first, _Compare __comp,
  65. typename iterator_traits<_RandomAccessIterator>::difference_type __len)
  66. {
  67. using difference_type = typename iterator_traits<_RandomAccessIterator>::difference_type;
  68. _LIBCPP_ASSERT(__len >= 2, "shouldn't be called unless __len >= 2");
  69. _RandomAccessIterator __hole = __first;
  70. _RandomAccessIterator __child_i = __first;
  71. difference_type __child = 0;
  72. while (true) {
  73. __child_i += difference_type(__child + 1);
  74. __child = 2 * __child + 1;
  75. if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + difference_type(1)))) {
  76. // right-child exists and is greater than left-child
  77. ++__child_i;
  78. ++__child;
  79. }
  80. // swap __hole with its largest child
  81. *__hole = std::move(*__child_i);
  82. __hole = __child_i;
  83. // if __hole is now a leaf, we're done
  84. if (__child > (__len - 2) / 2)
  85. return __hole;
  86. }
  87. }
  88. _LIBCPP_END_NAMESPACE_STD
  89. #endif // _LIBCPP___ALGORITHM_SIFT_DOWN_H