DDC 0.0.0

a discrete domain computation library

reducer.hpp
1 #pragma once
2 
3 #include <limits>
4 #include <utility>
5 
6 namespace ddc::reducer {
7 
8 template <class T>
9 struct sum
10 {
11  using value_type = T;
12 
13  constexpr value_type operator()(value_type const& lhs, value_type const& rhs) const noexcept
14  {
15  return lhs + rhs;
16  }
17 };
18 
19 template <class T>
20 struct prod
21 {
22  using value_type = T;
23 
24  constexpr value_type operator()(value_type const& lhs, value_type const& rhs) const noexcept
25  {
26  return lhs * rhs;
27  }
28 };
29 
30 struct land
31 {
32  using value_type = bool;
33 
34  constexpr value_type operator()(value_type const& lhs, value_type const& rhs) const noexcept
35  {
36  return lhs && rhs;
37  }
38 };
39 
40 struct lor
41 {
42  using value_type = bool;
43 
44  constexpr value_type operator()(value_type const& lhs, value_type const& rhs) const noexcept
45  {
46  return lhs || rhs;
47  }
48 };
49 
50 template <class T>
51 struct band
52 {
53  using value_type = T;
54 
55  constexpr value_type operator()(value_type const& lhs, value_type const& rhs) const noexcept
56  {
57  return lhs & rhs;
58  }
59 };
60 
61 template <class T>
62 struct bor
63 {
64  using value_type = T;
65 
66  constexpr value_type operator()(value_type const& lhs, value_type const& rhs) const noexcept
67  {
68  return lhs | rhs;
69  }
70 };
71 
72 template <class T>
73 struct bxor
74 {
75  using value_type = T;
76 
77  constexpr value_type operator()(value_type const& lhs, value_type const& rhs) const noexcept
78  {
79  return lhs ^ rhs;
80  }
81 };
82 
83 template <class T>
84 struct min
85 {
86  using value_type = T;
87 
88  constexpr value_type operator()(value_type const& lhs, value_type const& rhs) const noexcept
89  {
90  return lhs < rhs ? lhs : rhs;
91  }
92 };
93 
94 template <class T>
95 struct max
96 {
97  using value_type = T;
98 
99  constexpr value_type operator()(value_type const& lhs, value_type const& rhs) const noexcept
100  {
101  return lhs > rhs ? lhs : rhs;
102  }
103 };
104 
105 template <class T>
106 struct minmax
107 {
108  using value_type = std::pair<T, T>;
109 
110  constexpr value_type operator()(value_type const& lhs, value_type const& rhs) const noexcept
111  {
112  return value_type(
113  lhs.first < rhs.first ? lhs.first : rhs.first,
114  lhs.second > rhs.second ? lhs.second : rhs.second);
115  }
116 };
117 
118 } // namespace ddc::reducer
Definition: reducer.hpp:6
Definition: reducer.hpp:52
constexpr value_type operator()(value_type const &lhs, value_type const &rhs) const noexcept
Definition: reducer.hpp:55
T value_type
Definition: reducer.hpp:53
Definition: reducer.hpp:63
constexpr value_type operator()(value_type const &lhs, value_type const &rhs) const noexcept
Definition: reducer.hpp:66
T value_type
Definition: reducer.hpp:64
Definition: reducer.hpp:74
T value_type
Definition: reducer.hpp:75
constexpr value_type operator()(value_type const &lhs, value_type const &rhs) const noexcept
Definition: reducer.hpp:77
Definition: reducer.hpp:31
bool value_type
Definition: reducer.hpp:32
constexpr value_type operator()(value_type const &lhs, value_type const &rhs) const noexcept
Definition: reducer.hpp:34
Definition: reducer.hpp:41
constexpr value_type operator()(value_type const &lhs, value_type const &rhs) const noexcept
Definition: reducer.hpp:44
bool value_type
Definition: reducer.hpp:42
Definition: reducer.hpp:96
constexpr value_type operator()(value_type const &lhs, value_type const &rhs) const noexcept
Definition: reducer.hpp:99
T value_type
Definition: reducer.hpp:97
Definition: reducer.hpp:85
constexpr value_type operator()(value_type const &lhs, value_type const &rhs) const noexcept
Definition: reducer.hpp:88
T value_type
Definition: reducer.hpp:86
Definition: reducer.hpp:107
constexpr value_type operator()(value_type const &lhs, value_type const &rhs) const noexcept
Definition: reducer.hpp:110
std::pair< T, T > value_type
Definition: reducer.hpp:108
Definition: reducer.hpp:21
constexpr value_type operator()(value_type const &lhs, value_type const &rhs) const noexcept
Definition: reducer.hpp:24
T value_type
Definition: reducer.hpp:22
Definition: reducer.hpp:10
constexpr value_type operator()(value_type const &lhs, value_type const &rhs) const noexcept
Definition: reducer.hpp:13
T value_type
Definition: reducer.hpp:11