m1une's library

This documentation is automatically generated by online-judge-tools/verification-helper

View on GitHub

:question: monoid/monoids/affine_monoid.hpp

Depends on

Required by

Verified with

Code

#ifndef M1UNE_AFFINE_MONOID_HPP
#define M1UNE_AFFINE_MONOID_HPP 1

#include <utility>

#include "../monoid.hpp"

namespace m1une {

// Affine transformation f(x) = ax + b is represented as (a, b)
// perform f first, then g
// op(f, g)(x) = g(f(x))
template <typename T>
using affine_monoid = monoid<std::pair<T, T>,
                             [](std::pair<T, T> f, std::pair<T, T> g) {
                                 return std::pair<T, T>(f.first * g.first, f.second * g.first + g.second);
                             },
                             []() { return std::pair<T, T>(1, 0); }, false>;

}  // namespace m1une

#endif  // M1UNE_AFFINE_MONOID_HPP
#line 1 "monoid/monoids/affine_monoid.hpp"



#include <utility>

#line 1 "monoid/monoid.hpp"



#include <concepts>
#include <functional>
#include <type_traits>

namespace m1une {

template <typename T, auto operation, auto identity, bool commutative>
struct monoid {
    static_assert(std::is_invocable_r_v<T, decltype(operation), T, T>, "operation must work as T(T, T)");
    static_assert(std::is_invocable_r_v<T, decltype(identity)>, "identity must work as T()");

    using value_type = T;
    static constexpr auto op = operation;
    static constexpr auto id = identity;
    static constexpr bool is_commutative = commutative;
};

template <typename T>
concept Monoid = requires(typename T::value_type v) {
    typename T::value_type;
    { T::op(v, v) } -> std::same_as<typename T::value_type>;
    { T::id() } -> std::same_as<typename T::value_type>;
    { T::is_commutative } -> std::convertible_to<bool>;
};

}  // namespace m1une


#line 7 "monoid/monoids/affine_monoid.hpp"

namespace m1une {

// Affine transformation f(x) = ax + b is represented as (a, b)
// perform f first, then g
// op(f, g)(x) = g(f(x))
template <typename T>
using affine_monoid = monoid<std::pair<T, T>,
                             [](std::pair<T, T> f, std::pair<T, T> g) {
                                 return std::pair<T, T>(f.first * g.first, f.second * g.first + g.second);
                             },
                             []() { return std::pair<T, T>(1, 0); }, false>;

}  // namespace m1une
Back to top page