min を得る演算のモノイド (utility/monoid/min.cpp)
- category: utility/monoid
-
View this file on GitHub
- Last commit date: 2020-07-11 14:49:45+09:00
Depends on
Required by
- 区間最小値・区間加算用のヘルパークラス (utility/action/add_min.cpp)
- 区間最小値・区間最小値更新用のヘルパークラス (utility/action/min_min.cpp)
Verified with
- test/aoj_DSL_2_A.test.cpp
- test/aoj_DSL_2_H.test.cpp
- test/aoj_DSL_3_D.test.cpp
- test/yj_staticrmq.test.cpp
Code
#ifndef H_min_monoid
#define H_min_monoid
/**
* @brief min を得る演算のモノイド
* @author えびちゃん
*/
#include <algorithm>
#include <utility>
#include "utility/limits.cpp"
template <typename Tp>
class min_monoid {
public:
using value_type = Tp;
private:
value_type M_x = limits<Tp>::max();
public:
min_monoid() = default; // identity
min_monoid(value_type const& x): M_x(x) {}
min_monoid& operator +=(min_monoid const& that) {
M_x = std::min(M_x, that.M_x);
return *this;
}
friend bool operator ==(min_monoid const& lhs, min_monoid const& rhs) {
return lhs.M_x == rhs.M_x;
}
friend min_monoid operator +(min_monoid lhs, min_monoid const& rhs) {
return lhs += rhs;
}
friend bool operator !=(min_monoid const& lhs, min_monoid const& rhs) {
return !(lhs == rhs);
}
value_type const& get() const { return M_x; }
};
#endif /* !defined(H_min_monoid) */
#line 1 "utility/monoid/min.cpp"
/**
* @brief min を得る演算のモノイド
* @author えびちゃん
*/
#include <algorithm>
#include <utility>
#line 1 "utility/limits.cpp"
/**
* @brief 型依存の定数
* @author えびちゃん
*/
#include <limits>
#line 11 "utility/limits.cpp"
template <typename Tp>
class limits: public std::numeric_limits<Tp> {};
template <typename T1, typename T2>
class limits<std::pair<T1, T2>> {
public:
static constexpr auto min() {
return std::make_pair(limits<T1>::min(), limits<T2>::min());
}
static constexpr auto max() {
return std::make_pair(limits<T1>::max(), limits<T2>::max());
}
};
#line 13 "utility/monoid/min.cpp"
template <typename Tp>
class min_monoid {
public:
using value_type = Tp;
private:
value_type M_x = limits<Tp>::max();
public:
min_monoid() = default; // identity
min_monoid(value_type const& x): M_x(x) {}
min_monoid& operator +=(min_monoid const& that) {
M_x = std::min(M_x, that.M_x);
return *this;
}
friend bool operator ==(min_monoid const& lhs, min_monoid const& rhs) {
return lhs.M_x == rhs.M_x;
}
friend min_monoid operator +(min_monoid lhs, min_monoid const& rhs) {
return lhs += rhs;
}
friend bool operator !=(min_monoid const& lhs, min_monoid const& rhs) {
return !(lhs == rhs);
}
value_type const& get() const { return M_x; }
};