push_heap.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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_PUSH_HEAP_H
  9. #define _LIBCPP___ALGORITHM_PUSH_HEAP_H
  10. #include <__algorithm/comp.h>
  11. #include <__algorithm/comp_ref_type.h>
  12. #include <__algorithm/iterator_operations.h>
  13. #include <__config>
  14. #include <__iterator/iterator_traits.h>
  15. #include <__type_traits/is_copy_assignable.h>
  16. #include <__type_traits/is_copy_constructible.h>
  17. #include <__utility/move.h>
  18. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  19. # pragma GCC system_header
  20. #endif
  21. _LIBCPP_PUSH_MACROS
  22. #include <__undef_macros>
  23. _LIBCPP_BEGIN_NAMESPACE_STD
  24. template <class _AlgPolicy, class _Compare, class _RandomAccessIterator>
  25. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void
  26. __sift_up(_RandomAccessIterator __first,
  27. _RandomAccessIterator __last,
  28. _Compare&& __comp,
  29. typename iterator_traits<_RandomAccessIterator>::difference_type __len) {
  30. using value_type = typename iterator_traits<_RandomAccessIterator>::value_type;
  31. if (__len > 1) {
  32. __len = (__len - 2) / 2;
  33. _RandomAccessIterator __ptr = __first + __len;
  34. if (__comp(*__ptr, *--__last)) {
  35. value_type __t(_IterOps<_AlgPolicy>::__iter_move(__last));
  36. do {
  37. *__last = _IterOps<_AlgPolicy>::__iter_move(__ptr);
  38. __last = __ptr;
  39. if (__len == 0)
  40. break;
  41. __len = (__len - 1) / 2;
  42. __ptr = __first + __len;
  43. } while (__comp(*__ptr, __t));
  44. *__last = std::move(__t);
  45. }
  46. }
  47. }
  48. template <class _AlgPolicy, class _RandomAccessIterator, class _Compare>
  49. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void
  50. __push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare& __comp) {
  51. typename iterator_traits<_RandomAccessIterator>::difference_type __len = __last - __first;
  52. std::__sift_up<_AlgPolicy, __comp_ref_type<_Compare> >(std::move(__first), std::move(__last), __comp, __len);
  53. }
  54. template <class _RandomAccessIterator, class _Compare>
  55. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
  56. push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) {
  57. static_assert(std::is_copy_constructible<_RandomAccessIterator>::value, "Iterators must be copy constructible.");
  58. static_assert(std::is_copy_assignable<_RandomAccessIterator>::value, "Iterators must be copy assignable.");
  59. std::__push_heap<_ClassicAlgPolicy>(std::move(__first), std::move(__last), __comp);
  60. }
  61. template <class _RandomAccessIterator>
  62. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
  63. push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last) {
  64. std::push_heap(std::move(__first), std::move(__last), __less<>());
  65. }
  66. _LIBCPP_END_NAMESPACE_STD
  67. _LIBCPP_POP_MACROS
  68. #endif // _LIBCPP___ALGORITHM_PUSH_HEAP_H