Struct range_collections::range_set::RangeSet[][src]

pub struct RangeSet<T, A: Array<Item = T> = [T; 2]> { /* fields omitted */ }
Expand description

A set of non-overlapping ranges

let mut a: RangeSet<i32> = RangeSet::from(10..);
let b: RangeSet<i32> = RangeSet::from(1..5);

a |= b;
let r = !a;

A data structure to represent a set of non-overlapping ranges of element type T: Ord. It uses a SmallVec<T> of sorted boundaries internally.

It can represent not just finite ranges but also ranges with unbounded start or end. Because it can represent infinite ranges, it can also represent the set of all elements, and therefore all boolean operations including negation.

It does not put any constraints on the element type for requriring an Ord instance. Adjacent ranges will be merged.

It provides very fast operations for set operations (&, |, ^) as well as for intersection tests (is_disjoint, is_subset).

In addition to the fast set operations that produce a new range set, it also supports the equivalent in-place operations.

Complexity

Complexity is given separately for the number of comparisons and the number of copies, since sometimes you have a comparison operation that is basically free (any of the primitive types), whereas sometimes you have a comparison operation that is many orders of magnitude more expensive than a copy (long strings, arbitrary precision integers, …)

Number of comparisons

operationbestworstremark
negation1      1       
unionO(log(N))O(N)binary merge
intersectionO(log(N))O(N)binary merge
differenceO(log(N))O(N)binary merge
xorO(log(N))O(N)binary merge
membershipO(log(N))O(log(N))binary search
is_disjointO(log(N))O(N)binary merge with cutoff
is_subsetO(log(N))O(N)binary merge with cutoff

Number of copies

For creating new sets, obviously there needs to be at least one copy for each element of the result set, so the complexity is always O(N). For in-place operations it gets more interesting. In case the number of elements of the result being identical to the number of existing elements, there will be no copies and no allocations.

E.g. if the result just has some of the ranges of the left hand side extended or truncated, but the same number of boundaries, there will be no allocations and no copies except for the changed boundaries themselves.

If the result has fewer boundaries than then lhs, there will be some copying but no allocations. Only if the result is larger than the capacity of the underlying vector of the lhs will there be allocations.

operationbestworst
negation1      1       
union1O(N)
intersection1O(N)
difference1O(N)
xor1O(N)

Testing

Testing is done by some simple smoke tests as well as quickcheck tests of the algebraic properties of the boolean operations.

Implementations

iterate over all ranges in this range set

boundaries in this range set

get the boundaries in this range set as a SmallVec

the empty range set

a range set containing all values

a range set with a constant value everywhere

true if the range set is empty

true if the range set contains all values

true if this range set is disjoint from another range set

true if this range set is a superset of another range set

A range set is considered to be a superset of itself

true if this range set is a subset of another range set

A range set is considered to be a subset of itself

true if the value is contained in the range set

Trait Implementations

compute the intersection of this range set with another, producing a new range set

∀ t ∈ T, r(t) = a(t) & b(t)

The resulting type after applying the & operator.

Performs the & operation. Read more

Performs the &= operation. Read more

compute the union of this range set with another, producing a new range set

∀ t ∈ T, r(t) = a(t) | b(t)

The resulting type after applying the | operator.

Performs the | operation. Read more

Performs the |= operation. Read more

compute the exclusive or of this range set with another, producing a new range set

∀ t ∈ T, r(t) = a(t) ^ b(t)

The resulting type after applying the ^ operator.

Performs the ^ operation. Read more

Performs the ^= operation. Read more

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Deserialize this value from the given Serde deserializer. Read more

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

compute the negation of this range set

∀ t ∈ T, r(t) = !a(t)

The resulting type after applying the ! operator.

Performs the unary ! operation. Read more

compute the negation of this range set

∀ t ∈ T, r(t) = !a(t)

The resulting type after applying the ! operator.

Performs the unary ! operation. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Serialize this value into the given Serde serializer. Read more

compute the difference of this range set with another, producing a new range set

∀ t ∈ T, r(t) = a(t) & !b(t)

The resulting type after applying the - operator.

Performs the - operation. Read more

Performs the -= operation. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.