expanding things
This commit is contained in:
parent
ff39f36b56
commit
36826b9934
2 changed files with 63 additions and 3 deletions
|
|
@ -1,8 +1,55 @@
|
|||
#ifndef BMATH_HEADER_ONLY_MATH_LIB
|
||||
#define BMATH_HEADER_ONLY_MATH_LIB
|
||||
|
||||
#include <compare>
|
||||
#include <concepts>
|
||||
#include <optional>
|
||||
|
||||
namespace bmath {
|
||||
inline int add(int x, int y) { return x + y; }
|
||||
} // namespace bmath
|
||||
|
||||
template <std::integral IntegralType, typename Modulus = std::nullopt_t>
|
||||
requires(std::integral<Modulus> || std::is_same_v<Modulus, std::nullopt_t>)
|
||||
class mint {
|
||||
public:
|
||||
[[nodiscard]] constexpr explicit mint() : value{} {}
|
||||
[[nodiscard]] constexpr explicit mint(IntegralType _value) : value{_value} {}
|
||||
[[nodiscard]] constexpr explicit operator IntegralType() const noexcept {
|
||||
return value;
|
||||
}
|
||||
|
||||
template <typename OtherIntegralType, typename OtherModulus>
|
||||
[[nodiscard]] constexpr mint operator+(
|
||||
mint<OtherIntegralType, OtherModulus> const& otherMint) const noexcept {
|
||||
return mint{value + otherMint.value};
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr mint& operator-(mint const& other) const noexcept {}
|
||||
[[nodiscard]] constexpr mint& operator*(mint const& other) const noexcept {}
|
||||
[[nodiscard]] constexpr mint& operator/(mint const& other) const noexcept {}
|
||||
[[nodiscard]] constexpr mint& operator%(mint const& other) const noexcept {}
|
||||
|
||||
friend constexpr bool operator==(std::convertible_to<IntegralType> auto a,
|
||||
mint const& b) noexcept {
|
||||
return static_cast<IntegralType>(a) == b.value;
|
||||
}
|
||||
|
||||
friend constexpr bool operator==(
|
||||
mint const& a, std::convertible_to<IntegralType> auto b) noexcept {
|
||||
return a.value == static_cast<IntegralType>(b);
|
||||
}
|
||||
|
||||
template <typename OtherIntegralType, typename OtherModulus>
|
||||
[[nodiscard]] constexpr bool operator==(
|
||||
mint<OtherIntegralType, OtherModulus> const& otherMint) const noexcept {
|
||||
return value == otherMint.value;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr IntegralType get() const { return value; }
|
||||
|
||||
private:
|
||||
IntegralType value{};
|
||||
};
|
||||
|
||||
} // namespace bmath
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -1,8 +1,21 @@
|
|||
#include <cassert>
|
||||
#include <cstdint>
|
||||
|
||||
#include "../include/bmath.hh"
|
||||
|
||||
using namespace bmath;
|
||||
|
||||
int main() {
|
||||
assert(bmath::add(3, 4) == 3 + 3);
|
||||
constexpr uint64_t four{4}, five{5};
|
||||
constexpr mint<uint64_t> mintfour{four};
|
||||
constexpr mint<uint64_t> mintfive{five};
|
||||
|
||||
constexpr auto mintnine = mintfour + mintfive;
|
||||
static_assert(four + five == mintnine.get());
|
||||
static_assert(mintnine == four + five);
|
||||
|
||||
static_assert(4 + 5 == mint<uint64_t>{9});
|
||||
static_assert(mint<uint64_t>{8} == 4 + 3);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue