sift_down.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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, _Compare&& __comp,
  24. typename iterator_traits<_RandomAccessIterator>::difference_type __len,
  25. _RandomAccessIterator __start)
  26. {
  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. {
  49. // we are not in heap-order, swap the parent with its largest child
  50. *__start = _Ops::__iter_move(__child_i);
  51. __start = __child_i;
  52. if ((__len - 2) / 2 < __child)
  53. break;
  54. // recompute the child based off of the updated parent
  55. __child = 2 * __child + 1;
  56. __child_i = __first + __child;
  57. if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + difference_type(1)))) {
  58. // right-child exists and is greater than left-child
  59. ++__child_i;
  60. ++__child;
  61. }
  62. // check if we are in heap-order
  63. } while (!__comp(*__child_i, __top));
  64. *__start = _VSTD::move(__top);
  65. }
  66. template <class _AlgPolicy, class _Compare, class _RandomAccessIterator>
  67. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _RandomAccessIterator
  68. __floyd_sift_down(_RandomAccessIterator __first, _Compare&& __comp,
  69. typename iterator_traits<_RandomAccessIterator>::difference_type __len)
  70. {
  71. using difference_type = typename iterator_traits<_RandomAccessIterator>::difference_type;
  72. _LIBCPP_ASSERT_UNCATEGORIZED(__len >= 2, "shouldn't be called unless __len >= 2");
  73. _RandomAccessIterator __hole = __first;
  74. _RandomAccessIterator __child_i = __first;
  75. difference_type __child = 0;
  76. while (true) {
  77. __child_i += difference_type(__child + 1);
  78. __child = 2 * __child + 1;
  79. if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + difference_type(1)))) {
  80. // right-child exists and is greater than left-child
  81. ++__child_i;
  82. ++__child;
  83. }
  84. // swap __hole with its largest child
  85. *__hole = _IterOps<_AlgPolicy>::__iter_move(__child_i);
  86. __hole = __child_i;
  87. // if __hole is now a leaf, we're done
  88. if (__child > (__len - 2) / 2)
  89. return __hole;
  90. }
  91. }
  92. _LIBCPP_END_NAMESPACE_STD
  93. _LIBCPP_POP_MACROS
  94. #endif // _LIBCPP___ALGORITHM_SIFT_DOWN_H