Skip to main content

HashSet

Struct HashSet 

Source
pub struct HashSet<K, ALLOCATOR = Global>
where ALLOCATOR: Allocator,
{ /* 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() -> HashSet<K>

Creates a HashSet

Source

pub fn with_size(size: usize) -> HashSet<K>

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

Source

pub fn with_capacity(capacity: usize) -> HashSet<K>

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> HashSet<K, ALLOCATOR>
where ALLOCATOR: ClonableAllocator,

Source

pub fn with_size_in(size: usize, alloc: ALLOCATOR) -> HashSet<K, ALLOCATOR>

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) -> HashSet<K, ALLOCATOR>

Creates a HashSet with a specified allocator

Source

pub fn with_capacity_in( capacity: usize, alloc: ALLOCATOR, ) -> HashSet<K, ALLOCATOR>

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> HashSet<K, ALLOCATOR>
where ALLOCATOR: ClonableAllocator, 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 get<Q>(&self, value: &Q) -> Option<&K>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Returns the value contained in the hash set if the set contains the value value.

§Examples
use agb_hashmap::HashSet;

let set = HashSet::from([1, 2, 3]);
assert_eq!(set.get(&2), Some(&2));
assert_eq!(set.get(&4), None);
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, ALLOCATOR> Clone for HashSet<K, ALLOCATOR>
where K: Clone, ALLOCATOR: Clone + Allocator,

Source§

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

Returns a duplicate of the value. Read more
1.0.0 (const: unstable)§

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

Performs copy-assignment from source. Read more
Source§

impl<K, ALLOCATOR> Debug for HashSet<K, ALLOCATOR>
where ALLOCATOR: ClonableAllocator, K: Debug,

Source§

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

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

impl<K> Default for HashSet<K>

Source§

fn default() -> HashSet<K>

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

impl<'de, K> Deserialize<'de> for HashSet<K>
where K: Deserialize<'de> + Hash + Eq,

Source§

fn deserialize<D>( deserializer: D, ) -> Result<HashSet<K>, <D as Deserializer<'de>>::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

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

Source§

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

Source§

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

Extends a collection with the contents of an iterator. Read more
§

fn extend_one(&mut self, item: A)

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

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]) -> HashSet<K>

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>(iter: T) -> HashSet<K>
where T: IntoIterator<Item = K>,

Creates a value from an iterator. Read more
Source§

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

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) -> <HashSet<K, ALLOCATOR> as IntoIterator>::IntoIter

Creates an iterator from a value. Read more
Source§

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

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) -> <&'a HashSet<K, ALLOCATOR> as IntoIterator>::IntoIter

Creates an iterator from a value. Read more
Source§

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

Source§

fn eq(&self, other: &HashSet<K, ALLOCATOR>) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable)§

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

Inequality operator !=. Read more
Source§

impl<K, ALLOCATOR> Serialize for HashSet<K, ALLOCATOR>
where K: Serialize, ALLOCATOR: ClonableAllocator,

Source§

fn serialize<S>( &self, serializer: S, ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more

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> UnsafeUnpin for HashSet<K, ALLOCATOR>
where ALLOCATOR: UnsafeUnpin,

§

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

Blanket Implementations§

§

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

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

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

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

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

§

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

Mutably borrows from an owned value. Read more
§

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

§

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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, W> HasTypeWitness<W> for T
where W: MakeTypeWitness<Arg = T>, T: ?Sized,

Source§

const WITNESS: W = W::MAKE

A constant of the type witness
Source§

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

Source§

const TYPE_EQ: TypeEq<T, <T as Identity>::Type> = TypeEq::NEW

Proof that Self is the same type as Self::Type, provides methods for casting between Self and Self::Type.
Source§

type Type = T

The same type as Self, used to emulate type equality bounds (T == U) with associated type equality constraints (T: Identity<Type = U>).
§

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

§

fn into(self) -> U

Calls U::from(self).

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

§

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

§

type Owned = T

The resulting type after obtaining ownership.
§

fn to_owned(&self) -> T

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

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

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

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

§

type Error = Infallible

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

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

Performs the conversion.
§

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

§

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

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

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

Performs the conversion.