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