sift_down.h 3.8 KB

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