pub struct ZosditMap<I, K, V> { /* private fields */ }
Expand description
A Zero Overlap Sequential Discrete Interval Tree Map Data-Structure based off BTreeMap
and
SmallVec
See the zosdit
module documentation for a more detailed explanation of how this
data-structure works.
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::ZosditMap;
// Make a map of intervals to booleans
let mut map = ZosditMap::from_slice_strict_back([
(ie(4, 8), false),
(ie(8, 18), true),
(ie(20, 100), false),
])
.unwrap();
// Iterate over the entries in the map
for (interval, value) in map.iter() {
println!("{interval:?}, {value:?}");
}
Implementations§
source§impl<I, K, V> ZosditMap<I, K, V>where
I: PointType,
K: IntervalType<I>,
impl<I, K, V> ZosditMap<I, K, V>where
I: PointType,
K: IntervalType<I>,
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
See NoditMap::len()
for more details.
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
See NoditMap::is_empty()
for more details.
sourcepub fn first_key_value(&self) -> Option<(&K, &V)>
pub fn first_key_value(&self) -> Option<(&K, &V)>
Returns the first key-value pair in the map.
§Examples
use nodit::interval::ii;
use nodit::ZosditMap;
let map = ZosditMap::from_slice_strict_back([
(ii(0, 4), -2),
(ii(4, 4), -4),
(ii(4, 4), -8),
])
.unwrap();
assert_eq!(
map.first_key_value(),
Some((&ii(0, 4), &-2))
);
sourcepub fn last_key_value(&self) -> Option<(&K, &V)>
pub fn last_key_value(&self) -> Option<(&K, &V)>
Returns the last key-value pair in the map.
§Examples
use nodit::interval::ii;
use nodit::ZosditMap;
let map = ZosditMap::from_slice_strict_back([
(ii(0, 4), -2),
(ii(4, 4), -4),
(ii(4, 4), -8),
])
.unwrap();
assert_eq!(
map.last_key_value(),
Some((&ii(4, 4), &-8))
);
sourcepub fn get_last_value_at_point(&self, point: I) -> Option<&V>
pub fn get_last_value_at_point(&self, point: I) -> Option<&V>
Gets the last value stored in the SmallVec
for the interval(s)
that contain that point.
§Examples
use nodit::interval::ii;
use nodit::ZosditMap;
let map = ZosditMap::from_slice_strict_back([
(ii(0, 4), -2),
(ii(4, 4), -4),
(ii(4, 4), -8),
(ii(4, 8), -10),
])
.unwrap();
assert_eq!(map.get_last_value_at_point(0), Some(&-2));
assert_eq!(map.get_last_value_at_point(4), Some(&-10));
assert_eq!(map.get_last_value_at_point(10), None);
sourcepub fn remove_last_value_at_point(&mut self, point: I) -> Option<V>
pub fn remove_last_value_at_point(&mut self, point: I) -> Option<V>
Removes the last value stored in the SmallVec
for the interval(s)
that contain that point.
§Examples
use nodit::interval::ii;
use nodit::ZosditMap;
let mut map = ZosditMap::from_slice_strict_back([
(ii(0, 4), -2),
(ii(4, 4), -4),
(ii(4, 4), -8),
(ii(4, 8), -10),
])
.unwrap();
assert_eq!(map.get_last_value_at_point(4), Some(&-10));
assert_eq!(map.remove_last_value_at_point(4), Some(-10));
assert_eq!(map.get_last_value_at_point(4), Some(&-8));
assert_eq!(map.remove_last_value_at_point(4), Some(-8));
assert_eq!(map.get_last_value_at_point(4), Some(&-4));
assert_eq!(map.remove_last_value_at_point(4), Some(-4));
assert_eq!(map.get_last_value_at_point(4), Some(&-2));
assert_eq!(map.remove_last_value_at_point(4), Some(-2));
assert_eq!(map.get_last_value_at_point(4), None);
assert_eq!(map.remove_last_value_at_point(4), None);
sourcepub fn insert_strict_back(
&mut self,
interval: K,
value: V
) -> Result<(), NonZeroOverlapError<V>>
pub fn insert_strict_back( &mut self, interval: K, value: V ) -> Result<(), NonZeroOverlapError<V>>
Appends the value to the SmallVec
corresponding to the interval.
If the given interval non-zero-overlaps one or more intervals already in the
map, then an NonZeroOverlapError
is returned and the map is not
updated.
If the given interval is singular and there is an identical singular interval entry already
in the map then the value is appended to the back on the internal SmallVec
.
§Panics
Panics if the given interval is an invalid interval. See Invalid Intervals
for more details.
§Examples
use nodit::interval::ii;
use nodit::ZosditMap;
let mut map = ZosditMap::new();
assert_eq!(map.insert_strict_back(ii(0, 10), -2), Ok(()));
assert_eq!(map.insert_strict_back(ii(10, 10), -4), Ok(()));
assert_eq!(map.insert_strict_back(ii(10, 10), -6), Ok(()));
assert_eq!(
map.into_iter().collect::<Vec<_>>(),
[(ii(0, 10), -2), (ii(10, 10), -4), (ii(10, 10), -6)]
);
sourcepub fn is_zero_overlap<Q>(&self, interval: Q) -> boolwhere
Q: IntervalType<I>,
pub fn is_zero_overlap<Q>(&self, interval: Q) -> boolwhere
Q: IntervalType<I>,
Returns true
if the given interval zero-overlaps 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::ii;
use nodit::ZosditMap;
let mut map = ZosditMap::new();
assert_eq!(map.insert_strict_back(ii(0, 10), -2), Ok(()));
assert_eq!(map.insert_strict_back(ii(10, 10), -4), Ok(()));
assert_eq!(map.insert_strict_back(ii(10, 10), -6), Ok(()));
assert_eq!(map.is_zero_overlap(ii(0, 0)), true);
assert_eq!(map.is_zero_overlap(ii(10, 10)), true);
assert_eq!(map.is_zero_overlap(ii(10, 12)), true);
assert_eq!(map.is_zero_overlap(ii(10, 12)), true);
assert_eq!(map.is_zero_overlap(ii(0, 2)), false);
assert_eq!(map.is_zero_overlap(ii(4, 4)), false);
assert_eq!(map.is_zero_overlap(ii(4, 12)), 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,
The same as NoditMap::cut()
except it flattens the SmallVec
s of values into the
returned iterator.
See NoditMap::cut()
for more details.
§Panics
Panics if the given interval is an invalid interval. See Invalid Intervals
for more details.
§Examples
use nodit::interval::{ee, ii};
use nodit::ZosditMap;
let mut base = ZosditMap::from_slice_strict_back([
(ii(0, 4), -2),
(ii(4, 4), -4),
(ii(4, 4), -6),
(ii(4, 8), -8),
])
.unwrap();
assert_eq!(base.len(), 4);
let after_cut = ZosditMap::from_slice_strict_back([
(ii(0, 2), -2),
(ii(6, 8), -8),
])
.unwrap();
assert_eq!(
base.cut(ee(2, 6)).collect::<Vec<_>>(),
[
(ii(3, 4), -2),
(ii(4, 4), -4),
(ii(4, 4), -6),
(ii(4, 5), -8)
]
);
assert_eq!(base.len(), 2);
assert_eq!(base, after_cut);
sourcepub fn overlapping<Q>(&self, interval: Q) -> impl Iterator<Item = (&K, &V)>where
Q: IntervalType<I>,
pub fn overlapping<Q>(&self, interval: Q) -> impl Iterator<Item = (&K, &V)>where
Q: IntervalType<I>,
The same as NoditMap::overlapping()
except it flattens the SmallVec
s of values into
the returned iterator.
See NoditMap::overlapping()
for more details.
§Panics
Panics if the given interval is an invalid interval. See Invalid Intervals
for more details.
§Examples
use nodit::interval::{ee, ii};
use nodit::ZosditMap;
let mut base = ZosditMap::from_slice_strict_back([
(ii(0, 4), -2),
(ii(4, 4), -4),
(ii(4, 4), -6),
(ii(4, 8), -8),
(ii(8, 12), -10),
])
.unwrap();
assert_eq!(
base.overlapping(ii(4, 4)).collect::<Vec<_>>(),
[
(&ii(0, 4), &-2),
(&ii(4, 4), &-4),
(&ii(4, 4), &-6),
(&ii(4, 8), &-8),
]
);
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::ZosditMap;
let map = ZosditMap::from_slice_strict_back([
(ie(1, 4), -2),
(ie(4, 8), -4),
(ie(8, 100), -6),
])
.unwrap();
let mut iter = map.iter();
assert_eq!(iter.next(), Some((&ie(1, 4), &-2)));
assert_eq!(iter.next(), Some((&ie(4, 8), &-4)));
assert_eq!(iter.next(), Some((&ie(8, 100), &-6)));
assert_eq!(iter.next(), None);
sourcepub fn from_slice_strict_back<const N: usize>(
slice: [(K, V); N]
) -> Result<ZosditMap<I, K, V>, NonZeroOverlapError<V>>
pub fn from_slice_strict_back<const N: usize>( slice: [(K, V); N] ) -> Result<ZosditMap<I, K, V>, NonZeroOverlapError<V>>
Allocates a ZosditMap
and moves the given entries from the given
slice into the map using ZosditMap::insert_strict_back()
.
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::ZosditMap;
let map = ZosditMap::from_slice_strict_back([
(ie(1, 4), -2),
(ie(4, 8), -4),
(ie(8, 100), -6),
])
.unwrap();
sourcepub fn from_iter_strict_back(
iter: impl Iterator<Item = (K, V)>
) -> Result<ZosditMap<I, K, V>, NonZeroOverlapError<V>>
pub fn from_iter_strict_back( iter: impl Iterator<Item = (K, V)> ) -> Result<ZosditMap<I, K, V>, NonZeroOverlapError<V>>
Collects a ZosditMap
from an iterator of (interval,
value) tuples using ZosditMap::insert_strict_back()
.
May return an Err
while inserting. See
ZosditMap::insert_strict_back()
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::ZosditMap;
let slice = [(ie(1, 4), -2), (ie(4, 8), -4), (ie(8, 100), -6)];
let map: ZosditMap<_, _, _> = ZosditMap::from_iter_strict_back(
slice
.into_iter()
.filter(|(interval, _)| interval.start() > 2),
)
.unwrap();