int-interval-stack
Canonical stack-height functions for integer half-open intervals.
int-interval-stack builds an immutable representation of interval overlap multiplicity. Given a collection of half-open integer intervals, it stores the resulting piecewise-constant height function as canonical change points.
[start, end) contributes +1 height on every covered coordinate.
This is useful when a boolean interval set is not enough and the number of overlapping intervals matters.
Model
Input intervals are interpreted as half-open ranges:
[start, end)
The stack height at coordinate x is:
height_at(x) = number of input intervals containing x
Internally, the stack is represented as ordered change points:
ChangePoint { at, height_after }
Each change point means that immediately after at, the active stack height becomes height_after.
The representation is canonical:
- change-point coordinates are strictly increasing;
- adjacent change points always have different heights;
- redundant zero-net boundaries are omitted;
- the final change point restores the height to zero when the stack is non-empty.
Installation
[]
= "0.1"
= "0.9"
Basic usage
use I32CO;
use IntCOStack;
let stack: =
.into_iter
.collect;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
Change points
use I32CO;
use IntCOStack;
let stack: =
.into_iter
.collect;
let points = stack.change_points;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
Iterating covered segments
iter_intervals() returns canonical covered segments and their heights.
use IntCO;
use I32CO;
use IntCOStack;
let stack: =
.into_iter
.collect;
let segments: = stack
.iter_intervals
.map
.collect;
assert_eq!;
Height-filtered iteration
The stack can iterate only segments whose height matches a selected condition.
use IntCO;
use I32CO;
use IntCOStack;
let stack: =
.into_iter
.collect;
let peak: = stack
.peak_intervals
.map
.collect;
assert_eq!;
Available iterators:
stack.iter_intervals;
stack.iter_intervals_at_least;
stack.iter_intervals_at_most;
stack.iter_intervals_exactly;
stack.iter_intervals_between;
stack.peak_intervals;
Predicates
use I32CO;
use IntCOStack;
let stack: =
.into_iter
.collect;
assert!;
assert!;
assert!;
assert!;
assert!;
assert!;
Parallel construction
IntCOStack implements Rayon parallel collection.
use I32CO;
use IntCOStack;
use *;
let intervals = vec!;
let stack: = intervals
.into_par_iter
.collect;
assert_eq!;
Parallel construction is intended for larger inputs. For small collections, sequential construction may be faster due to Rayon scheduling overhead.
Complexity
Let n be the number of input intervals and m be the number of canonical change points.
| Operation | Complexity |
|---|---|
| Build from iterator | O(n log n) dominated by endpoint sorting |
| Build from parallel iterator | parallel endpoint compaction plus reduction |
height_at(x) |
O(log m) |
contains_point(x) |
O(log m) |
max_height() |
O(m) |
iter_intervals() |
O(m) |
intersects_interval(query) |
O(log m + k) |
contains_interval(query) |
O(log m + k) |
Here k is the number of change points scanned inside the query range.
Benchmarks
This crate includes Criterion / CodSpeed benchmarks for construction, point queries, interval predicates, height-filtered iteration, peak iteration, and parallel construction.
Run locally:
Generate the longer report profile:
BENCH_PROFILE=report
License
Licensed under either of:
- MIT license
- Apache License, Version 2.0