Struct scapegoat::SGSet [−][src]
pub struct SGSet<T: Ord> { /* fields omitted */ }Expand description
Ordered set.
API examples and descriptions are all adapted or directly copied from the standard library’s BTreeSet.
Implementations
#![no_std]: total capacity, e.g. maximum number of set elements.
Attempting to insert elements beyond capacity will panic.
If using std: fast capacity, e.g. number of set elements stored on the stack.
Elements inserted beyond capacity will be stored on the heap.
Examples
use scapegoat::SGSet;
let mut set: SGSet<i32> = SGSet::new();
assert!(set.capacity() > 0)Moves all elements from other into self, leaving other empty.
Examples
use scapegoat::SGSet;
let mut a = SGSet::new();
a.insert(1);
a.insert(2);
a.insert(3);
let mut b = SGSet::new();
b.insert(3);
b.insert(4);
b.insert(5);
a.append(&mut b);
assert_eq!(a.len(), 5);
assert_eq!(b.len(), 0);
assert!(a.contains(&1));
assert!(a.contains(&2));
assert!(a.contains(&3));
assert!(a.contains(&4));
assert!(a.contains(&5));Adds a value to the set.
If the set did not have this value present, true is returned.
If the set did have this value present, false is returned, and the entry is overwritten.
Examples
use scapegoat::SGSet;
let mut set = SGSet::new();
assert_eq!(set.insert(2), true);
assert_eq!(set.insert(2), false);
assert_eq!(set.len(), 1);Gets an iterator that visits the values in the SGSet in ascending order.
Examples
use scapegoat::SGSet;
let set: SGSet<usize> = [1, 2, 3].iter().cloned().collect();
let mut set_iter = set.iter();
assert_eq!(set_iter.next(), Some(&1));
assert_eq!(set_iter.next(), Some(&2));
assert_eq!(set_iter.next(), Some(&3));
assert_eq!(set_iter.next(), None);Values returned by the iterator are returned in ascending order:
use scapegoat::SGSet;
let set: SGSet<usize> = [3, 1, 2].iter().cloned().collect();
let mut set_iter = set.iter();
assert_eq!(set_iter.next(), Some(&1));
assert_eq!(set_iter.next(), Some(&2));
assert_eq!(set_iter.next(), Some(&3));
assert_eq!(set_iter.next(), None);Removes a value from the set. Returns whether the value was present in the set.
Examples
use scapegoat::SGSet;
let mut set = SGSet::new();
set.insert(2);
assert_eq!(set.remove(&2), true);
assert_eq!(set.remove(&2), false);Returns a reference to the value in the set, if any, that is equal to the given value.
Examples
use scapegoat::SGSet;
let set: SGSet<_> = [1, 2, 3].iter().cloned().collect();
assert_eq!(set.get(&2), Some(&2));
assert_eq!(set.get(&4), None);Clears the set, removing all values.
Examples
use scapegoat::SGSet;
let mut v = SGSet::new();
v.insert(1);
v.clear();
assert!(v.is_empty());;Returns true if the set contains a value.
Examples
use scapegoat::SGSet;
let set: SGSet<_> = [1, 2, 3].iter().cloned().collect();
assert_eq!(set.contains(&1), true);
assert_eq!(set.contains(&4), false);Returns a reference to the first/minium value in the set, if any.
Examples
use scapegoat::SGSet;
let mut map = SGSet::new();
assert_eq!(map.first(), None);
map.insert(1);
assert_eq!(map.first(), Some(&1));
map.insert(2);
assert_eq!(map.first(), Some(&1));Removes the first value from the set and returns it, if any. The first value is the minimum value that was in the set.
Examples
use scapegoat::SGSet;
let mut set = SGSet::new();
set.insert(1);
while let Some(n) = set.pop_first() {
assert_eq!(n, 1);
}
assert!(set.is_empty());Returns the last/maximum value in the set, if any.
Examples
use scapegoat::SGSet;
let mut map = SGSet::new();
assert_eq!(map.first(), None);
map.insert(1);
assert_eq!(map.last(), Some(&1));
map.insert(2);
assert_eq!(map.last(), Some(&2));Removes the last value from the set and returns it, if any. The last value is the maximum value that was in the set.
Examples
use scapegoat::SGSet;
let mut set = SGSet::new();
set.insert(1);
while let Some(n) = set.pop_last() {
assert_eq!(n, 1);
}
assert!(set.is_empty());Returns the number of elements in the set.
Examples
use scapegoat::SGSet;
let mut v = SGSet::new();
assert_eq!(v.len(), 0);
v.insert(1);
assert_eq!(v.len(), 1);Returns an iterator over values representing set difference, e.g., values in self but not in other, in ascending order.
Examples
use scapegoat::SGSet;
let mut a = SGSet::new();
a.insert(1);
a.insert(2);
let mut b = SGSet::new();
b.insert(2);
b.insert(3);
let diff: Vec<_> = a.difference(&b).cloned().collect();
assert_eq!(diff, [1]);Returns an iterator over values representing symmetric set difference, e.g., values in self or other but not both, in ascending order.
Examples
use scapegoat::SGSet;
let mut a = SGSet::new();
a.insert(1);
a.insert(2);
let mut b = SGSet::new();
b.insert(2);
b.insert(3);
let sym_diff: Vec<_> = a.symmetric_difference(&b).cloned().collect();
assert_eq!(sym_diff, [1, 3]);Returns an iterator over values representing set intersection, e.g., values in both self and other, in ascending order.
Examples
use scapegoat::SGSet;
let mut a = SGSet::new();
a.insert(1);
a.insert(2);
let mut b = SGSet::new();
b.insert(2);
b.insert(3);
let intersection: Vec<_> = a.intersection(&b).cloned().collect();
assert_eq!(intersection, [2]);Returns an iterator over values representing set union, e.g., values in self or other, in ascending order.
Examples
use scapegoat::SGSet;
let mut a = SGSet::new();
a.insert(1);
let mut b = SGSet::new();
b.insert(2);
let union: Vec<_> = a.union(&b).cloned().collect();
assert_eq!(union, [1, 2]);Returns true if the set contains no elements.
Examples
use scapegoat::SGSet;
let mut v = SGSet::new();
assert!(v.is_empty());
v.insert(1);
assert!(!v.is_empty());Returns true if self has no elements in common with other (empty intersection).
Examples
use scapegoat::SGSet;
let a: SGSet<_> = [1, 2, 3].iter().cloned().collect();
let mut b = SGSet::new();
assert_eq!(a.is_disjoint(&b), true);
b.insert(4);
assert_eq!(a.is_disjoint(&b), true);
b.insert(1);
assert_eq!(a.is_disjoint(&b), false);Returns true if self is a subset of other, e.g., other contains at least all the values in self.
Examples
use scapegoat::SGSet;
let sup: SGSet<_> = [1, 2, 3].iter().cloned().collect();
let mut set = SGSet::new();
assert_eq!(set.is_subset(&sup), true);
set.insert(2);
assert_eq!(set.is_subset(&sup), true);
set.insert(4);
assert_eq!(set.is_subset(&sup), false);Returns true if self is a superset of other, e.g., self contains at least all the values in other.
Examples
use scapegoat::SGSet;
let sub: SGSet<_> = [1, 2].iter().cloned().collect();
let mut set = SGSet::new();
assert_eq!(set.is_superset(&sub), false);
set.insert(0);
set.insert(1);
assert_eq!(set.is_superset(&sub), false);
set.insert(2);
assert_eq!(set.is_superset(&sub), true);Trait Implementations
Returns the intersection of self and rhs as a new SGSet<T>.
Examples
use scapegoat::SGSet;
let a: SGSet<_> = vec![1, 2, 3].into_iter().collect();
let b: SGSet<_> = 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]);Returns the union of self and rhs as a new SGSet<T>.
Examples
use scapegoat::SGSet;
let a: SGSet<_> = vec![1, 2, 3].into_iter().collect();
let b: SGSet<_> = 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]);Returns the symmetric difference of self and rhs as a new SGSet<T>.
Examples
use scapegoat::SGSet;
let a: SGSet<_> = vec![1, 2, 3].into_iter().collect();
let b: SGSet<_> = 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]);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
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
Creates a value from an iterator. Read more
Returns the difference of self and rhs as a new SGSet<T>.
Examples
use scapegoat::SGSet;
let a: SGSet<_> = vec![1, 2, 3].into_iter().collect();
let b: SGSet<_> = 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]);