int-interval
A zero-allocation, no_std-friendly half-open interval algebra library for primitive integers.
- Half-open intervals
[start, end) - Branch-light, allocation-free, and
const fnfriendly concrete APIs - Capability traits for generic interval algorithms
- Close to
std::ops::Rangeperformance
Supported types:
U8CO/I8CO, U16CO/I16CO, U32CO/I32CO, U64CO/I64CO, U128CO/I128CO, UsizeCO/IsizeCO
Interval Model
Intervals are represented as [start, end):
start < end_excllen = end_excl - start- Empty intervals are not representable
let x = U8COtry_new.unwrap; // {2, 3, 4}
Core API
Construction:
let x = U8COtry_new.unwrap;
let y = unsafe ;
Accessors and predicates:
x.start;
x.end_excl;
x.end_incl;
x.len;
x.midpoint;
x.contains;
x.contains_interval;
x.intersects;
x.is_adjacent;
x.is_contiguous_with;
x.to_range;
x.iter;
Interval Algebra
The core algebra returns fixed-capacity result containers rather than heap-backed collections:
| Operation | Result |
|---|---|
intersection |
Option<T> |
convex_hull |
T |
between |
Option<T> |
union |
OneTwo<T> |
difference |
ZeroOneTwo<T> |
symmetric_difference |
ZeroOneTwo<T> |
Both result containers implement owned iteration. Their iterators are stack-based and support:
IteratorDoubleEndedIteratorExactSizeIteratorFusedIterator
let lhs = U8COtry_new.unwrap;
let rhs = U8COtry_new.unwrap;
let mut residuals = lhs.difference.into_iter;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
This makes algebra results directly consumable without allocating an intermediate Vec:
for residual in lhs.difference
Generic Programming with Traits
The crate exposes capability traits so downstream algorithms can operate over any supported closed-open integer interval type without selecting a concrete width or signedness up front.
Primitive and Construction Capabilities
| Trait | Capability |
|---|---|
IntPrimitive |
Supported primitive integer coordinate type |
UnsignedPrimitive |
Supported unsigned exact-measure type |
COPrimitive |
Associated coordinate and measure types |
COConstruct |
Checked and unchecked [start, end_excl) construction |
COMidpointConstruct |
Checked and saturating midpoint/length construction |
Observation and Algebra Capabilities
| Trait | Capability |
|---|---|
COBounds |
start, end_excl, end_incl |
COPredicates |
Containment, intersection, adjacency, contiguity |
CORange |
Projection to core::ops::Range and iteration |
COMeasure |
Exact interval length |
COMidpoint |
Midpoint coordinate |
COAlgebra |
Intersection, hull, gap, union, difference, symmetric difference |
Minkowski Capabilities
| Trait | Capability |
|---|---|
COCheckedMinkowskiLinear |
Exact checked add/subtract/translation operations |
COCheckedMinkowskiHull |
Checked multiplication/division interval hulls |
COSaturatingMinkowskiLinear |
Saturating add/subtract/translation operations |
COSaturatingMinkowskiHull |
Saturating multiplication/division interval hulls |
IntCO
IntCO is the compact capability bound for ordinary closed-open integer interval algebra:
It is suitable for downstream interval-set implementations and generic algorithms that need construction, bounds, predicates, algebra, and exact measures, while leaving midpoint, range projection, and Minkowski capabilities opt-in.
When an algorithm needs additional capabilities, extend the bound explicitly:
Concrete interval methods remain the const fn-friendly API surface. The trait layer exists for stable generic programming; if Rust eventually supports the required const trait method model, these two surfaces may be consolidated.
Minkowski Arithmetic
Minkowski APIs distinguish exact linear images from interval hulls of potentially non-contiguous images.
Checked Linear Operations
These operations return None when the exact result cannot be represented:
x.checked_minkowski_add;
x.checked_minkowski_sub;
x.checked_minkowski_add_scalar;
x.checked_minkowski_sub_scalar;
Checked Hull Operations
For discrete integer intervals, multiplication and division may produce non-contiguous point sets. These methods return a containing interval hull:
x.checked_minkowski_mul_hull;
x.checked_minkowski_div_hull;
x.checked_minkowski_mul_scalar_hull;
x.checked_minkowski_div_scalar_hull;
Saturating Linear Operations
These operations clamp endpoint arithmetic to the representable primitive domain, then revalidate the half-open interval:
x.saturating_minkowski_add;
x.saturating_minkowski_sub;
x.saturating_minkowski_add_scalar;
x.saturating_minkowski_sub_scalar;
Saturating Hull Operations
These operations return representable saturated hulls for multiplication and division images:
x.saturating_minkowski_mul_hull;
x.saturating_minkowski_div_hull;
x.saturating_minkowski_mul_scalar_hull;
x.saturating_minkowski_div_scalar_hull;
Example:
let a = I8COtry_new.unwrap;
let b = I8COtry_new.unwrap;
let sum = a.checked_minkowski_add;
let product_hull = a.checked_minkowski_mul_hull;
let saturated_hull = a.saturating_minkowski_mul_hull;
For bounded integer types, saturating results remain constrained by representability under the half-open model.
Midpoint-Based Construction
int-interval supports constructing intervals from a midpoint and an exact unsigned length for every supported interval type.
let x = I16COchecked_from_midpoint_len.unwrap;
assert_eq!;
assert_eq!;
let y = I64COsaturating_from_midpoint_len.unwrap;
assert_eq!;
checked_from_midpoint_len(mid, len)returnsNonewhenlenis zero or the resulting bounds are not representable.saturating_from_midpoint_len(mid, len)clamps endpoint arithmetic to the primitive domain.- Both preserve the half-open
[start, end_excl)invariant.
Features
- Fast primitive interval algebra
- Zero-allocation fixed-capacity algebra results
- Direct iteration over
OneTwo<T>andZeroOneTwo<T> - Public capability traits for generic downstream algorithms
- Explicit checked versus saturating Minkowski semantics
- Suitable for
no_std, embedded, and constrained environments
Good fits include:
- Geometry and raster operations
- Compiler span analysis
- Scheduling ranges
- DNA and sequence slicing
- Numeric algorithms
- Interval-set building blocks
Not intended for large interval sets or tree-based interval queries.
License
MIT OR Apache-2.0