based

Opinionated utility library
git clone git://git.dimitrijedobrota.com/based.git
Log | Files | Refs | README | HACKING | CONTRIBUTING | CODE_OF_CONDUCT | BUILDING |

commite40197a1ba5b00fd5924606f51386efdad65fc6f
parent927ba815575d69920bce3f172b922d917e95cc06
authorDimitrije Dobrota <mail@dimitrijedobrota.com>
dateThu, 27 Mar 2025 13:44:02 +0100

Redifine Integer in terms of operation it supports

Diffstat:
Minclude/based/functional.hpp|++--
Ainclude/based/integer.hpp|++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Minclude/based/type_traits.hpp|++++++++++++++-

3 files changed, 88 insertions(+), 3 deletions(-)


diff --git a/include/based/functional.hpp b/include/based/functional.hpp

@@ -134,8 +134,8 @@ orbit_structure_nonterminating_orbit(const domain_t<F>& x, F f)

template<Transformation F, Integer N>
codomain_t<F> power_unary(codomain_t<F> x, N n, F f)
{
while (n != N {0}) {
n = n - N {1};
while (!zero(n)) {
n = predecesor(n);
x = f(x);
}
return x;

diff --git a/include/based/integer.hpp b/include/based/integer.hpp

@@ -0,0 +1,72 @@

#pragma once
namespace based
{
template<typename T>
T successor(T n)
{
return n + T {1};
}
template<typename T>
T predecesor(T n)
{
return n - T {1};
}
template<typename T>
T twice(T n)
{
return n * T {2};
}
template<typename T>
T half(T n)
{
return n / T {2};
}
template<typename T>
bool positive(T n)
{
return n > T {0};
}
template<typename T>
bool negative(T n)
{
return n < T {0};
}
template<typename T>
bool zero(T n)
{
return n == T {0};
}
template<typename T>
bool one(T n)
{
return n == T {1};
}
template<typename T>
bool two(T n)
{
return n == T {2};
}
template<typename T>
bool even(T n)
{
return n % T{2} == T{0};
}
template<typename T>
bool odd(T n)
{
return n % T{2} != T{0};
}
} // namespace based

diff --git a/include/based/type_traits.hpp b/include/based/type_traits.hpp

@@ -4,11 +4,24 @@

#include <tuple>
#include <type_traits>
#include "based/integer.hpp"
namespace based
{
template<typename T>
concept Integer = std::integral<T>;
concept Integer = requires(T n) {
successor(n);
predecesor(n);
twice(n);
half(n);
positive(n);
negative(n);
zero(n);
one(n);
even(n);
odd(n);
};
template<typename T>
concept Regular = std::regular<T>;