pub struct IntegerIntervalMap<T> { /* private fields */ }
Expand description

Maps I64Intervals to values of a numeric type T.

Implementations§

source§

impl<T: Copy + Num> IntegerIntervalMap<T>

source

pub fn new() -> Self

source

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

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

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

pub fn into_map(self) -> BTreeMap<I64Interval, T>

Converts into the underlying BTreeMap

source

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>

source§

fn clone(&self) -> IntegerIntervalMap<T>

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<T: Debug> Debug for IntegerIntervalMap<T>

source§

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

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

impl<T: Copy + Num + Debug> Default for IntegerIntervalMap<T>

source§

fn default() -> Self

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

impl<T> IntoIterator for IntegerIntervalMap<T>

§

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

The type of the elements being iterated over.
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<T: PartialEq> PartialEq for IntegerIntervalMap<T>

source§

fn eq(&self, other: &IntegerIntervalMap<T>) -> 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<T> SubsetIndexable<ContiguousIntegerSet<i64>, ContiguousIntegerSet<i64>> for IntegerIntervalMap<T>

source§

impl<T: Eq> Eq for IntegerIntervalMap<T>

source§

impl<T> StructuralEq for IntegerIntervalMap<T>

source§

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> 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.

§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V