Headers move, CMake targets merge, C++ namespaces stay unchanged: - Core: Scope, Lazy, Counted, Flags, Hash, Bits, Uuid, TypeList, TypeMap, Variant - Functional: Pipeline, Either, Result, Retry - Containers: Ring, Pool, PriorityQueue, LRU, Trie, Graph, DisjointSet, Bitset, Interval - Math: +Matrix, Numeric, Random, Histogram - Text: Str, Format, Tokenizer, Diff, Encoding, Semver - Data: Json, Csv, Ini - Async: Coro, Concurrency, EventBus, Observable - Time: +Bench - Log: Log facade + Logging framework (facade now header-only, breaking the Stacktrace<->Log link cycle the merge would have created) - System: FileSystem + Process - Configuration: +Argparse - Unchanged: Stacktrace, Plugin, StateMachine, Testing, TestingMock, Detail Module system fixes: - OPTIONAL_DEPS resolution deferred to void_finalize_modules() so it no longer depends on the alphabetical discovery order of src/ subdirs (Testing -> Text would have been silently dropped). - Every header-only module now has a tracked module.cmake; previously the empty src/<Name> marker dirs were untracked, so a fresh clone would have silently skipped those modules. - VOID_HAS_LOGGING/CONCURRENCY/STR consumers renamed to LOG/ASYNC/TEXT. Tests move to tests/standalone/<Umbrella>/<Feature>.cpp (Result suite files prefixed: ResultBasic etc.). Test count verified identical (831) before and after. README/CLAUDE.md module tables updated. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
184 lines
6.0 KiB
C++
184 lines
6.0 KiB
C++
#pragma once
|
|
|
|
#include <concepts>
|
|
#include <cstdlib>
|
|
#include <expected>
|
|
#include <limits>
|
|
#include <optional>
|
|
#include <string_view>
|
|
#include <type_traits>
|
|
|
|
namespace Void::Numeric
|
|
{
|
|
|
|
// ── Error tags ────────────────────────────────────────────────────────────────
|
|
|
|
enum class Error
|
|
{
|
|
Overflow,
|
|
Underflow,
|
|
DivisionByZero,
|
|
InvalidInput,
|
|
};
|
|
|
|
[[nodiscard]] constexpr std::string_view to_string(Error e) noexcept
|
|
{
|
|
switch (e) {
|
|
case Error::Overflow: return "overflow";
|
|
case Error::Underflow: return "underflow";
|
|
case Error::DivisionByZero: return "division by zero";
|
|
case Error::InvalidInput: return "invalid input";
|
|
}
|
|
return "unknown";
|
|
}
|
|
|
|
// ── Checked arithmetic ────────────────────────────────────────────────────────
|
|
|
|
template <std::integral T>
|
|
[[nodiscard]] constexpr std::expected<T, Error> checked_add(T a, T b) noexcept
|
|
{
|
|
T result;
|
|
if (__builtin_add_overflow(a, b, &result)) {
|
|
return std::unexpected{b > 0 ? Error::Overflow : Error::Underflow};
|
|
}
|
|
return result;
|
|
}
|
|
|
|
template <std::integral T>
|
|
[[nodiscard]] constexpr std::expected<T, Error> checked_sub(T a, T b) noexcept
|
|
{
|
|
T result;
|
|
if (__builtin_sub_overflow(a, b, &result)) {
|
|
return std::unexpected{b > 0 ? Error::Underflow : Error::Overflow};
|
|
}
|
|
return result;
|
|
}
|
|
|
|
template <std::integral T>
|
|
[[nodiscard]] constexpr std::expected<T, Error> checked_mul(T a, T b) noexcept
|
|
{
|
|
T result;
|
|
if (__builtin_mul_overflow(a, b, &result)) {
|
|
return std::unexpected{Error::Overflow};
|
|
}
|
|
return result;
|
|
}
|
|
|
|
template <std::integral T>
|
|
[[nodiscard]] constexpr std::expected<T, Error> checked_div(T a, T b) noexcept
|
|
{
|
|
if (b == T{0}) return std::unexpected{Error::DivisionByZero};
|
|
if constexpr (std::is_signed_v<T>) {
|
|
if (a == std::numeric_limits<T>::min() && b == T{-1}) {
|
|
return std::unexpected{Error::Overflow};
|
|
}
|
|
}
|
|
return a / b;
|
|
}
|
|
|
|
template <std::integral T>
|
|
[[nodiscard]] constexpr std::expected<T, Error>
|
|
checked_neg(T a) noexcept
|
|
{
|
|
if constexpr (std::is_unsigned_v<T>) {
|
|
if (a != T{0}) return std::unexpected{Error::Underflow};
|
|
return T{0};
|
|
} else {
|
|
if (a == std::numeric_limits<T>::min()) return std::unexpected{Error::Overflow};
|
|
return static_cast<T>(-a);
|
|
}
|
|
}
|
|
|
|
// ── Saturating arithmetic (never overflows, clamps to min/max) ───────────────
|
|
|
|
template <std::integral T>
|
|
[[nodiscard]] constexpr T saturating_add(T a, T b) noexcept
|
|
{
|
|
return checked_add(a, b).value_or(
|
|
b > 0 ? std::numeric_limits<T>::max() : std::numeric_limits<T>::min());
|
|
}
|
|
|
|
template <std::integral T>
|
|
[[nodiscard]] constexpr T saturating_sub(T a, T b) noexcept
|
|
{
|
|
return checked_sub(a, b).value_or(
|
|
b > 0 ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max());
|
|
}
|
|
|
|
template <std::integral T>
|
|
[[nodiscard]] constexpr T saturating_mul(T a, T b) noexcept
|
|
{
|
|
return checked_mul(a, b).value_or(
|
|
(a > 0) == (b > 0) ? std::numeric_limits<T>::max()
|
|
: std::numeric_limits<T>::min());
|
|
}
|
|
|
|
// ── Wrapping arithmetic (defined modular wrap) ───────────────────────────────
|
|
|
|
template <std::unsigned_integral T>
|
|
[[nodiscard]] constexpr T wrapping_add(T a, T b) noexcept { return static_cast<T>(a + b); }
|
|
|
|
template <std::unsigned_integral T>
|
|
[[nodiscard]] constexpr T wrapping_sub(T a, T b) noexcept { return static_cast<T>(a - b); }
|
|
|
|
template <std::unsigned_integral T>
|
|
[[nodiscard]] constexpr T wrapping_mul(T a, T b) noexcept { return static_cast<T>(a * b); }
|
|
|
|
// ── Clamping ─────────────────────────────────────────────────────────────────
|
|
|
|
template <typename T>
|
|
requires std::totally_ordered<T>
|
|
[[nodiscard]] constexpr T clamp(T val, T lo, T hi) noexcept
|
|
{
|
|
if (val < lo) return lo;
|
|
if (val > hi) return hi;
|
|
return val;
|
|
}
|
|
|
|
// ── Numeric parsing ───────────────────────────────────────────────────────────
|
|
|
|
[[nodiscard]] inline std::expected<int, Error>
|
|
parse_int(std::string_view s, int base = 10) noexcept
|
|
{
|
|
if (s.empty()) return std::unexpected{Error::InvalidInput};
|
|
char* end = nullptr;
|
|
const long v = std::strtol(s.data(), &end, base);
|
|
if (end == s.data() || end != s.data() + s.size()) {
|
|
return std::unexpected{Error::InvalidInput};
|
|
}
|
|
if (v > std::numeric_limits<int>::max()) return std::unexpected{Error::Overflow};
|
|
if (v < std::numeric_limits<int>::min()) return std::unexpected{Error::Underflow};
|
|
return static_cast<int>(v);
|
|
}
|
|
|
|
[[nodiscard]] inline std::expected<double, Error>
|
|
parse_double(std::string_view s) noexcept
|
|
{
|
|
if (s.empty()) return std::unexpected{Error::InvalidInput};
|
|
char* end = nullptr;
|
|
const double v = std::strtod(s.data(), &end);
|
|
if (end == s.data() || end != s.data() + s.size()) {
|
|
return std::unexpected{Error::InvalidInput};
|
|
}
|
|
return v;
|
|
}
|
|
|
|
// ── Integer traits helpers ────────────────────────────────────────────────────
|
|
|
|
template <std::integral T>
|
|
[[nodiscard]] constexpr bool in_range(long long v) noexcept
|
|
{
|
|
return v >= static_cast<long long>(std::numeric_limits<T>::min()) &&
|
|
v <= static_cast<long long>(std::numeric_limits<T>::max());
|
|
}
|
|
|
|
template <std::integral T>
|
|
[[nodiscard]] constexpr std::optional<T>
|
|
narrow_cast(long long v) noexcept
|
|
{
|
|
if (!in_range<T>(v)) return std::nullopt;
|
|
return static_cast<T>(v);
|
|
}
|
|
|
|
} // namespace Void::Numeric
|