pub struct IntervalSet<T> { /* private fields */ }Expand description
IntervalSet is an efficient structure for storing sets of consecutive numbers. Instead
of storing an individual entry per value, only the lower and upper bounds (Interval) are stored.
§Usage
use s2n_quic_transport::interval_set::IntervalSet;
let mut set = IntervalSet::new();
set.insert_value(0);
set.insert_value(1);
set.insert_value(2);
set.insert_value(3);
// because 0 to 3 are consecutive, only a single interval is stored
assert_eq!(set.interval_len(), 1);
set.insert_value(5);
set.insert_value(6);
// 5 and 6 are not consecutive with 0 to 3 so a new entry is created
assert_eq!(set.interval_len(), 2);
set.insert_value(4);
// inserting a 4 merges all of the intervals into a single entry
assert_eq!(set.interval_len(), 1);
// ranges of numbers can be inserted at the same time
set.insert(12..15);
set.insert(18..=21);
assert_eq!(set.interval_len(), 3);
// ranges can also be removed
set.remove(0..22);
assert_eq!(set.interval_len(), 0);Implementations§
Source§impl<T> IntervalSet<T>
impl<T> IntervalSet<T>
Sourcepub fn new() -> IntervalSet<T>
pub fn new() -> IntervalSet<T>
Sourcepub fn with_capacity(capacity: usize) -> IntervalSet<T>
pub fn with_capacity(capacity: usize) -> IntervalSet<T>
Creates an empty IntervalSet with a specific capacity.
This preallocates enough memory for capacity elements,
so that the IntervalSet does not have to be reallocated
until it contains at least that many values.
§Examples
let mut set = IntervalSet::with_capacity(10);
assert!(set.insert(0..4).is_ok());Sourcepub fn with_limit(limit: NonZeroUsize) -> IntervalSet<T>
pub fn with_limit(limit: NonZeroUsize) -> IntervalSet<T>
Creates an empty IntervalSet with a specific limit.
The number of elements in the set cannot exceed this
amount, otherwise insert calls will be rejected.
§Examples
use core::num::NonZeroUsize;
let mut set = IntervalSet::with_limit(NonZeroUsize::new(1).unwrap());
assert!(set.insert(0..4).is_ok());
assert!(set.insert(12..16).is_err());
assert!(set.insert(4..12).is_ok());
assert!(set.insert(12..16).is_ok());Sourcepub fn set_limit(&mut self, limit: NonZeroUsize)
pub fn set_limit(&mut self, limit: NonZeroUsize)
Sets an element limit for the given IntervalSet.
The number of elements in the set cannot exceed this
amount, otherwise insert calls will be rejected
Note: calling this will not drop existing intervals
that exceed the new limit and will only be
applied to later calls to insert.
§Examples
use core::num::NonZeroUsize;
let mut set = IntervalSet::new();
assert!(set.insert(0..4).is_ok());
set.set_limit(NonZeroUsize::new(1).unwrap());
assert!(set.insert(4..8).is_ok());
assert!(set.insert(12..16).is_err());Sourcepub fn remove_limit(&mut self)
pub fn remove_limit(&mut self)
Removes the element limit for the given IntervalSet.
§Examples
use core::num::NonZeroUsize;
let mut set = IntervalSet::with_limit(NonZeroUsize::new(1).unwrap());
assert!(set.insert(0..4).is_ok());
assert!(set.insert(4..8).is_ok());
assert!(set.insert(12..16).is_err());
set.remove_limit();
assert!(set.insert(12..16).is_ok());Sourcepub fn interval_len(&self) -> usize
pub fn interval_len(&self) -> usize
Source§impl<T: IntervalBound> IntervalSet<T>
impl<T: IntervalBound> IntervalSet<T>
Sourcepub fn insert<R: RangeBounds<T>>(
&mut self,
interval: R,
) -> Result<(), IntervalSetError>
pub fn insert<R: RangeBounds<T>>( &mut self, interval: R, ) -> Result<(), IntervalSetError>
Sourcepub fn insert_front<R: RangeBounds<T>>(
&mut self,
interval: R,
) -> Result<(), IntervalSetError>
pub fn insert_front<R: RangeBounds<T>>( &mut self, interval: R, ) -> Result<(), IntervalSetError>
Inserts the supplied interval at the beginning of the IntervalSet.
This method can be used to optimize insertion when the interval is less
than all of the current intervals.
§Examples
let mut set = IntervalSet::new();
assert!(set.insert_front(0..4).is_ok());
assert!(set.contains(&3));
assert!(!set.contains(&5));Sourcepub fn insert_value(&mut self, value: T) -> Result<(), IntervalSetError>
pub fn insert_value(&mut self, value: T) -> Result<(), IntervalSetError>
Sourcepub fn union(&mut self, other: &Self) -> Result<(), IntervalSetError>
pub fn union(&mut self, other: &Self) -> Result<(), IntervalSetError>
Performs a union, i.e., all the values in self or other will
be present in self
§Examples
let mut a = IntervalSet::new();
assert!(a.insert(0..4).is_ok());
let mut b = IntervalSet::new();
assert!(b.insert(4..8).is_ok());
a.union(&b);
assert_eq!(a.iter().collect::<Vec<_>>(), (0..8).collect::<Vec<_>>());Sourcepub fn remove<R: RangeBounds<T>>(
&mut self,
interval: R,
) -> Result<(), IntervalSetError>
pub fn remove<R: RangeBounds<T>>( &mut self, interval: R, ) -> Result<(), IntervalSetError>
Sourcepub fn remove_value(&mut self, value: T) -> Result<(), IntervalSetError>
pub fn remove_value(&mut self, value: T) -> Result<(), IntervalSetError>
Sourcepub fn difference(&mut self, other: &Self) -> Result<(), IntervalSetError>
pub fn difference(&mut self, other: &Self) -> Result<(), IntervalSetError>
Performs a difference, i.e., all the values that are in self but not
in other will be present in self.
§Examples
let mut set_a = IntervalSet::new();
assert!(set_a.insert(0..=10).is_ok());
let mut set_b = IntervalSet::new();
assert!(set_b.insert(4..=8).is_ok());
assert!(set_a.difference(&set_b).is_ok());
assert_eq!(set_a.iter().collect::<Vec<_>>(), vec![0, 1, 2, 3, 9, 10]);Sourcepub fn intersection(&mut self, other: &Self) -> Result<(), IntervalSetError>
pub fn intersection(&mut self, other: &Self) -> Result<(), IntervalSetError>
Performs an intersection, i.e., all the values in both self and other will
be present in self.
§Examples
let mut set_a = IntervalSet::new();
assert!(set_a.insert(0..=10).is_ok());
let mut set_b = IntervalSet::new();
assert!(set_b.insert(4..=8).is_ok());
assert!(set_a.intersection(&set_b).is_ok());
assert_eq!(set_a.iter().collect::<Vec<_>>(), vec![4, 5, 6, 7, 8]);Sourcepub fn intersection_iter<'a>(&'a self, other: &'a Self) -> Intersection<'a, T> ⓘ
pub fn intersection_iter<'a>(&'a self, other: &'a Self) -> Intersection<'a, T> ⓘ
Returns an iterator of Intervals over the intersection, i.e., all
the values in both self and other will be returned.
§Examples
let mut set_a = IntervalSet::new();
assert!(set_a.insert(0..=10).is_ok());
let mut set_b = IntervalSet::new();
assert!(set_b.insert(4..=8).is_ok());
let intersection = set_a.intersection_iter(&set_b).flatten();
assert_eq!(intersection.collect::<Vec<_>>(), vec![4, 5, 6, 7, 8]);Source§impl<T: Copy> IntervalSet<T>
impl<T: Copy> IntervalSet<T>
Sourcepub fn intervals(&self) -> IntervalIter<'_, T> ⓘ
pub fn intervals(&self) -> IntervalIter<'_, T> ⓘ
Sourcepub fn inclusive_ranges(&self) -> RangeInclusiveIter<'_, T> ⓘ
pub fn inclusive_ranges(&self) -> RangeInclusiveIter<'_, T> ⓘ
Trait Implementations§
Source§impl<T: Clone> Clone for IntervalSet<T>
impl<T: Clone> Clone for IntervalSet<T>
Source§fn clone(&self) -> IntervalSet<T>
fn clone(&self) -> IntervalSet<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<T> Default for IntervalSet<T>
impl<T> Default for IntervalSet<T>
Source§impl<'a, T: 'a + IntervalBound> Extend<&'a Interval<T>> for IntervalSet<T>
impl<'a, T: 'a + IntervalBound> Extend<&'a Interval<T>> for IntervalSet<T>
Source§fn extend<I: IntoIterator<Item = &'a Interval<T>>>(&mut self, intervals: I)
fn extend<I: IntoIterator<Item = &'a Interval<T>>>(&mut self, intervals: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<'a, T: 'a + IntervalBound> Extend<&'a Range<T>> for IntervalSet<T>
impl<'a, T: 'a + IntervalBound> Extend<&'a Range<T>> for IntervalSet<T>
Source§fn extend<I: IntoIterator<Item = &'a Range<T>>>(&mut self, intervals: I)
fn extend<I: IntoIterator<Item = &'a Range<T>>>(&mut self, intervals: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<'a, T: 'a + IntervalBound> Extend<&'a RangeInclusive<T>> for IntervalSet<T>
impl<'a, T: 'a + IntervalBound> Extend<&'a RangeInclusive<T>> for IntervalSet<T>
Source§fn extend<I: IntoIterator<Item = &'a RangeInclusive<T>>>(
&mut self,
intervals: I,
)
fn extend<I: IntoIterator<Item = &'a RangeInclusive<T>>>( &mut self, intervals: I, )
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<T: IntervalBound> Extend<Interval<T>> for IntervalSet<T>
impl<T: IntervalBound> Extend<Interval<T>> for IntervalSet<T>
Source§fn extend<I: IntoIterator<Item = Interval<T>>>(&mut self, intervals: I)
fn extend<I: IntoIterator<Item = Interval<T>>>(&mut self, intervals: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<T: IntervalBound> Extend<Range<T>> for IntervalSet<T>
impl<T: IntervalBound> Extend<Range<T>> for IntervalSet<T>
Source§fn extend<I: IntoIterator<Item = Range<T>>>(&mut self, intervals: I)
fn extend<I: IntoIterator<Item = Range<T>>>(&mut self, intervals: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<T: IntervalBound> Extend<RangeInclusive<T>> for IntervalSet<T>
impl<T: IntervalBound> Extend<RangeInclusive<T>> for IntervalSet<T>
Source§fn extend<I: IntoIterator<Item = RangeInclusive<T>>>(&mut self, intervals: I)
fn extend<I: IntoIterator<Item = RangeInclusive<T>>>(&mut self, intervals: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)