一次関数の合成を得る演算のモノイド (utility/monoid/composite.cpp)
- category: utility/monoid
-
View this file on GitHub
- Last commit date: 2020-04-04 02:52:37+09:00
Required by
Verified with
- test/yj_point_set_range_composite.test.cpp
- test/yj_queue_operate_all_composite.test.cpp
- test/yj_range_affine_range_sum.test.cpp
- test/yj_vertex_set_path_composite.test.cpp
Code
/**
* @brief 一次関数の合成を得る演算のモノイド
* @author えびちゃん
*/
#include <algorithm>
#include <utility>
#ifndef H_composite_monoid
#define H_composite_monoid
template <typename Tp>
class composite_monoid {
public:
using value_type = Tp;
private:
value_type M_a = 1;
value_type M_b = 0;
public:
composite_monoid() = default; // identity
composite_monoid(value_type a, value_type b): M_a(a), M_b(b) {};
composite_monoid& operator +=(composite_monoid that) {
M_a *= that.M_a;
M_b *= that.M_a;
M_b += that.M_b;
return *this;
}
composite_monoid operator +(composite_monoid const& that) const {
return composite_monoid(*this) += that;
}
composite_monoid operator +(composite_monoid&& that) const {
return composite_monoid(*this) += std::move(that);
}
bool operator ==(composite_monoid const& that) const {
return (M_a == that.M_a && M_b == that.M_b);
}
bool operator !=(composite_monoid const& that) const { return !(*this == that); }
auto get() const { return std::make_pair(M_a, M_b); }
value_type operator ()(value_type x) const { return M_a * x + M_b; }
};
#endif /* !defined(H_composite_monoid) */
#line 1 "utility/monoid/composite.cpp"
/**
* @brief 一次関数の合成を得る演算のモノイド
* @author えびちゃん
*/
#include <algorithm>
#include <utility>
#ifndef H_composite_monoid
#define H_composite_monoid
template <typename Tp>
class composite_monoid {
public:
using value_type = Tp;
private:
value_type M_a = 1;
value_type M_b = 0;
public:
composite_monoid() = default; // identity
composite_monoid(value_type a, value_type b): M_a(a), M_b(b) {};
composite_monoid& operator +=(composite_monoid that) {
M_a *= that.M_a;
M_b *= that.M_a;
M_b += that.M_b;
return *this;
}
composite_monoid operator +(composite_monoid const& that) const {
return composite_monoid(*this) += that;
}
composite_monoid operator +(composite_monoid&& that) const {
return composite_monoid(*this) += std::move(that);
}
bool operator ==(composite_monoid const& that) const {
return (M_a == that.M_a && M_b == that.M_b);
}
bool operator !=(composite_monoid const& that) const { return !(*this == that); }
auto get() const { return std::make_pair(M_a, M_b); }
value_type operator ()(value_type x) const { return M_a * x + M_b; }
};
#endif /* !defined(H_composite_monoid) */