Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:09

0001 //===----------------------------------------------------------------------===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef _LIBCPP___ALGORITHM_POP_HEAP_H
0010 #define _LIBCPP___ALGORITHM_POP_HEAP_H
0011 
0012 #include <__algorithm/comp.h>
0013 #include <__algorithm/comp_ref_type.h>
0014 #include <__algorithm/iterator_operations.h>
0015 #include <__algorithm/push_heap.h>
0016 #include <__algorithm/sift_down.h>
0017 #include <__assert>
0018 #include <__config>
0019 #include <__iterator/iterator_traits.h>
0020 #include <__type_traits/is_assignable.h>
0021 #include <__type_traits/is_constructible.h>
0022 #include <__utility/move.h>
0023 
0024 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0025 #  pragma GCC system_header
0026 #endif
0027 
0028 _LIBCPP_PUSH_MACROS
0029 #include <__undef_macros>
0030 
0031 _LIBCPP_BEGIN_NAMESPACE_STD
0032 
0033 template <class _AlgPolicy, class _Compare, class _RandomAccessIterator>
0034 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void
0035 __pop_heap(_RandomAccessIterator __first,
0036            _RandomAccessIterator __last,
0037            _Compare& __comp,
0038            typename iterator_traits<_RandomAccessIterator>::difference_type __len) {
0039   // Calling `pop_heap` on an empty range is undefined behavior, but in practice it will be a no-op.
0040   _LIBCPP_ASSERT_PEDANTIC(__len > 0, "The heap given to pop_heap must be non-empty");
0041 
0042   __comp_ref_type<_Compare> __comp_ref = __comp;
0043 
0044   using value_type = typename iterator_traits<_RandomAccessIterator>::value_type;
0045   if (__len > 1) {
0046     value_type __top             = _IterOps<_AlgPolicy>::__iter_move(__first); // create a hole at __first
0047     _RandomAccessIterator __hole = std::__floyd_sift_down<_AlgPolicy>(__first, __comp_ref, __len);
0048     --__last;
0049 
0050     if (__hole == __last) {
0051       *__hole = std::move(__top);
0052     } else {
0053       *__hole = _IterOps<_AlgPolicy>::__iter_move(__last);
0054       ++__hole;
0055       *__last = std::move(__top);
0056       std::__sift_up<_AlgPolicy>(__first, __hole, __comp_ref, __hole - __first);
0057     }
0058   }
0059 }
0060 
0061 template <class _RandomAccessIterator, class _Compare>
0062 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
0063 pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) {
0064   static_assert(std::is_copy_constructible<_RandomAccessIterator>::value, "Iterators must be copy constructible.");
0065   static_assert(std::is_copy_assignable<_RandomAccessIterator>::value, "Iterators must be copy assignable.");
0066 
0067   typename iterator_traits<_RandomAccessIterator>::difference_type __len = __last - __first;
0068   std::__pop_heap<_ClassicAlgPolicy>(std::move(__first), std::move(__last), __comp, __len);
0069 }
0070 
0071 template <class _RandomAccessIterator>
0072 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
0073 pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last) {
0074   std::pop_heap(std::move(__first), std::move(__last), __less<>());
0075 }
0076 
0077 _LIBCPP_END_NAMESPACE_STD
0078 
0079 _LIBCPP_POP_MACROS
0080 
0081 #endif // _LIBCPP___ALGORITHM_POP_HEAP_H