Struct IntervalSet

Source
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>

Source

pub fn new() -> IntervalSet<T>

Creates an empty IntervalSet

§Examples
let mut set = IntervalSet::new();
assert!(set.insert(0..4).is_ok());
Source

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());
Source

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());
Source

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());
Source

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());
Source

pub fn interval_len(&self) -> usize

Returns the number of intervals in IntervalSet.

§Examples
let mut set = IntervalSet::new();
assert_eq!(set.interval_len(), 0);
assert!(set.insert(0..4).is_ok());
assert_eq!(set.interval_len(), 1);
Source

pub fn capacity(&self) -> usize

Returns the allocated capacity of the IntervalSet.

§Examples
let mut set = IntervalSet::with_capacity(1);
assert_eq!(set.capacity(), 1);
assert!(set.insert(0..4).is_ok());
assert!(set.insert(6..10).is_ok());
assert!(set.capacity() > 1);
Source

pub fn clear(&mut self)

Clears all elements contained in the IntervalSet.

§Examples
let mut set = IntervalSet::new();
assert!(set.insert(0..4).is_ok());
set.clear();
assert!(set.is_empty());
Source

pub fn pop_min(&mut self) -> Option<Interval<T>>

Removes the lowest Interval in the set, if any

§Examples
let mut set = IntervalSet::new();
assert_eq!(set.pop_min(), None);
assert!(set.insert(0..4).is_ok());
assert_eq!(set.pop_min(), Some((0..4).into()));
Source§

impl<T: IntervalBound> IntervalSet<T>

Source

pub fn count(&self) -> usize

Returns the number of elements in IntervalSet.

§Examples
let mut set = IntervalSet::new();
assert_eq!(set.count(), 0);
assert!(set.insert(0..4).is_ok());
assert_eq!(set.count(), 4);
Source

pub fn is_empty(&self) -> bool

Returns true if the IntervalSet has no intervals.

§Examples
let mut set = IntervalSet::new();
assert!(set.is_empty());
assert!(set.insert(0..4).is_ok());
assert!(!set.is_empty());
Source

pub fn insert<R: RangeBounds<T>>( &mut self, interval: R, ) -> Result<(), IntervalSetError>

Inserts the supplied interval into the IntervalSet

§Examples
let mut set = IntervalSet::new();
assert!(set.insert(0..4).is_ok());
assert!(set.contains(&3));
assert!(!set.contains(&5));
Source

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));
Source

pub fn insert_value(&mut self, value: T) -> Result<(), IntervalSetError>

Inserts a single value into the IntervalSet

§Examples
let mut set = IntervalSet::new();
assert!(set.insert_value(1).is_ok());
assert!(set.contains(&1));
assert!(!set.contains(&0));
assert!(!set.contains(&2));
Source

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<_>>());
Source

pub fn remove<R: RangeBounds<T>>( &mut self, interval: R, ) -> Result<(), IntervalSetError>

Removes the supplied interval from the IntervalSet

§Examples
let mut set = IntervalSet::new();
assert!(set.insert(1..3).is_ok());
assert!(set.remove(0..4).is_ok());
assert!(set.is_empty());
Source

pub fn remove_value(&mut self, value: T) -> Result<(), IntervalSetError>

Removes a single value from the IntervalSet

§Examples
let mut set = IntervalSet::new();
assert!(set.insert_value(1).is_ok());
assert!(set.remove_value(1).is_ok());
assert!(!set.contains(&1));
Source

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]);
Source

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]);
Source

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

pub fn iter(&self) -> Iter<'_, T>

Returns an iterator over all of the values contained in the given IntervalSet.

§Examples
let mut set = IntervalSet::new();
assert!(set.insert(0..5).is_ok());
assert!(set.insert(10..15).is_ok());
let items: Vec<_> = set.iter().collect();
assert_eq!(vec![0, 1, 2, 3, 4, 10, 11, 12, 13, 14], items);
Source

pub fn min_value(&self) -> Option<T>

Returns the smallest value in the given IntervalSet. If no items are present in the set, None is returned.

§Examples
let mut set = IntervalSet::new();
assert_eq!(set.min_value(), None);
assert!(set.insert(0..5).is_ok());
assert_eq!(set.min_value(), Some(0));
Source

pub fn max_value(&self) -> Option<T>

Returns the largest value in the given IntervalSet. If no items are present in the set, None is returned.

§Examples
let mut set = IntervalSet::new();
assert_eq!(set.max_value(), None);
assert!(set.insert(0..5).is_ok());
assert_eq!(set.max_value(), Some(4));
Source

pub fn contains(&self, value: &T) -> bool

Returns true if the set contains a value

§Examples
let mut set = IntervalSet::new();
assert_eq!(set.contains(&1), false);
assert!(set.insert(0..5).is_ok());
assert_eq!(set.contains(&1), true);
Source§

impl<T: Copy> IntervalSet<T>

Source

pub fn intervals(&self) -> IntervalIter<'_, T>

Returns an iterator of Intervals contained in the IntervalSet

§Examples
let mut set = IntervalSet::new();
set.insert(0..=10);
assert_eq!(set.intervals().collect::<Vec<_>>(), vec![0..=10]);
Source

pub fn ranges(&self) -> RangeIter<'_, T>

Returns an iterator of Ranges contained in the IntervalSet

§Examples
let mut set = IntervalSet::new();
set.insert(0..=10);
assert_eq!(set.ranges().collect::<Vec<_>>(), vec![0..11]);
Source

pub fn inclusive_ranges(&self) -> RangeInclusiveIter<'_, T>

Returns an iterator of RangeInclusives contained in the IntervalSet

§Examples
let mut set = IntervalSet::new();
set.insert(0..=10);
assert_eq!(set.inclusive_ranges().collect::<Vec<_>>(), vec![0..=10]);

Trait Implementations§

Source§

impl<T: Clone> Clone for IntervalSet<T>

Source§

fn clone(&self) -> IntervalSet<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Copy + Debug> Debug for IntervalSet<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Default for IntervalSet<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

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)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

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)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

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, )

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<T: IntervalBound> Extend<Interval<T>> for IntervalSet<T>

Source§

fn extend<I: IntoIterator<Item = Interval<T>>>(&mut self, intervals: I)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<T: IntervalBound> Extend<Range<T>> for IntervalSet<T>

Source§

fn extend<I: IntoIterator<Item = Range<T>>>(&mut self, intervals: I)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<T: IntervalBound> Extend<RangeInclusive<T>> for IntervalSet<T>

Source§

fn extend<I: IntoIterator<Item = RangeInclusive<T>>>(&mut self, intervals: I)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<T: IntervalBound> From<Interval<T>> for IntervalSet<T>

Source§

fn from(interval: Interval<T>) -> Self

Converts to this type from the input type.
Source§

impl<T: IntervalBound> From<Range<T>> for IntervalSet<T>

Source§

fn from(interval: Range<T>) -> Self

Converts to this type from the input type.
Source§

impl<T: IntervalBound> From<RangeInclusive<T>> for IntervalSet<T>

Source§

fn from(interval: RangeInclusive<T>) -> Self

Converts to this type from the input type.
Source§

impl<'a, T: 'a + IntervalBound> FromIterator<&'a Interval<T>> for IntervalSet<T>

Source§

fn from_iter<I: IntoIterator<Item = &'a Interval<T>>>(intervals: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<'a, T: 'a + IntervalBound> FromIterator<&'a Range<T>> for IntervalSet<T>

Source§

fn from_iter<I: IntoIterator<Item = &'a Range<T>>>(intervals: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<'a, T: 'a + IntervalBound> FromIterator<&'a RangeInclusive<T>> for IntervalSet<T>

Source§

fn from_iter<I: IntoIterator<Item = &'a RangeInclusive<T>>>( intervals: I, ) -> Self

Creates a value from an iterator. Read more
Source§

impl<'a, T: 'a + IntervalBound> FromIterator<&'a T> for IntervalSet<T>

Source§

fn from_iter<I: IntoIterator<Item = &'a T>>(values: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<T: IntervalBound> FromIterator<Interval<T>> for IntervalSet<T>

Source§

fn from_iter<I: IntoIterator<Item = Interval<T>>>(intervals: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<T: IntervalBound> FromIterator<Range<T>> for IntervalSet<T>

Source§

fn from_iter<I: IntoIterator<Item = Range<T>>>(intervals: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<T: IntervalBound> FromIterator<RangeInclusive<T>> for IntervalSet<T>

Source§

fn from_iter<I: IntoIterator<Item = RangeInclusive<T>>>(intervals: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<T: IntervalBound> FromIterator<T> for IntervalSet<T>

Source§

fn from_iter<I: IntoIterator<Item = T>>(values: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<T: Ord> Ord for IntervalSet<T>

Source§

fn cmp(&self, other: &IntervalSet<T>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T: PartialEq> PartialEq for IntervalSet<T>

Source§

fn eq(&self, other: &IntervalSet<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: PartialOrd> PartialOrd for IntervalSet<T>

Source§

fn partial_cmp(&self, other: &IntervalSet<T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T: Eq> Eq for IntervalSet<T>

Source§

impl<T> StructuralPartialEq for IntervalSet<T>

Auto Trait Implementations§

§

impl<T> Freeze for IntervalSet<T>

§

impl<T> RefUnwindSafe for IntervalSet<T>
where T: RefUnwindSafe,

§

impl<T> Send for IntervalSet<T>
where T: Send,

§

impl<T> Sync for IntervalSet<T>
where T: Sync,

§

impl<T> Unpin for IntervalSet<T>
where T: Unpin,

§

impl<T> UnwindSafe for IntervalSet<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T, U> Upcast<T> for U
where T: UpcastFrom<U>,

Source§

fn upcast(self) -> T

Source§

impl<T, B> UpcastFrom<Counter<T, B>> for T

Source§

fn upcast_from(value: Counter<T, B>) -> T