Struct math::partition::integer_interval_map::IntegerIntervalMap
source · pub struct IntegerIntervalMap<T> { /* private fields */ }
Expand description
Maps I64Interval
s to values of a numeric type T
.
Implementations§
source§impl<T: Copy + Num> IntegerIntervalMap<T>
impl<T: Copy + Num> IntegerIntervalMap<T>
pub fn new() -> Self
sourcepub fn aggregate(&mut self, key: I64Interval, value: T)
pub fn aggregate(&mut self, key: I64Interval, value: T)
Adds an integer interval as the key
with an associated value
.
Any existing intervals intersecting the key
will be broken up,
where the region of intersection will have a value being the sum of
the existing value and the new value
, while the non-intersecting
regions will retain their original values.
Example
use math::{
interval::I64Interval,
partition::integer_interval_map::IntegerIntervalMap,
};
// | value
// -1 0 1 2 3 4 | +2
// 6 7 8 | +4
// 4 5 6 7 | +1
//---------------------------
// 2 2 2 2 2 3 1 5 5 4 | superposed values
let mut interval_map = IntegerIntervalMap::new();
interval_map.aggregate(I64Interval::new(-1, 4), 2);
interval_map.aggregate(I64Interval::new(6, 8), 4);
interval_map.aggregate(I64Interval::new(4, 7), 1);
assert_eq!(interval_map.get(&I64Interval::new(-1, 3)), Some(2));
assert_eq!(interval_map.get(&I64Interval::new(4, 4)), Some(3));
assert_eq!(interval_map.get(&I64Interval::new(5, 5)), Some(1));
assert_eq!(interval_map.get(&I64Interval::new(6, 7)), Some(5));
assert_eq!(interval_map.get(&I64Interval::new(8, 8)), Some(4));
assert_eq!(interval_map.get(&I64Interval::new(-1, 4)), None);
assert_eq!(interval_map.get(&I64Interval::new(6, 8)), None);
assert_eq!(interval_map.get(&I64Interval::new(4, 7)), None);
sourcepub fn iter(&self) -> Iter<'_, I64Interval, T>
pub fn iter(&self) -> Iter<'_, I64Interval, T>
Example
use math::{
interval::I64Interval,
partition::integer_interval_map::IntegerIntervalMap,
};
let mut interval_map = IntegerIntervalMap::new();
interval_map.aggregate(I64Interval::new(-1, 4), 2);
interval_map.aggregate(I64Interval::new(6, 8), 4);
interval_map.aggregate(I64Interval::new(4, 7), 1);
let expected = vec![
(I64Interval::new(-1, 3), 2),
(I64Interval::new(4, 4), 3),
(I64Interval::new(5, 5), 1),
(I64Interval::new(6, 7), 5),
(I64Interval::new(8, 8), 4),
];
assert_eq!(interval_map.len(), 5);
for ((interval, val), (expected_interval, exptected_val)) in
interval_map.iter().zip(expected.iter())
{
assert_eq!(interval, expected_interval);
assert_eq!(val, exptected_val);
}
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of common refinements resulted from aggregating the intervals
Example
use math::{
interval::I64Interval,
partition::integer_interval_map::IntegerIntervalMap,
};
let mut interval_map = IntegerIntervalMap::new();
interval_map.aggregate(I64Interval::new(2, 6), 2.);
interval_map.aggregate(I64Interval::new(4, 8), 2.);
interval_map.aggregate(I64Interval::new(8, 9), 3.);
// the common refinements are now [2, 3], [4, 6], [7, 7], [8, 8], [9, 9]
assert_eq!(interval_map.len(), 5);
let expected = vec![
(I64Interval::new(2, 3), 2.),
(I64Interval::new(4, 6), 4.),
(I64Interval::new(7, 7), 2.),
(I64Interval::new(8, 8), 5.),
(I64Interval::new(9, 9), 3.),
];
for ((interval, val), (expected_interval, exptected_val)) in
interval_map.iter().zip(expected.iter())
{
assert_eq!(interval, expected_interval);
assert_eq!(val, exptected_val);
}
sourcepub fn into_map(self) -> BTreeMap<I64Interval, T>
pub fn into_map(self) -> BTreeMap<I64Interval, T>
Converts into the underlying BTreeMap
sourcepub fn get(&self, key: &I64Interval) -> Option<T>
pub fn get(&self, key: &I64Interval) -> Option<T>
Returns a Some
value only if the key corresponds to one of the current
exact intervals and not its subset or superset.
Example
use math::{
interval::I64Interval,
partition::integer_interval_map::IntegerIntervalMap,
};
let mut interval_map = IntegerIntervalMap::new();
interval_map.aggregate(I64Interval::new(2, 5), 1);
assert_eq!(interval_map.get(&I64Interval::new(2, 5)), Some(1));
assert_eq!(interval_map.get(&I64Interval::new(2, 4)), None);
assert_eq!(interval_map.get(&I64Interval::new(2, 6)), None);
Trait Implementations§
source§impl<T: Clone> Clone for IntegerIntervalMap<T>
impl<T: Clone> Clone for IntegerIntervalMap<T>
source§fn clone(&self) -> IntegerIntervalMap<T>
fn clone(&self) -> IntegerIntervalMap<T>
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moresource§impl<T: Debug> Debug for IntegerIntervalMap<T>
impl<T: Debug> Debug for IntegerIntervalMap<T>
source§impl<T> IntoIterator for IntegerIntervalMap<T>
impl<T> IntoIterator for IntegerIntervalMap<T>
§type IntoIter = <BTreeMap<ContiguousIntegerSet<i64>, T> as IntoIterator>::IntoIter
type IntoIter = <BTreeMap<ContiguousIntegerSet<i64>, T> as IntoIterator>::IntoIter
Which kind of iterator are we turning this into?
§type Item = <BTreeMap<ContiguousIntegerSet<i64>, T> as IntoIterator>::Item
type Item = <BTreeMap<ContiguousIntegerSet<i64>, T> as IntoIterator>::Item
The type of the elements being iterated over.
source§impl<T: PartialEq> PartialEq for IntegerIntervalMap<T>
impl<T: PartialEq> PartialEq for IntegerIntervalMap<T>
source§fn eq(&self, other: &IntegerIntervalMap<T>) -> bool
fn eq(&self, other: &IntegerIntervalMap<T>) -> bool
This method tests for
self
and other
values to be equal, and is used
by ==
.source§impl<T> SubsetIndexable<ContiguousIntegerSet<i64>, ContiguousIntegerSet<i64>> for IntegerIntervalMap<T>
impl<T> SubsetIndexable<ContiguousIntegerSet<i64>, ContiguousIntegerSet<i64>> for IntegerIntervalMap<T>
fn get_set_containing(&self, subset: &I64Interval) -> Option<I64Interval>
impl<T: Eq> Eq for IntegerIntervalMap<T>
impl<T> StructuralEq for IntegerIntervalMap<T>
impl<T> StructuralPartialEq for IntegerIntervalMap<T>
Auto Trait Implementations§
impl<T> RefUnwindSafe for IntegerIntervalMap<T>where
T: RefUnwindSafe,
impl<T> Send for IntegerIntervalMap<T>where
T: Send,
impl<T> Sync for IntegerIntervalMap<T>where
T: Sync,
impl<T> Unpin for IntegerIntervalMap<T>
impl<T> UnwindSafe for IntegerIntervalMap<T>where
T: RefUnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more