These crates provide generic bounded and unbounded intervals with associated set operations.
See the intervalsets and intervalsets-core documentation for details.
The intervalsets-core crate encapsulates functionality for no-alloc environments.
The intervalsets crate builds upon the core functionality to support arbitrary disjoint sets.
todo: link to core/examples and intervalsets/examples
use intervalsets::prelude::*;
let reserved = Interval::closed_open(0, 100)
.union(Interval::closed_open(200, 300))
.union(Interval::closed_open(400, 500));
let requests: Vec<Interval<_>> = vec![
[10, 20].into(),
(150..160).into(),
[200, 210].into(),
(300, 400).into()
];
let (acceptable, rejected): (Vec<_>, Vec<_>) = requests.into_iter()
.partition(|interval| !reserved.intersects(interval));
assert_eq!(acceptable, vec![
Interval::closed_open(150, 160),
Interval::open(300, 400),
]);
assert_eq!(rejected, vec![
Interval::closed(10, 20),
Interval::closed(200, 210),
])The interval! and enum_interval! macros parse a string literal at
macro expansion time. Malformed input — bad syntax, closed bracket on
an unbounded side, crossed numeric-literal bounds — fails to build
instead of panicking at runtime. Bound bodies are tokenized as Rust
expressions, so they're not limited to literals. An optional second
argument supplies a storage-type hint as a turbofish on the
constructor.
use intervalsets::prelude::*;
let half_open: Interval<i32> = interval!("[0, 10)");
let unbounded: Interval<f64> = interval!("(.., ..)");
let n = 5_i32;
let from_expr: Interval<i32> = interval!("[n, n + 10]");
// Storage-type hint resolves inference for forms with no T-bearing arg:
let hinted = interval!("(.., ..)", i32);intervalsets_core::enum_interval! is the no-std / no-alloc analogue.
Both macros share the same grammar as the runtime FromStr impl.
The set! macro is the multi-piece analogue, accepting the Display
form for IntervalSet:
use intervalsets::prelude::*;
let empty: IntervalSet<i32> = set!("{}");
let single: IntervalSet<i32> = set!("{[0, 10]}");
let multi = set!("{[0, 5] U [10, 15] U [20, 30]}", i32);Pieces don't need to be sorted, non-overlapping, or non-empty —
IntervalSet's union machinery normalizes everything to satisfy the
set invariants.