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>
impl<K> HashSet<K>
Sourcepub fn with_size(size: usize) -> Self
pub fn with_size(size: usize) -> Self
Creates an empty HashSet with specified internal size. The size must be a power of 2
Sourcepub fn with_capacity(capacity: usize) -> Self
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>
impl<K, ALLOCATOR: ClonableAllocator> HashSet<K, ALLOCATOR>
Sourcepub fn with_size_in(size: usize, alloc: ALLOCATOR) -> Self
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
Sourcepub fn with_capacity_in(capacity: usize, alloc: ALLOCATOR) -> Self
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
Sourcepub fn allocator(&self) -> &ALLOCATOR
pub fn allocator(&self) -> &ALLOCATOR
Returns a reference to the underlying allocator
Source§impl<K, ALLOCATOR: ClonableAllocator> HashSet<K, ALLOCATOR>
impl<K, ALLOCATOR: ClonableAllocator> HashSet<K, ALLOCATOR>
Sourcepub fn insert(&mut self, value: K) -> bool
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,
trueis returned - If the set already contained this value,
falseis 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);Sourcepub fn remove<Q>(&mut self, value: &Q) -> bool
pub fn remove<Q>(&mut self, value: &Q) -> bool
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());Sourcepub fn contains<Q>(&self, value: &Q) -> bool
pub fn contains<Q>(&self, value: &Q) -> bool
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);Sourcepub fn difference<'a>(
&'a self,
other: &'a HashSet<K, ALLOCATOR>,
) -> impl Iterator<Item = &'a K>
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]));Sourcepub fn symmetric_difference<'a>(
&'a self,
other: &'a HashSet<K, ALLOCATOR>,
) -> impl Iterator<Item = &'a K>
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]));Sourcepub fn intersection<'a>(
&'a self,
other: &'a HashSet<K, ALLOCATOR>,
) -> impl Iterator<Item = &'a K>
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]));Sourcepub fn union<'a>(
&'a self,
other: &'a HashSet<K, ALLOCATOR>,
) -> impl Iterator<Item = &'a K>
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> Extend<K> for HashSet<K>
impl<K> Extend<K> for HashSet<K>
Source§fn extend<T: IntoIterator<Item = K>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = K>>(&mut self, iter: T)
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)