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>
impl<K> HashSet<K>
Sourcepub fn with_size(size: usize) -> HashSet<K>
pub fn with_size(size: usize) -> HashSet<K>
Creates an empty HashSet with specified internal size. The size must be a power of 2
Sourcepub fn with_capacity(capacity: usize) -> HashSet<K>
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,
impl<K, ALLOCATOR> HashSet<K, ALLOCATOR>where
ALLOCATOR: ClonableAllocator,
Sourcepub fn with_size_in(size: usize, alloc: ALLOCATOR) -> HashSet<K, ALLOCATOR>
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
Sourcepub const fn new_in(alloc: ALLOCATOR) -> HashSet<K, ALLOCATOR>
pub const fn new_in(alloc: ALLOCATOR) -> HashSet<K, ALLOCATOR>
Creates a HashSet with a specified allocator
Sourcepub fn with_capacity_in(
capacity: usize,
alloc: ALLOCATOR,
) -> HashSet<K, ALLOCATOR>
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§impl<K, ALLOCATOR> HashSet<K, ALLOCATOR>where
ALLOCATOR: ClonableAllocator,
K: Eq + Hash,
impl<K, ALLOCATOR> HashSet<K, ALLOCATOR>where
ALLOCATOR: ClonableAllocator,
K: Eq + Hash,
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) -> boolwhere
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
pub fn remove<Q>(&mut self, value: &Q) -> boolwhere
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());Sourcepub fn contains<Q>(&self, value: &Q) -> boolwhere
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
pub fn contains<Q>(&self, value: &Q) -> boolwhere
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);Sourcepub fn get<Q>(&self, value: &Q) -> Option<&K>where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
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);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, ALLOCATOR> Clone for HashSet<K, ALLOCATOR>where
K: Clone,
ALLOCATOR: Clone + Allocator,
impl<K, ALLOCATOR> Clone for HashSet<K, ALLOCATOR>where
K: Clone,
ALLOCATOR: Clone + Allocator,
Source§impl<K, ALLOCATOR> Debug for HashSet<K, ALLOCATOR>where
ALLOCATOR: ClonableAllocator,
K: Debug,
impl<K, ALLOCATOR> Debug for HashSet<K, ALLOCATOR>where
ALLOCATOR: ClonableAllocator,
K: Debug,
Source§impl<'de, K> Deserialize<'de> for HashSet<K>where
K: Deserialize<'de> + Hash + Eq,
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>,
fn deserialize<D>(
deserializer: D,
) -> Result<HashSet<K>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
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,
impl<K> Extend<K> for HashSet<K>where
K: Eq + Hash,
Source§fn extend<T>(&mut self, iter: T)where
T: IntoIterator<Item = K>,
fn extend<T>(&mut self, iter: T)where
T: IntoIterator<Item = K>,
§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<K, ALLOCATOR> IntoIterator for HashSet<K, ALLOCATOR>where
ALLOCATOR: ClonableAllocator,
impl<K, ALLOCATOR> IntoIterator for HashSet<K, ALLOCATOR>where
ALLOCATOR: ClonableAllocator,
Source§impl<'a, K, ALLOCATOR> IntoIterator for &'a HashSet<K, ALLOCATOR>where
ALLOCATOR: ClonableAllocator,
impl<'a, K, ALLOCATOR> IntoIterator for &'a HashSet<K, ALLOCATOR>where
ALLOCATOR: ClonableAllocator,
Source§impl<K, ALLOCATOR> PartialEq for HashSet<K, ALLOCATOR>where
ALLOCATOR: ClonableAllocator,
K: Eq + Hash,
impl<K, ALLOCATOR> PartialEq for HashSet<K, ALLOCATOR>where
ALLOCATOR: ClonableAllocator,
K: Eq + Hash,
Source§impl<K, ALLOCATOR> Serialize for HashSet<K, ALLOCATOR>where
K: Serialize,
ALLOCATOR: ClonableAllocator,
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,
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
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 Twhere
T: 'static + ?Sized,
impl<T> Any for Twhere
T: 'static + ?Sized,
§impl<T> Borrow<T> for Twhere
T: ?Sized,
impl<T> Borrow<T> for Twhere
T: ?Sized,
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§unsafe fn clone_to_uninit(&self, dest: *mut u8)
unsafe fn clone_to_uninit(&self, dest: *mut u8)
clone_to_uninit)