Struct agb_hashmap::HashMap
source · pub struct HashMap<K, V, ALLOCATOR: Allocator = Global> { /* private fields */ }
Expand description
A hash map implemented very simply using robin hood hashing.
HashMap
uses FxHasher
internally, which is a very fast hashing algorithm used
by rustc and firefox in non-adversarial places. It is incredibly fast, and good
enough for most cases.
It is required that the keys implement the Eq
and Hash
traits, although this
can be frequently achieved by using #[derive(PartialEq, Eq, Hash)]
. If you
implement these yourself, it is important that the following property holds:
k1 == k2 => hash(k1) == hash(k2)
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::HashMap
implementation with fewer guarantees, and better optimised for the GameBoy Advance
.
§Example
use agb_hashmap::HashMap;
// Type inference lets you omit the type signature (which would be HashMap<String, String> in this example)
let mut game_reviews = HashMap::new();
// Review some games
game_reviews.insert(
"Pokemon Emerald".to_string(),
"Best post-game battle experience of any generation.".to_string(),
);
game_reviews.insert(
"Golden Sun".to_string(),
"Some of the best music on the console".to_string(),
);
game_reviews.insert(
"Super Dodge Ball Advance".to_string(),
"Really great launch title".to_string(),
);
// Check for a specific entry
if !game_reviews.contains_key("Legend of Zelda: The Minish Cap") {
println!("We've got {} reviews, but The Minish Cap ain't one", game_reviews.len());
}
// Iterate over everything
for (game, review) in &game_reviews {
println!("{game}: \"{review}\"");
}
Implementations§
source§impl<K, V> HashMap<K, V>
impl<K, V> HashMap<K, V>
sourcepub fn with_size(size: usize) -> Self
pub fn with_size(size: usize) -> Self
Creates an empty HashMap
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 HashMap
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, V, ALLOCATOR: ClonableAllocator> HashMap<K, V, ALLOCATOR>
impl<K, V, ALLOCATOR: ClonableAllocator> HashMap<K, V, ALLOCATOR>
sourcepub fn with_size_in(size: usize, alloc: ALLOCATOR) -> Self
pub fn with_size_in(size: usize, alloc: ALLOCATOR) -> Self
Creates an empty HashMap
with specified internal size using the
specified allocator. The size must be a power of 2
sourcepub fn allocator(&self) -> &ALLOCATOR
pub fn allocator(&self) -> &ALLOCATOR
Returns a reference to the underlying allocator
sourcepub fn with_capacity_in(capacity: usize, alloc: ALLOCATOR) -> Self
pub fn with_capacity_in(capacity: usize, alloc: ALLOCATOR) -> Self
Creates an empty HashMap
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 is larger than 2^32 * .85
sourcepub fn keys(&self) -> impl Iterator<Item = &K>
pub fn keys(&self) -> impl Iterator<Item = &K>
An iterator visiting all keys in an arbitrary order
sourcepub fn values(&self) -> impl Iterator<Item = &V>
pub fn values(&self) -> impl Iterator<Item = &V>
An iterator visiting all values in an arbitrary order
sourcepub fn values_mut(&mut self) -> impl Iterator<Item = &mut V>
pub fn values_mut(&mut self) -> impl Iterator<Item = &mut V>
An iterator visiting all values in an arbitrary order allowing for mutation
sourcepub fn iter(&self) -> impl Iterator<Item = (&K, &V)>
pub fn iter(&self) -> impl Iterator<Item = (&K, &V)>
An iterator visiting all key-value pairs in an arbitrary order
sourcepub fn iter_mut(&mut self) -> impl Iterator<Item = (&K, &mut V)>
pub fn iter_mut(&mut self) -> impl Iterator<Item = (&K, &mut V)>
An iterator visiting all key-value pairs in an arbitrary order, with mutable references to the values
source§impl<K, V, ALLOCATOR: ClonableAllocator> HashMap<K, V, ALLOCATOR>
impl<K, V, ALLOCATOR: ClonableAllocator> HashMap<K, V, ALLOCATOR>
sourcepub fn insert(&mut self, key: K, value: V) -> Option<V>
pub fn insert(&mut self, key: K, value: V) -> Option<V>
Inserts a key-value pair into the map.
If the map did not have this key present, None
is returned.
If the map did have this key present, the value is updated and the old value
is returned. The key is not updated, which matters for types that can be ==
without being identical.
sourcepub fn contains_key<Q>(&self, k: &Q) -> bool
pub fn contains_key<Q>(&self, k: &Q) -> bool
Returns true
if the map contains a value for the specified key.
sourcepub fn get_key_value<Q>(&self, key: &Q) -> Option<(&K, &V)>
pub fn get_key_value<Q>(&self, key: &Q) -> Option<(&K, &V)>
Returns the key-value pair corresponding to the supplied key
sourcepub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
Returns a mutable reference to the value corresponding to the key. Return None
if
there is no element in the map with the given key.
§Example
use agb_hashmap::HashMap;
let mut map = HashMap::new();
map.insert("a".to_string(), "A");
if let Some(x) = map.get_mut("a") {
*x = "b";
}
assert_eq!(map["a"], "b");
Trait Implementations§
source§impl<K, V, ALLOCATOR: ClonableAllocator> Debug for HashMap<K, V, ALLOCATOR>
impl<K, V, ALLOCATOR: ClonableAllocator> Debug for HashMap<K, V, ALLOCATOR>
source§impl<K, V> Extend<(K, V)> for HashMap<K, V>
impl<K, V> Extend<(K, V)> for HashMap<K, V>
source§fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = (K, V)>>(&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
)