Struct Set

Source
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>

Source

pub fn new() -> Set<T>

Creates an empty set..

Source

pub fn with_capacity(cap: usize) -> Set<T>

Creates an empty set with the specified capacity.

Source

pub fn len(&self) -> usize

Returns the number of elements in the set.

Source

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.

Source

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.

Source

pub fn remove<Q>(&mut self, value: &Q) -> bool
where T: Borrow<Q>, Q: Hash + Eq + ?Sized,

Removes an element, and returns true if that element was present.

Source

pub fn contains<Q>(&self, value: &Q) -> bool
where T: Borrow<Q>, Q: Hash + Eq + ?Sized,

Returns true if the set contains a value.

Source

pub fn iter(&self) -> Iter<'_, T>

Returns an iterator over the set.

Source

pub fn drain(&mut self) -> IntoIter<T>

Clears the set, returning all elements in an iterator.

Trait Implementations§

Source§

impl<'a, 'b, T: Eq + Hash + Copy> BitOr<&'b Set<T>> for &'a Set<T>

Source§

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§

type Output = Set<T>

The resulting type after applying the | operator.
Source§

impl<T: Clone + Copy + Eq + Hash> Clone for Set<T>

Source§

fn clone(&self) -> Set<T>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug + Copy + Eq + Hash> Debug for Set<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Eq + Hash + Copy> Extend<T> for Set<T>

Source§

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)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<T: Hash + Copy + Eq> FromIterator<T> for Set<T>

Source§

fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<'a, T: Eq + Hash + Copy> IntoIterator for &'a Set<T>

Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Iter<'a, T>

Creates an iterator from a value. Read more
Source§

impl<T: Eq + Hash + Copy> IntoIterator for Set<T>

Source§

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§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<T>

Which kind of iterator are we turning this into?
Source§

impl<'a, 'b, T: Eq + Hash + Copy> Sub<&'b Set<T>> for &'a Set<T>

Source§

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());
Source§

type Output = Set<T>

The resulting type after applying the - operator.

Auto Trait Implementations§

§

impl<T> Freeze for Set<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Set<T>
where T: RefUnwindSafe,

§

impl<T> Send for Set<T>
where T: Send,

§

impl<T> Sync for Set<T>
where T: Sync,

§

impl<T> Unpin for Set<T>
where T: Unpin,

§

impl<T> UnwindSafe for Set<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.