Skip to main content

IntCOSet

Struct IntCOSet 

Source
pub struct IntCOSet<I: IntCO> { /* private fields */ }
Expand description

Immutable canonical closed-open integer interval set.

Internally this is an Arc<[I]>, so cloning an IntCOSet<I> is cheap.

Canonical invariant:

for every adjacent pair a, b:
    a.end_excl() < b.start()

The strict < means both overlap and adjacency have already been merged.

I::Ord is expected to follow interval boundary ordering, consistent with the primitive interval implementations provided by int_interval.

Implementations§

Source§

impl<I: IntCO> IntCOSet<I>

Source

pub fn interval_count(&self) -> usize

Returns the number of canonical intervals.

Source

pub fn is_empty(&self) -> bool

Returns whether the set contains no intervals.

Source

pub fn as_slice(&self) -> &[I]

Returns the canonical interval slice.

The returned slice is sorted, non-overlapping, and contains no adjacent intervals.

Source

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

Iterates over canonical intervals by value.

Source§

impl<I: IntCO> IntCOSet<I>

Source

pub fn intersection_with_interval(&self, query: I) -> Self

Returns the intersection of this set with query.

The returned set contains the clipped segments of all canonical intervals intersecting query.

Example:

set:   [10, 20), [30, 40)
query: [15, 35)
out:   [15, 20), [30, 35)

Complexity: O(log n + k), where k is the number of intersecting intervals.

Source

pub fn union_with_interval(&self, query: I) -> Self

Returns the union of this set with query.

Intervals intersecting or adjacent to query are merged with it. If query is disjoint from all existing intervals, it is inserted at its canonical position.

Example:

set:   [10, 20), [30, 40)
query: [20, 30)
out:   [10, 40)

Complexity: O(log n + n) because the returned immutable interval slice must be allocated and populated.

Source

pub fn difference_with_interval(&self, query: I) -> Self

Returns the difference of this set and query.

The operation removes every point covered by query from this set. Intervals outside query are retained unchanged; intersecting boundary intervals may be clipped into left and right residual segments.

Example:

set:   [10, 20), [30, 40), [50, 60)
query: [15, 55)
out:   [10, 15), [55, 60)

Complexity: O(log n) if query is disjoint from the set; otherwise O(n) because the returned immutable interval slice must be copied.

Source

pub fn symmetric_difference_with_interval(&self, query: I) -> Self

Returns the symmetric difference self △ query.

The returned set contains points covered by exactly one of self and query.

Equivalently:

self △ query = (self ∪ query) \ (self ∩ query)

Example:

self:  [10, 20), [30, 40)
query: [15, 35)
out:   [10, 15), [20, 30), [35, 40)

Complexity: O(log n + k + n), where k is the number of canonical intervals in the union component affected by query.

Source§

impl<I: IntCO> IntCOSet<I>

Source

pub fn intersection_with_set(&self, other: &Self) -> Self

Returns the intersection of this set and other.

Both sets are canonical, so their sorted interval slices can be intersected with a two-pointer scan.

Example:

self:  [0, 10), [20, 30), [40, 50)
other: [5, 25), [45, 60)
out:   [5, 10), [20, 25), [45, 50)

Complexity: O(n + m), where n and m are the canonical interval counts of the two input sets.

Source

pub fn union_with_set(&self, other: &Self) -> Self

Source

pub fn difference_with_set(&self, other: &Self) -> Self

Returns the difference of this set and other.

The returned set contains every point covered by self but not by other.

Both source sets are canonical, so intervals from other are scanned monotonically while residual segments from self are emitted.

Example:

self:  [0, 10), [20, 30), [40, 50)
other: [5, 25), [45, 60)
out:   [0, 5), [25, 30), [40, 45)

Complexity: O(n + m), where n and m are the canonical interval counts of the two input sets.

Source

pub fn symmetric_difference_with_set(&self, other: &Self) -> Self

Returns the symmetric difference of this set and other.

The returned set contains every point covered by exactly one of the two input sets.

Both source sets are canonical. This method performs a two-way sweep over their virtual boundary events while tracking whether each side currently covers the sweep position.

Unlike an event-materializing implementation, the boundary event stream is represented by interval indices plus per-side coverage states:

  • if a side is currently outside its current interval, the next event is that interval’s start;
  • if a side is currently inside its current interval, the next event is that interval’s exclusive end.

Example:

self:  [0, 10), [20, 30)
other: [5, 15), [25, 35)
out:   [0, 5), [10, 15), [20, 25), [30, 35)

Complexity: O(n + m), where n and m are the canonical interval counts of the two input sets.

Source§

impl<I: IntCO> IntCOSet<I>

Source

pub unsafe fn new_unchecked(intervals: Vec<I>) -> Self

Builds a set from an already canonical interval vector.

§Safety

The caller must guarantee that intervals is canonical:

  • intervals are sorted by ascending start;
  • intervals are non-overlapping;
  • contiguous intervals have already been merged;
  • therefore, for every adjacent pair a, b, a.end_excl() < b.start() holds.

Violating this invariant can make binary-search based queries return incorrect results.

Source§

impl<I: IntCO> IntCOSet<I>

Source

pub fn covered_len_of(&self, query: I) -> I::MeasureType

Returns the covered length inside query.

Since IntCOSet<I> is canonical, all intersection segments are disjoint, so summing their lengths is valid.

The result is always <= query.len().

Source

pub fn uncovered_len_of(&self, query: I) -> I::MeasureType

Returns the uncovered length inside query.

Source

pub fn coverage_ratio_f32_of(&self, query: I) -> f32

Returns covered_len(query) / query.len() as f32.

query.len() is non-zero because I cannot represent an empty interval.

Source

pub fn coverage_ratio_f64_of(&self, query: I) -> f64

Returns covered_len(query) / query.len() as f64.

query.len() is non-zero because I cannot represent an empty interval.

Source§

impl<I: IntCO> IntCOSet<I>

Source

pub fn contains_point(&self, x: I::CoordType) -> bool

Returns whether x is covered by any interval in the set.

Complexity: O(log n).

Source

pub fn contains_interval(&self, query: I) -> bool

Returns whether query is fully contained by one interval.

Since the set is canonical, a contained query interval can only be contained by the interval immediately preceding or starting at query.start().

Complexity: O(log n).

Source

pub fn intersects_interval(&self, query: I) -> bool

Returns whether query intersects any interval in the set.

Complexity: O(log n).

Source§

impl<I: IntCO> IntCOSet<I>

Source

pub fn intervals_intersecting(&self, query: I) -> impl Iterator<Item = I> + '_

Iterates over all canonical intervals intersecting query.

The iterator yields original intervals stored in the set, not clipped intersection segments.

Complexity: O(log n + k), where k is the number of returned intervals.

Source§

impl<I: IntCO> IntCOSet<I>

Source

pub fn interval_containing_point(&self, x: I::CoordType) -> Option<I>

Returns the unique interval containing x, if any.

Because the set is canonical, at most one interval can contain a single point.

Complexity: O(log n).

Trait Implementations§

Source§

impl<I: Clone + IntCO> Clone for IntCOSet<I>

Source§

fn clone(&self) -> IntCOSet<I>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<I: Debug + IntCO> Debug for IntCOSet<I>

Source§

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

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

impl<I: IntCO> Default for IntCOSet<I>

Source§

fn default() -> Self

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

impl<I: IntCO> FromIterator<I> for IntCOSet<I>

Source§

fn from_iter<T>(iter: T) -> Self
where T: IntoIterator<Item = I>,

Creates a value from an iterator. Read more
Source§

impl<I> FromParallelIterator<I> for IntCOSet<I>
where I: IntCO + Send,

Source§

fn from_par_iter<T>(iter: T) -> Self
where T: IntoParallelIterator<Item = I>,

Creates an instance of the collection from the parallel iterator par_iter. Read more
Source§

impl<I: PartialEq + IntCO> PartialEq for IntCOSet<I>

Source§

fn eq(&self, other: &IntCOSet<I>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<I: Eq + IntCO> Eq for IntCOSet<I>

Source§

impl<I: IntCO> StructuralPartialEq for IntCOSet<I>

Auto Trait Implementations§

§

impl<I> Freeze for IntCOSet<I>

§

impl<I> RefUnwindSafe for IntCOSet<I>
where I: RefUnwindSafe,

§

impl<I> Send for IntCOSet<I>
where I: Sync + Send,

§

impl<I> Sync for IntCOSet<I>
where I: Sync + Send,

§

impl<I> Unpin for IntCOSet<I>

§

impl<I> UnsafeUnpin for IntCOSet<I>

§

impl<I> UnwindSafe for IntCOSet<I>
where I: 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

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

Initializes a with the given initializer. Read more
Source§

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

Dereferences the given pointer. Read more
Source§

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

Mutably dereferences the given pointer. Read more
Source§

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,

Source§

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

Source§

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

Source§

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.