HashSet

Struct HashSet 

Source
pub struct HashSet<K, ALLOCATOR: Allocator = Global> { /* private fields */ }
Expand description

A HashSet is implemented as a HashMap where the value is ().

As with the HashMap type, a HashSet requires that the elements implement the Eq and Hash traits, although this is frequently achieved by using #[derive(PartialEq, Eq, Hash)]. If you implement these yourself, it is important that the following property holds:

It is a logic error for the key to be modified in such a way that the key’s hash, as determined by the Hash trait, or its equality as determined by the Eq trait, changes while it is in the map. The behaviour for such a logic error is not specified, but will not result in undefined behaviour. This could include panics, incorrect results, aborts, memory leaks and non-termination.

The API surface provided is incredibly similar to the std::collections::HashSet implementation with fewer guarantees, and better optimised for the GameBoy Advance.

§Example

use agb_hashmap::HashSet;

// Type inference lets you omit the type signature (which would be HashSet<String> in this example)
let mut games = HashSet::new();

// Add some games
games.insert("Pokemon Emerald".to_string());
games.insert("Golden Sun".to_string());
games.insert("Super Dodge Ball Advance".to_string());

// Check for a specific game
if !games.contains("Legend of Zelda: The Minish Cap") {
    println!("We've got {} games, but The Minish Cap ain't one", games.len());
}

// Remove a game
games.remove("Golden Sun");

// Iterate over everything
for game in &games {
    println!("{game}");
}

Implementations§

Source§

impl<K> HashSet<K>

Source

pub const fn new() -> Self

Creates a HashSet

Source

pub fn with_size(size: usize) -> Self

Creates an empty HashSet with specified internal size. The size must be a power of 2

Source

pub fn with_capacity(capacity: usize) -> Self

Creates an empty HashSet which can hold at least capacity elements before resizing. The actual internal size may be larger as it must be a power of 2

Source§

impl<K, ALLOCATOR: ClonableAllocator> HashSet<K, ALLOCATOR>

Source

pub fn with_size_in(size: usize, alloc: ALLOCATOR) -> Self

Creates an empty HashSet with specified internal size using the specified allocator. The size must be a power of 2

Source

pub const fn new_in(alloc: ALLOCATOR) -> Self

Creates a HashSet with a specified allocator

Source

pub fn with_capacity_in(capacity: usize, alloc: ALLOCATOR) -> Self

Creates an empty HashSet which can hold at least capacity elements before resizing. The actual internal size may be larger as it must be a power of 2

§Panics

Panics if capacity >= 2^31 * 0.6

Source

pub fn allocator(&self) -> &ALLOCATOR

Returns a reference to the underlying allocator

Source

pub fn len(&self) -> usize

Returns the number of elements in the set

Source

pub fn is_empty(&self) -> bool

Returns whether or not the set is empty

Source

pub fn capacity(&self) -> usize

Returns the number of elements the set can hold without resizing

Source

pub fn clear(&mut self)

Removes all elements from the set

Source

pub fn iter(&self) -> impl Iterator<Item = &K>

An iterator visiting all the values in the set

Source

pub fn retain<F>(&mut self, f: F)
where F: FnMut(&K) -> bool,

Retains only the elements specified by the predicate f

Source§

impl<K, ALLOCATOR: ClonableAllocator> HashSet<K, ALLOCATOR>
where K: Eq + Hash,

Source

pub fn insert(&mut self, value: K) -> bool

Inserts a value into the set. This does not replace the value if it already existed.

Returns whether the value was newly inserted, that is:

  • If the set did not previously contain this value, true is returned
  • If the set already contained this value, false is returned.
§Examples
use agb_hashmap::HashSet;

let mut set = HashSet::new();
assert_eq!(set.insert(2), true);
assert_eq!(set.insert(2), false);
assert_eq!(set.len(), 1);
Source

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

Removes a value from the set. Returns whether the value was present in the set.

§Examples
use agb_hashmap::HashSet;

let mut set = HashSet::new();
set.insert(2);

assert_eq!(set.remove(&2), true);
assert_eq!(set.remove(&2), false);
assert!(set.is_empty());
Source

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

Returns true if the set contains the value value.

§Examples
use agb_hashmap::HashSet;

let set = HashSet::from([1, 2, 3]);
assert_eq!(set.contains(&1), true);
assert_eq!(set.contains(&4), false);
Source

pub fn difference<'a>( &'a self, other: &'a HashSet<K, ALLOCATOR>, ) -> impl Iterator<Item = &'a K>

Visits the values representing the difference i.e. the values that are in self but not in other.

§Examples
use agb_hashmap::HashSet;

let a = HashSet::from([1, 2, 3]);
let b = HashSet::from([4, 2, 3, 4]);

// Can be seen as `a - b`
let diff: HashSet<_> = a.difference(&b).collect();
assert_eq!(diff, HashSet::from([&1]));

// Difference is not symmetric. `b - a` means something different
let diff: HashSet<_> = b.difference(&a).collect();
assert_eq!(diff, HashSet::from([&4]));
Source

pub fn symmetric_difference<'a>( &'a self, other: &'a HashSet<K, ALLOCATOR>, ) -> impl Iterator<Item = &'a K>

Visits the values which are in self or other but not both.

§Examples
use agb_hashmap::HashSet;

let a = HashSet::from([1, 2, 3]);
let b = HashSet::from([4, 2, 3, 4]);

let diff1: HashSet<_> = a.symmetric_difference(&b).collect();
let diff2: HashSet<_> = b.symmetric_difference(&a).collect();

assert_eq!(diff1, diff2);
assert_eq!(diff1, HashSet::from([&1, &4]));
Source

pub fn intersection<'a>( &'a self, other: &'a HashSet<K, ALLOCATOR>, ) -> impl Iterator<Item = &'a K>

Visits the values in the intersection of self and other.

When an equal element is present in self and other, then the resulting intersection may yield references to one or the other. This can be relevant if K contains fields which are not covered by the Eq implementation.

§Examples
use agb_hashmap::HashSet;

let a = HashSet::from([1, 2, 3]);
let b = HashSet::from([4, 2, 3, 4]);

let intersection: HashSet<_> = a.intersection(&b).collect();
assert_eq!(intersection, HashSet::from([&2, &3]));
Source

pub fn union<'a>( &'a self, other: &'a HashSet<K, ALLOCATOR>, ) -> impl Iterator<Item = &'a K>

Visits the values in self and other without duplicates.

When an equal element is present in self and other, then the resulting union may yield references to one or the other. This can be relevant if K contains fields which are not covered by the Eq implementation.

§Examples
use agb_hashmap::HashSet;

let a = HashSet::from([1, 2, 3]);
let b = HashSet::from([4, 2, 3, 4]);

let union: Vec<_> = a.union(&b).collect();
assert_eq!(union.len(), 4);
assert_eq!(HashSet::from_iter(union), HashSet::from([&1, &2, &3, &4]));

Trait Implementations§

Source§

impl<K: Clone, ALLOCATOR: Clone + Allocator> Clone for HashSet<K, ALLOCATOR>

Source§

fn clone(&self) -> HashSet<K, ALLOCATOR>

Returns a duplicate 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<K, ALLOCATOR: ClonableAllocator> Debug for HashSet<K, ALLOCATOR>
where K: Debug,

Source§

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

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

impl<K> Default for HashSet<K>

Source§

fn default() -> Self

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

impl<K> Extend<K> for HashSet<K>
where K: Eq + Hash,

Source§

fn extend<T: IntoIterator<Item = K>>(&mut self, iter: T)

Extends a collection with the contents of an iterator. Read more
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<K, const N: usize> From<[K; N]> for HashSet<K>
where K: Eq + Hash,

Source§

fn from(value: [K; N]) -> Self

Converts to this type from the input type.
Source§

impl<K> FromIterator<K> for HashSet<K>
where K: Eq + Hash,

Source§

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

Creates a value from an iterator. Read more
Source§

impl<'a, K, ALLOCATOR: ClonableAllocator> IntoIterator for &'a HashSet<K, ALLOCATOR>

Source§

type Item = &'a K

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, K, ALLOCATOR>

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

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<K, ALLOCATOR: ClonableAllocator> IntoIterator for HashSet<K, ALLOCATOR>

Source§

type Item = K

The type of the elements being iterated over.
Source§

type IntoIter = IterOwned<K, ALLOCATOR>

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

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<K, ALLOCATOR: ClonableAllocator> PartialEq for HashSet<K, ALLOCATOR>
where K: Eq + Hash,

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<K, ALLOCATOR: ClonableAllocator> Eq for HashSet<K, ALLOCATOR>
where K: Eq + Hash,

Auto Trait Implementations§

§

impl<K, ALLOCATOR> Freeze for HashSet<K, ALLOCATOR>
where ALLOCATOR: Freeze,

§

impl<K, ALLOCATOR> RefUnwindSafe for HashSet<K, ALLOCATOR>
where ALLOCATOR: RefUnwindSafe, K: RefUnwindSafe,

§

impl<K, ALLOCATOR> Send for HashSet<K, ALLOCATOR>
where ALLOCATOR: Send, K: Send,

§

impl<K, ALLOCATOR> Sync for HashSet<K, ALLOCATOR>
where ALLOCATOR: Sync, K: Sync,

§

impl<K, ALLOCATOR> Unpin for HashSet<K, ALLOCATOR>
where ALLOCATOR: Unpin, K: Unpin,

§

impl<K, ALLOCATOR> UnwindSafe for HashSet<K, ALLOCATOR>
where ALLOCATOR: UnwindSafe, K: 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.