Change distance_to_initial_bucket to be an i32

This commit is contained in:
Gwilym Kuiper 2022-03-19 21:54:10 +00:00
parent 9f6797f4ed
commit cc53b0a911

View file

@ -10,7 +10,7 @@ type HashType = u32;
struct Node<K, V> { struct Node<K, V> {
hash: HashType, hash: HashType,
distance_to_initial_bucket: u32, distance_to_initial_bucket: i32,
key: K, key: K,
value: V, value: V,
} }
@ -48,8 +48,8 @@ impl<K, V> NodeStorage<K, V> {
value: V, value: V,
hash: HashType, hash: HashType,
number_of_elements: usize, number_of_elements: usize,
max_distance_to_initial_bucket: u32, max_distance_to_initial_bucket: i32,
) -> u32 { ) -> i32 {
debug_assert!( debug_assert!(
self.len() * 85 / 100 > number_of_elements, self.len() * 85 / 100 > number_of_elements,
"Do not have space to insert into len {} with {number_of_elements}", "Do not have space to insert into len {} with {number_of_elements}",
@ -68,7 +68,7 @@ impl<K, V> NodeStorage<K, V> {
loop { loop {
let location = fast_mod( let location = fast_mod(
self.len(), self.len(),
new_node.hash + new_node.distance_to_initial_bucket, new_node.hash + new_node.distance_to_initial_bucket as HashType,
); );
let current_node = self.0[location].as_mut(); let current_node = self.0[location].as_mut();
@ -123,7 +123,7 @@ impl<K, V> NodeStorage<K, V> {
pub struct HashMap<K, V> { pub struct HashMap<K, V> {
number_of_elements: usize, number_of_elements: usize,
max_distance_to_initial_bucket: u32, max_distance_to_initial_bucket: i32,
nodes: NodeStorage<K, V>, nodes: NodeStorage<K, V>,
hasher: BuildHasherDefault<FxHasher>, hasher: BuildHasherDefault<FxHasher>,
@ -196,7 +196,10 @@ where
{ {
fn get_location(&self, key: &K, hash: HashType) -> Option<usize> { fn get_location(&self, key: &K, hash: HashType) -> Option<usize> {
for distance_to_initial_bucket in 0..=self.max_distance_to_initial_bucket { for distance_to_initial_bucket in 0..=self.max_distance_to_initial_bucket {
let location = fast_mod(self.nodes.len(), hash + distance_to_initial_bucket); let location = fast_mod(
self.nodes.len(),
hash + distance_to_initial_bucket as HashType,
);
let node = &self.nodes.0[location]; let node = &self.nodes.0[location];
if let Some(node) = node { if let Some(node) = node {