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

pub struct RangeSet<A: Array>(_);
Expand description

A set of non-overlapping ranges

let mut a: RangeSet2<i32> = RangeSet::from(10..);
let b: RangeSet2<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: RangeSetEntry. It uses a SmallVec<T> of sorted boundaries internally.

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

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      O(N)    
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

get the boundaries in this range set as a SmallVec

the empty range set

a range set containing all values

intersection in place

union in place

difference in place

symmetric difference in place (xor)

Trait Implementations

the boundaries as a reference - must be strictly sorted

convert to a normal range set

true if the value is contained in the range set

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 Read more

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

iterate over all ranges in this range set

intersection

union

difference

symmetric difference (xor)

the boundaries as a reference - must be strictly sorted

convert to a normal range set

true if the value is contained in the range set

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 Read more

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

iterate over all ranges in this range set

intersection

union

difference

symmetric difference (xor)

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

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

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)

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.