pub struct Set<T: Copy + Eq + Hash> { /* private fields */ }
Expand description
A set that is a HashSet
when it has many elements, but is just
an array for small set sizes.
As with the HashSet
type, a Set
requires that the
elements implement the Eq and Hash traits. This can frequently be
achieved by using #[derive(PartialEq, Eq, Hash)]. In addition,
Set
requires that the elements implement the Copy
trait,
and really they should be pretty small, since Set always
stores room for CAPACITY
elements.
Implementations§
Source§impl<T: Copy + Eq + Hash> Set<T>
impl<T: Copy + Eq + Hash> Set<T>
Sourcepub fn with_capacity(cap: usize) -> Set<T>
pub fn with_capacity(cap: usize) -> Set<T>
Creates an empty set with the specified capacity.
Sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional
more elements to be
inserted in the set. The collection may reserve more space
to avoid frequent reallocations.
Sourcepub fn insert(&mut self, elem: T) -> bool
pub fn insert(&mut self, elem: T) -> bool
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.
Sourcepub fn remove<Q>(&mut self, value: &Q) -> bool
pub fn remove<Q>(&mut self, value: &Q) -> bool
Removes an element, and returns true if that element was present.
Trait Implementations§
Source§impl<'a, 'b, T: Eq + Hash + Copy> BitOr<&'b Set<T>> for &'a Set<T>
impl<'a, 'b, T: Eq + Hash + Copy> BitOr<&'b Set<T>> for &'a Set<T>
Source§fn bitor(self, rhs: &Set<T>) -> Set<T>
fn bitor(self, rhs: &Set<T>) -> Set<T>
Returns the union of self
and rhs
as a new Set<T>
.
§Examples
use david_set::Set;
let a: Set<u32> = vec![1, 2, 3].into_iter().collect();
let b: Set<u32> = vec![3, 4, 5].into_iter().collect();
let set = &a | &b;
let mut i = 0;
let expected = [1, 2, 3, 4, 5];
for x in &set {
assert!(expected.contains(x));
i += 1;
}
assert_eq!(i, expected.len());
Source§impl<T: Eq + Hash + Copy> Extend<T> for Set<T>
impl<T: Eq + Hash + Copy> Extend<T> for Set<T>
Source§fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
Adds a bunch of elements to the set
§Examples
use david_set::Set;
let mut a: Set<u32> = vec![1, 2, 3].into_iter().collect();
a.extend(vec![3, 4, 5]);
let mut i = 0;
let expected = [1, 2, 3, 4, 5];
for x in &a {
assert!(expected.contains(x));
i += 1;
}
assert_eq!(i, expected.len());
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Source§impl<T: Hash + Copy + Eq> FromIterator<T> for Set<T>
impl<T: Hash + Copy + Eq> FromIterator<T> for Set<T>
Source§fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
Source§impl<T: Eq + Hash + Copy> IntoIterator for Set<T>
impl<T: Eq + Hash + Copy> IntoIterator for Set<T>
Source§fn into_iter(self) -> IntoIter<T>
fn into_iter(self) -> IntoIter<T>
Creates a consuming iterator, that is, one that moves each value out of the set in arbitrary order. The set cannot be used after calling this.
§Examples
use david_set::Set;
let mut set: Set<u32> = Set::new();
set.insert(2);
set.insert(5);
// Not possible to collect to a Vec<String> with a regular `.iter()`.
let v: Vec<_> = set.into_iter().collect();
// Will print in an arbitrary order.
for x in &v {
println!("{}", x);
}
Source§impl<'a, 'b, T: Eq + Hash + Copy> Sub<&'b Set<T>> for &'a Set<T>
impl<'a, 'b, T: Eq + Hash + Copy> Sub<&'b Set<T>> for &'a Set<T>
Source§fn sub(self, rhs: &Set<T>) -> Set<T>
fn sub(self, rhs: &Set<T>) -> Set<T>
Returns the difference of self
and rhs
as a new Set<T>
.
§Examples
use david_set::Set;
let a: Set<u32> = vec![1, 2, 3].into_iter().collect();
let b: Set<u32> = vec![3, 4, 5].into_iter().collect();
let set = &a - &b;
let mut i = 0;
let expected = [1, 2];
for x in &set {
assert!(expected.contains(x));
i += 1;
}
assert_eq!(i, expected.len());