1 unstable release
Uses new Rust 2024
| 0.1.0 | Nov 4, 2025 |
|---|
#1010 in Data structures
Used in elements_rs
29KB
669 lines
Multi Ranged
Efficient data structures for representing and manipulating ranges of discrete values. The crate provides three range types with a unified MultiRanged trait: SimpleRange for contiguous ranges similar to Rust's std::ops::Range but with stable semantics, BiRange for ranges split into two parts, and MultiRange for arbitrary collections of disjoint ranges. All types support incremental insertion, merging, and efficient iteration over their elements. The Step trait abstracts over numeric types that can be used as range boundaries, providing operations for stepping forward and backward with saturating arithmetic.
Usage
Add this to your Cargo.toml:
[dependencies]
multi_ranged = "0.1"
Examples
Simple Range
A contiguous range from start to end:
use multi_ranged::{SimpleRange, MultiRanged};
let mut range = SimpleRange::try_from((0, 10)).unwrap();
assert_eq!(range.len(), 10);
assert!(range.contains(5));
assert!(!range.contains(15));
// Extend the range
range.insert(10).unwrap();
assert_eq!(range.len(), 11);
Multi Range
Multiple disjoint ranges that can be built incrementally:
use multi_ranged::{MultiRange, MultiRanged};
let mut range = MultiRange::try_from([1, 2, 3, 10, 11, 12]).unwrap();
assert!(!range.is_dense()); // Multiple separate ranges
// Insert values that bridge the gap
range.insert(4).unwrap();
range.insert(5).unwrap();
range.insert(6).unwrap();
range.insert(7).unwrap();
range.insert(8).unwrap();
range.insert(9).unwrap();
assert!(range.is_dense()); // Now a single contiguous range
assert_eq!(range.absolute_start(), Some(1));
assert_eq!(range.absolute_end(), Some(13));
Trait Overview
The MultiRanged trait provides a common interface for all range types with methods for insertion, merging, containment checking, and iteration. The Step trait enables generic range operations over any numeric type supporting saturating arithmetic and ordering.
Dependencies
~145KB