make_heap.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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_MAKE_HEAP_H
  9. #define _LIBCPP___ALGORITHM_MAKE_HEAP_H
  10. #include <__algorithm/comp.h>
  11. #include <__algorithm/comp_ref_type.h>
  12. #include <__algorithm/iterator_operations.h>
  13. #include <__algorithm/sift_down.h>
  14. #include <__config>
  15. #include <__iterator/iterator_traits.h>
  16. #include <__utility/move.h>
  17. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  18. # pragma GCC system_header
  19. #endif
  20. _LIBCPP_PUSH_MACROS
  21. #include <__undef_macros>
  22. _LIBCPP_BEGIN_NAMESPACE_STD
  23. template <class _AlgPolicy, class _Compare, class _RandomAccessIterator>
  24. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void
  25. __make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare&& __comp) {
  26. __comp_ref_type<_Compare> __comp_ref = __comp;
  27. using difference_type = typename iterator_traits<_RandomAccessIterator>::difference_type;
  28. difference_type __n = __last - __first;
  29. if (__n > 1) {
  30. // start from the first parent, there is no need to consider children
  31. for (difference_type __start = (__n - 2) / 2; __start >= 0; --__start) {
  32. std::__sift_down<_AlgPolicy>(__first, __comp_ref, __n, __first + __start);
  33. }
  34. }
  35. }
  36. template <class _RandomAccessIterator, class _Compare>
  37. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
  38. make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) {
  39. std::__make_heap<_ClassicAlgPolicy>(std::move(__first), std::move(__last), __comp);
  40. }
  41. template <class _RandomAccessIterator>
  42. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
  43. make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last) {
  44. std::make_heap(std::move(__first), std::move(__last), __less<>());
  45. }
  46. _LIBCPP_END_NAMESPACE_STD
  47. _LIBCPP_POP_MACROS
  48. #endif // _LIBCPP___ALGORITHM_MAKE_HEAP_H