2022-03-22 08:18:56 +11:00
|
|
|
//! A lot of the documentation for this module was copied straight out of the rust
|
|
|
|
//! standard library. The implementation however is not.
|
2023-04-24 02:39:00 +10:00
|
|
|
#![no_std]
|
|
|
|
#![feature(allocator_api)]
|
|
|
|
#![deny(clippy::all)]
|
2023-04-24 02:58:45 +10:00
|
|
|
#![deny(clippy::must_use_candidate)]
|
2023-04-24 02:39:00 +10:00
|
|
|
#![deny(missing_docs)]
|
2023-04-24 02:58:45 +10:00
|
|
|
#![deny(clippy::trivially_copy_pass_by_ref)]
|
|
|
|
#![deny(clippy::semicolon_if_nothing_returned)]
|
|
|
|
#![deny(clippy::map_unwrap_or)]
|
|
|
|
#![deny(clippy::needless_pass_by_value)]
|
|
|
|
#![deny(clippy::redundant_closure_for_method_calls)]
|
|
|
|
#![deny(clippy::cloned_instead_of_copied)]
|
|
|
|
#![deny(rustdoc::broken_intra_doc_links)]
|
|
|
|
#![deny(rustdoc::private_intra_doc_links)]
|
|
|
|
#![deny(rustdoc::invalid_html_tags)]
|
2023-05-10 06:46:57 +10:00
|
|
|
#![deny(unreachable_pub)]
|
2023-05-10 06:55:14 +10:00
|
|
|
#![deny(clippy::missing_safety_doc)]
|
|
|
|
#![deny(clippy::undocumented_unsafe_blocks)]
|
2023-04-24 02:39:00 +10:00
|
|
|
|
|
|
|
extern crate alloc;
|
|
|
|
|
2023-05-10 05:57:33 +10:00
|
|
|
use alloc::alloc::Global;
|
2022-03-18 09:46:38 +11:00
|
|
|
use core::{
|
2022-08-05 04:27:08 +10:00
|
|
|
alloc::Allocator,
|
2023-04-24 03:37:30 +10:00
|
|
|
borrow::Borrow,
|
2023-05-10 06:12:22 +10:00
|
|
|
fmt::Debug,
|
2022-03-18 09:46:38 +11:00
|
|
|
hash::{BuildHasher, BuildHasherDefault, Hash, Hasher},
|
2022-08-05 04:27:08 +10:00
|
|
|
iter::FromIterator,
|
2022-03-21 02:38:39 +11:00
|
|
|
ops::Index,
|
2022-03-18 09:46:38 +11:00
|
|
|
};
|
|
|
|
|
|
|
|
use rustc_hash::FxHasher;
|
|
|
|
|
2023-05-10 05:43:23 +10:00
|
|
|
mod node;
|
2023-05-10 05:57:33 +10:00
|
|
|
mod node_storage;
|
2023-05-10 05:43:23 +10:00
|
|
|
|
|
|
|
use node::Node;
|
2023-05-10 05:57:33 +10:00
|
|
|
use node_storage::NodeStorage;
|
2023-05-10 05:43:23 +10:00
|
|
|
|
2022-03-18 09:46:38 +11:00
|
|
|
type HashType = u32;
|
|
|
|
|
2022-03-22 08:16:36 +11:00
|
|
|
// # Robin Hood Hash Tables
|
|
|
|
//
|
|
|
|
// The problem with regular hash tables where failing to find a slot for a specific
|
|
|
|
// key will result in a linear search for the first free slot is that often these
|
|
|
|
// slots can end up being quite far away from the original chosen location in fuller
|
|
|
|
// hash tables. In Java, the hash table will resize when it is more than 2 thirds full
|
|
|
|
// which is quite wasteful in terms of space. Robin Hood hash tables can be much
|
|
|
|
// fuller before needing to resize and also keeps search times lower.
|
|
|
|
//
|
|
|
|
// The key concept is to keep the distance from the initial bucket chosen for a given
|
|
|
|
// key to a minimum. We shall call this distance the "distance to the initial bucket"
|
|
|
|
// or DIB for short. With each key - value pair, we store its DIB. When inserting
|
2022-11-18 08:15:48 +11:00
|
|
|
// a value into the hash table, we check to see if there is an element in the initial
|
2022-03-22 08:16:36 +11:00
|
|
|
// bucket. If there is, we move onto the next value. Then, we check to see if there
|
|
|
|
// is already a value there and if there is, we check its DIB. If our DIB is greater
|
|
|
|
// than or equal to the DIB of the value that is already there, we swap the working
|
|
|
|
// value and the current entry. This continues until an empty slot is found.
|
|
|
|
//
|
|
|
|
// Using this technique, the average DIB is kept fairly low which decreases search
|
|
|
|
// times. As a simple search time optimisation, the maximum DIB is kept track of
|
|
|
|
// and so we will only need to search as far as that in order to know whether or
|
|
|
|
// not a given element is in the hash table.
|
|
|
|
//
|
|
|
|
// # Deletion
|
|
|
|
//
|
|
|
|
// Special mention is given to deletion. Unfortunately, the maximum DIB is not
|
|
|
|
// kept track of after deletion, since we would not only need to keep track of
|
|
|
|
// the maximum DIB but also the number of elements which have that maximum DIB.
|
|
|
|
//
|
|
|
|
// In order to delete an element, we search to see if it exists. If it does,
|
|
|
|
// we remove that element and then iterate through the array from that point
|
|
|
|
// and move each element back one space (updating its DIB). If the DIB of the
|
|
|
|
// element we are trying to remove is 0, then we stop this algorithm.
|
|
|
|
//
|
|
|
|
// This means that deletion will lower the average DIB of the elements and
|
|
|
|
// keep searching and insertion fast.
|
|
|
|
//
|
|
|
|
// # Rehashing
|
|
|
|
//
|
|
|
|
// Currently, no incremental rehashing takes place. Once the HashMap becomes
|
|
|
|
// more than 85% full (this value may change when I do some benchmarking),
|
|
|
|
// a new list is allocated with double the capacity and the entire node list
|
|
|
|
// is migrated.
|
|
|
|
|
|
|
|
/// A hash map implemented very simply using robin hood hashing.
|
|
|
|
///
|
2022-06-12 05:33:32 +10:00
|
|
|
/// `HashMap` uses `FxHasher` internally, which is a very fast hashing algorithm used
|
2022-03-22 08:16:36 +11:00
|
|
|
/// 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
|
2022-06-12 05:33:32 +10:00
|
|
|
/// [`std::collections::HashMap`](https://doc.rust-lang.org/std/collections/struct.HashMap.html)
|
2022-03-22 08:16:36 +11:00
|
|
|
/// implementation with fewer guarantees, and better optimised for the GameBoy Advance.
|
|
|
|
///
|
|
|
|
/// [`Eq`]: https://doc.rust-lang.org/core/cmp/trait.Eq.html
|
|
|
|
/// [`Hash`]: https://doc.rust-lang.org/core/hash/trait.Hash.html
|
2023-04-24 03:37:30 +10:00
|
|
|
///
|
|
|
|
/// # 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}\"");
|
|
|
|
/// }
|
|
|
|
/// ```
|
2023-05-10 06:05:00 +10:00
|
|
|
#[derive(Clone)]
|
2022-08-05 04:27:08 +10:00
|
|
|
pub struct HashMap<K, V, ALLOCATOR: Allocator = Global> {
|
|
|
|
nodes: NodeStorage<K, V, ALLOCATOR>,
|
2022-03-18 09:46:38 +11:00
|
|
|
|
2022-03-22 08:16:36 +11:00
|
|
|
hasher: BuildHasherDefault<FxHasher>,
|
2022-03-18 09:46:38 +11:00
|
|
|
}
|
|
|
|
|
2022-08-05 04:27:08 +10:00
|
|
|
/// Trait for allocators that are clonable, blanket implementation for all types that implement Allocator and Clone
|
|
|
|
pub trait ClonableAllocator: Allocator + Clone {}
|
|
|
|
impl<T: Allocator + Clone> ClonableAllocator for T {}
|
|
|
|
|
2022-03-18 09:46:38 +11:00
|
|
|
impl<K, V> HashMap<K, V> {
|
2022-03-22 08:16:36 +11:00
|
|
|
/// Creates a `HashMap`
|
2022-06-12 05:56:05 +10:00
|
|
|
#[must_use]
|
2022-03-18 09:46:38 +11:00
|
|
|
pub fn new() -> Self {
|
2022-08-05 04:27:08 +10:00
|
|
|
Self::new_in(Global)
|
2022-03-18 10:54:42 +11:00
|
|
|
}
|
|
|
|
|
2022-03-22 08:16:36 +11:00
|
|
|
/// Creates an empty `HashMap` with specified internal size. The size must be a power of 2
|
2022-06-12 05:56:05 +10:00
|
|
|
#[must_use]
|
2022-03-22 08:16:36 +11:00
|
|
|
pub fn with_size(size: usize) -> Self {
|
2022-08-05 04:27:08 +10:00
|
|
|
Self::with_size_in(size, Global)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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
|
|
|
|
#[must_use]
|
|
|
|
pub fn with_capacity(capacity: usize) -> Self {
|
|
|
|
Self::with_capacity_in(capacity, Global)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<K, V, ALLOCATOR: ClonableAllocator> HashMap<K, V, ALLOCATOR> {
|
|
|
|
#[must_use]
|
|
|
|
/// Creates an empty `HashMap` with specified internal size using the
|
|
|
|
/// specified allocator. The size must be a power of 2
|
|
|
|
pub fn with_size_in(size: usize, alloc: ALLOCATOR) -> Self {
|
2022-03-18 09:46:38 +11:00
|
|
|
Self {
|
2022-08-05 04:27:08 +10:00
|
|
|
nodes: NodeStorage::with_size_in(size, alloc),
|
2022-03-18 09:46:38 +11:00
|
|
|
hasher: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
2022-03-18 10:18:49 +11:00
|
|
|
|
2022-08-05 04:27:08 +10:00
|
|
|
#[must_use]
|
|
|
|
/// Creates a `HashMap` with a specified allocator
|
|
|
|
pub fn new_in(alloc: ALLOCATOR) -> Self {
|
|
|
|
Self::with_size_in(16, alloc)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a reference to the underlying allocator
|
|
|
|
pub fn allocator(&self) -> &ALLOCATOR {
|
|
|
|
self.nodes.allocator()
|
|
|
|
}
|
|
|
|
|
2022-03-22 08:16:36 +11:00
|
|
|
/// 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
|
2022-06-12 05:56:05 +10:00
|
|
|
#[must_use]
|
2022-08-05 04:27:08 +10:00
|
|
|
pub fn with_capacity_in(capacity: usize, alloc: ALLOCATOR) -> Self {
|
2022-03-22 08:16:36 +11:00
|
|
|
for i in 0..32 {
|
|
|
|
let attempted_size = 1usize << i;
|
2022-03-25 05:31:18 +11:00
|
|
|
if number_before_resize(attempted_size) > capacity {
|
2022-08-05 04:27:08 +10:00
|
|
|
return Self::with_size_in(attempted_size, alloc);
|
2022-03-22 08:16:36 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
panic!(
|
|
|
|
"Failed to come up with a size which satisfies capacity {}",
|
|
|
|
capacity
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the number of elements in the map
|
2022-06-12 05:56:05 +10:00
|
|
|
#[must_use]
|
2022-03-18 10:18:49 +11:00
|
|
|
pub fn len(&self) -> usize {
|
2022-03-20 09:04:11 +11:00
|
|
|
self.nodes.len()
|
2022-03-18 10:18:49 +11:00
|
|
|
}
|
2022-03-18 11:18:39 +11:00
|
|
|
|
2022-03-22 08:16:36 +11:00
|
|
|
/// Returns the number of elements the map can hold
|
2022-06-12 05:56:05 +10:00
|
|
|
#[must_use]
|
2022-03-22 07:33:48 +11:00
|
|
|
pub fn capacity(&self) -> usize {
|
2022-03-25 05:31:18 +11:00
|
|
|
self.nodes.capacity()
|
2022-03-22 07:33:48 +11:00
|
|
|
}
|
|
|
|
|
2022-03-22 08:16:36 +11:00
|
|
|
/// An iterator visiting all keys in an arbitrary order
|
2022-03-22 07:33:48 +11:00
|
|
|
pub fn keys(&self) -> impl Iterator<Item = &'_ K> {
|
|
|
|
self.iter().map(|(k, _)| k)
|
|
|
|
}
|
|
|
|
|
2022-03-22 08:16:36 +11:00
|
|
|
/// An iterator visiting all values in an arbitrary order
|
2022-03-22 07:33:48 +11:00
|
|
|
pub fn values(&self) -> impl Iterator<Item = &'_ V> {
|
|
|
|
self.iter().map(|(_, v)| v)
|
|
|
|
}
|
|
|
|
|
2022-03-22 08:16:36 +11:00
|
|
|
/// An iterator visiting all values in an arbitrary order allowing for mutation
|
2022-03-22 07:33:48 +11:00
|
|
|
pub fn values_mut(&mut self) -> impl Iterator<Item = &'_ mut V> {
|
|
|
|
self.iter_mut().map(|(_, v)| v)
|
|
|
|
}
|
|
|
|
|
2022-03-22 08:16:36 +11:00
|
|
|
/// Removes all elements from the map
|
2022-03-22 07:33:48 +11:00
|
|
|
pub fn clear(&mut self) {
|
2022-08-05 04:27:08 +10:00
|
|
|
self.nodes =
|
|
|
|
NodeStorage::with_size_in(self.nodes.backing_vec_size(), self.allocator().clone());
|
2022-03-22 07:33:48 +11:00
|
|
|
}
|
|
|
|
|
2022-03-22 08:16:36 +11:00
|
|
|
/// An iterator visiting all key-value pairs in an arbitrary order
|
2022-03-22 07:33:48 +11:00
|
|
|
pub fn iter(&self) -> impl Iterator<Item = (&'_ K, &'_ V)> {
|
2022-10-03 05:10:50 +11:00
|
|
|
Iter {
|
|
|
|
map: self,
|
|
|
|
at: 0,
|
|
|
|
num_found: 0,
|
|
|
|
}
|
2022-03-22 07:33:48 +11:00
|
|
|
}
|
|
|
|
|
2022-03-22 08:16:36 +11:00
|
|
|
/// An iterator visiting all key-value pairs in an arbitrary order, with mutable references to the values
|
2022-03-22 07:33:48 +11:00
|
|
|
pub fn iter_mut(&mut self) -> impl Iterator<Item = (&'_ K, &'_ mut V)> {
|
2023-05-10 05:57:33 +10:00
|
|
|
self.nodes.iter_mut().filter_map(Node::key_value_mut)
|
2022-03-22 07:33:48 +11:00
|
|
|
}
|
|
|
|
|
2022-10-03 04:55:21 +11:00
|
|
|
/// Retains only the elements specified by the predicate `f`.
|
|
|
|
pub fn retain<F>(&mut self, f: F)
|
|
|
|
where
|
|
|
|
F: FnMut(&K, &mut V) -> bool,
|
|
|
|
{
|
|
|
|
self.nodes.retain(f);
|
|
|
|
}
|
|
|
|
|
2022-03-22 08:16:36 +11:00
|
|
|
/// Returns `true` if the map contains no elements
|
2022-06-12 05:56:05 +10:00
|
|
|
#[must_use]
|
2022-03-19 08:11:53 +11:00
|
|
|
pub fn is_empty(&self) -> bool {
|
2022-03-20 09:04:11 +11:00
|
|
|
self.len() == 0
|
2022-03-19 08:11:53 +11:00
|
|
|
}
|
|
|
|
|
2022-03-22 08:16:36 +11:00
|
|
|
fn resize(&mut self, new_size: usize) {
|
2022-03-18 11:18:39 +11:00
|
|
|
assert!(
|
2022-03-25 05:31:18 +11:00
|
|
|
new_size >= self.nodes.backing_vec_size(),
|
2022-03-18 11:18:39 +11:00
|
|
|
"Can only increase the size of a hash map"
|
|
|
|
);
|
2022-03-25 05:31:18 +11:00
|
|
|
if new_size == self.nodes.backing_vec_size() {
|
2022-03-18 11:18:39 +11:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-21 00:50:04 +11:00
|
|
|
self.nodes = self.nodes.resized_to(new_size);
|
2022-03-18 11:18:39 +11:00
|
|
|
}
|
2022-03-18 09:46:38 +11:00
|
|
|
}
|
|
|
|
|
2022-03-18 11:21:55 +11:00
|
|
|
impl<K, V> Default for HashMap<K, V> {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-05 04:27:08 +10:00
|
|
|
impl<K, V, ALLOCATOR: ClonableAllocator> HashMap<K, V, ALLOCATOR>
|
2022-03-18 09:46:38 +11:00
|
|
|
where
|
|
|
|
K: Eq + Hash,
|
|
|
|
{
|
2022-03-22 08:16:36 +11:00
|
|
|
/// 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.
|
2022-03-22 07:42:07 +11:00
|
|
|
pub fn insert(&mut self, key: K, value: V) -> Option<V> {
|
|
|
|
let hash = self.hash(&key);
|
|
|
|
|
2022-03-24 07:22:37 +11:00
|
|
|
if let Some(location) = self.nodes.location(&key, hash) {
|
2022-03-22 07:42:07 +11:00
|
|
|
Some(self.nodes.replace_at_location(location, key, value))
|
|
|
|
} else {
|
2022-03-25 05:31:18 +11:00
|
|
|
if self.nodes.capacity() <= self.len() {
|
|
|
|
self.resize(self.nodes.backing_vec_size() * 2);
|
2022-03-22 07:42:07 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
self.nodes.insert_new(key, value, hash);
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn insert_and_get(&mut self, key: K, value: V) -> &'_ mut V {
|
2022-03-18 10:18:49 +11:00
|
|
|
let hash = self.hash(&key);
|
2022-03-18 09:46:38 +11:00
|
|
|
|
2022-03-24 07:22:37 +11:00
|
|
|
let location = if let Some(location) = self.nodes.location(&key, hash) {
|
2022-03-21 01:54:53 +11:00
|
|
|
self.nodes.replace_at_location(location, key, value);
|
|
|
|
location
|
|
|
|
} else {
|
2022-03-25 05:31:18 +11:00
|
|
|
if self.nodes.capacity() <= self.len() {
|
|
|
|
self.resize(self.nodes.backing_vec_size() * 2);
|
2022-03-21 01:54:53 +11:00
|
|
|
}
|
2022-03-18 09:46:38 +11:00
|
|
|
|
2022-03-21 01:54:53 +11:00
|
|
|
self.nodes.insert_new(key, value, hash)
|
|
|
|
};
|
2022-03-18 11:18:39 +11:00
|
|
|
|
2023-05-10 05:57:33 +10:00
|
|
|
self.nodes.node_at_mut(location).value_mut().unwrap()
|
2022-03-18 09:46:38 +11:00
|
|
|
}
|
|
|
|
|
2022-03-22 08:16:36 +11:00
|
|
|
/// Returns `true` if the map contains a value for the specified key.
|
2023-04-24 03:37:30 +10:00
|
|
|
pub fn contains_key<Q>(&self, k: &Q) -> bool
|
|
|
|
where
|
|
|
|
K: Borrow<Q>,
|
|
|
|
Q: Hash + Eq + ?Sized,
|
|
|
|
{
|
2022-03-22 07:33:48 +11:00
|
|
|
let hash = self.hash(k);
|
2022-03-24 07:22:37 +11:00
|
|
|
self.nodes.location(k, hash).is_some()
|
2022-03-22 07:33:48 +11:00
|
|
|
}
|
|
|
|
|
2022-03-22 08:16:36 +11:00
|
|
|
/// Returns the key-value pair corresponding to the supplied key
|
2023-04-24 03:40:13 +10:00
|
|
|
pub fn get_key_value<Q>(&self, key: &Q) -> Option<(&K, &V)>
|
|
|
|
where
|
|
|
|
K: Borrow<Q>,
|
|
|
|
Q: Hash + Eq + ?Sized,
|
|
|
|
{
|
2022-03-18 10:18:49 +11:00
|
|
|
let hash = self.hash(key);
|
2022-03-18 09:46:38 +11:00
|
|
|
|
2023-05-10 06:37:20 +10:00
|
|
|
let location = self.nodes.location(key, hash)?;
|
2023-05-10 06:55:14 +10:00
|
|
|
Some(
|
|
|
|
// SAFETY: we know that a node exists and has a value from the location call above
|
|
|
|
unsafe {
|
|
|
|
self.nodes
|
|
|
|
.node_at_unchecked(location)
|
|
|
|
.key_value_ref_unchecked()
|
|
|
|
},
|
|
|
|
)
|
2022-03-22 07:33:48 +11:00
|
|
|
}
|
|
|
|
|
2022-03-22 08:16:36 +11:00
|
|
|
/// Returns a reference to the value corresponding to the key. Returns [`None`] if there is
|
|
|
|
/// no element in the map with the given key.
|
2023-04-24 03:40:13 +10:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
/// ```
|
|
|
|
/// use agb_hashmap::HashMap;
|
|
|
|
///
|
|
|
|
/// let mut map = HashMap::new();
|
|
|
|
/// map.insert("a".to_string(), "A");
|
|
|
|
/// assert_eq!(map.get("a"), Some(&"A"));
|
|
|
|
/// assert_eq!(map.get("b"), None);
|
|
|
|
/// ```
|
|
|
|
pub fn get<Q>(&self, key: &Q) -> Option<&V>
|
|
|
|
where
|
|
|
|
K: Borrow<Q>,
|
|
|
|
Q: Hash + Eq + ?Sized,
|
|
|
|
{
|
2022-03-22 07:33:48 +11:00
|
|
|
self.get_key_value(key).map(|(_, v)| v)
|
2022-03-18 09:46:38 +11:00
|
|
|
}
|
2022-03-18 10:18:49 +11:00
|
|
|
|
2022-03-22 08:16:36 +11:00
|
|
|
/// 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.
|
2023-04-24 03:44:03 +10:00
|
|
|
///
|
|
|
|
/// # 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");
|
|
|
|
/// ```
|
|
|
|
pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
|
|
|
|
where
|
|
|
|
K: Borrow<Q>,
|
|
|
|
Q: Hash + Eq + ?Sized,
|
|
|
|
{
|
2022-03-18 11:21:55 +11:00
|
|
|
let hash = self.hash(key);
|
|
|
|
|
2023-05-10 06:37:52 +10:00
|
|
|
let location = self.nodes.location(key, hash)?;
|
2023-05-10 06:55:14 +10:00
|
|
|
Some(
|
|
|
|
// SAFETY: we know that a node exists and has a value from the location call above
|
|
|
|
unsafe {
|
|
|
|
self.nodes
|
|
|
|
.node_at_unchecked_mut(location)
|
|
|
|
.value_mut_unchecked()
|
|
|
|
},
|
|
|
|
)
|
2022-03-18 11:21:55 +11:00
|
|
|
}
|
|
|
|
|
2022-03-22 08:16:36 +11:00
|
|
|
/// Removes the given key from the map. Returns the current value if it existed, or [`None`]
|
|
|
|
/// if it did not.
|
2023-04-24 03:45:39 +10:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
/// ```
|
|
|
|
/// use agb_hashmap::HashMap;
|
|
|
|
///
|
|
|
|
/// let mut map = HashMap::new();
|
|
|
|
/// map.insert(1, "a");
|
|
|
|
/// assert_eq!(map.remove(&1), Some("a"));
|
|
|
|
/// assert_eq!(map.remove(&1), None);
|
|
|
|
/// ```
|
|
|
|
pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
|
|
|
|
where
|
|
|
|
K: Borrow<Q>,
|
|
|
|
Q: Hash + Eq + ?Sized,
|
|
|
|
{
|
2022-03-18 10:18:49 +11:00
|
|
|
let hash = self.hash(key);
|
|
|
|
|
2022-03-20 09:04:53 +11:00
|
|
|
self.nodes
|
2022-03-24 07:22:37 +11:00
|
|
|
.location(key, hash)
|
2022-03-18 11:05:03 +11:00
|
|
|
.map(|location| self.nodes.remove_from_location(location))
|
2022-03-18 10:18:49 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-05 04:27:08 +10:00
|
|
|
impl<K, V, ALLOCATOR: ClonableAllocator> HashMap<K, V, ALLOCATOR>
|
2022-03-18 10:18:49 +11:00
|
|
|
where
|
|
|
|
K: Hash,
|
|
|
|
{
|
2023-04-24 03:37:30 +10:00
|
|
|
fn hash<Q>(&self, key: &Q) -> HashType
|
|
|
|
where
|
|
|
|
K: Borrow<Q>,
|
|
|
|
Q: Hash + ?Sized,
|
|
|
|
{
|
2022-03-18 10:18:49 +11:00
|
|
|
let mut hasher = self.hasher.build_hasher();
|
|
|
|
key.hash(&mut hasher);
|
|
|
|
hasher.finish() as HashType
|
|
|
|
}
|
2022-03-18 09:46:38 +11:00
|
|
|
}
|
|
|
|
|
2022-08-03 07:52:16 +10:00
|
|
|
/// An iterator over entries of a [`HashMap`]
|
|
|
|
///
|
|
|
|
/// This struct is created using the `into_iter()` method on [`HashMap`]. See its
|
|
|
|
/// documentation for more.
|
2022-08-05 04:27:08 +10:00
|
|
|
pub struct Iter<'a, K: 'a, V: 'a, ALLOCATOR: ClonableAllocator> {
|
|
|
|
map: &'a HashMap<K, V, ALLOCATOR>,
|
2022-03-18 10:44:57 +11:00
|
|
|
at: usize,
|
2022-10-03 05:10:50 +11:00
|
|
|
num_found: usize,
|
2022-03-18 10:44:57 +11:00
|
|
|
}
|
|
|
|
|
2022-08-05 04:27:08 +10:00
|
|
|
impl<'a, K, V, ALLOCATOR: ClonableAllocator> Iterator for Iter<'a, K, V, ALLOCATOR> {
|
2022-03-18 10:44:57 +11:00
|
|
|
type Item = (&'a K, &'a V);
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
loop {
|
2022-03-25 05:31:18 +11:00
|
|
|
if self.at >= self.map.nodes.backing_vec_size() {
|
2022-03-18 10:44:57 +11:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2023-05-10 05:57:33 +10:00
|
|
|
let node = &self.map.nodes.node_at(self.at);
|
2022-03-18 10:44:57 +11:00
|
|
|
self.at += 1;
|
2022-03-20 09:42:09 +11:00
|
|
|
|
2023-05-10 06:44:14 +10:00
|
|
|
if let Some(key_value) = node.key_value_ref() {
|
2022-10-03 05:10:50 +11:00
|
|
|
self.num_found += 1;
|
2023-05-10 06:44:14 +10:00
|
|
|
return Some(key_value);
|
2022-03-20 09:42:09 +11:00
|
|
|
}
|
2022-03-18 10:44:57 +11:00
|
|
|
}
|
|
|
|
}
|
2022-10-03 04:44:48 +11:00
|
|
|
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
2022-10-03 05:10:50 +11:00
|
|
|
(
|
|
|
|
self.map.len() - self.num_found,
|
|
|
|
Some(self.map.len() - self.num_found),
|
|
|
|
)
|
2022-10-03 04:44:48 +11:00
|
|
|
}
|
2022-03-18 10:44:57 +11:00
|
|
|
}
|
|
|
|
|
2022-08-05 04:27:08 +10:00
|
|
|
impl<'a, K, V, ALLOCATOR: ClonableAllocator> IntoIterator for &'a HashMap<K, V, ALLOCATOR> {
|
2022-03-18 10:44:57 +11:00
|
|
|
type Item = (&'a K, &'a V);
|
2022-08-05 04:27:08 +10:00
|
|
|
type IntoIter = Iter<'a, K, V, ALLOCATOR>;
|
2022-03-18 10:44:57 +11:00
|
|
|
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
2022-10-03 05:10:50 +11:00
|
|
|
Iter {
|
|
|
|
map: self,
|
|
|
|
at: 0,
|
|
|
|
num_found: 0,
|
|
|
|
}
|
2022-03-18 10:44:57 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-03 07:52:16 +10:00
|
|
|
/// An iterator over entries of a [`HashMap`]
|
|
|
|
///
|
|
|
|
/// This struct is created using the `into_iter()` method on [`HashMap`] as part of its implementation
|
|
|
|
/// of the IntoIterator trait.
|
2022-08-05 04:27:08 +10:00
|
|
|
pub struct IterOwned<K, V, ALLOCATOR: Allocator = Global> {
|
|
|
|
map: HashMap<K, V, ALLOCATOR>,
|
2022-04-06 08:11:32 +10:00
|
|
|
at: usize,
|
2022-10-03 05:10:50 +11:00
|
|
|
num_found: usize,
|
2022-04-06 08:11:32 +10:00
|
|
|
}
|
|
|
|
|
2022-08-05 04:27:08 +10:00
|
|
|
impl<K, V, ALLOCATOR: ClonableAllocator> Iterator for IterOwned<K, V, ALLOCATOR> {
|
2022-04-06 08:11:32 +10:00
|
|
|
type Item = (K, V);
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
loop {
|
|
|
|
if self.at >= self.map.nodes.backing_vec_size() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2023-05-10 05:57:33 +10:00
|
|
|
let maybe_kv = self.map.nodes.node_at_mut(self.at).take_key_value();
|
2022-04-06 08:11:32 +10:00
|
|
|
self.at += 1;
|
|
|
|
|
|
|
|
if let Some((k, v, _)) = maybe_kv {
|
2022-10-03 05:10:50 +11:00
|
|
|
self.num_found += 1;
|
2022-04-06 08:11:32 +10:00
|
|
|
return Some((k, v));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-03 04:44:48 +11:00
|
|
|
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
2022-10-03 05:10:50 +11:00
|
|
|
(
|
|
|
|
self.map.len() - self.num_found,
|
|
|
|
Some(self.map.len() - self.num_found),
|
|
|
|
)
|
2022-10-03 04:44:48 +11:00
|
|
|
}
|
2022-04-06 08:11:32 +10:00
|
|
|
}
|
|
|
|
|
2022-08-03 07:52:16 +10:00
|
|
|
/// An iterator over entries of a [`HashMap`]
|
|
|
|
///
|
|
|
|
/// This struct is created using the `into_iter()` method on [`HashMap`] as part of its implementation
|
|
|
|
/// of the IntoIterator trait.
|
2022-08-05 04:27:08 +10:00
|
|
|
impl<K, V, ALLOCATOR: ClonableAllocator> IntoIterator for HashMap<K, V, ALLOCATOR> {
|
2022-04-06 08:11:32 +10:00
|
|
|
type Item = (K, V);
|
2022-08-05 04:27:08 +10:00
|
|
|
type IntoIter = IterOwned<K, V, ALLOCATOR>;
|
2022-04-06 08:11:32 +10:00
|
|
|
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
2022-10-03 05:10:50 +11:00
|
|
|
IterOwned {
|
|
|
|
map: self,
|
|
|
|
at: 0,
|
|
|
|
num_found: 0,
|
|
|
|
}
|
2022-04-06 08:11:32 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-10 07:00:13 +10:00
|
|
|
mod entries {
|
|
|
|
use core::{alloc::Allocator, hash::Hash};
|
|
|
|
|
|
|
|
use super::{ClonableAllocator, HashMap};
|
|
|
|
|
|
|
|
/// A view into an occupied entry in a `HashMap`. This is part of the [`Entry`] enum.
|
|
|
|
pub struct OccupiedEntry<'a, K: 'a, V: 'a, ALLOCATOR: Allocator> {
|
|
|
|
key: K,
|
|
|
|
map: &'a mut HashMap<K, V, ALLOCATOR>,
|
|
|
|
location: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, K: 'a, V: 'a, ALLOCATOR: ClonableAllocator> OccupiedEntry<'a, K, V, ALLOCATOR> {
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// You must call this with a valid location (one where the entry is defined)
|
|
|
|
pub(crate) unsafe fn new(
|
|
|
|
key: K,
|
|
|
|
map: &'a mut HashMap<K, V, ALLOCATOR>,
|
|
|
|
location: usize,
|
|
|
|
) -> Self {
|
|
|
|
Self { key, map, location }
|
|
|
|
}
|
2022-03-18 11:47:10 +11:00
|
|
|
|
2023-05-10 07:00:13 +10:00
|
|
|
/// Gets a reference to the key in the entry.
|
|
|
|
pub fn key(&self) -> &K {
|
|
|
|
&self.key
|
|
|
|
}
|
2023-05-10 06:55:14 +10:00
|
|
|
|
2023-05-10 07:00:13 +10:00
|
|
|
/// Take the ownership of the key and value from the map.
|
|
|
|
pub fn remove_entry(self) -> (K, V) {
|
|
|
|
let old_value = self.map.nodes.remove_from_location(self.location);
|
|
|
|
(self.key, old_value)
|
|
|
|
}
|
2022-03-21 02:07:25 +11:00
|
|
|
|
2023-05-10 07:00:13 +10:00
|
|
|
/// Gets a reference to the value in the entry.
|
|
|
|
pub fn get(&self) -> &V {
|
|
|
|
// SAFETY: This can only be constructed with valid locations
|
|
|
|
unsafe {
|
|
|
|
self.map
|
|
|
|
.nodes
|
|
|
|
.node_at_unchecked(self.location)
|
|
|
|
.value_ref_unchecked()
|
|
|
|
}
|
|
|
|
}
|
2022-03-21 02:13:22 +11:00
|
|
|
|
2023-05-10 07:00:13 +10:00
|
|
|
/// Gets a mutable reference to the value in the entry.
|
|
|
|
///
|
|
|
|
/// If you need a reference to the `OccupiedEntry` which may outlive the destruction
|
|
|
|
/// of the `Entry` value, see [`into_mut`].
|
|
|
|
///
|
|
|
|
/// [`into_mut`]: Self::into_mut
|
|
|
|
pub fn get_mut(&mut self) -> &mut V {
|
|
|
|
// SAFETY: This can only be constructed with valid locations
|
|
|
|
unsafe {
|
|
|
|
self.map
|
|
|
|
.nodes
|
|
|
|
.node_at_unchecked_mut(self.location)
|
|
|
|
.value_mut_unchecked()
|
|
|
|
}
|
2023-05-10 06:33:20 +10:00
|
|
|
}
|
2022-03-21 02:13:22 +11:00
|
|
|
|
2023-05-10 07:00:13 +10:00
|
|
|
/// Converts the `OccupiedEntry` into a mutable reference to the value in the entry with
|
|
|
|
/// a lifetime bound to the map itself.
|
|
|
|
///
|
|
|
|
/// If you need multiple references to the `OccupiedEntry`, see [`get_mut`].
|
|
|
|
///
|
|
|
|
/// [`get_mut`]: Self::get_mut
|
|
|
|
pub fn into_mut(self) -> &'a mut V {
|
|
|
|
// SAFETY: This can only be constructed with valid locations
|
|
|
|
unsafe {
|
|
|
|
self.map
|
|
|
|
.nodes
|
|
|
|
.node_at_unchecked_mut(self.location)
|
|
|
|
.value_mut_unchecked()
|
|
|
|
}
|
2023-05-10 06:33:20 +10:00
|
|
|
}
|
2022-03-21 02:07:25 +11:00
|
|
|
|
2023-05-10 07:00:13 +10:00
|
|
|
/// Sets the value of the entry and returns the entry's old value.
|
|
|
|
pub fn insert(&mut self, value: V) -> V {
|
|
|
|
// SAFETY: This can only be constructed with valid locations
|
|
|
|
unsafe {
|
|
|
|
self.map
|
|
|
|
.nodes
|
|
|
|
.node_at_unchecked_mut(self.location)
|
|
|
|
.replace_value_unchecked(value)
|
|
|
|
}
|
2023-05-10 06:33:20 +10:00
|
|
|
}
|
2022-03-21 02:07:25 +11:00
|
|
|
|
2023-05-10 07:00:13 +10:00
|
|
|
/// Takes the value out of the entry and returns it.
|
|
|
|
pub fn remove(self) -> V {
|
|
|
|
self.map.nodes.remove_from_location(self.location)
|
2023-05-10 06:33:20 +10:00
|
|
|
}
|
2022-03-21 02:13:22 +11:00
|
|
|
}
|
|
|
|
|
2023-05-10 07:00:13 +10:00
|
|
|
/// A view into a vacant entry in a `HashMap`. It is part of the [`Entry`] enum.
|
|
|
|
pub struct VacantEntry<'a, K: 'a, V: 'a, ALLOCATOR: Allocator> {
|
|
|
|
key: K,
|
|
|
|
map: &'a mut HashMap<K, V, ALLOCATOR>,
|
2022-03-21 02:07:25 +11:00
|
|
|
}
|
2022-03-21 01:54:53 +11:00
|
|
|
|
2023-05-10 07:00:13 +10:00
|
|
|
impl<'a, K: 'a, V: 'a, ALLOCATOR: ClonableAllocator> VacantEntry<'a, K, V, ALLOCATOR> {
|
|
|
|
pub(crate) fn new(key: K, map: &'a mut HashMap<K, V, ALLOCATOR>) -> Self {
|
|
|
|
Self { key, map }
|
|
|
|
}
|
2022-03-18 11:47:10 +11:00
|
|
|
|
2023-05-10 07:00:13 +10:00
|
|
|
/// Gets a reference to the key that would be used when inserting a value through `VacantEntry`
|
|
|
|
pub fn key(&self) -> &K {
|
|
|
|
&self.key
|
|
|
|
}
|
2022-03-21 02:14:44 +11:00
|
|
|
|
2023-05-10 07:00:13 +10:00
|
|
|
/// Take ownership of the key
|
|
|
|
pub fn into_key(self) -> K {
|
|
|
|
self.key
|
|
|
|
}
|
2022-03-21 02:14:44 +11:00
|
|
|
|
2023-05-10 07:00:13 +10:00
|
|
|
/// Sets the value of the entry with the `VacantEntry`'s key and returns a mutable reference to it.
|
|
|
|
pub fn insert(self, value: V) -> &'a mut V
|
|
|
|
where
|
|
|
|
K: Hash + Eq,
|
|
|
|
{
|
|
|
|
self.map.insert_and_get(self.key, value)
|
|
|
|
}
|
2022-03-18 11:47:10 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-10 07:00:13 +10:00
|
|
|
pub use entries::{OccupiedEntry, VacantEntry};
|
|
|
|
|
2022-03-22 08:16:36 +11:00
|
|
|
/// A view into a single entry in a map, which may be vacant or occupied.
|
|
|
|
///
|
|
|
|
/// This is constructed using the [`entry`] method on [`HashMap`]
|
|
|
|
///
|
|
|
|
/// [`entry`]: HashMap::entry()
|
2022-08-05 04:27:08 +10:00
|
|
|
pub enum Entry<'a, K: 'a, V: 'a, ALLOCATOR: Allocator = Global> {
|
2022-03-22 08:16:36 +11:00
|
|
|
/// An occupied entry
|
2022-08-05 04:27:08 +10:00
|
|
|
Occupied(OccupiedEntry<'a, K, V, ALLOCATOR>),
|
2022-03-22 08:16:36 +11:00
|
|
|
/// A vacant entry
|
2022-08-05 04:27:08 +10:00
|
|
|
Vacant(VacantEntry<'a, K, V, ALLOCATOR>),
|
2022-03-18 11:47:10 +11:00
|
|
|
}
|
|
|
|
|
2022-08-05 04:27:08 +10:00
|
|
|
impl<'a, K, V, ALLOCATOR: ClonableAllocator> Entry<'a, K, V, ALLOCATOR>
|
2022-03-18 11:47:10 +11:00
|
|
|
where
|
|
|
|
K: Hash + Eq,
|
|
|
|
{
|
2022-03-22 08:16:36 +11:00
|
|
|
/// Ensures a value is in the entry by inserting the given value, and returns a mutable
|
|
|
|
/// reference to the value in the entry.
|
2022-03-21 01:54:53 +11:00
|
|
|
pub fn or_insert(self, value: V) -> &'a mut V {
|
2022-03-18 11:47:10 +11:00
|
|
|
match self {
|
2022-03-21 02:07:25 +11:00
|
|
|
Entry::Occupied(e) => e.into_mut(),
|
2022-03-18 11:47:10 +11:00
|
|
|
Entry::Vacant(e) => e.insert(value),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 08:16:36 +11:00
|
|
|
/// Ensures a value is in the entry by inserting the result of the function if empty, and
|
|
|
|
/// returns a mutable reference to the value in the entry.
|
2022-03-21 01:54:53 +11:00
|
|
|
pub fn or_insert_with<F>(self, f: F) -> &'a mut V
|
|
|
|
where
|
|
|
|
F: FnOnce() -> V,
|
|
|
|
{
|
|
|
|
match self {
|
2022-03-21 02:07:25 +11:00
|
|
|
Entry::Occupied(e) => e.into_mut(),
|
2022-03-21 01:54:53 +11:00
|
|
|
Entry::Vacant(e) => e.insert(f()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 08:16:36 +11:00
|
|
|
/// Ensures a value is in the entry by inserting the result of the function if empty, and
|
|
|
|
/// returns a mutable reference to the value in the entry. This method allows for key-derived
|
|
|
|
/// values for insertion by providing the function with a reference to the key.
|
|
|
|
///
|
|
|
|
/// The reference to the moved key is provided so that cloning or copying the key is unnecessary,
|
|
|
|
/// unlike with `.or_insert_with(|| ...)`.
|
2022-03-21 01:54:53 +11:00
|
|
|
pub fn or_insert_with_key<F>(self, f: F) -> &'a mut V
|
2022-03-21 01:21:45 +11:00
|
|
|
where
|
|
|
|
F: FnOnce(&K) -> V,
|
|
|
|
{
|
|
|
|
match self {
|
2022-03-21 02:07:25 +11:00
|
|
|
Entry::Occupied(e) => e.into_mut(),
|
2022-03-21 01:21:45 +11:00
|
|
|
Entry::Vacant(e) => {
|
2023-05-10 07:00:13 +10:00
|
|
|
let value = f(e.key());
|
2022-03-21 01:54:53 +11:00
|
|
|
e.insert(value)
|
2022-03-21 01:21:45 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 08:16:36 +11:00
|
|
|
/// Provides in-place mutable access to an occupied entry before any potential inserts
|
|
|
|
/// into the map.
|
2022-03-18 11:47:10 +11:00
|
|
|
pub fn and_modify<F>(self, f: F) -> Self
|
|
|
|
where
|
|
|
|
F: FnOnce(&mut V),
|
|
|
|
{
|
|
|
|
match self {
|
2022-03-21 02:13:22 +11:00
|
|
|
Entry::Occupied(mut e) => {
|
|
|
|
f(e.get_mut());
|
2022-03-18 11:47:10 +11:00
|
|
|
Entry::Occupied(e)
|
|
|
|
}
|
|
|
|
Entry::Vacant(e) => Entry::Vacant(e),
|
|
|
|
}
|
|
|
|
}
|
2022-03-21 01:54:53 +11:00
|
|
|
|
2022-03-22 08:16:36 +11:00
|
|
|
/// Ensures a value is in th entry by inserting the default value if empty. Returns a
|
|
|
|
/// mutable reference to the value in the entry.
|
2022-03-21 01:54:53 +11:00
|
|
|
pub fn or_default(self) -> &'a mut V
|
|
|
|
where
|
|
|
|
V: Default,
|
|
|
|
{
|
|
|
|
match self {
|
2022-03-21 02:07:25 +11:00
|
|
|
Entry::Occupied(e) => e.into_mut(),
|
2022-03-21 01:54:53 +11:00
|
|
|
Entry::Vacant(e) => e.insert(Default::default()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 08:16:36 +11:00
|
|
|
/// Returns a reference to this entry's key.
|
2022-03-21 01:54:53 +11:00
|
|
|
pub fn key(&self) -> &K {
|
|
|
|
match self {
|
2023-05-10 07:00:13 +10:00
|
|
|
Entry::Occupied(e) => e.key(),
|
|
|
|
Entry::Vacant(e) => e.key(),
|
2022-03-21 01:54:53 +11:00
|
|
|
}
|
|
|
|
}
|
2022-03-18 11:47:10 +11:00
|
|
|
}
|
|
|
|
|
2022-08-05 04:27:08 +10:00
|
|
|
impl<K, V, ALLOCATOR: ClonableAllocator> HashMap<K, V, ALLOCATOR>
|
2022-03-18 11:47:10 +11:00
|
|
|
where
|
|
|
|
K: Hash + Eq,
|
|
|
|
{
|
2022-03-22 08:16:36 +11:00
|
|
|
/// Gets the given key's corresponding entry in the map for in-place manipulation.
|
2022-08-05 04:27:08 +10:00
|
|
|
pub fn entry(&mut self, key: K) -> Entry<'_, K, V, ALLOCATOR> {
|
2022-03-18 11:47:10 +11:00
|
|
|
let hash = self.hash(&key);
|
2022-03-24 07:22:37 +11:00
|
|
|
let location = self.nodes.location(&key, hash);
|
2022-03-18 11:47:10 +11:00
|
|
|
|
|
|
|
if let Some(location) = location {
|
2023-05-10 06:55:14 +10:00
|
|
|
Entry::Occupied(
|
|
|
|
// SAFETY: location is valid by the call to location above
|
|
|
|
unsafe { OccupiedEntry::new(key, self, location) },
|
|
|
|
)
|
2022-03-18 11:47:10 +11:00
|
|
|
} else {
|
2023-05-10 07:00:13 +10:00
|
|
|
Entry::Vacant(VacantEntry::new(key, self))
|
2022-03-18 11:47:10 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-21 02:38:39 +11:00
|
|
|
impl<K, V> FromIterator<(K, V)> for HashMap<K, V>
|
|
|
|
where
|
|
|
|
K: Eq + Hash,
|
|
|
|
{
|
|
|
|
fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self {
|
|
|
|
let mut map = HashMap::new();
|
|
|
|
map.extend(iter);
|
|
|
|
map
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<K, V> Extend<(K, V)> for HashMap<K, V>
|
|
|
|
where
|
|
|
|
K: Eq + Hash,
|
|
|
|
{
|
|
|
|
fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T) {
|
|
|
|
for (k, v) in iter {
|
|
|
|
self.insert(k, v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-24 03:44:03 +10:00
|
|
|
impl<K, V, Q, ALLOCATOR: ClonableAllocator> Index<&Q> for HashMap<K, V, ALLOCATOR>
|
2022-03-21 02:38:39 +11:00
|
|
|
where
|
2023-04-24 03:44:03 +10:00
|
|
|
K: Eq + Hash + Borrow<Q>,
|
|
|
|
Q: Eq + Hash + ?Sized,
|
2022-03-21 02:38:39 +11:00
|
|
|
{
|
|
|
|
type Output = V;
|
|
|
|
|
2023-04-24 03:44:03 +10:00
|
|
|
fn index(&self, key: &Q) -> &V {
|
2022-03-21 02:38:39 +11:00
|
|
|
self.get(key).expect("no entry found for key")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-10 06:09:44 +10:00
|
|
|
impl<K, V, ALLOCATOR: ClonableAllocator> PartialEq for HashMap<K, V, ALLOCATOR>
|
|
|
|
where
|
|
|
|
K: Eq + Hash,
|
|
|
|
V: PartialEq,
|
|
|
|
{
|
|
|
|
fn eq(&self, other: &HashMap<K, V, ALLOCATOR>) -> bool {
|
|
|
|
if self.len() != other.len() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.iter()
|
|
|
|
.all(|(key, value)| other.get(key).map_or(false, |v| *value == *v))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<K, V, ALLOCATOR: ClonableAllocator> Eq for HashMap<K, V, ALLOCATOR>
|
|
|
|
where
|
|
|
|
K: Eq + Hash,
|
|
|
|
V: PartialEq,
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-05-10 06:12:22 +10:00
|
|
|
impl<K, V, ALLOCATOR: ClonableAllocator> Debug for HashMap<K, V, ALLOCATOR>
|
|
|
|
where
|
|
|
|
K: Debug,
|
|
|
|
V: Debug,
|
|
|
|
{
|
|
|
|
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
|
|
|
f.debug_map().entries(self.iter()).finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-25 05:31:18 +11:00
|
|
|
const fn number_before_resize(capacity: usize) -> usize {
|
|
|
|
capacity * 85 / 100
|
|
|
|
}
|
|
|
|
|
2022-03-18 09:46:38 +11:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2022-03-20 09:59:00 +11:00
|
|
|
use core::cell::RefCell;
|
|
|
|
|
2023-05-10 05:57:33 +10:00
|
|
|
use alloc::vec::Vec;
|
|
|
|
|
2022-03-18 09:46:38 +11:00
|
|
|
use super::*;
|
|
|
|
|
2023-04-24 02:39:00 +10:00
|
|
|
#[test]
|
|
|
|
fn can_store_and_retrieve_8_elements() {
|
2022-03-18 09:46:38 +11:00
|
|
|
let mut map = HashMap::new();
|
|
|
|
|
|
|
|
for i in 0..8 {
|
2022-03-18 11:20:24 +11:00
|
|
|
map.insert(i, i % 4);
|
2022-03-18 09:46:38 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
for i in 0..8 {
|
|
|
|
assert_eq!(map.get(&i), Some(&(i % 4)));
|
|
|
|
}
|
|
|
|
}
|
2022-03-18 10:18:49 +11:00
|
|
|
|
2023-04-24 02:39:00 +10:00
|
|
|
#[test]
|
|
|
|
fn can_get_the_length() {
|
2022-03-18 10:18:49 +11:00
|
|
|
let mut map = HashMap::new();
|
|
|
|
|
|
|
|
for i in 0..8 {
|
2022-03-18 11:20:24 +11:00
|
|
|
map.insert(i / 2, true);
|
2022-03-18 10:18:49 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(map.len(), 4);
|
|
|
|
}
|
|
|
|
|
2023-04-24 02:39:00 +10:00
|
|
|
#[test]
|
|
|
|
fn returns_none_if_element_does_not_exist() {
|
2022-03-18 10:18:49 +11:00
|
|
|
let mut map = HashMap::new();
|
|
|
|
|
|
|
|
for i in 0..8 {
|
2022-03-18 11:20:24 +11:00
|
|
|
map.insert(i, i % 3);
|
2022-03-18 10:18:49 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(map.get(&12), None);
|
|
|
|
}
|
|
|
|
|
2023-04-24 02:39:00 +10:00
|
|
|
#[test]
|
|
|
|
fn can_delete_entries() {
|
2022-03-18 10:18:49 +11:00
|
|
|
let mut map = HashMap::new();
|
|
|
|
|
|
|
|
for i in 0..8 {
|
2022-03-18 11:20:24 +11:00
|
|
|
map.insert(i, i % 3);
|
2022-03-18 10:18:49 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
for i in 0..4 {
|
|
|
|
map.remove(&i);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(map.len(), 4);
|
|
|
|
assert_eq!(map.get(&3), None);
|
|
|
|
assert_eq!(map.get(&7), Some(&1));
|
|
|
|
}
|
2022-03-18 10:44:57 +11:00
|
|
|
|
2023-04-24 02:39:00 +10:00
|
|
|
#[test]
|
|
|
|
fn can_iterate_through_all_entries() {
|
2022-03-18 10:44:57 +11:00
|
|
|
let mut map = HashMap::new();
|
|
|
|
|
|
|
|
for i in 0..8 {
|
2022-03-18 11:20:24 +11:00
|
|
|
map.insert(i, i);
|
2022-03-18 10:44:57 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut max_found = -1;
|
|
|
|
let mut num_found = 0;
|
|
|
|
|
|
|
|
for (_, value) in map.into_iter() {
|
2022-04-06 08:11:32 +10:00
|
|
|
max_found = max_found.max(value);
|
2022-03-18 10:44:57 +11:00
|
|
|
num_found += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(num_found, 8);
|
|
|
|
assert_eq!(max_found, 7);
|
|
|
|
}
|
2022-03-18 11:18:39 +11:00
|
|
|
|
2023-04-24 02:39:00 +10:00
|
|
|
#[test]
|
|
|
|
fn can_insert_more_than_initial_capacity() {
|
2022-03-18 11:18:39 +11:00
|
|
|
let mut map = HashMap::new();
|
|
|
|
|
|
|
|
for i in 0..65 {
|
2022-03-18 11:20:24 +11:00
|
|
|
map.insert(i, i % 4);
|
2022-03-18 11:18:39 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
for i in 0..65 {
|
|
|
|
assert_eq!(map.get(&i), Some(&(i % 4)));
|
|
|
|
}
|
|
|
|
}
|
2022-03-19 08:08:26 +11:00
|
|
|
|
2022-03-21 01:10:38 +11:00
|
|
|
struct NoisyDrop {
|
|
|
|
i: i32,
|
|
|
|
dropped: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl NoisyDrop {
|
2023-04-24 02:58:45 +10:00
|
|
|
#[cfg(not(miri))]
|
2022-03-21 01:10:38 +11:00
|
|
|
fn new(i: i32) -> Self {
|
|
|
|
Self { i, dropped: false }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialEq for NoisyDrop {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.i == other.i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Eq for NoisyDrop {}
|
|
|
|
|
|
|
|
impl Hash for NoisyDrop {
|
|
|
|
fn hash<H: Hasher>(&self, hasher: &mut H) {
|
|
|
|
hasher.write_i32(self.i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for NoisyDrop {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
if self.dropped {
|
|
|
|
panic!("NoisyDropped dropped twice");
|
|
|
|
}
|
|
|
|
|
|
|
|
self.dropped = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-24 02:39:00 +10:00
|
|
|
trait RngNextI32 {
|
|
|
|
fn next_i32(&mut self) -> i32;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> RngNextI32 for T
|
|
|
|
where
|
|
|
|
T: rand::RngCore,
|
|
|
|
{
|
|
|
|
fn next_i32(&mut self) -> i32 {
|
|
|
|
self.next_u32() as i32
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-24 02:43:30 +10:00
|
|
|
#[cfg(not(miri))] // takes way too long to run under miri
|
2023-04-24 02:39:00 +10:00
|
|
|
#[test]
|
|
|
|
fn extreme_case() {
|
|
|
|
use rand::SeedableRng;
|
|
|
|
|
2022-03-19 08:08:26 +11:00
|
|
|
let mut map = HashMap::new();
|
2023-04-24 02:39:00 +10:00
|
|
|
let mut rng = rand::rngs::SmallRng::seed_from_u64(20);
|
2022-03-19 08:08:26 +11:00
|
|
|
|
|
|
|
let mut answers: [Option<i32>; 128] = [None; 128];
|
|
|
|
|
|
|
|
for _ in 0..5_000 {
|
2023-04-24 02:39:00 +10:00
|
|
|
let command = rng.next_i32().rem_euclid(2);
|
|
|
|
let key = rng.next_i32().rem_euclid(answers.len() as i32);
|
|
|
|
let value = rng.next_i32();
|
2022-03-19 08:08:26 +11:00
|
|
|
|
|
|
|
match command {
|
|
|
|
0 => {
|
|
|
|
// insert
|
|
|
|
answers[key as usize] = Some(value);
|
2022-03-21 01:10:38 +11:00
|
|
|
map.insert(NoisyDrop::new(key), NoisyDrop::new(value));
|
2022-03-19 08:08:26 +11:00
|
|
|
}
|
|
|
|
1 => {
|
|
|
|
// remove
|
|
|
|
answers[key as usize] = None;
|
2022-03-21 01:10:38 +11:00
|
|
|
map.remove(&NoisyDrop::new(key));
|
2022-03-19 08:08:26 +11:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i, answer) in answers.iter().enumerate() {
|
2022-03-21 01:10:38 +11:00
|
|
|
assert_eq!(
|
|
|
|
map.get(&NoisyDrop::new(i as i32)).map(|nd| &nd.i),
|
|
|
|
answer.as_ref()
|
|
|
|
);
|
2022-03-19 08:08:26 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-20 09:59:00 +11:00
|
|
|
|
2022-03-21 00:41:31 +11:00
|
|
|
#[derive(Clone)]
|
2022-03-20 09:59:00 +11:00
|
|
|
struct Droppable<'a> {
|
|
|
|
id: usize,
|
|
|
|
drop_registry: &'a DropRegistry,
|
|
|
|
}
|
|
|
|
|
2022-03-21 00:41:31 +11:00
|
|
|
impl Hash for Droppable<'_> {
|
|
|
|
fn hash<H: Hasher>(&self, hasher: &mut H) {
|
|
|
|
hasher.write_usize(self.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialEq for Droppable<'_> {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.id == other.id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Eq for Droppable<'_> {}
|
|
|
|
|
|
|
|
impl Drop for Droppable<'_> {
|
2022-03-20 09:59:00 +11:00
|
|
|
fn drop(&mut self) {
|
|
|
|
self.drop_registry.dropped(self.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct DropRegistry {
|
2022-03-21 00:41:31 +11:00
|
|
|
are_dropped: RefCell<Vec<i32>>,
|
2022-03-20 09:59:00 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DropRegistry {
|
2023-05-10 06:46:57 +10:00
|
|
|
fn new() -> Self {
|
2022-03-20 09:59:00 +11:00
|
|
|
Self {
|
|
|
|
are_dropped: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-10 06:46:57 +10:00
|
|
|
fn new_droppable(&self) -> Droppable<'_> {
|
2022-03-21 00:41:31 +11:00
|
|
|
self.are_dropped.borrow_mut().push(0);
|
2022-03-20 09:59:00 +11:00
|
|
|
Droppable {
|
|
|
|
id: self.are_dropped.borrow().len() - 1,
|
|
|
|
drop_registry: self,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-10 06:46:57 +10:00
|
|
|
fn dropped(&self, id: usize) {
|
2022-03-21 00:41:31 +11:00
|
|
|
self.are_dropped.borrow_mut()[id] += 1;
|
2022-03-20 09:59:00 +11:00
|
|
|
}
|
|
|
|
|
2023-05-10 06:46:57 +10:00
|
|
|
fn assert_dropped_once(&self, id: usize) {
|
2022-03-21 00:41:31 +11:00
|
|
|
assert_eq!(self.are_dropped.borrow()[id], 1);
|
2022-03-20 09:59:00 +11:00
|
|
|
}
|
|
|
|
|
2023-05-10 06:46:57 +10:00
|
|
|
fn assert_not_dropped(&self, id: usize) {
|
2022-03-21 00:41:31 +11:00
|
|
|
assert_eq!(self.are_dropped.borrow()[id], 0);
|
|
|
|
}
|
|
|
|
|
2023-05-10 06:46:57 +10:00
|
|
|
fn assert_dropped_n_times(&self, id: usize, num_drops: i32) {
|
2022-03-21 00:41:31 +11:00
|
|
|
assert_eq!(self.are_dropped.borrow()[id], num_drops);
|
2022-03-20 09:59:00 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-24 02:39:00 +10:00
|
|
|
#[test]
|
|
|
|
fn correctly_drops_on_remove_and_overall_drop() {
|
2022-03-20 09:59:00 +11:00
|
|
|
let drop_registry = DropRegistry::new();
|
|
|
|
|
|
|
|
let droppable1 = drop_registry.new_droppable();
|
|
|
|
let droppable2 = drop_registry.new_droppable();
|
|
|
|
|
|
|
|
let id1 = droppable1.id;
|
|
|
|
let id2 = droppable2.id;
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut map = HashMap::new();
|
|
|
|
|
|
|
|
map.insert(1, droppable1);
|
|
|
|
map.insert(2, droppable2);
|
|
|
|
|
|
|
|
drop_registry.assert_not_dropped(id1);
|
|
|
|
drop_registry.assert_not_dropped(id2);
|
|
|
|
|
|
|
|
map.remove(&1);
|
2022-03-21 00:41:31 +11:00
|
|
|
drop_registry.assert_dropped_once(id1);
|
2022-03-20 09:59:00 +11:00
|
|
|
drop_registry.assert_not_dropped(id2);
|
|
|
|
}
|
|
|
|
|
2022-03-21 00:41:31 +11:00
|
|
|
drop_registry.assert_dropped_once(id2);
|
2022-03-20 09:59:00 +11:00
|
|
|
}
|
|
|
|
|
2023-04-24 02:39:00 +10:00
|
|
|
#[test]
|
|
|
|
fn correctly_drop_on_override() {
|
2022-03-20 09:59:00 +11:00
|
|
|
let drop_registry = DropRegistry::new();
|
|
|
|
|
|
|
|
let droppable1 = drop_registry.new_droppable();
|
|
|
|
let droppable2 = drop_registry.new_droppable();
|
|
|
|
|
|
|
|
let id1 = droppable1.id;
|
|
|
|
let id2 = droppable2.id;
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut map = HashMap::new();
|
|
|
|
|
|
|
|
map.insert(1, droppable1);
|
|
|
|
drop_registry.assert_not_dropped(id1);
|
|
|
|
map.insert(1, droppable2);
|
|
|
|
|
2022-03-21 00:41:31 +11:00
|
|
|
drop_registry.assert_dropped_once(id1);
|
2022-03-20 09:59:00 +11:00
|
|
|
drop_registry.assert_not_dropped(id2);
|
|
|
|
}
|
|
|
|
|
2022-03-21 00:41:31 +11:00
|
|
|
drop_registry.assert_dropped_once(id2);
|
|
|
|
}
|
|
|
|
|
2023-04-24 02:39:00 +10:00
|
|
|
#[test]
|
|
|
|
fn correctly_drops_key_on_override() {
|
2022-03-21 00:41:31 +11:00
|
|
|
let drop_registry = DropRegistry::new();
|
|
|
|
|
|
|
|
let droppable1 = drop_registry.new_droppable();
|
|
|
|
let droppable1a = droppable1.clone();
|
|
|
|
|
|
|
|
let id1 = droppable1.id;
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut map = HashMap::new();
|
|
|
|
|
|
|
|
map.insert(droppable1, 1);
|
|
|
|
drop_registry.assert_not_dropped(id1);
|
|
|
|
map.insert(droppable1a, 2);
|
|
|
|
|
|
|
|
drop_registry.assert_dropped_once(id1);
|
|
|
|
}
|
|
|
|
|
|
|
|
drop_registry.assert_dropped_n_times(id1, 2);
|
2022-03-20 09:59:00 +11:00
|
|
|
}
|
2022-03-21 02:38:39 +11:00
|
|
|
|
2023-04-24 02:39:00 +10:00
|
|
|
#[test]
|
|
|
|
fn test_retain() {
|
2022-10-03 05:05:29 +11:00
|
|
|
let mut map = HashMap::new();
|
|
|
|
|
|
|
|
for i in 0..100 {
|
|
|
|
map.insert(i, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
map.retain(|k, _| k % 2 == 0);
|
|
|
|
|
|
|
|
assert_eq!(map[&2], 2);
|
|
|
|
assert_eq!(map.get(&3), None);
|
|
|
|
|
|
|
|
assert_eq!(map.iter().count(), 50); // force full iteration
|
|
|
|
}
|
|
|
|
|
2023-04-24 02:39:00 +10:00
|
|
|
#[test]
|
|
|
|
fn test_size_hint_iter() {
|
2022-10-03 05:10:50 +11:00
|
|
|
let mut map = HashMap::new();
|
|
|
|
|
|
|
|
for i in 0..100 {
|
|
|
|
map.insert(i, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut iter = map.iter();
|
|
|
|
assert_eq!(iter.size_hint(), (100, Some(100)));
|
|
|
|
|
|
|
|
iter.next();
|
|
|
|
|
|
|
|
assert_eq!(iter.size_hint(), (99, Some(99)));
|
|
|
|
}
|
|
|
|
|
2023-04-24 02:39:00 +10:00
|
|
|
#[test]
|
|
|
|
fn test_size_hint_into_iter() {
|
2022-10-03 05:10:50 +11:00
|
|
|
let mut map = HashMap::new();
|
|
|
|
|
|
|
|
for i in 0..100 {
|
|
|
|
map.insert(i, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut iter = map.into_iter();
|
|
|
|
assert_eq!(iter.size_hint(), (100, Some(100)));
|
|
|
|
|
|
|
|
iter.next();
|
|
|
|
|
|
|
|
assert_eq!(iter.size_hint(), (99, Some(99)));
|
|
|
|
}
|
|
|
|
|
2022-03-21 02:38:39 +11:00
|
|
|
// Following test cases copied from the rust source
|
|
|
|
// https://github.com/rust-lang/rust/blob/master/library/std/src/collections/hash/map/tests.rs
|
|
|
|
mod rust_std_tests {
|
2023-05-10 06:12:28 +10:00
|
|
|
use alloc::format;
|
|
|
|
|
2023-04-24 02:39:00 +10:00
|
|
|
use crate::{Entry::*, HashMap};
|
2022-03-21 02:38:39 +11:00
|
|
|
|
2023-04-24 02:39:00 +10:00
|
|
|
#[test]
|
|
|
|
fn test_entry() {
|
2022-03-21 02:38:39 +11:00
|
|
|
let xs = [(1, 10), (2, 20), (3, 30), (4, 40), (5, 50), (6, 60)];
|
|
|
|
|
2022-06-12 05:56:05 +10:00
|
|
|
let mut map: HashMap<_, _> = xs.iter().copied().collect();
|
2022-03-21 02:38:39 +11:00
|
|
|
|
|
|
|
// Existing key (insert)
|
|
|
|
match map.entry(1) {
|
|
|
|
Vacant(_) => unreachable!(),
|
|
|
|
Occupied(mut view) => {
|
|
|
|
assert_eq!(view.get(), &10);
|
|
|
|
assert_eq!(view.insert(100), 10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert_eq!(map.get(&1).unwrap(), &100);
|
|
|
|
assert_eq!(map.len(), 6);
|
|
|
|
|
|
|
|
// Existing key (update)
|
|
|
|
match map.entry(2) {
|
|
|
|
Vacant(_) => unreachable!(),
|
|
|
|
Occupied(mut view) => {
|
|
|
|
let v = view.get_mut();
|
|
|
|
let new_v = (*v) * 10;
|
|
|
|
*v = new_v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert_eq!(map.get(&2).unwrap(), &200);
|
|
|
|
assert_eq!(map.len(), 6);
|
|
|
|
|
|
|
|
// Existing key (take)
|
|
|
|
match map.entry(3) {
|
|
|
|
Vacant(_) => unreachable!(),
|
|
|
|
Occupied(view) => {
|
|
|
|
assert_eq!(view.remove(), 30);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert_eq!(map.get(&3), None);
|
|
|
|
assert_eq!(map.len(), 5);
|
|
|
|
|
|
|
|
// Inexistent key (insert)
|
|
|
|
match map.entry(10) {
|
|
|
|
Occupied(_) => unreachable!(),
|
|
|
|
Vacant(view) => {
|
|
|
|
assert_eq!(*view.insert(1000), 1000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert_eq!(map.get(&10).unwrap(), &1000);
|
|
|
|
assert_eq!(map.len(), 6);
|
|
|
|
}
|
|
|
|
|
2023-04-24 02:39:00 +10:00
|
|
|
#[test]
|
|
|
|
fn test_occupied_entry_key() {
|
2022-03-21 02:38:39 +11:00
|
|
|
let mut a = HashMap::new();
|
|
|
|
let key = "hello there";
|
|
|
|
let value = "value goes here";
|
|
|
|
assert!(a.is_empty());
|
|
|
|
a.insert(key, value);
|
|
|
|
assert_eq!(a.len(), 1);
|
|
|
|
assert_eq!(a[key], value);
|
|
|
|
|
|
|
|
match a.entry(key) {
|
|
|
|
Vacant(_) => panic!(),
|
|
|
|
Occupied(e) => assert_eq!(key, *e.key()),
|
|
|
|
}
|
|
|
|
assert_eq!(a.len(), 1);
|
|
|
|
assert_eq!(a[key], value);
|
|
|
|
}
|
|
|
|
|
2023-04-24 02:39:00 +10:00
|
|
|
#[test]
|
|
|
|
fn test_vacant_entry_key() {
|
2022-03-21 02:38:39 +11:00
|
|
|
let mut a = HashMap::new();
|
|
|
|
let key = "hello there";
|
|
|
|
let value = "value goes here";
|
|
|
|
|
|
|
|
assert!(a.is_empty());
|
|
|
|
match a.entry(key) {
|
|
|
|
Occupied(_) => panic!(),
|
|
|
|
Vacant(e) => {
|
|
|
|
assert_eq!(key, *e.key());
|
|
|
|
e.insert(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert_eq!(a.len(), 1);
|
|
|
|
assert_eq!(a[key], value);
|
|
|
|
}
|
|
|
|
|
2023-04-24 02:39:00 +10:00
|
|
|
#[test]
|
|
|
|
fn test_index() {
|
2022-03-21 02:38:39 +11:00
|
|
|
let mut map = HashMap::new();
|
|
|
|
|
|
|
|
map.insert(1, 2);
|
|
|
|
map.insert(2, 1);
|
|
|
|
map.insert(3, 4);
|
|
|
|
|
|
|
|
assert_eq!(map[&2], 1);
|
|
|
|
}
|
2023-05-10 06:12:28 +10:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_eq() {
|
|
|
|
let mut m1 = HashMap::new();
|
|
|
|
m1.insert(1, 2);
|
|
|
|
m1.insert(2, 3);
|
|
|
|
m1.insert(3, 4);
|
|
|
|
|
|
|
|
let mut m2 = HashMap::new();
|
|
|
|
m2.insert(1, 2);
|
|
|
|
m2.insert(2, 3);
|
|
|
|
|
|
|
|
assert!(m1 != m2);
|
|
|
|
|
|
|
|
m2.insert(3, 4);
|
|
|
|
|
|
|
|
assert_eq!(m1, m2);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_show() {
|
|
|
|
let mut map = HashMap::new();
|
|
|
|
let empty: HashMap<i32, i32> = HashMap::new();
|
|
|
|
|
|
|
|
map.insert(1, 2);
|
|
|
|
map.insert(3, 4);
|
|
|
|
|
|
|
|
let map_str = format!("{map:?}");
|
|
|
|
|
|
|
|
assert!(map_str == "{1: 2, 3: 4}" || map_str == "{3: 4, 1: 2}");
|
|
|
|
assert_eq!(format!("{empty:?}"), "{}");
|
|
|
|
}
|
2022-03-21 02:38:39 +11:00
|
|
|
}
|
2022-03-18 09:46:38 +11:00
|
|
|
}
|