sift_down.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 <__algorithm/iterator_operations.h>
  11. #include <__assert>
  12. #include <__config>
  13. #include <__iterator/iterator_traits.h>
  14. #include <__utility/move.h>
  15. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  16. # pragma GCC system_header
  17. #endif
  18. _LIBCPP_PUSH_MACROS
  19. #include <__undef_macros>
  20. _LIBCPP_BEGIN_NAMESPACE_STD
  21. template <class _AlgPolicy, class _Compare, class _RandomAccessIterator>
  22. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void
  23. __sift_down(_RandomAccessIterator __first,
  24. _Compare&& __comp,
  25. typename iterator_traits<_RandomAccessIterator>::difference_type __len,
  26. _RandomAccessIterator __start) {
  27. using _Ops = _IterOps<_AlgPolicy>;
  28. typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
  29. typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
  30. // left-child of __start is at 2 * __start + 1
  31. // right-child of __start is at 2 * __start + 2
  32. difference_type __child = __start - __first;
  33. if (__len < 2 || (__len - 2) / 2 < __child)
  34. return;
  35. __child = 2 * __child + 1;
  36. _RandomAccessIterator __child_i = __first + __child;
  37. if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + difference_type(1)))) {
  38. // right-child exists and is greater than left-child
  39. ++__child_i;
  40. ++__child;
  41. }
  42. // check if we are in heap-order
  43. if (__comp(*__child_i, *__start))
  44. // we are, __start is larger than its largest child
  45. return;
  46. value_type __top(_Ops::__iter_move(__start));
  47. do {
  48. // we are not in heap-order, swap the parent with its largest child
  49. *__start = _Ops::__iter_move(__child_i);
  50. __start = __child_i;
  51. if ((__len - 2) / 2 < __child)
  52. break;
  53. // recompute the child based off of the updated parent
  54. __child = 2 * __child + 1;
  55. __child_i = __first + __child;
  56. if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + difference_type(1)))) {
  57. // right-child exists and is greater than left-child
  58. ++__child_i;
  59. ++__child;
  60. }
  61. // check if we are in heap-order
  62. } while (!__comp(*__child_i, __top));
  63. *__start = std::move(__top);
  64. }
  65. template <class _AlgPolicy, class _Compare, class _RandomAccessIterator>
  66. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _RandomAccessIterator __floyd_sift_down(
  67. _RandomAccessIterator __first,
  68. _Compare&& __comp,
  69. typename iterator_traits<_RandomAccessIterator>::difference_type __len) {
  70. using difference_type = typename iterator_traits<_RandomAccessIterator>::difference_type;
  71. _LIBCPP_ASSERT_INTERNAL(__len >= 2, "shouldn't be called unless __len >= 2");
  72. _RandomAccessIterator __hole = __first;
  73. _RandomAccessIterator __child_i = __first;
  74. difference_type __child = 0;
  75. while (true) {
  76. __child_i += difference_type(__child + 1);
  77. __child = 2 * __child + 1;
  78. if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + difference_type(1)))) {
  79. // right-child exists and is greater than left-child
  80. ++__child_i;
  81. ++__child;
  82. }
  83. // swap __hole with its largest child
  84. *__hole = _IterOps<_AlgPolicy>::__iter_move(__child_i);
  85. __hole = __child_i;
  86. // if __hole is now a leaf, we're done
  87. if (__child > (__len - 2) / 2)
  88. return __hole;
  89. }
  90. }
  91. _LIBCPP_END_NAMESPACE_STD
  92. _LIBCPP_POP_MACROS
  93. #endif // _LIBCPP___ALGORITHM_SIFT_DOWN_H