Struct range_map::RangeMap

source ·
pub struct RangeMap<K, V> { /* private fields */ }
Expand description

A map from a range of keys to values.

At any given time, the map contains a set of non-overlapping, non-empty ranges of type K that are associated with values of type V.

A given range can be split into two separate ranges if some of the intermediate values are removed from the map of if another value is inserted over the intermediate values. When that happens, the value for the split range is cloned using the Clone trait.

Adjacent ranges are not merged. Even if the value is “the same” (for some definition of “the same”), the ranges are kept separately.

Querying a point in the map returns not only the value stored at that point but also the range that value occupies in the map.

Implementations§

source§

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

source

pub fn new() -> Self

Returns an empty RangeMap.

source

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

Returns the range (and associated value) that contains the given point, if any.

At most one range and value can exist at a given point because the ranges in the map are non-overlapping.

Empty ranges do not contain any points and therefore cannot be found using this method. Rather than being stored in the map, values associated with empty ranges are dropped.

source

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

Inserts a range with the given value.

The keys included in the given range are now associated with the given value. If those keys were previously associated with another value, are no longer associated with that previous value.

This method can cause one or more values in the map to be dropped if the all of the keys associated with those values are contained within the given range.

If the inserted range is directly adjacent to another range with an equal value, the inserted range will be merged with the adjacent ranges.

source

pub fn remove(&mut self, range: &Range<K>) -> Vec<V>

Remove the given range from the map.

The keys included in the given range are no longer associated with any values.

This method can cause one or more values in the map to be dropped if all of the keys associated with those values are contained within the given range.

Returns any removed values.

source

pub fn is_empty(&self) -> bool

source

pub fn iter(&self) -> impl Iterator<Item = (&Range<K>, &V)>

Iterate over the ranges in the map.

source

pub fn iter_mut(&mut self) -> impl Iterator<Item = (&Range<K>, &mut V)>

Iterate over the ranges in the map, mut.

source

pub fn iter_starting_at( &self, point: &K, ) -> impl Iterator<Item = (&Range<K>, &V)>

Iterate over the ranges in the map, starting at the first range starting after or at the given point.

source

pub fn iter_ending_at( &self, point: &K, ) -> impl DoubleEndedIterator<Item = (&Range<K>, &V)>

Iterate over the ranges in the map, starting at the last range starting before or at the given point.

source

pub fn intersection<R>(&self, range: R) -> impl Iterator<Item = (&Range<K>, &V)>
where R: Borrow<Range<K>>,

Iterate over the ranges in the map that intersect the requested range.

source

pub fn last_range(&self) -> Option<Range<K>>

Returns the final range in the map.

Trait Implementations§

source§

impl<K: Debug, V: Debug> Debug for RangeMap<K, V>

source§

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

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

impl<K, V> Default for RangeMap<K, V>
where K: Ord + Clone, V: Clone + Eq,

source§

fn default() -> Self

By default, a RangeMap is empty.

Auto Trait Implementations§

§

impl<K, V> Freeze for RangeMap<K, V>

§

impl<K, V> RefUnwindSafe for RangeMap<K, V>

§

impl<K, V> Send for RangeMap<K, V>
where V: Send, K: Send,

§

impl<K, V> Sync for RangeMap<K, V>
where V: Sync, K: Sync,

§

impl<K, V> Unpin for RangeMap<K, V>

§

impl<K, V> UnwindSafe for RangeMap<K, V>

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