VCCC  2024.05
VisualCamp Common C++ library
sort_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_SORT_HEAP_HPP_
6 #define VCCC_ALGORITHM_RANGES_SORT_HEAP_HPP_
7 
8 #include <algorithm>
9 #include <type_traits>
10 #include <utility>
11 
17 #include "vccc/__iterator/next.hpp"
21 #include "vccc/__ranges/begin.hpp"
23 #include "vccc/__ranges/end.hpp"
27 
28 namespace vccc {
29 namespace ranges {
30 namespace detail {
31 
32 struct sort_heap_niebloid {
33  template<typename I, typename S, typename Comp = ranges::less, typename Proj = identity, std::enable_if_t<conjunction<
34  random_access_iterator<I>,
35  sentinel_for<S, I>,
36  sortable<I, Comp, Proj>
37  >::value, int> = 0>
38  constexpr I operator()(I first, S last, Comp comp = {}, Proj proj = {}) const {
39  auto last_iter = ranges::next(first, last);
40  for (; !(first == last); --last)
41  ranges::pop_heap(first, last, comp, proj);
42  return last_iter;
43  }
44 
45  template<typename R, typename Comp = ranges::less, typename Proj = identity, std::enable_if_t<conjunction<
46  random_access_range<R>,
47  sortable<iterator_t<R>, Comp, Proj>
48  >::value, int> = 0>
49  constexpr borrowed_iterator_t<R> operator()(R&& r, Comp comp = {}, Proj proj = {}) const {
50  return (*this)(ranges::begin(r), ranges::end(r), std::move(comp), std::move(proj));
51  }
52 };
53 
54 } // namespace detail
55 
58 
59 VCCC_INLINE_OR_STATIC constexpr detail::sort_heap_niebloid sort_heap{};
60 
62 
63 } // namespace ranges
64 } // namespace vccc
65 
66 #endif // VCCC_ALGORITHM_RANGES_SORT_HEAP_HPP_
constexpr VCCC_INLINE_OR_STATIC detail::pop_heap_niebloid pop_heap
Definition: pop_heap.hpp:64
constexpr VCCC_INLINE_OR_STATIC detail::sort_heap_niebloid sort_heap
Definition: sort_heap.hpp:59
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