Expand description
A set of values represented using ranges.
A RangeSet is similar to any other kind of set, such as HashSet, with
the difference being that the values in the set are represented using ranges
rather than storing each value individually.
§Invariants
RangeSet enforces the following invariants on the ranges it contains:
- The ranges are sorted.
- The ranges are non-adjacent.
- The ranges are non-intersecting.
- The ranges are non-empty.
This is enforced in the constructor, and guaranteed to hold after applying any operation on a range or set.
§Examples
use rangeset::{
ops::Set,
set::RangeSet,
};
// Create a set from an iterator of unsanitized ranges.
let set = RangeSet::from_iter([0..1, 4..5, 3..4, 0..0, 2..1].into_iter());
assert_eq!(set, RangeSet::from([0..1, 3..5]));
let a = 10..20;
// Difference
let diff: RangeSet<_> = a.difference(15..25).collect();
assert_eq!(diff, RangeSet::from(10..15));
let diff: RangeSet<_> = a.difference(12..15).collect();
assert_eq!(diff, RangeSet::from([10..12, 15..20]));
// Union
let union: RangeSet<_> = a.union(15..25).collect();
assert_eq!(union, RangeSet::from(10..25));
let union: RangeSet<_> = a.union(0..0).collect();
assert_eq!(union, RangeSet::from(10..20));
// Comparison
assert!(a.is_subset(0..30));
assert!(a.is_disjoint(0..10));
assert_eq!(a.clone(), RangeSet::from(a));Structs§
- Into
Range Iter - Iterator over the ranges in
RangeSet. - Range
Iter - Iterator over the ranges in
RangeSet. - Range
Set - Set of values stored as ranges.
- Value
Iter - Iterator over the values in
RangeSet.
Traits§
- ToRange
Set - A type which has a corresponding range set.