int-interval
A zero-allocation, half-open interval library for primitive integers.
- Half-open intervals
[start, end) - Branch-light, allocation-free,
const fnfriendly - Close to
std::ops::Rangeperformance
Supported types:
U8CO/I8CO, U16CO/I16CO, U32CO/I32CO, U64CO/I64CO, U128CO/I128CO, UsizeCO/IsizeCO
Interval Model
- Intervals are
[start, end)withstart < end len = end - start- Empty intervals are not representable
let x = U8COtry_new.unwrap; // {2,3,4}
Core API
Construction:
let x = U8COtry_new.unwrap;
let y = U8COnew_unchecked;
Accessors and predicates:
x.start;
x.end_excl;
x.end_incl;
x.len;
x.contains;
x.iter;
x.to_range;
x.intersects;
x.is_adjacent;
x.is_contiguous_with;
Interval Algebra
intersection→Option<T>convex_hull→Tbetween→Option<T>union→OneTwo<T>difference→ZeroOneTwo<T>symmetric_difference→ZeroOneTwo<T>
Fully stack-based, with no heap allocation.
Minkowski Arithmetic
Supported operations:
- Interval-to-interval:
add,sub,mul,div - Interval-to-scalar:
add_n,sub_n,mul_n,div_n
Two overflow policies are provided:
- Checked: returns
Nonewhen the result cannot be represented - Saturating: clamps intermediate boundary arithmetic to the primitive type range, then re-validates the resulting half-open interval
This distinction is explicit in the API:
x.checked_minkowski_add;
x.checked_minkowski_mul_n;
x.saturating_minkowski_add;
x.saturating_minkowski_mul_n;
All Minkowski operations preserve half-open [start, end) semantics and are available as const fn.
let a = I8COtry_new.unwrap;
let b = I8COtry_new.unwrap;
let c = a.checked_minkowski_mul;
let d = a.saturating_minkowski_mul;
For bounded integer types, saturating results are still constrained by representability under the half-open model.
Features
- Fast primitive interval algebra
- Predictable, allocation-free behavior
- Explicit checked vs saturating Minkowski semantics
- Suitable for embedded or constrained environments
Good fits include:
- Geometry / raster operations
- Compiler span analysis
- Scheduling ranges
- DNA / sequence slicing
- Numeric algorithms
Not intended for large interval sets or tree-based interval queries.
License
MIT