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
pub fn iter_difference<'a>(&'a self, other: &'a Self) -> Difference<'a, T>ⓘNotable traits for Difference<'a, T>
impl<'a, T: Ord> Iterator for Difference<'a, T> type Item = Segment<&'a T>;
where
T: Ord,
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.
pub fn iter_intersection<'a>(&'a self, other: &'a Self) -> Intersection<'a, T>ⓘNotable traits for Intersection<'a, T>
impl<'a, T: Ord> Iterator for Intersection<'a, T> type Item = Segment<&'a T>;
pub fn symmetric_difference_iter<'a>(
&'a self,
other: &'a Self
) -> SymmetricDifference<'a, T>ⓘNotable traits for SymmetricDifference<'a, T>
impl<'a, T: Ord> Iterator for SymmetricDifference<'a, T> type Item = Segment<&'a T>;
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
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
andSegmentMap::set
for the internal map’s insertion semantics. Because values are always()
and returning overwritten values is not necessary, this method usesset
.
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
SegmentMap::remove
andSegmentMap::clear_range
for the internal map’s removal semantics. However, this method will not allocate anything to return.SegmentSet::take
if you want the removed elements
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
SegmentMap::remove
andSegmentMap::clear_range
for the internal map’s removal semantics. However, this method will not allocate anything to return.SegmentSet::remove
if you don’t want the removed elements
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
SegmentMap::retain
, which is called internally
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
type Output = SegmentSet<T>
type Output = SegmentSet<T>
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]);
type Output = SegmentSet<&'a T>
type Output = SegmentSet<&'a T>
The resulting type after applying the &
operator.
type Output = SegmentSet<T>
type Output = SegmentSet<T>
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]);
type Output = SegmentSet<T>
type Output = SegmentSet<T>
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]);
type Output = SegmentSet<&'a T>
type Output = SegmentSet<&'a T>
The resulting type after applying the ^
operator.
Extends a collection with the contents of an iterator. Read more
extend_one
)Extends a collection with exactly one element.
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.
extend_one
)Extends a collection with exactly one element.
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
Set Complement
Set Difference
type Output = SegmentSet<&'a T>
type Output = SegmentSet<&'a T>
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
Auto Trait Implementations
impl<T> Send for SegmentSet<T> where
T: Send,
impl<T> Sync for SegmentSet<T> where
T: Sync,
impl<T> Unpin for SegmentSet<T> where
T: Unpin,
Blanket Implementations
Mutably borrows from an owned value. Read more