VCCC  2024.05
VisualCamp Common C++ library
sort.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_HPP_
6 #define VCCC_ALGORITHM_RANGES_SORT_HPP_
7 
8 #include <algorithm>
9 #include <functional>
10 #include <type_traits>
11 #include <utility>
12 
19 #include "vccc/__iterator/next.hpp"
23 #include "vccc/__ranges/begin.hpp"
25 #include "vccc/__ranges/end.hpp"
29 
30 namespace vccc {
31 namespace ranges {
32 namespace detail {
33 
34 struct sort_niebloid {
35  template<typename I, typename S, typename Comp = ranges::less, typename Proj = identity, std::enable_if_t<conjunction<
36  random_access_iterator<I>,
37  sentinel_for<S, I>,
38  sortable<I, Comp, Proj>
39  >::value, int> = 0>
40  constexpr I operator()(I first, S last, Comp comp = {}, Proj proj = {}) const {
41  if (first == last)
42  return first;
43 
44  auto last_iter = ranges::next(first, last);
45  ranges::make_heap(first, last_iter, std::ref(comp), std::ref(proj));
46  ranges::sort_heap(first, last_iter, std::ref(comp), std::ref(proj));
47 
48  return last_iter;
49  }
50 
51  template<typename R, typename Comp = ranges::less, typename Proj = identity, std::enable_if_t<conjunction<
52  random_access_range<R>,
53  sortable<iterator_t<R>, Comp, Proj>
54  >::value, int> = 0>
55  constexpr borrowed_iterator_t<R> operator()(R&& r, Comp comp = {}, Proj proj = {}) const {
56  return (*this)(ranges::begin(r), ranges::end(r), std::move(comp), std::move(proj));
57  }
58 };
59 
60 } // namespace detail
61 
64 
65 VCCC_INLINE_OR_STATIC constexpr detail::sort_niebloid sort{};
66 
68 
69 } // namespace ranges
70 } // namespace vccc
71 
72 #endif // VCCC_ALGORITHM_RANGES_SORT_HPP_
constexpr VCCC_INLINE_OR_STATIC detail::sort_niebloid sort
Definition: sort.hpp:65
constexpr VCCC_INLINE_OR_STATIC detail::make_heap_niebloid make_heap
Definition: make_heap.hpp:65
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