ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Algebra.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------------- File Algebra.h ------------------------------*/
3/*--------------------------------------------------------------------------*/
55/*--------------------------------------------------------------------------*/
56/*----------------------------- DEFINITIONS --------------------------------*/
57/*--------------------------------------------------------------------------*/
58
59#ifndef __ADS_Algebra
60 #define __ADS_Algebra
61/* self-identification: #endif constant */
62
63/*--------------------------------------------------------------------------*/
64/*------------------------------ INCLUDES ----------------------------------*/
65/*--------------------------------------------------------------------------*/
66
67#include <concepts>
68#include <cstddef>
69#include <limits>
70#include <type_traits>
71
72/*--------------------------------------------------------------------------*/
73/*--------------------------- NAMESPACE ------------------------------------*/
74/*--------------------------------------------------------------------------*/
75
76namespace ADS
77{
78
79/*--------------------------------------------------------------------------*/
80/*------------------------------ THE CONCEPTS ------------------------------*/
81/*--------------------------------------------------------------------------*/
82
84namespace detail
85{
86
88template< class G >
89constexpr bool commutativeFlag( void )
90{
91 if constexpr( requires {
92 { G::commutative } -> std::convertible_to< bool >;
93 } )
94 return( bool( G::commutative ) );
95 else
96 return( false );
97 }
98
100template< class G >
101constexpr bool selectiveFlag( void )
102{
103 if constexpr( requires {
104 { G::selective } -> std::convertible_to< bool >;
105 } )
106 return( bool( G::selective ) );
107 else
108 return( false );
109 }
110
111} // end( namespace detail )
113
116template< class G >
117concept Monoid = requires( const typename G::Value & a )
118{
119 { G::identity() } -> std::convertible_to< typename G::Value >;
120 { G::combine( a , a ) } -> std::convertible_to< typename G::Value >;
121 };
122
124template< class G >
125concept CommutativeMonoid = Monoid< G > && detail::commutativeFlag< G >();
126
129template< class G >
130concept Group = Monoid< G > && requires( const typename G::Value & a )
131{
132 { G::inverse( a ) } -> std::convertible_to< typename G::Value >;
133 };
134
136template< class G >
138
141template< class G >
142concept Selective = Monoid< G > && detail::selectiveFlag< G >();
143
148template< class A , class G >
149concept Action =
150 Monoid< G > && requires( const typename A::Map & f ,
151 const typename G::Value & a , std::size_t n )
152{
153 { A::id() } -> std::convertible_to< typename A::Map >;
154 { A::compose( f , f ) } -> std::convertible_to< typename A::Map >;
155 { A::apply( f , a , n ) } -> std::convertible_to< typename G::Value >;
156 };
157
160template< class G >
161inline constexpr bool exact_v =
162 ! std::is_floating_point_v< typename G::Value >;
163
164/*--------------------------------------------------------------------------*/
165/*------------------------------ THE ALGEBRAS ------------------------------*/
166/*--------------------------------------------------------------------------*/
167
169template< class T >
170struct SumOf
171{
172 using Value = T;
173 static constexpr bool commutative = true;
175 static constexpr Value identity( void ) { return( T() ); }
177 static constexpr Value combine( const Value & a , const Value & b ) {
178 return( a + b );
179 }
180
181 static constexpr Value inverse( const Value & a ) { return( -a ); }
182 };
183
186template< class T >
187struct MinOf
188{
189 using Value = T;
190 static constexpr bool commutative = true;
191 static constexpr bool selective = true;
193 static constexpr Value identity( void ) {
194 if constexpr( std::numeric_limits< T >::has_infinity )
195 return( std::numeric_limits< T >::infinity() );
196 else
197 return( std::numeric_limits< T >::max() );
198 }
199
200 static constexpr Value combine( const Value & a , const Value & b ) {
201 return( b < a ? b : a );
202 }
203 };
204
207template< class T >
208struct MaxOf
209{
210 using Value = T;
211 static constexpr bool commutative = true;
212 static constexpr bool selective = true;
214 static constexpr Value identity( void ) {
215 if constexpr( std::numeric_limits< T >::has_infinity )
216 return( -std::numeric_limits< T >::infinity() );
217 else
218 return( std::numeric_limits< T >::lowest() );
219 }
220
221 static constexpr Value combine( const Value & a , const Value & b ) {
222 return( a < b ? b : a );
223 }
224 };
225
226/*--------------------------------------------------------------------------*/
227
228} // end( namespace ADS )
229
230/*--------------------------------------------------------------------------*/
231
232#endif /* Algebra.h included */
233
234/*--------------------------------------------------------------------------*/
235/*--------------------------- End File Algebra.h ---------------------------*/
236/*--------------------------------------------------------------------------*/
a group whose operation commutes
Definition Algebra.h:137
a monoid of the maps A::Map acting on the values of the monoid G: the identity map A::id(),...
Definition Algebra.h:149
a monoid whose operation commutes, by the opt-in G::commutative
Definition Algebra.h:125
a monoid with the inverse G::inverse( a ), i.e., combine( inverse( a) , a ) and combine( a ,...
Definition Algebra.h:130
a monoid on G::Value: the identity G::identity() and the associative operation G::combine( a ,...
Definition Algebra.h:117
a monoid whose combine( a , b ) is always or , by the opt-in G::selective
Definition Algebra.h:142
constexpr bool exact_v
whether the values of G are exact, false for the floating point types, whose groups drift by rounding...
Definition Algebra.h:161
Definition ADT.h:96
the selective commutative monoid of the largest value of T, of identity if T has it,...
Definition Algebra.h:209
static constexpr bool selective
one of the two
Definition Algebra.h:212
static constexpr bool commutative
it commutes
Definition Algebra.h:211
static constexpr Value identity(void)
the least value of the type
Definition Algebra.h:214
static constexpr Value combine(const Value &a, const Value &b)
, if they are equivalent
Definition Algebra.h:221
T Value
the maxima
Definition Algebra.h:210
the selective commutative monoid of the least value of T, of identity if T has it,...
Definition Algebra.h:188
static constexpr bool selective
one of the two
Definition Algebra.h:191
static constexpr Value combine(const Value &a, const Value &b)
, if they are equivalent
Definition Algebra.h:200
T Value
the minima
Definition Algebra.h:189
static constexpr Value identity(void)
the largest value of the type
Definition Algebra.h:193
static constexpr bool commutative
it commutes
Definition Algebra.h:190
the abelian group of the sums of T, of identity T()
Definition Algebra.h:171
static constexpr Value combine(const Value &a, const Value &b)
Definition Algebra.h:177
static constexpr bool commutative
Definition Algebra.h:173
static constexpr Value inverse(const Value &a)
Definition Algebra.h:181
static constexpr Value identity(void)
0, the value made by T()
Definition Algebra.h:175
T Value
the sums
Definition Algebra.h:172