VCCC  2024.05
VisualCamp Common C++ library
pop_heap.hpp
Go to the documentation of this file.
1 //
2 // Created by YongGyu Lee on 4/15/24.
3 //
4 
5 #ifndef VCCC_ALGORITHM_RANGES_POP_HEAP_HPP_
6 #define VCCC_ALGORITHM_RANGES_POP_HEAP_HPP_
7 
8 #include <algorithm>
9 #include <type_traits>
10 #include <utility>
11 
16 #include "vccc/__iterator/next.hpp"
20 #include "vccc/__ranges/begin.hpp"
22 #include "vccc/__ranges/end.hpp"
26 
27 namespace vccc {
28 namespace ranges {
29 namespace detail {
30 
31 struct pop_heap_niebloid {
32  template<typename I, typename S, typename Comp = ranges::less, typename Proj = identity, std::enable_if_t<conjunction<
33  random_access_iterator<I>,
34  sentinel_for<S, I>,
35  sortable<I, Comp, Proj>
36  >::value, int> = 0>
37  constexpr I operator()(I first, S last, Comp comp = {}, Proj proj = {}) const {
38  auto last_iter = ranges::next(first, last);
39 
40  std::pop_heap(std::move(first), last_iter, [&](auto&& lhs, auto&& rhs) {
41  return vccc::invoke(
42  comp,
43  vccc::invoke(proj, std::forward<decltype(lhs)>(lhs)),
44  vccc::invoke(proj, std::forward<decltype(rhs)>(rhs))
45  );
46  });
47  return last_iter;
48  }
49 
50  template<typename R, typename Comp = ranges::less, typename Proj = identity, std::enable_if_t<conjunction<
51  random_access_range<R>,
52  sortable<iterator_t<R>, Comp, Proj>
53  >::value, int> = 0>
54  constexpr borrowed_iterator_t<R> operator()(R&& r, Comp comp = {}, Proj proj = {}) const {
55  return (*this)(ranges::begin(r), ranges::end(r), std::move(comp), std::move(proj));
56  }
57 };
58 
59 } // namespace detail
60 
63 
64 VCCC_INLINE_OR_STATIC constexpr detail::pop_heap_niebloid pop_heap{};
65 
67 
68 } // namespace ranges
69 } // namespace vccc
70 
71 #endif // VCCC_ALGORITHM_RANGES_POP_HEAP_HPP_
constexpr VCCC_INLINE_OR_STATIC detail::pop_heap_niebloid pop_heap
Definition: pop_heap.hpp:64
constexpr invoke_result_t< F, Args... > invoke(F &&f, Args &&... args) noexcept(is_nothrow_invocable< F, Args... >::value)
Definition: invoke.hpp:38
constexpr VCCC_INLINE_OR_STATIC detail::next_niebloid next
Definition: next.hpp:65
constexpr VCCC_INLINE_OR_STATIC detail::begin_niebloid begin
returns an iterator to the beginning of a range
Definition: begin.hpp:116
constexpr VCCC_INLINE_OR_STATIC detail::end_niebloid end
returns a sentinel indicating the end of a range
Definition: end.hpp:120
#define VCCC_INLINE_OR_STATIC
Definition: inline_or_static.hpp:9
Definition: directory.h:12
constexpr VCCC_INLINE_OR_STATIC detail::element_niebloid< 0 > first
Definition: key_value.hpp:34
constexpr VCCC_INLINE_OR_STATIC detail::element_niebloid< 1 > value
Definition: key_value.hpp:35