Skip to content

Commit

Permalink
Fix the Wyhash implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
devraymondsh committed Jan 18, 2024
1 parent c2e7317 commit 299a484
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/core/ByteMap.zig
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ table: []Entry,
table_size: usize,

var collisions: usize = 0;
var hasher = Wyhash.init(0);

pub fn init(self: *ByteMap, allocator: *Mmap, size_arg: usize) !void {
self.table_size = try std.math.ceilPowerOfTwo(usize, size_arg);
Expand All @@ -57,7 +58,7 @@ pub fn init(self: *ByteMap, allocator: *Mmap, size_arg: usize) !void {
}

fn get_index(self: *ByteMap, key: []const u8) usize {
return std.hash.Wyhash.hash(@intCast(key.len), key) & (self.table_size - 1);
return hasher.reset_hash(@intCast(key.len), key) & (self.table_size - 1);
}

fn find_entry(self: *ByteMap, key: []const u8, comptime insertion: bool) ?*Entry {
Expand Down
11 changes: 11 additions & 0 deletions src/core/Wyhash.zig
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,17 @@ pub fn hash(self: *Wyhash, input: []const u8) u64 {
return self.final2();
}

pub fn reset_hash(self: *Wyhash, seed: u64, input: []const u8) u64 {
self.total_len = 0;
self.buf_len = 0;

self.state[0] = seed ^ mix(seed ^ secret[0], secret[1]);
self.state[1] = self.state[0];
self.state[2] = self.state[0];

return self.hash(input);
}

pub fn reset(self: *Wyhash) void {
self.total_len = 0;
self.buf_len = 0;
Expand Down

0 comments on commit 299a484

Please sign in to comment.