Files
Library/include/Void/Containers/Interval.hpp
faumarayandClaude Fable 5 28b94883fd Restructure into umbrella modules (~55 -> 16 targets)
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>
2026-06-12 18:50:02 +03:00

93 lines
3.2 KiB
C++

#pragma once
#include <algorithm>
#include <concepts>
#include <stdexcept>
namespace Void::Interval
{
// ── Interval<T> ───────────────────────────────────────────────────────────────
// Half-open [lo, hi) interval. T must be totally ordered.
template <std::totally_ordered T>
struct Interval
{
T lo{};
T hi{};
// Construct [lo, hi). Throws if lo > hi.
constexpr Interval(T lo_, T hi_) : lo{std::move(lo_)}, hi{std::move(hi_)}
{
if (lo > hi) throw std::invalid_argument{"Interval: lo > hi"};
}
// ── query ─────────────────────────────────────────────────────────────────
[[nodiscard]] constexpr bool contains(const T& v) const noexcept
{
return v >= lo && v < hi;
}
[[nodiscard]] constexpr bool contains_closed(const T& v) const noexcept
{
return v >= lo && v <= hi;
}
[[nodiscard]] constexpr bool empty() const noexcept { return lo >= hi; }
[[nodiscard]] constexpr auto length() const { return hi - lo; }
[[nodiscard]] constexpr bool overlaps(const Interval& o) const noexcept
{
return lo < o.hi && o.lo < hi;
}
[[nodiscard]] constexpr bool adjacent(const Interval& o) const noexcept
{
return hi == o.lo || o.hi == lo;
}
// ── set operations ────────────────────────────────────────────────────────
[[nodiscard]] constexpr Interval hull(const Interval& o) const noexcept
{
return Interval{std::min(lo, o.lo), std::max(hi, o.hi)};
}
// Returns the intersection, or an empty [lo,lo) if disjoint.
[[nodiscard]] constexpr Interval intersection(const Interval& o) const noexcept
{
const T new_lo = std::max(lo, o.lo);
const T new_hi = std::min(hi, o.hi);
if (new_lo >= new_hi) return Interval{lo, lo};
return Interval{new_lo, new_hi};
}
[[nodiscard]] constexpr T clamp(const T& v) const noexcept
{
return std::clamp(v, lo, hi);
}
// ── comparison ────────────────────────────────────────────────────────────
[[nodiscard]] constexpr bool operator==(const Interval&) const noexcept = default;
};
// ── factory helpers ───────────────────────────────────────────────────────────
template <std::totally_ordered T>
[[nodiscard]] constexpr Interval<T> make_interval(T lo, T hi)
{
return Interval<T>{std::move(lo), std::move(hi)};
}
// Closed [lo, hi] interval as [lo, hi+1) — only safe for integer types.
template <std::integral T>
[[nodiscard]] constexpr Interval<T> closed(T lo, T hi)
{
return Interval<T>{lo, static_cast<T>(hi + 1)};
}
} // namespace Void::Interval