Struct numerated::tree::IntervalsTree

source ·
pub struct IntervalsTree<T> { /* private fields */ }
Expand description

§Non overlapping intervals tree

Can be considered as set of points, but with possibility to work with continuous sets of this points (the same as interval) as fast as with points. Insert and remove operations has complexity between [O(log(n)), O(n)], where n is amount of intervals in tree. So, even if you insert for example points from [0u64] to u64::MAX, then removing all of them or any part of them is as fast as removing one point.

§Examples

use numerated::{tree::IntervalsTree, interval::Interval};
use std::collections::BTreeSet;
use std::ops::RangeInclusive;

let mut tree = IntervalsTree::new();
let mut set = BTreeSet::new();

tree.insert(1i32);
// now `tree` contains only one interval: [1..=1]
set.insert(1i32);
// `points_iter()` - is iterator over all points in `tree`.
assert_eq!(set, tree.points_iter().collect());

// We can insert points from 3 to 100_000 only by one insert operation.
// `try` is only for range check, that it has start ≤ end.
// After `tree` will contain two intervals: `[1..=1]` and `[3..=100_000]`.
tree.insert(Interval::try_from(3..=100_000).unwrap());
// For `set` insert complexity == `O(n)`, where n is amount of elements in range.
set.extend(3..=100_000);
assert_eq!(set, tree.points_iter().collect());

// We can remove points from 1 to 99_000 (not inclusive) only by one remove operation.
// `try` is only for range check, that it has start ≤ end.
// After `tree` will contain two intervals: [99_000..=100_000]
tree.remove(Interval::try_from(1..99_000).unwrap());
// For `set` insert complexity == O(n*log(m)),
// where `n` is amount of elements in range and `m` is len of `set`.
(1..99_000).for_each(|i| { set.remove(&i); });
assert_eq!(set, tree.points_iter().collect());

// Can insert or remove all possible points just by one operation:
tree.insert(..);
tree.remove(..);

// Iterate over voids (intervals between intervals in tree):
tree.insert(Interval::try_from(1..=3).unwrap());
tree.insert(Interval::try_from(5..=7).unwrap());
let voids = tree.voids(..).map(RangeInclusive::from).collect::<Vec<_>>();
assert_eq!(voids, vec![i32::MIN..=0, 4..=4, 8..=i32::MAX]);

// Difference iterator: iterate over intervals from `tree` which are not in `other_tree`.
let other_tree: IntervalsTree<i32> = [3, 4, 5, 7, 8, 9].into_iter().collect();
let difference: Vec<_> = tree.difference(&other_tree).map(RangeInclusive::from).collect();
assert_eq!(difference, vec![1..=2, 6..=6]);

§Possible panic cases

Using IntervalsTree for type T: Numerated cannot cause panics, if implementation Numerated, Copy, Ord, Eq are correct for T. In other cases IntervalsTree does not guarantees execution without panics.

Implementations§

source§

impl<T: Copy> IntervalsTree<T>

source

pub const fn new() -> Self

Creates new empty intervals tree.

source

pub fn intervals_amount(&self) -> usize

Returns amount of not empty intervals in tree.

Complexity: O(1).

source§

impl<T: Copy + Ord> IntervalsTree<T>

source

pub fn end(&self) -> Option<T>

Returns the biggest point in tree.

source

pub fn start(&self) -> Option<T>

Returns the smallest point in tree.

source§

impl<T: Numerated> IntervalsTree<T>

source

pub fn iter(&self) -> impl Iterator<Item = Interval<T>> + '_

Returns iterator over all intervals in tree.

source

pub fn contains<I: Into<IntervalIterator<T>>>(&self, interval: I) -> bool

Returns true if for each pintervalpself, otherwise returns false.

source

pub fn insert<I: Into<IntervalIterator<T>>>(&mut self, interval: I)

Insert interval into tree.

  • if interval is empty, then nothing will be inserted.
  • if interval is not empty, then after insertion: for each pintervalpself.

Complexity: O(m * log(n)), where

  • n is amount of intervals in self
  • m is amount of intervals in selfinterval
source

pub fn remove<I: Into<IntervalIterator<T>>>(&mut self, interval: I)

Remove interval from tree.

  • if interval is empty, then nothing will be removed.
  • if interval is not empty, then after removing: for each pintervalpself.

Complexity: O(m * log(n)), where

  • n is amount of intervals in self
  • m is amount of intervals in selfinterval
source

pub fn voids<I: Into<IntervalIterator<T>>>( &self, interval: I ) -> VoidsIterator<T, impl Iterator<Item = Interval<T>> + '_>

Returns iterator over non empty intervals, that consist of points p: T where each pself and pinterval. Intervals in iterator are sorted in ascending order.

Iterating complexity: O(log(n) + m), where

  • n is amount of intervals in self
  • m is amount of intervals in selfinterval
source

pub fn difference<'a>( &'a self, other: &'a Self ) -> impl Iterator<Item = Interval<T>> + '_

Returns iterator over intervals, which consist of points p: T, where each pself and pother.

Iterating complexity: O(n + m), where

  • n is amount of intervals in self
  • m is amount of intervals in other
source

pub fn points_amount(&self) -> Option<T::Distance>

Number of points in tree set.

Complexity: O(n), where n is amount of intervals in self.

source

pub fn points_iter(&self) -> impl Iterator<Item = T> + '_

Iterator over all points in tree set.

source

pub fn to_vec(&self) -> Vec<RangeInclusive<T>>

Convert tree to vector of inclusive ranges.

Trait Implementations§

source§

impl<T: Clone> Clone for IntervalsTree<T>

source§

fn clone(&self) -> IntervalsTree<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 + Numerated> Debug for IntervalsTree<T>

source§

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

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

impl<T> Decode for IntervalsTree<T>
where BTreeMap<T, T>: Decode,

source§

fn decode<__CodecInputEdqy: Input>( __codec_input_edqy: &mut __CodecInputEdqy ) -> Result<Self, Error>

Attempt to deserialise the value from input.
source§

fn decode_into<I>( input: &mut I, dst: &mut MaybeUninit<Self> ) -> Result<DecodeFinished, Error>
where I: Input,

Attempt to deserialize the value from input into a pre-allocated piece of memory. Read more
source§

fn skip<I>(input: &mut I) -> Result<(), Error>
where I: Input,

Attempt to skip the encoded value from input. Read more
source§

fn encoded_fixed_size() -> Option<usize>

Returns the fixed encoded size of the type. Read more
source§

impl<T: Copy> Default for IntervalsTree<T>

source§

fn default() -> Self

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

impl<T> Encode for IntervalsTree<T>
where BTreeMap<T, T>: Encode,

source§

fn size_hint(&self) -> usize

If possible give a hint of expected size of the encoding. Read more
source§

fn encode_to<__CodecOutputEdqy: Output + ?Sized>( &self, __codec_dest_edqy: &mut __CodecOutputEdqy )

Convert self to a slice and append it to the destination.
source§

fn encode(&self) -> Vec<u8>

Convert self to an owned vector.
source§

fn using_encoded<__CodecOutputReturn, __CodecUsingEncodedCallback: FnOnce(&[u8]) -> __CodecOutputReturn>( &self, f: __CodecUsingEncodedCallback ) -> __CodecOutputReturn

Convert self to a slice and then invoke the given closure with it.
source§

fn encoded_size(&self) -> usize

Calculates the encoded size. Read more
source§

impl<T: Numerated, D: Into<IntervalIterator<T>>> FromIterator<D> for IntervalsTree<T>

source§

fn from_iter<I: IntoIterator<Item = D>>(iter: I) -> Self

Creates a value from an iterator. Read more
source§

impl<T: PartialEq> PartialEq for IntervalsTree<T>

source§

fn eq(&self, other: &IntervalsTree<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> TypeInfo for IntervalsTree<T>
where BTreeMap<T, T>: TypeInfo + 'static, T: TypeInfo + 'static,

§

type Identity = IntervalsTree<T>

The type identifying for which type info is provided. Read more
source§

fn type_info() -> Type

Returns the static type identifier for Self.
source§

impl<T> EncodeLike for IntervalsTree<T>
where BTreeMap<T, T>: Encode,

source§

impl<T: Eq> Eq for IntervalsTree<T>

source§

impl<T> StructuralPartialEq for IntervalsTree<T>

Auto Trait Implementations§

§

impl<T> Freeze for IntervalsTree<T>

§

impl<T> RefUnwindSafe for IntervalsTree<T>
where T: RefUnwindSafe,

§

impl<T> Send for IntervalsTree<T>
where T: Send,

§

impl<T> Sync for IntervalsTree<T>
where T: Sync,

§

impl<T> Unpin for IntervalsTree<T>

§

impl<T> UnwindSafe for IntervalsTree<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> DecodeAll for T
where T: Decode,

source§

fn decode_all(input: &mut &[u8]) -> Result<T, Error>

Decode Self and consume all of the given input data. Read more
source§

impl<T> DecodeLimit for T
where T: Decode,

source§

fn decode_all_with_depth_limit( limit: u32, input: &mut &[u8] ) -> Result<T, Error>

Decode Self and consume all of the given input data. Read more
source§

fn decode_with_depth_limit<I>(limit: u32, input: &mut I) -> Result<T, Error>
where I: Input,

Decode Self with the given maximum recursion depth and advance input by the number of bytes consumed. 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> KeyedVec for T
where T: Codec,

source§

fn to_keyed_vec(&self, prepend_key: &[u8]) -> Vec<u8>

Return an encoding of Self prepended by given slice.
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.
source§

impl<S> Codec for S
where S: Decode + Encode,

source§

impl<T> EncodeLike<&&T> for T
where T: Encode,

source§

impl<T> EncodeLike<&T> for T
where T: Encode,

source§

impl<T> EncodeLike<&mut T> for T
where T: Encode,

source§

impl<T> EncodeLike<Arc<T>> for T
where T: Encode,

source§

impl<T> EncodeLike<Box<T>> for T
where T: Encode,

source§

impl<'a, T> EncodeLike<Cow<'a, T>> for T
where T: ToOwned + Encode,

source§

impl<T> EncodeLike<Rc<T>> for T
where T: Encode,

source§

impl<S> FullCodec for S
where S: Decode + FullEncode,

source§

impl<S> FullEncode for S
where S: Encode + EncodeLike,

source§

impl<T> JsonSchemaMaybe for T

source§

impl<T> StaticTypeInfo for T
where T: TypeInfo + 'static,