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