pub struct NoditMap<I, K, V> { /* private fields */ }
Expand description
An ordered map of non-overlapping intervals based on BTreeMap
.
I
is the generic type parameter for the Ord
type the K
type is a interval over.
K
is the generic type parameter for the interval type stored as the
keys in the map.
V
is the generic type parameter for the values associated with the
keys in the map.
Phrasing it another way: I
is the point type, K
is the interval type, and V
is the value type.
§Examples
use nodit::interval::ie;
use nodit::NoditMap;
// Make a map of intervals to booleans
let mut map = NoditMap::from_slice_strict([
(ie(4, 8), false),
(ie(8, 18), true),
(ie(20, 100), false),
])
.unwrap();
// Change a value in the map
*map.get_at_point_mut(7).unwrap() = true;
if map.contains_point(99) {
println!("Map contains value at 99 :)");
}
// Iterate over the entries in the map
for (interval, value) in map.iter() {
println!("{interval:?}, {value:?}");
}
Implementations§
source§impl<I, K, V> NoditMap<I, K, V>where
I: PointType,
K: IntervalType<I>,
impl<I, K, V> NoditMap<I, K, V>where
I: PointType,
K: IntervalType<I>,
sourcepub fn overlaps<Q>(&self, interval: Q) -> boolwhere
Q: IntervalType<I>,
pub fn overlaps<Q>(&self, interval: Q) -> boolwhere
Q: IntervalType<I>,
Returns true
if the given interval overlaps any of the
intervals in the map, and false
if not.
§Panics
Panics if the given interval is an invalid interval. See Invalid Intervals
for more details.
§Examples
use nodit::interval::{ie, ii};
use nodit::NoditMap;
let mut map = NoditMap::new();
map.insert_strict(ie(5, 10), false);
assert_eq!(map.overlaps(ii(1, 3)), false);
assert_eq!(map.overlaps(ie(4, 5)), false);
assert_eq!(map.overlaps(ii(4, 5)), true);
assert_eq!(map.overlaps(ie(4, 6)), true);
sourcepub fn overlapping<Q>(
&self,
interval: Q
) -> impl DoubleEndedIterator<Item = (&K, &V)>where
Q: IntervalType<I>,
pub fn overlapping<Q>(
&self,
interval: Q
) -> impl DoubleEndedIterator<Item = (&K, &V)>where
Q: IntervalType<I>,
Returns an iterator over every entry in the map that overlaps the given interval in ascending order.
§Panics
Panics if the given interval is an invalid interval. See Invalid Intervals
for more details.
§Examples
use nodit::interval::ie;
use nodit::NoditMap;
let map = NoditMap::from_slice_strict([
(ie(1, 4), false),
(ie(4, 8), true),
(ie(8, 100), false),
])
.unwrap();
let mut overlapping = map.overlapping(ie(2, 8));
assert_eq!(
overlapping.collect::<Vec<_>>(),
[(&ie(1, 4), &false), (&ie(4, 8), &true)]
);
sourcepub fn overlapping_mut<Q>(
&mut self,
interval: Q
) -> impl DoubleEndedIterator<Item = (&K, &mut V)>where
Q: IntervalType<I>,
pub fn overlapping_mut<Q>(
&mut self,
interval: Q
) -> impl DoubleEndedIterator<Item = (&K, &mut V)>where
Q: IntervalType<I>,
Returns an mutable iterator over every entry in the map that overlaps the given interval in ascending order.
§Panics
Panics if the given interval is an invalid interval. See Invalid Intervals
for more details.
§Examples
use nodit::interval::ie;
use nodit::NoditMap;
let mut map = NoditMap::from_slice_strict([
(ie(1, 4), false),
(ie(4, 8), true),
(ie(8, 100), false),
])
.unwrap();
for (interval, value) in map.overlapping_mut(ie(3, 7)) {
if *interval == ie(4, 8) {
*value = false
} else {
*value = true
}
}
sourcepub fn get_at_point(&self, point: I) -> Option<&V>
pub fn get_at_point(&self, point: I) -> Option<&V>
Returns a reference to the value corresponding to the interval in the map that overlaps the given point, if any.
§Examples
use nodit::interval::ie;
use nodit::NoditMap;
let map = NoditMap::from_slice_strict([
(ie(1, 4), false),
(ie(4, 8), true),
(ie(8, 100), false),
])
.unwrap();
assert_eq!(map.get_at_point(3), Some(&false));
assert_eq!(map.get_at_point(4), Some(&true));
assert_eq!(map.get_at_point(101), None);
sourcepub fn get_at_point_mut(&mut self, point: I) -> Option<&mut V>
pub fn get_at_point_mut(&mut self, point: I) -> Option<&mut V>
Returns a mutable reference to the value corresponding to the interval that overlaps the given point, if any.
§Examples
use nodit::interval::ie;
use nodit::NoditMap;
let mut map =
NoditMap::from_slice_strict([(ie(1, 4), false)]).unwrap();
if let Some(x) = map.get_at_point_mut(2) {
*x = true;
}
assert_eq!(map.get_at_point(1), Some(&true));
sourcepub fn contains_point(&self, point: I) -> bool
pub fn contains_point(&self, point: I) -> bool
Returns true
if the map contains a interval that overlaps the
given point, and false
if not.
§Examples
use nodit::interval::ie;
use nodit::NoditMap;
let map = NoditMap::from_slice_strict([
(ie(1, 4), false),
(ie(4, 8), true),
(ie(8, 100), false),
])
.unwrap();
assert_eq!(map.contains_point(3), true);
assert_eq!(map.contains_point(4), true);
assert_eq!(map.contains_point(101), false);
sourcepub fn get_key_value_at_point(&self, point: I) -> Result<(&K, &V), K>
pub fn get_key_value_at_point(&self, point: I) -> Result<(&K, &V), K>
Returns the entry corresponding to the interval that overlaps the given point, if any.
If there is no interval that overlaps the given point the maximally-sized gap at the given point is returned.
§Examples
use nodit::interval::{ie, iu};
use nodit::NoditMap;
let map = NoditMap::from_slice_strict([
(ie(1, 4), false),
(ie(4, 6), true),
(ie(8, 100), false),
])
.unwrap();
assert_eq!(
map.get_key_value_at_point(3),
Ok((&ie(1, 4), &false))
);
assert_eq!(map.get_key_value_at_point(5), Ok((&ie(4, 6), &true)));
assert_eq!(map.get_key_value_at_point(7), Err(ie(6, 8)));
assert_eq!(map.get_key_value_at_point(101), Err(iu(100)));
sourcepub fn remove_overlapping<'a, Q>(
&'a mut self,
interval: Q
) -> impl Iterator<Item = (K, V)>where
Q: IntervalType<I> + 'a,
pub fn remove_overlapping<'a, Q>(
&'a mut self,
interval: Q
) -> impl Iterator<Item = (K, V)>where
Q: IntervalType<I> + 'a,
Removes every entry in the map which overlaps the given interval and returns them in an iterator in ascending order.
§Panics
Panics if the given interval is an invalid interval. See Invalid Intervals
for more details.
§Examples
use nodit::interval::ie;
use nodit::NoditMap;
let mut map = NoditMap::from_slice_strict([
(ie(1, 4), false),
(ie(4, 8), true),
(ie(8, 100), false),
])
.unwrap();
let mut removed = map.remove_overlapping(ie(2, 8));
assert_eq!(
removed.collect::<Vec<_>>(),
[(ie(1, 4), false), (ie(4, 8), true)]
);
assert_eq!(
map.into_iter().collect::<Vec<_>>(),
[(ie(8, 100), false)]
);
sourcepub fn cut<'a, Q>(&'a mut self, interval: Q) -> impl Iterator<Item = (K, V)>where
Q: IntervalType<I> + 'a,
V: Clone,
pub fn cut<'a, Q>(&'a mut self, interval: Q) -> impl Iterator<Item = (K, V)>where
Q: IntervalType<I> + 'a,
V: Clone,
Cuts a given interval out of the map and returns an iterator of the full or partial intervals with their values that were cut in ascending order.
V
must implement Clone
as if you try to cut out the center
of a interval in the map it will split into two different entries
using Clone
. Or if you partially cut a interval then
V
must be cloned to be returned in the iterator.
§Panics
Panics if the given interval is an invalid interval. See Invalid Intervals
for more details.
§Examples
use nodit::interval::{ie, ii};
use nodit::NoditMap;
let mut base = NoditMap::from_slice_strict([
(ie(1, 4), false),
(ie(4, 8), true),
(ie(8, 100), false),
])
.unwrap();
let after_cut = NoditMap::from_slice_strict([
(ie(1, 2), false),
(ie(40, 100), false),
])
.unwrap();
assert_eq!(
base.cut(ie(2, 40)).collect::<Vec<_>>(),
[(ie(2, 4), false), (ie(4, 8), true), (ie(8, 40), false),]
);
assert_eq!(base, after_cut);
sourcepub fn gaps_untrimmed<'a, Q>(
&'a self,
interval: Q
) -> impl Iterator<Item = K> + '_where
Q: IntervalType<I> + 'a,
pub fn gaps_untrimmed<'a, Q>(
&'a self,
interval: Q
) -> impl Iterator<Item = K> + '_where
Q: IntervalType<I> + 'a,
Returns an iterator of all the gaps in the map that overlap the given
interval
in ascending order.
See NoditMap::gaps_trimmed()
if you require the returned
gaps to be trimmed to be fully contained within given interval
.
§Panics
Panics if the given interval is an invalid interval. See Invalid Intervals
for more details.
§Examples
use nodit::interval::{ie, ii, iu};
use nodit::NoditMap;
let map = NoditMap::from_slice_strict([
(ie(1, 3), false),
(ie(5, 7), true),
(ie(9, 100), false),
])
.unwrap();
let mut gaps = map.gaps_untrimmed(ii(4, 120));
assert_eq!(
gaps.collect::<Vec<_>>(),
[ie(3, 5), ie(7, 9), iu(100)]
);
sourcepub fn gaps_trimmed<'a, Q>(
&'a self,
interval: Q
) -> impl Iterator<Item = K> + '_where
Q: IntervalType<I> + 'a,
pub fn gaps_trimmed<'a, Q>(
&'a self,
interval: Q
) -> impl Iterator<Item = K> + '_where
Q: IntervalType<I> + 'a,
Returns an iterator of all the gaps in the map that overlap the given
interval
that are also trimmed so they are all fully contained within the
given interval
, in ascending order.
See NoditMap::gaps_untrimmed()
if you do not want the
returned gaps to be trimmed.
§Panics
Panics if the given interval is an invalid interval. See Invalid Intervals
for more details.
§Examples
use nodit::interval::{ie, ii, iu};
use nodit::NoditMap;
let map = NoditMap::from_slice_strict([
(ie(1, 3), false),
(ie(5, 7), true),
(ie(9, 100), false),
])
.unwrap();
let mut gaps = map.gaps_trimmed(ii(4, 120));
assert_eq!(
gaps.collect::<Vec<_>>(),
[ie(4, 5), ie(7, 9), ii(100, 120)]
);
sourcepub fn contains_interval<Q>(&self, interval: Q) -> boolwhere
Q: IntervalType<I>,
pub fn contains_interval<Q>(&self, interval: Q) -> boolwhere
Q: IntervalType<I>,
Returns true
if the map covers every point in the given
interval, and false
if it does not.
§Panics
Panics if the given interval is an invalid interval. See Invalid Intervals
for more details.
§Examples
use nodit::interval::ie;
use nodit::NoditMap;
let map = NoditMap::from_slice_strict([
(ie(1, 3), false),
(ie(5, 8), true),
(ie(8, 100), false),
])
.unwrap();
assert_eq!(map.contains_interval(ie(1, 3)), true);
assert_eq!(map.contains_interval(ie(2, 6)), false);
assert_eq!(map.contains_interval(ie(6, 100)), true);
sourcepub fn insert_strict(
&mut self,
interval: K,
value: V
) -> Result<(), OverlapError<V>>
pub fn insert_strict( &mut self, interval: K, value: V ) -> Result<(), OverlapError<V>>
Adds a new entry to the map without modifying other entries.
If the given interval overlaps one or more intervals already in the
map, then an OverlapError
is returned and the map is not
updated.
§Panics
Panics if the given interval is an invalid interval. See Invalid Intervals
for more details.
§Examples
use nodit::interval::ie;
use nodit::{NoditMap, OverlapError};
let mut map = NoditMap::new();
assert_eq!(map.insert_strict(ie(5, 10), 9), Ok(()));
assert_eq!(
map.insert_strict(ie(5, 10), 2),
Err(OverlapError { value: 2 })
);
assert_eq!(map.len(), 1);
sourcepub fn insert_merge_touching(
&mut self,
interval: K,
value: V
) -> Result<K, OverlapError<V>>
pub fn insert_merge_touching( &mut self, interval: K, value: V ) -> Result<K, OverlapError<V>>
Adds a new entry to the map and merges into other intervals in the map which touch it.
The value of the merged-together interval is set to the value given for this insertion.
If successful then the newly inserted (possibly merged) interval is returned.
If the given interval overlaps one or more intervals already in the
map, then an OverlapError
is returned and the map is not
updated.
§Panics
Panics if the given interval is an invalid interval. See Invalid Intervals
for more details.
§Examples
use nodit::interval::ie;
use nodit::{NoditMap, OverlapError};
let mut map = NoditMap::from_slice_strict([
(ie(1, 4), false),
(ie(6, 8), true),
])
.unwrap();
// Touching
assert_eq!(
map.insert_merge_touching(ie(4, 6), true),
Ok(ie(1, 8))
);
// Overlapping
assert_eq!(
map.insert_merge_touching(ie(4, 8), false),
Err(OverlapError { value: false }),
);
// Neither Touching or Overlapping
assert_eq!(
map.insert_merge_touching(ie(10, 16), false),
Ok(ie(10, 16))
);
assert_eq!(
map.into_iter().collect::<Vec<_>>(),
[(ie(1, 8), true), (ie(10, 16), false)]
);
sourcepub fn insert_merge_touching_if_values_equal(
&mut self,
interval: K,
value: V
) -> Result<K, OverlapError<V>>where
V: Eq,
pub fn insert_merge_touching_if_values_equal(
&mut self,
interval: K,
value: V
) -> Result<K, OverlapError<V>>where
V: Eq,
Adds a new entry to the map and merges into other intervals in the map which touch it if the touching intervals’ values are equal to the value being inserted.
If successful then the newly inserted (possibly merged) interval is returned.
If the given interval overlaps one or more intervals already in the
map, then an OverlapError
is returned and the map is not
updated.
§Panics
Panics if the given interval is an invalid interval. See Invalid Intervals
for more details.
§Examples
use nodit::interval::ie;
use nodit::{NoditMap, OverlapError};
let mut map = NoditMap::from_slice_strict([
(ie(1, 4), false),
(ie(6, 8), true),
])
.unwrap();
// Touching
assert_eq!(
map.insert_merge_touching_if_values_equal(ie(4, 6), true),
Ok(ie(4, 8))
);
// Overlapping
assert_eq!(
map.insert_merge_touching_if_values_equal(ie(4, 8), false),
Err(OverlapError { value: false }),
);
// Neither Touching or Overlapping
assert_eq!(
map.insert_merge_touching_if_values_equal(ie(10, 16), false),
Ok(ie(10, 16))
);
assert_eq!(
map.into_iter().collect::<Vec<_>>(),
[(ie(1, 4), false), (ie(4, 8), true), (ie(10, 16), false)]
);
sourcepub fn insert_merge_overlapping(&mut self, interval: K, value: V) -> K
pub fn insert_merge_overlapping(&mut self, interval: K, value: V) -> K
Adds a new entry to the map and merges into other intervals in the map which overlap it.
The value of the merged-together interval is set to the value given for this insertion.
The newly inserted (possibly merged) interval is returned.
§Panics
Panics if the given interval is an invalid interval. See Invalid Intervals
for more details.
§Examples
use nodit::interval::ie;
use nodit::{NoditMap, OverlapError};
let mut map = NoditMap::from_slice_strict([
(ie(1, 4), false),
(ie(6, 8), true),
])
.unwrap();
// Touching
assert_eq!(
map.insert_merge_overlapping(ie(4, 6), true),
ie(4, 6)
);
// Overlapping
assert_eq!(
map.insert_merge_overlapping(ie(4, 8), false),
ie(4, 8)
);
// Neither Touching or Overlapping
assert_eq!(
map.insert_merge_overlapping(ie(10, 16), false),
ie(10, 16)
);
assert_eq!(
map.into_iter().collect::<Vec<_>>(),
[(ie(1, 4), false), (ie(4, 8), false), (ie(10, 16), false)]
);
sourcepub fn insert_merge_touching_or_overlapping(
&mut self,
interval: K,
value: V
) -> K
pub fn insert_merge_touching_or_overlapping( &mut self, interval: K, value: V ) -> K
Adds a new entry to the map and merges into other intervals in the map which touch or overlap it.
The value of the merged-together interval is set to the value given for this insertion.
The newly inserted (possibly merged) interval is returned.
§Panics
Panics if the given interval is an invalid interval. See Invalid Intervals
for more details.
§Examples
use nodit::interval::ie;
use nodit::{NoditMap, OverlapError};
let mut map = NoditMap::from_slice_strict([
(ie(1, 4), false),
(ie(6, 8), true),
])
.unwrap();
// Touching
assert_eq!(
map.insert_merge_touching_or_overlapping(ie(4, 6), true),
ie(1, 8)
);
// Overlapping
assert_eq!(
map.insert_merge_touching_or_overlapping(ie(4, 8), false),
ie(1, 8)
);
// Neither Touching or Overlapping
assert_eq!(
map.insert_merge_touching_or_overlapping(ie(10, 16), false),
ie(10, 16)
);
assert_eq!(
map.into_iter().collect::<Vec<_>>(),
[(ie(1, 8), false), (ie(10, 16), false)]
);
sourcepub fn insert_overwrite(
&mut self,
interval: K,
value: V
) -> impl Iterator<Item = (K, V)>where
V: Clone,
pub fn insert_overwrite(
&mut self,
interval: K,
value: V
) -> impl Iterator<Item = (K, V)>where
V: Clone,
Adds a new entry to the map and overwrites any other intervals that overlap the new interval.
Returns an iterator over the full or partial cut entries in ascending order.
This is equivalent to using NoditMap::cut()
followed by NoditMap::insert_strict()
. Hence the
same V: Clone
trait bound applies.
§Panics
Panics if the given interval is an invalid interval. See Invalid Intervals
for more details.
§Examples
use nodit::interval::ie;
use nodit::NoditMap;
let mut map =
NoditMap::from_slice_strict([(ie(2, 8), false)]).unwrap();
map.insert_overwrite(ie(4, 6), true);
assert_eq!(
map.into_iter().collect::<Vec<_>>(),
[(ie(2, 4), false), (ie(4, 6), true), (ie(6, 8), false)]
);
sourcepub fn from_slice_strict<const N: usize>(
slice: [(K, V); N]
) -> Result<NoditMap<I, K, V>, OverlapError<V>>
pub fn from_slice_strict<const N: usize>( slice: [(K, V); N] ) -> Result<NoditMap<I, K, V>, OverlapError<V>>
Allocates a NoditMap
and moves the given entries from
the given slice into the map using
NoditMap::insert_strict()
.
May return an Err
while inserting. See
NoditMap::insert_strict()
for details.
§Panics
Panics if the given interval is an invalid interval. See Invalid Intervals
for more details.
§Examples
use nodit::interval::ie;
use nodit::NoditMap;
let map = NoditMap::from_slice_strict([
(ie(1, 4), false),
(ie(4, 8), true),
(ie(8, 100), false),
])
.unwrap();
sourcepub fn from_iter_strict(
iter: impl Iterator<Item = (K, V)>
) -> Result<NoditMap<I, K, V>, OverlapError<V>>
pub fn from_iter_strict( iter: impl Iterator<Item = (K, V)> ) -> Result<NoditMap<I, K, V>, OverlapError<V>>
Collects a NoditMap
from an iterator of (interval,
value) tuples using NoditMap::insert_strict()
.
May return an Err
while inserting. See
NoditMap::insert_strict()
for details.
§Panics
Panics if the given interval is an invalid interval. See Invalid Intervals
for more details.
§Examples
use nodit::interval::ie;
use nodit::NoditMap;
let slice =
[(ie(1, 4), false), (ie(4, 8), true), (ie(8, 100), false)];
let map: NoditMap<_, _, _> = NoditMap::from_iter_strict(
slice
.into_iter()
.filter(|(interval, _)| interval.start() > 2),
)
.unwrap();
source§impl<I, K, V> NoditMap<I, K, V>
impl<I, K, V> NoditMap<I, K, V>
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of intervals in the map.
§Examples
use nodit::interval::ie;
use nodit::NoditMap;
let mut map = NoditMap::new();
assert_eq!(map.len(), 0);
map.insert_strict(ie(0, 1), false).unwrap();
assert_eq!(map.len(), 1);
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true
if the map contains no intervals, and
false
if it does.
§Examples
use nodit::interval::ie;
use nodit::NoditMap;
let mut map = NoditMap::new();
assert_eq!(map.is_empty(), true);
map.insert_strict(ie(0, 1), false).unwrap();
assert_eq!(map.is_empty(), false);
sourcepub fn iter(&self) -> impl DoubleEndedIterator<Item = (&K, &V)>
pub fn iter(&self) -> impl DoubleEndedIterator<Item = (&K, &V)>
Returns an iterator over every entry in the map in ascending order.
§Examples
use nodit::interval::ie;
use nodit::NoditMap;
let map = NoditMap::from_slice_strict([
(ie(1, 4), false),
(ie(4, 8), true),
(ie(8, 100), false),
])
.unwrap();
let mut iter = map.iter();
assert_eq!(iter.next(), Some((&ie(1, 4), &false)));
assert_eq!(iter.next(), Some((&ie(4, 8), &true)));
assert_eq!(iter.next(), Some((&ie(8, 100), &false)));
assert_eq!(iter.next(), None);
sourcepub fn iter_mut(&mut self) -> impl DoubleEndedIterator<Item = (&K, &mut V)>
pub fn iter_mut(&mut self) -> impl DoubleEndedIterator<Item = (&K, &mut V)>
Returns an mutable iterator over every entry in the map in ascending order.
§Examples
use nodit::interval::ie;
use nodit::NoditMap;
let mut map = NoditMap::from_slice_strict([
(ie(1, 4), false),
(ie(4, 8), true),
(ie(8, 100), false),
])
.unwrap();
for (interval, value) in map.iter_mut() {
if *interval == ie(4, 8) {
*value = false
} else {
*value = true
}
}
sourcepub fn first_key_value(&self) -> Option<(&K, &V)>
pub fn first_key_value(&self) -> Option<(&K, &V)>
Returns the first entry in the map, if any.
§Examples
use nodit::interval::ie;
use nodit::NoditMap;
let map = NoditMap::from_slice_strict([
(ie(1, 4), false),
(ie(4, 8), true),
(ie(8, 100), false),
])
.unwrap();
assert_eq!(map.first_key_value(), Some((&ie(1, 4), &false)));
sourcepub fn last_key_value(&self) -> Option<(&K, &V)>
pub fn last_key_value(&self) -> Option<(&K, &V)>
Returns the last entry in the map, if any.
§Examples
use nodit::NoditMap;
use nodit::interval::ie;
let map = NoditMap::from_slice_strict([
(ie(1, 4), false),
(ie(4, 8), true),
(ie(8, 100), false),
])
.unwrap();
assert_eq!(
map.last_key_value(),
Some((&ie(8, 100), &false))
);