Struct nodit::nodit::map::NoditMap

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

source

pub fn overlaps<Q>(&self, interval: Q) -> bool
where 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);
source

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)]
);
source

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
	}
}
source

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);
source

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));
source

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);
source

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)));
source

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)]
);
source

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);
source

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)]
);
source

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)]
);
source

pub fn contains_interval<Q>(&self, interval: Q) -> bool
where 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);
source

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);
source

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)]
);
source

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)]
);
source

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)]
);
source

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)]
);
source

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)]
);
source

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();
source

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>

source

pub fn new() -> Self

Makes a new, empty NoditMap.

§Examples
use nodit::{Interval, NoditMap};

let map: NoditMap<i8, Interval<i8>, bool> = NoditMap::new();
source

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);
source

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);
source

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);
source

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
	}
}
source

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)));
source

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))
);

Trait Implementations§

source§

impl<I: Clone, K: Clone, V: Clone> Clone for NoditMap<I, K, V>

source§

fn clone(&self) -> NoditMap<I, K, V>

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<I: Debug, K: Debug, V: Debug> Debug for NoditMap<I, K, V>

source§

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

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

impl<I, K, V> Default for NoditMap<I, K, V>

source§

fn default() -> Self

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

impl<I, K, V> IntoIterator for NoditMap<I, K, V>

§

type Item = (K, V)

The type of the elements being iterated over.
§

type IntoIter = IntoIter<I, 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<I: PartialEq, K: PartialEq, V: PartialEq> PartialEq for NoditMap<I, K, V>

source§

fn eq(&self, other: &NoditMap<I, K, V>) -> 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<I: Eq, K: Eq, V: Eq> Eq for NoditMap<I, K, V>

source§

impl<I, K, V> StructuralPartialEq for NoditMap<I, K, V>

Auto Trait Implementations§

§

impl<I, K, V> Freeze for NoditMap<I, K, V>

§

impl<I, K, V> RefUnwindSafe for NoditMap<I, K, V>

§

impl<I, K, V> Send for NoditMap<I, K, V>
where I: Send, K: Send, V: Send,

§

impl<I, K, V> Sync for NoditMap<I, K, V>
where I: Sync, K: Sync, V: Sync,

§

impl<I, K, V> Unpin for NoditMap<I, K, V>
where I: Unpin,

§

impl<I, K, V> UnwindSafe for NoditMap<I, 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> 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.