VCCC  2024.05
VisualCamp Common C++ library
max_element.hpp
Go to the documentation of this file.
1 //
2 // Created by YongGyu Lee on 01/08/24.
3 //
4 
5 #ifndef VCCC_ALGORITHM_RANGES_MAX_ELEMENT_HPP
6 #define VCCC_ALGORITHM_RANGES_MAX_ELEMENT_HPP
7 
8 #include <functional>
9 #include <type_traits>
10 
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 max_element_niebloid {
32  template<typename I, typename S, typename Proj = identity, typename Comp = ranges::less, std::enable_if_t<
33  algo_check_unary_forward_iterator<indirect_strict_weak_order, I, S, Proj, Comp>
34  ::value, int> = 0>
35  constexpr I operator()(I first, S last, Comp comp = {}, Proj proj = {}) const {
36  if (first == last)
37  return last;
38 
39  auto it = first;
40  while (++first != last) {
41  if (vccc::invoke(comp, vccc::invoke(proj, *it), vccc::invoke(proj, *first)))
42  it = first;
43  }
44  return it;
45  }
46 
47  template<typename R, typename Proj = identity, typename Comp = less, std::enable_if_t<
48  algo_check_unary_forward_range<indirect_strict_weak_order, R, Proj, Comp>
49  ::value, int> = 0>
50  constexpr borrowed_iterator_t<R>
51  operator()(R&& r, Comp comp = {}, Proj proj = {}) const {
52  return (*this)(ranges::begin(r), ranges::end(r), std::ref(comp), std::ref(proj));
53  }
54 };
55 
56 } // namespace detail
57 
60 
61 VCCC_INLINE_OR_STATIC constexpr detail::max_element_niebloid max_element{};
62 
64 
65 } // namespace ranges
66 } // namespace vccc
67 
68 #endif // VCCC_ALGORITHM_RANGES_MAX_ELEMENT_HPP
constexpr VCCC_INLINE_OR_STATIC detail::max_element_niebloid max_element
Definition: max_element.hpp:61
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::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