Struct segmap::set::SegmentSet[][src]

pub struct SegmentSet<T> { /* fields omitted */ }
Expand description

SegmentSet

A set based on a SegmentMap. Like SegmentMap, adjacent ranges will be merged into a single range.

See SegmentMap’s documentation for more details on implementation. The internal representation of this struct is is a SegmentMap<T, ()>

Examples

let mut set = SegmentSet::new();

// Add some ranges
set.insert(0..5);
set.insert(5..10); // Note, this will be merged with 0..5!
set.insert(20..25);

// Check if a point is covered
assert!(set.contains(&7));
assert!(!set.contains(&12));

// Remove a range (or parts of some ranges)
assert!(set.contains(&5));
assert!(set.contains(&24));
set.remove(3..6);
set.remove(22..);
assert!(!set.contains(&5));
assert!(!set.contains(&24));

// Check which ranges are covered
assert!(set.into_iter().eq(vec![
    Segment::from(0..3),
    Segment::from(6..10),
    Segment::from(20..22),
]));

Implementations

Converts the set into a Vec by chaining [into_iter] and [collect]

Return a set representing the difference of two sets

I.e. All elements in self that are not in other

If you need an iterator over the different items, use [SegmentSet::difference_iter], which is called here internally. However, this may be slightly faster if you need a set.

Check whether self and other are disjoint sets

That is, the intersection between self and other is empty

Check whether self is a subset of other

That is, all elements of self exist in other, or (as implemented) self.difference(other) is empty

Makes a new empty SegmentSet.

Make a new SegmentSet with a single range present, representing all possible values.


// Thus, this
let full = SegmentSet::<u32>::full();

// Is equivalent to
let mut manual = SegmentSet::new();
manual.insert(..);

assert_eq!(full, manual);

Clears the set, removing all elements.

Examples

let mut set = SegmentSet::new();
set.insert(0..1);
set.clear();
assert!(set.is_empty());

Returns true if any range in the set covers the specified value.

Examples

let mut set = SegmentSet::new();
set.insert(0..5);
assert!(set.contains(&3));

Returns a reference to the range covering the given value, if any.

Examples

let mut set = SegmentSet::new();
set.insert(0..5);

assert_eq!(set.get_range_for(&3), Some(&Segment::from(0..5)));
assert!(set.get_range_for(&6).is_none());

Insert a range into the set.

If the inserted range either overlaps or is immediately adjacent any existing range, then the ranges will be coalesced into a single contiguous range.

Examples

let mut set = SegmentSet::new();
set.insert(0..5);
assert!(!set.is_empty())

See Also

  • SegmentMap::insert and SegmentMap::set for the internal map’s insertion semantics. Because values are always () and returning overwritten values is not necessary, this method uses set.

Removes a range from the set returning if all or any of it was present.

Examples

let mut set = SegmentSet::new();
set.insert(0..5);
assert!(set.remove(0..2));

See Also

Removes a range from the set, returning a set containing the removed elements

Examples

let mut set = SegmentSet::new();
set.insert(0..5);
let removed = set.take(0..2);

See Also

Retains only the elements specified by the predicate.

In other words, remove all ranges f(v) returns false.

Examples

let mut set = SegmentSet::new();
set.insert(0..4);
set.insert(5..9);
set.insert(10..14);
set.insert(15..19);
set.insert(20..24);

// Keep only the ranges with even numbered starts
set.retain(|r| r.start_value().unwrap() % 2 == 0);

assert!(set.contains(&0));
assert!(set.contains(&10));
assert!(set.contains(&12));
assert!(set.contains(&20));
assert!(set.contains(&23));

assert!(!set.contains(&15));

See Also

Moves all elements from other into Self, leaving other empty.

Examples

let mut a = SegmentSet::new();
a.insert(0..1);
a.insert(1..2);
a.insert(2..3);

let mut b = SegmentSet::new();
b.insert(2..3);
b.insert(3..4);
b.insert(4..5);

a.append(&mut b);

// Ranges in a should all be coalesced to 0..5
assert!(a.into_iter().eq(vec![
    Segment::from(0..5)
]));
assert!(b.is_empty());

Split the set into two at the given bound. Returns everything including and after that bound.

Examples

Basic Usage

let mut a = SegmentSet::new();
a.insert(0..1);
a.insert(2..3);
a.insert(4..5);
a.insert(6..7);

let b = a.split_off(Bound::Included(4));

assert!(a.into_iter().eq(vec![
    Segment::from(0..1),
    Segment::from(2..3),
]));
assert!(b.into_iter().eq(vec![
    Segment::from(4..5),
    Segment::from(6..7),
]));

Mixed Bounds

let mut a = SegmentSet::new();
a.insert(0..7);

let c = a.split_off(Bound::Excluded(4));
let b = a.split_off(Bound::Included(2));

assert!(a.into_iter().eq(vec![
    Segment::from(0..2)
]));
assert!(b.into_iter().eq(vec![
    Segment::from(2..=4)
]));
assert!(c.into_iter().eq(vec![
    Segment::new(Bound::Excluded(4), Bound::Excluded(7))
]));

Trait Implementations

Set Union

The resulting type after applying the + operator.

Performs the + operation. Read more

Set Intersection A & B

Returns the intersection of self and rhs as a new BTreeSet<T>.

Examples

use std::collections::BTreeSet;

let a: BTreeSet<_> = vec![1, 2, 3].into_iter().collect();
let b: BTreeSet<_> = vec![2, 3, 4].into_iter().collect();

let result = &a & &b;
let result_vec: Vec<_> = result.into_iter().collect();
assert_eq!(result_vec, [2, 3]);

The resulting type after applying the & operator.

The resulting type after applying the & operator.

Performs the & operation. Read more

Set Union

Returns the union of self and rhs as a new BTreeSet<T>.

Examples

use std::collections::BTreeSet;

let a: BTreeSet<_> = vec![1, 2, 3].into_iter().collect();
let b: BTreeSet<_> = vec![3, 4, 5].into_iter().collect();

let result = &a | &b;
let result_vec: Vec<_> = result.into_iter().collect();
assert_eq!(result_vec, [1, 2, 3, 4, 5]);

The resulting type after applying the | operator.

Set Symmetric Difference

Returns the symmetric difference of self and rhs as a new BTreeSet<T>.

Examples

use std::collections::BTreeSet;

let a: BTreeSet<_> = vec![1, 2, 3].into_iter().collect();
let b: BTreeSet<_> = vec![2, 3, 4].into_iter().collect();

let result = &a ^ &b;
let result_vec: Vec<_> = result.into_iter().collect();
assert_eq!(result_vec, [1, 4]);

The resulting type after applying the ^ operator.

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

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

Extends a collection with the contents of an iterator. Read more

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

Extends a collection with exactly one element.

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

Reserves capacity in a collection for the given number of additional elements. Read more

Insert all the items from iter into self.

NOTE: Inserted items will overwrite existing ranges in self if they overlap. If you don’t want to overwrite existing ranges, use [extend_into_gaps].

Clone is required for insertion, since we can’t guarantee elements in iter are ordered or non-overlapping, so ranges may need to be split.

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

Extends a collection with exactly one element.

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

Reserves capacity in a collection for the given number of additional elements. Read more

Performs the conversion.

Creates a value from an iterator. Read more

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

Set Complement

The resulting type after applying the ! operator.

Performs the unary ! operation. Read more

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

Set Difference

The resulting type after applying the - operator.

Performs the - operation. Read more

Set Removal // TODO: self.into_difference() may be quicker for these?

Performs the -= operation. Read more

Set Removal

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.