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>
impl<K, V> RangeMap<K, V>
sourcepub fn get(&self, point: &K) -> Option<(&Range<K>, &V)>
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.
sourcepub fn insert(&mut self, range: Range<K>, value: V)
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.
sourcepub fn remove(&mut self, range: &Range<K>) -> Vec<V>
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.
pub fn is_empty(&self) -> bool
sourcepub fn iter(&self) -> impl Iterator<Item = (&Range<K>, &V)>
pub fn iter(&self) -> impl Iterator<Item = (&Range<K>, &V)>
Iterate over the ranges in the map.
sourcepub fn iter_mut(&mut self) -> impl Iterator<Item = (&Range<K>, &mut V)>
pub fn iter_mut(&mut self) -> impl Iterator<Item = (&Range<K>, &mut V)>
Iterate over the ranges in the map, mut.
sourcepub fn iter_starting_at(
&self,
point: &K,
) -> impl Iterator<Item = (&Range<K>, &V)>
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.
sourcepub fn iter_ending_at(
&self,
point: &K,
) -> impl DoubleEndedIterator<Item = (&Range<K>, &V)>
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.
sourcepub fn intersection<R>(&self, range: R) -> impl Iterator<Item = (&Range<K>, &V)>
pub fn intersection<R>(&self, range: R) -> impl Iterator<Item = (&Range<K>, &V)>
Iterate over the ranges in the map that intersect the requested range.
sourcepub fn last_range(&self) -> Option<Range<K>>
pub fn last_range(&self) -> Option<Range<K>>
Returns the final range in the map.