2021-06-17 11:30:11 -04:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef _LIBCPP___ALGORITHM_POP_HEAP_H
|
|
|
|
#define _LIBCPP___ALGORITHM_POP_HEAP_H
|
|
|
|
|
|
|
|
#include <__algorithm/comp.h>
|
|
|
|
#include <__algorithm/comp_ref_type.h>
|
2022-07-17 18:11:51 -07:00
|
|
|
#include <__algorithm/iterator_operations.h>
|
2021-12-29 14:11:46 -05:00
|
|
|
#include <__algorithm/push_heap.h>
|
2021-06-17 11:30:11 -04:00
|
|
|
#include <__algorithm/sift_down.h>
|
2022-07-08 13:46:27 -07:00
|
|
|
#include <__assert>
|
2022-01-07 09:45:05 -05:00
|
|
|
#include <__config>
|
2021-06-17 11:30:11 -04:00
|
|
|
#include <__iterator/iterator_traits.h>
|
[libc++][NFC] Merge is{,_nothrow,_trivially}{,_copy,_move,_default}{_assignable,_constructible} (#85308)
These headers have become very small by using compiler builtins, often
containing only two declarations. This merges these headers, since
there doesn't seem to be much of a benefit keeping them separate.
Specifically, `is_{,_nothrow,_trivially}{assignable,constructible}` are
kept and the `copy`, `move` and `default` versions of these type traits
are moved in to the respective headers.
2024-03-18 08:29:44 +01:00
|
|
|
#include <__type_traits/is_assignable.h>
|
|
|
|
#include <__type_traits/is_constructible.h>
|
2021-12-29 14:11:46 -05:00
|
|
|
#include <__utility/move.h>
|
2021-06-17 11:30:11 -04:00
|
|
|
|
|
|
|
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
|
2022-02-01 20:16:40 -05:00
|
|
|
# pragma GCC system_header
|
2021-06-17 11:30:11 -04:00
|
|
|
#endif
|
|
|
|
|
2023-07-06 17:12:05 +00:00
|
|
|
_LIBCPP_PUSH_MACROS
|
|
|
|
#include <__undef_macros>
|
|
|
|
|
2021-06-17 11:30:11 -04:00
|
|
|
_LIBCPP_BEGIN_NAMESPACE_STD
|
|
|
|
|
2022-07-17 18:11:51 -07:00
|
|
|
template <class _AlgPolicy, class _Compare, class _RandomAccessIterator>
|
2022-08-19 13:08:01 +02:00
|
|
|
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void
|
2022-07-08 13:46:27 -07:00
|
|
|
__pop_heap(_RandomAccessIterator __first,
|
|
|
|
_RandomAccessIterator __last,
|
|
|
|
_Compare& __comp,
|
|
|
|
typename iterator_traits<_RandomAccessIterator>::difference_type __len) {
|
2024-01-05 16:29:23 -08:00
|
|
|
// Calling `pop_heap` on an empty range is undefined behavior, but in practice it will be a no-op.
|
|
|
|
_LIBCPP_ASSERT_PEDANTIC(__len > 0, "The heap given to pop_heap must be non-empty");
|
2021-12-29 14:11:46 -05:00
|
|
|
|
2022-10-01 15:42:00 +02:00
|
|
|
__comp_ref_type<_Compare> __comp_ref = __comp;
|
2022-07-08 13:46:27 -07:00
|
|
|
|
|
|
|
using value_type = typename iterator_traits<_RandomAccessIterator>::value_type;
|
|
|
|
if (__len > 1) {
|
2022-07-17 18:11:51 -07:00
|
|
|
value_type __top = _IterOps<_AlgPolicy>::__iter_move(__first); // create a hole at __first
|
2022-07-30 02:42:05 -07:00
|
|
|
_RandomAccessIterator __hole = std::__floyd_sift_down<_AlgPolicy>(__first, __comp_ref, __len);
|
2022-07-08 13:46:27 -07:00
|
|
|
--__last;
|
|
|
|
|
|
|
|
if (__hole == __last) {
|
|
|
|
*__hole = std::move(__top);
|
|
|
|
} else {
|
2022-07-17 18:11:51 -07:00
|
|
|
*__hole = _IterOps<_AlgPolicy>::__iter_move(__last);
|
2022-07-08 13:46:27 -07:00
|
|
|
++__hole;
|
|
|
|
*__last = std::move(__top);
|
2022-07-30 02:42:05 -07:00
|
|
|
std::__sift_up<_AlgPolicy>(__first, __hole, __comp_ref, __hole - __first);
|
2021-06-17 11:30:11 -04:00
|
|
|
}
|
2022-07-08 13:46:27 -07:00
|
|
|
}
|
2021-06-17 11:30:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class _RandomAccessIterator, class _Compare>
|
2022-08-19 13:08:01 +02:00
|
|
|
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
|
2022-07-08 13:46:27 -07:00
|
|
|
pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) {
|
2022-07-19 20:10:02 -07:00
|
|
|
static_assert(std::is_copy_constructible<_RandomAccessIterator>::value, "Iterators must be copy constructible.");
|
|
|
|
static_assert(std::is_copy_assignable<_RandomAccessIterator>::value, "Iterators must be copy assignable.");
|
|
|
|
|
2022-07-08 13:46:27 -07:00
|
|
|
typename iterator_traits<_RandomAccessIterator>::difference_type __len = __last - __first;
|
2022-07-17 18:11:51 -07:00
|
|
|
std::__pop_heap<_ClassicAlgPolicy>(std::move(__first), std::move(__last), __comp, __len);
|
2021-06-17 11:30:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class _RandomAccessIterator>
|
2022-08-19 13:08:01 +02:00
|
|
|
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
|
2022-07-08 13:46:27 -07:00
|
|
|
pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last) {
|
2023-06-06 13:57:45 -07:00
|
|
|
std::pop_heap(std::move(__first), std::move(__last), __less<>());
|
2021-06-17 11:30:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_END_NAMESPACE_STD
|
|
|
|
|
2023-07-06 17:12:05 +00:00
|
|
|
_LIBCPP_POP_MACROS
|
|
|
|
|
2021-06-17 11:30:11 -04:00
|
|
|
#endif // _LIBCPP___ALGORITHM_POP_HEAP_H
|