pub struct RangeInclusiveMap<K, V, StepFnsT = K> { /* private fields */ }
Expand description

A map whose keys are stored as ranges bounded inclusively below and above (start..=end).

Contiguous and overlapping ranges that map to the same value are coalesced into a single range.

Successor and predecessor functions must be provided for the key type K, so that we can detect adjacent but non-overlapping (closed) ranges. (This is not a problem for half-open ranges, because adjacent ranges can be detected using equality of range ends alone.)

You can provide these functions either by implementing the StepLite trait for your key type K, or, if this is impossible because of Rust’s “orphan rules”, you can provide equivalent free functions using the StepFnsT type parameter. StepLite is implemented for all standard integer types, but not for any third party crate types.

Implementations§

source§

impl<K, V, StepFnsT> RangeInclusiveMap<K, V, StepFnsT>

source

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

Gets an iterator over all pairs of key range and value, ordered by key range.

The iterator element type is (&'a RangeInclusive<K>, &'a V).

source§

impl<K, V> RangeInclusiveMap<K, V, K>
where K: Ord + Clone + StepLite, V: Eq + Clone,

source

pub fn new() -> Self

Makes a new empty RangeInclusiveMap.

source§

impl<K, V, StepFnsT> RangeInclusiveMap<K, V, StepFnsT>
where K: Ord + Clone, V: Eq + Clone, StepFnsT: StepFns<K>,

source

pub fn new_with_step_fns() -> Self

Makes a new empty RangeInclusiveMap, specifying successor and predecessor functions defined separately from K itself.

This is useful as a workaround for Rust’s “orphan rules”, which prevent you from implementing StepLite for K if K is a foreign type.

NOTE: This will likely be deprecated and then eventually removed once the standard library’s Step trait is stabilised, as most crates will then likely implement Step for their types where appropriate.

See this issue for details about that stabilization process.

source

pub fn get(&self, key: &K) -> Option<&V>

Returns a reference to the value corresponding to the given key, if the key is covered by any range in the map.

source

pub fn get_key_value(&self, key: &K) -> Option<(&RangeInclusive<K>, &V)>

Returns the range-value pair (as a pair of references) corresponding to the given key, if the key is covered by any range in the map.

source

pub fn contains_key(&self, key: &K) -> bool

Returns true if any range in the map covers the specified key.

source

pub fn clear(&mut self)

Clears the map, removing all elements.

source

pub fn len(&self) -> usize

Returns the number of elements in the map.

source

pub fn is_empty(&self) -> bool

Returns true if the map contains no elements.

source

pub fn insert(&mut self, range: RangeInclusive<K>, value: V)

Insert a pair of key range and value into the map.

If the inserted range partially or completely overlaps any existing range in the map, then the existing range (or ranges) will be partially or completely replaced by the inserted range.

If the inserted range either overlaps or is immediately adjacent any existing range mapping to the same value, then the ranges will be coalesced into a single contiguous range.

§Panics

Panics if range start > end.

source

pub fn remove(&mut self, range: RangeInclusive<K>)

Removes a range from the map, if all or any of it was present.

If the range to be removed partially overlaps any ranges in the map, then those ranges will be contracted to no longer cover the removed range.

§Panics

Panics if range start > end.

source

pub fn gaps<'a>( &'a self, outer_range: &'a RangeInclusive<K> ) -> Gaps<'a, K, V, StepFnsT>

Gets an iterator over all the maximally-sized ranges contained in outer_range that are not covered by any range stored in the map.

The iterator element type is RangeInclusive<K>.

source

pub fn overlapping<R: Borrow<RangeInclusive<K>>>( &self, range: R ) -> Overlapping<'_, K, V, R>

Gets an iterator over all the stored ranges that are either partially or completely overlapped by the given range.

source

pub fn overlaps(&self, range: &RangeInclusive<K>) -> bool

Returns true if any range in the map completely or partially overlaps the given range.

source

pub fn first_range_value(&self) -> Option<(&RangeInclusive<K>, &V)>

Returns the first range-value pair in this map, if one exists. The range in this pair is the minimum range in the map.

source

pub fn last_range_value(&self) -> Option<(&RangeInclusive<K>, &V)>

Returns the last range-value pair in this map, if one exists. The range in this pair is the maximum range in the map.

Trait Implementations§

source§

impl<K: Clone, V: Clone, StepFnsT: Clone> Clone for RangeInclusiveMap<K, V, StepFnsT>

source§

fn clone(&self) -> RangeInclusiveMap<K, V, StepFnsT>

Returns a copy 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<K, V> Debug for RangeInclusiveMap<K, V>
where K: Ord + Clone + StepLite + Debug, V: Eq + Clone + Debug,

source§

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

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

impl<K, V, StepFnsT> Default for RangeInclusiveMap<K, V, StepFnsT>

source§

fn default() -> Self

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

impl<K, V> Extend<(RangeInclusive<K>, V)> for RangeInclusiveMap<K, V>
where K: Ord + Clone + StepLite, V: Eq + Clone,

source§

fn extend<T: IntoIterator<Item = (RangeInclusive<K>, V)>>(&mut self, iter: T)

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<K: Ord + Clone + StepLite, V: Eq + Clone, const N: usize> From<[(RangeInclusive<K>, V); N]> for RangeInclusiveMap<K, V>

source§

fn from(value: [(RangeInclusive<K>, V); N]) -> Self

Converts to this type from the input type.
source§

impl<K, V> FromIterator<(RangeInclusive<K>, V)> for RangeInclusiveMap<K, V>
where K: Ord + Clone + StepLite, V: Eq + Clone,

source§

fn from_iter<T: IntoIterator<Item = (RangeInclusive<K>, V)>>(iter: T) -> Self

Creates a value from an iterator. Read more
source§

impl<K, V, StepFnsT> Hash for RangeInclusiveMap<K, V, StepFnsT>
where K: Hash, V: Hash,

source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl<K, V> IntoIterator for RangeInclusiveMap<K, V>

§

type Item = (RangeInclusive<K>, V)

The type of the elements being iterated over.
§

type IntoIter = IntoIter<K, V>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<K, V, StepFnsT> Ord for RangeInclusiveMap<K, V, StepFnsT>
where K: Ord, V: Ord,

source§

fn cmp(&self, other: &RangeInclusiveMap<K, V, StepFnsT>) -> 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 + PartialOrd,

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

impl<K, V, StepFnsT> PartialEq for RangeInclusiveMap<K, V, StepFnsT>
where K: PartialEq, V: PartialEq,

source§

fn eq(&self, other: &RangeInclusiveMap<K, V, StepFnsT>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<K, V, StepFnsT> PartialOrd for RangeInclusiveMap<K, V, StepFnsT>
where K: PartialOrd, V: PartialOrd,

source§

fn partial_cmp( &self, other: &RangeInclusiveMap<K, V, StepFnsT> ) -> 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

This method 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

This method 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

This method 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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<K, V, StepFnsT> Eq for RangeInclusiveMap<K, V, StepFnsT>
where K: Eq, V: Eq,

Auto Trait Implementations§

§

impl<K, V, StepFnsT> RefUnwindSafe for RangeInclusiveMap<K, V, StepFnsT>
where K: RefUnwindSafe, StepFnsT: RefUnwindSafe, V: RefUnwindSafe,

§

impl<K, V, StepFnsT> Send for RangeInclusiveMap<K, V, StepFnsT>
where K: Send, StepFnsT: Send, V: Send,

§

impl<K, V, StepFnsT> Sync for RangeInclusiveMap<K, V, StepFnsT>
where K: Sync, StepFnsT: Sync, V: Sync,

§

impl<K, V, StepFnsT> Unpin for RangeInclusiveMap<K, V, StepFnsT>
where StepFnsT: Unpin,

§

impl<K, V, StepFnsT> UnwindSafe for RangeInclusiveMap<K, V, StepFnsT>
where K: RefUnwindSafe, StepFnsT: UnwindSafe, V: RefUnwindSafe,

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

§

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

§

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

§

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.