Max Monoid
(monoid/max.hpp)
- View this file on GitHub
- Last update: 2026-07-21 20:17:47+09:00
- Include:
#include "monoid/max.hpp"
Overview
A monoid representing the Maximum operation. It is an idempotent monoid, meaning it is compatible with Sparse Tables for $O(1)$ Range Maximum Queries (RMQ).
Template Parameters
-
T: The underlying data type. -
Id: The identity element. Defaults tostd::numeric_limits<T>::lowest().
Properties
-
Operation:
std::max(a, b) -
Identity Element:
Id
Interface and Complexity
This is a stateless algebra tag. Generic data structures use its public
value_type, id(), and op(a, b) members. If the type also provides helpers
such as make(...) or inv(x), they are described above or in the documented
properties.
Each static operation runs in the cost of the underlying operation shown in the
properties. Scalar monoids are $O(1)$; monoids whose value_type stores several
items, permutations, or matrices scale with that stored size.
Verified with
Code
#ifndef M1UNE_MONOID_MAX_HPP
#define M1UNE_MONOID_MAX_HPP 1
#include <algorithm>
#include <limits>
namespace m1une {
namespace monoid {
// Monoid for maximum (Range Maximum).
// The identity element defaults to the lowest possible value of type T, but can be overridden.
template <typename T, T Id = std::numeric_limits<T>::lowest()>
struct Max {
using value_type = T;
static constexpr bool commutative = true;
// Returns the identity element for maximum.
static constexpr T id() {
return Id;
}
// Returns the maximum of a and b.
static constexpr T op(const T& a, const T& b) {
return std::max(a, b);
}
};
} // namespace monoid
} // namespace m1une
#endif // M1UNE_MONOID_MAX_HPP#line 1 "monoid/max.hpp"
#include <algorithm>
#include <limits>
namespace m1une {
namespace monoid {
// Monoid for maximum (Range Maximum).
// The identity element defaults to the lowest possible value of type T, but can be overridden.
template <typename T, T Id = std::numeric_limits<T>::lowest()>
struct Max {
using value_type = T;
static constexpr bool commutative = true;
// Returns the identity element for maximum.
static constexpr T id() {
return Id;
}
// Returns the maximum of a and b.
static constexpr T op(const T& a, const T& b) {
return std::max(a, b);
}
};
} // namespace monoid
} // namespace m1une