Skip to content

Commit

Permalink
Update hashbrown and adjust API
Browse files Browse the repository at this point in the history
Signed-off-by: Heinz N. Gies <[email protected]>
  • Loading branch information
Licenser committed Jan 6, 2025
1 parent 650c7b0 commit 0495a36
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 132 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ repository = "https://github.com/Licenser/halfbrown"
version = "0.2.5"

[dependencies]
hashbrown = "0.14"
rustc-hash = { version = "1.1", optional = true }
hashbrown = "0.15"
rustc-hash = { version = "2.1", optional = true }
serde = { version = "1", default-features = false, optional = true }
arrayvec = { version = "0.7", optional = true }
[dev-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion benches/compare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ fn bench_group(b: &mut Criterion, name: &str, bench_input: BenchInput) {
|data| {
let mut m = halfbrown::HashMap::with_capacity(bench_input.initial_cap);
for e in data {
m.insert_nocheck(e, e);
unsafe { m.insert_nocheck(e, e) };
}
},
BatchSize::SmallInput,
Expand Down
78 changes: 39 additions & 39 deletions src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,57 +438,57 @@ impl<'a, K, V, const N: usize, S> OccupiedEntry<'a, K, V, N, S> {
/// # Examples
///
/// ```
/// use hashbrown::hash_map::{Entry, HashMap};
/// use halfbrown::{Entry, HashMap};
/// use std::rc::Rc;
///
/// let mut map: HashMap<Rc<String>, u32> = HashMap::new();
/// map.insert(Rc::new("Stringthing".to_string()), 15);
///
/// let my_key = Rc::new("Stringthing".to_string());
/// let mut map: HashMap<&str, u32> = HashMap::new();
/// map.insert("poneyland", 42);
///
/// let entry = match map.entry("poneyland") {
/// Entry::Occupied(e) => {
/// e.replace_entry_with(|k, v| {
/// assert_eq!(k, &"poneyland");
/// assert_eq!(v, 42);
/// Some(v + 1)
/// })
/// }
/// Entry::Vacant(_) => panic!(),
/// };
///
/// if let Entry::Occupied(entry) = map.entry(my_key) {
/// // Also replace the key with a handle to our other key.
/// let (old_key, old_value): (Rc<String>, u32) = entry.replace_entry(16);
/// match entry {
/// Entry::Occupied(e) => {
/// assert_eq!(e.key(), &"poneyland");
/// assert_eq!(e.get(), &43);
/// }
/// Entry::Vacant(_) => panic!(),
/// }
///
/// ```
#[inline]
pub fn replace_entry(self, value: V) -> (K, V) {
match self.0 {
OccupiedEntryInt::Map(m) => m.replace_entry(value),
OccupiedEntryInt::Vec(m) => m.replace_entry(value),
}
}

/// Replaces the key in the hash map with the key used to create this entry.
///
/// # Examples
///
/// ```
/// use hashbrown::hash_map::{Entry, HashMap};
/// use std::rc::Rc;
///
/// let mut map: HashMap<Rc<String>, u32> = HashMap::new();
/// let mut known_strings: Vec<Rc<String>> = Vec::new();
///
/// // Initialise known strings, run program, etc.
/// assert_eq!(map["poneyland"], 43);
///
/// reclaim_memory(&mut map, &known_strings);
/// let entry = match map.entry("poneyland") {
/// Entry::Occupied(e) => e.replace_entry_with(|_k, _v| None),
/// Entry::Vacant(_) => panic!(),
/// };
///
/// fn reclaim_memory(map: &mut HashMap<Rc<String>, u32>, known_strings: &[Rc<String>] ) {
/// for s in known_strings {
/// if let Entry::Occupied(entry) = map.entry(s.clone()) {
/// // Replaces the entry's key with our version of it in `known_strings`.
/// entry.replace_key();
/// }
/// match entry {
/// Entry::Vacant(e) => {
/// assert_eq!(e.key(), &"poneyland");
/// }
/// Entry::Occupied(_) => panic!(),
/// }
///
/// assert!(!map.contains_key("poneyland"));
///
/// ```
#[inline]
pub fn replace_key(self) -> K {
pub fn replace_entry_with<F>(self, f: F) -> Entry<'a, K, V, N, S>
where
F: FnOnce(&K, V) -> Option<V>,
V: Clone,
{
match self.0 {
OccupiedEntryInt::Map(m) => m.replace_key(),
OccupiedEntryInt::Vec(m) => m.replace_key(),
OccupiedEntryInt::Map(m) => m.replace_entry_with(f).into(),
OccupiedEntryInt::Vec(m) => m.replace_entry_with(f).into(),
}
}
}
Expand Down
22 changes: 16 additions & 6 deletions src/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::iter::{FromIterator, FusedIterator, IntoIterator};
pub struct Iter<'a, K, V>(IterInt<'a, K, V>);

/// Manual implementation so that `Clone` isn't required for `K` nor `V`
impl<'a, K, V> Clone for Iter<'a, K, V> {
impl<K, V> Clone for Iter<'_, K, V> {
fn clone(&self) -> Self {
Iter(self.0.clone())
}
Expand All @@ -27,7 +27,7 @@ pub(crate) enum IterInt<'a, K, V> {
}

/// Manual implementation so that `Clone` isn't required for `K` nor `V`
impl<'a, K, V> Clone for IterInt<'a, K, V> {
impl<K, V> Clone for IterInt<'_, K, V> {
fn clone(&self) -> Self {
match self {
IterInt::Map(i) => IterInt::Map(i.clone()),
Expand Down Expand Up @@ -60,7 +60,7 @@ impl<'a, K, V> Iterator for Iter<'a, K, V> {
}
}

impl<'a, K, V> ExactSizeIterator for Iter<'a, K, V> {
impl<K, V> ExactSizeIterator for Iter<'_, K, V> {
#[inline]
fn len(&self) -> usize {
match &self.0 {
Expand All @@ -70,7 +70,7 @@ impl<'a, K, V> ExactSizeIterator for Iter<'a, K, V> {
}
}

impl<'a, K, V> FusedIterator for Iter<'a, K, V> {}
impl<K, V> FusedIterator for Iter<'_, K, V> {}

/// Into iterator for a Halfbrown map
pub struct IntoIter<K, V, const N: usize>(IntoIterInt<K, V, N>);
Expand Down Expand Up @@ -149,6 +149,16 @@ impl<'a, K, V, S, const N: usize> IntoIterator for &'a SizedHashMap<K, V, S, N>
}
}

impl<'a, K, V, S, const N: usize> IntoIterator for &'a mut SizedHashMap<K, V, S, N> {
type Item = (&'a K, &'a V);
type IntoIter = Iter<'a, K, V>;

#[inline]
fn into_iter(self) -> Iter<'a, K, V> {
self.iter()
}
}

impl<K, V, S, const N: usize> FromIterator<(K, V)> for SizedHashMap<K, V, S, N>
where
K: Eq + Hash,
Expand Down Expand Up @@ -198,7 +208,7 @@ impl<'a, K, V> Iterator for IterMut<'a, K, V> {
}
}

impl<'a, K, V> ExactSizeIterator for IterMut<'a, K, V> {
impl<K, V> ExactSizeIterator for IterMut<'_, K, V> {
#[inline]
fn len(&self) -> usize {
match &self.0 {
Expand All @@ -208,4 +218,4 @@ impl<'a, K, V> ExactSizeIterator for IterMut<'a, K, V> {
}
}

impl<'a, K, V> FusedIterator for IterMut<'a, K, V> {}
impl<K, V> FusedIterator for IterMut<'_, K, V> {}
41 changes: 21 additions & 20 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,14 @@
//! their copyright.
#![warn(unused_extern_crates)]
#![cfg_attr(
feature = "cargo-clippy",
deny(
clippy::all,
clippy::unwrap_used,
clippy::unnecessary_unwrap,
clippy::pedantic
),
// We might want to revisit inline_always
allow(clippy::module_name_repetitions, clippy::inline_always)
#![deny(
clippy::all,
clippy::unwrap_used,
clippy::unnecessary_unwrap,
clippy::pedantic
)]
// We might want to revisit inline_always
#![allow(clippy::module_name_repetitions, clippy::inline_always)]
#![deny(missing_docs)]

mod entry;
Expand Down Expand Up @@ -58,7 +55,7 @@ pub type DefaultHashBuilder = core::hash::BuildHasherDefault<rustc_hash::FxHashe

use crate::vectypes::VecDrain;
#[cfg(not(feature = "fxhash"))]
pub use hashbrown::hash_map::DefaultHashBuilder;
pub use hashbrown::DefaultHashBuilder;

/// `SizedHashMap` implementation that alternates between a vector
/// and a hashmap to improve performance for low key counts.
Expand All @@ -67,7 +64,6 @@ pub type HashMap<K, V, S = DefaultHashBuilder> = SizedHashMap<K, V, S, 32>;

/// Maximum nymber of elements before the representaiton is swapped from
/// Vec to `HashMap`
/// `SizedHashMap` implementation that alternates between a vector
/// and a hashmap to improve performance for low key counts. With a configurable upper vector limit
#[derive(Clone)]
Expand Down Expand Up @@ -172,8 +168,8 @@ impl<K, V, S, const VEC_LIMIT_UPPER: usize> SizedHashMap<K, V, S, VEC_LIMIT_UPPE
/// # Examples
///
/// ```
/// use hashbrown::HashMap;
/// use hashbrown::hash_map::DefaultHashBuilder;
/// use halfbrown::HashMap;
/// use halfbrown::DefaultHashBuilder;
///
/// let s = DefaultHashBuilder::default();
/// let mut map = HashMap::with_hasher(s);
Expand All @@ -198,8 +194,8 @@ impl<K, V, S, const VEC_LIMIT_UPPER: usize> SizedHashMap<K, V, S, VEC_LIMIT_UPPE
/// # Examples
///
/// ```
/// use hashbrown::HashMap;
/// use hashbrown::hash_map::DefaultHashBuilder;
/// use halfbrown::HashMap;
/// use halfbrown::DefaultHashBuilder;
///
/// let s = DefaultHashBuilder::default();
/// let mut map = HashMap::with_capacity_and_hasher(10, s);
Expand All @@ -221,8 +217,8 @@ impl<K, V, S, const VEC_LIMIT_UPPER: usize> SizedHashMap<K, V, S, VEC_LIMIT_UPPE
/// # Examples
///
/// ```
/// use hashbrown::HashMap;
/// use hashbrown::hash_map::DefaultHashBuilder;
/// use halfbrown::HashMap;
/// use halfbrown::DefaultHashBuilder;
///
/// let hasher = DefaultHashBuilder::default();
/// let map: HashMap<i32, i32> = HashMap::with_hasher(hasher);
Expand Down Expand Up @@ -830,8 +826,13 @@ where
/// Inserts element, this ignores check in the vector
/// map if keys are present - it's a fast way to build
/// a new map when uniqueness is known ahead of time.
///
/// # Safety
///
/// Used for building new hashmaps wher eit is known that
/// the keys are unique.
#[inline]
pub fn insert_nocheck(&mut self, k: K, v: V)
pub unsafe fn insert_nocheck(&mut self, k: K, v: V)
where
S: Default,
{
Expand Down Expand Up @@ -1054,7 +1055,7 @@ enum DrainInt<'a, K, V, const N: usize> {
Vec(VecDrain<'a, (K, V), N>),
}

impl<'a, K, V, const N: usize> Iterator for Drain<'a, K, V, N> {
impl<K, V, const N: usize> Iterator for Drain<'_, K, V, N> {
type Item = (K, V);
#[inline]
fn next(&mut self) -> Option<Self::Item> {
Expand Down
8 changes: 4 additions & 4 deletions src/raw_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ where
/// # Examples
///
/// ```
/// use hashbrown::HashMap;
/// use halfbrown::HashMap;
///
/// let mut map: HashMap<&str, u32> = HashMap::new();
/// let entry = map.raw_entry_mut().from_key("horseyland").insert("horseyland", 37);
Expand Down Expand Up @@ -339,7 +339,7 @@ where
/// # Examples
///
/// ```
/// use hashbrown::HashMap;
/// use halfbrown::HashMap;
///
/// let mut map: HashMap<&str, u32> = HashMap::new();
///
Expand Down Expand Up @@ -367,7 +367,7 @@ where
/// # Examples
///
/// ```
/// use hashbrown::HashMap;
/// use halfbrown::HashMap;
///
/// let mut map: HashMap<&str, String> = HashMap::new();
///
Expand Down Expand Up @@ -399,7 +399,7 @@ where
/// # Examples
///
/// ```
/// use hashbrown::HashMap;
/// use halfbrown::HashMap;
///
/// let mut map: HashMap<&str, u32> = HashMap::new();
///
Expand Down
Loading

0 comments on commit 0495a36

Please sign in to comment.