di 0.1.0
Loading...
Searching...
No Matches
max.h
Go to the documentation of this file.
1#pragma once
2
10#include "di/meta/operations.h"
13
14namespace di::container {
15namespace detail {
16 struct MaxFunction {
17 template<typename T, typename Proj = function::Identity,
18 concepts::IndirectStrictWeakOrder<meta::Projected<T const*, Proj>> Comp = function::Compare>
19 constexpr auto operator()(T const& a, T const& b, Comp comp = {}, Proj proj = {}) const -> T const& {
20 // This is just how the standard library works.
21 // NOLINTNEXTLINE(bugprone-return-const-ref-from-parameter)
22 return function::invoke(comp, function::invoke(proj, a), function::invoke(proj, b)) >= 0 ? a : b;
23 }
24
25 template<concepts::Copyable T, typename Proj = function::Identity,
26 concepts::IndirectStrictWeakOrder<meta::Projected<T const*, Proj>> Comp = function::Compare>
27 constexpr auto operator()(std::initializer_list<T> list, Comp comp = {}, Proj proj = {}) const -> T {
28 return *max_element(list, util::ref(comp), util::ref(proj));
29 }
30
31 template<concepts::InputContainer Con, typename Proj = function::Identity,
32 concepts::IndirectStrictWeakOrder<meta::Projected<meta::ContainerIterator<Con>, Proj>> Comp =
33 function::Compare>
34 requires(concepts::IndirectlyCopyableStorable<meta::ContainerIterator<Con>, meta::ContainerValue<Con>*>)
35 constexpr auto operator()(Con&& container, Comp comp = {}, Proj proj = {}) const -> meta::ContainerValue<Con> {
36 auto it = container::begin(container);
37 auto ed = container::end(container);
38 auto result = meta::ContainerValue<Con> { *it };
39 while (++it != ed) {
40 if (function::invoke(comp, function::invoke(proj, *it), function::invoke(proj, result)) > 0) {
41 result = *it;
42 }
43 }
44 return result;
45 }
46 };
47}
48
49constexpr inline auto max = detail::MaxFunction {};
50}
51
52namespace di {
53using container::max;
54}
Definition sequence.h:12
constexpr auto max_element
Definition max_element.h:39
constexpr auto end
Definition end.h:55
constexpr auto max
Definition max.h:49
constexpr auto begin
Definition begin.h:52
constexpr auto invoke
Definition invoke.h:100
IteratorValue< ContainerIterator< T > > ContainerValue
Definition container_value.h:8
constexpr auto ref
Definition reference_wrapper.h:98
Definition any_storable.h:9
constexpr auto proj
Definition proj.h:59