Skip to content

Commit

Permalink
Wrap hashbrown hash builder
Browse files Browse the repository at this point in the history
  • Loading branch information
djc committed Dec 6, 2024
1 parent 722c814 commit b870a0f
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 6 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ circle-ci = { repository = "kyren/hashlink", branch = "master" }
serde_impl = ["serde"]

[dependencies]
foldhash = { version = "0.1.3", default-features = false }
hashbrown = { version = "0.15", default-features = false, features = ["default-hasher", "inline-more"] }
serde = { version = "1.0", default-features = false, optional = true }

Expand Down
95 changes: 95 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,101 @@ pub mod lru_cache;
#[cfg(feature = "serde_impl")]
pub mod serde;

use core::hash::{BuildHasher, Hasher};

pub use linked_hash_map::LinkedHashMap;
pub use linked_hash_set::LinkedHashSet;
pub use lru_cache::LruCache;

/// Default hash builder, matches hashbrown's default hasher.
///
/// See [`DefaultHasher`] for more details.
#[derive(Clone, Copy, Default, Debug)]
pub struct DefaultHashBuilder(hashbrown::DefaultHashBuilder);

impl BuildHasher for DefaultHashBuilder {
type Hasher = DefaultHasher;

#[inline]
fn build_hasher(&self) -> Self::Hasher {
DefaultHasher(self.0.build_hasher())
}
}

/// Default hasher, as selected by hashbrown.
///
/// Currently this is [`foldhash::fast::FoldHasher`].
#[derive(Clone)]
pub struct DefaultHasher(foldhash::fast::FoldHasher);

impl Hasher for DefaultHasher {
#[inline]
fn write(&mut self, bytes: &[u8]) {
self.0.write(bytes)
}

#[inline]
fn write_u8(&mut self, i: u8) {
self.0.write_u8(i)
}

#[inline]
fn write_u16(&mut self, i: u16) {
self.0.write_u16(i)
}

#[inline]
fn write_u32(&mut self, i: u32) {
self.0.write_u32(i)
}

#[inline]
fn write_u64(&mut self, i: u64) {
self.0.write_u64(i)
}

#[inline]
fn write_u128(&mut self, i: u128) {
self.0.write_u128(i)
}

#[inline]
fn write_usize(&mut self, i: usize) {
self.0.write_usize(i)
}

#[inline]
fn finish(&self) -> u64 {
self.0.finish()
}

#[inline]
fn write_i8(&mut self, i: i8) {
self.0.write_i8(i)
}

#[inline]
fn write_i16(&mut self, i: i16) {
self.0.write_i16(i)
}

#[inline]
fn write_i32(&mut self, i: i32) {
self.0.write_i32(i)
}

#[inline]
fn write_i64(&mut self, i: i64) {
self.0.write_i64(i)
}

#[inline]
fn write_i128(&mut self, i: i128) {
self.0.write_i128(i)
}

#[inline]
fn write_isize(&mut self, i: isize) {
self.0.write_isize(i)
}
}
3 changes: 2 additions & 1 deletion src/linked_hash_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ use core::{

use alloc::boxed::Box;
use hashbrown::hash_table::{self, HashTable};
use hashbrown::DefaultHashBuilder;

use crate::DefaultHashBuilder;

pub enum TryReserveError {
CapacityOverflow,
Expand Down
3 changes: 1 addition & 2 deletions src/linked_hash_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ use core::{
ops::{BitAnd, BitOr, BitXor, Sub},
};

use hashbrown::DefaultHashBuilder;

use crate::linked_hash_map::{self, LinkedHashMap, TryReserveError};
use crate::DefaultHashBuilder;

pub struct LinkedHashSet<T, S = DefaultHashBuilder> {
map: LinkedHashMap<T, (), S>,
Expand Down
3 changes: 1 addition & 2 deletions src/lru_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ use core::{
hash::{BuildHasher, Hash},
};

use hashbrown::DefaultHashBuilder;

use crate::linked_hash_map::{self, LinkedHashMap};
use crate::DefaultHashBuilder;

pub use crate::linked_hash_map::{
Drain, Entry, IntoIter, Iter, IterMut, OccupiedEntry, RawEntryBuilder, RawEntryBuilderMut,
Expand Down
2 changes: 1 addition & 1 deletion tests/linked_hash_set.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use hashbrown::DefaultHashBuilder;
use hashlink::linked_hash_set::{self, LinkedHashSet};
use hashlink::DefaultHashBuilder;

#[allow(dead_code)]
fn assert_covariance() {
Expand Down

0 comments on commit b870a0f

Please sign in to comment.