pop_heap.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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_POP_HEAP_H
  9. #define _LIBCPP___ALGORITHM_POP_HEAP_H
  10. #include <__algorithm/comp.h>
  11. #include <__algorithm/comp_ref_type.h>
  12. #include <__algorithm/iterator_operations.h>
  13. #include <__algorithm/push_heap.h>
  14. #include <__algorithm/sift_down.h>
  15. #include <__assert>
  16. #include <__config>
  17. #include <__iterator/iterator_traits.h>
  18. #include <__utility/move.h>
  19. #include <type_traits>
  20. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  21. # pragma GCC system_header
  22. #endif
  23. _LIBCPP_BEGIN_NAMESPACE_STD
  24. template <class _AlgPolicy, class _Compare, class _RandomAccessIterator>
  25. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  26. void __pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare& __comp,
  27. typename iterator_traits<_RandomAccessIterator>::difference_type __len) {
  28. _LIBCPP_ASSERT(__len > 0, "The heap given to pop_heap must be non-empty");
  29. __comp_ref_type<_Compare> __comp_ref = __comp;
  30. using value_type = typename iterator_traits<_RandomAccessIterator>::value_type;
  31. if (__len > 1) {
  32. value_type __top = _IterOps<_AlgPolicy>::__iter_move(__first); // create a hole at __first
  33. _RandomAccessIterator __hole = std::__floyd_sift_down<_AlgPolicy>(__first, __comp_ref, __len);
  34. --__last;
  35. if (__hole == __last) {
  36. *__hole = std::move(__top);
  37. } else {
  38. *__hole = _IterOps<_AlgPolicy>::__iter_move(__last);
  39. ++__hole;
  40. *__last = std::move(__top);
  41. std::__sift_up<_AlgPolicy>(__first, __hole, __comp_ref, __hole - __first);
  42. }
  43. }
  44. }
  45. template <class _RandomAccessIterator, class _Compare>
  46. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  47. void pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) {
  48. static_assert(std::is_copy_constructible<_RandomAccessIterator>::value, "Iterators must be copy constructible.");
  49. static_assert(std::is_copy_assignable<_RandomAccessIterator>::value, "Iterators must be copy assignable.");
  50. typename iterator_traits<_RandomAccessIterator>::difference_type __len = __last - __first;
  51. std::__pop_heap<_ClassicAlgPolicy>(std::move(__first), std::move(__last), __comp, __len);
  52. }
  53. template <class _RandomAccessIterator>
  54. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  55. void pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last) {
  56. std::pop_heap(std::move(__first), std::move(__last),
  57. __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
  58. }
  59. _LIBCPP_END_NAMESPACE_STD
  60. #endif // _LIBCPP___ALGORITHM_POP_HEAP_H