Skip to content

Instantly share code, notes, and snippets.

@andrewrk
Created April 26, 2021 05:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewrk/af54885fb1feb356648de5db17a95349 to your computer and use it in GitHub Desktop.
Save andrewrk/af54885fb1feb356648de5db17a95349 to your computer and use it in GitHub Desktop.
diff --git a/lib/std/array_hash_map.zig b/lib/std/array_hash_map.zig
index 83a061dfe..fc1f18771 100644
--- a/lib/std/array_hash_map.zig
+++ b/lib/std/array_hash_map.zig
@@ -571,21 +571,30 @@ pub fn ArrayHashMapUnmanaged(
}
pub fn getIndex(self: Self, key: K) ?usize {
+ const Matcher = struct {
+ const Key = K;
+ const hash2 = hash;
+ const eql2 = eql;
+ };
+ return self.getIndexMatcher(Matcher, key);
+ }
+
+ pub fn getIndexMatcher(self: Self, comptime Matcher: type, key: Matcher.Key) ?usize {
const header = self.index_header orelse {
// Linear scan.
- const h = if (store_hash) hash(key) else {};
+ const h = if (store_hash) Matcher.hash2(key) else {};
for (self.entries.items) |*item, i| {
- if (item.hash == h and eql(key, item.key)) {
+ if (item.hash == h and Matcher.eql2(key, item.key)) {
return i;
}
}
return null;
};
switch (header.capacityIndexType()) {
- .u8 => return self.getInternal(key, header, u8),
- .u16 => return self.getInternal(key, header, u16),
- .u32 => return self.getInternal(key, header, u32),
- .usize => return self.getInternal(key, header, usize),
+ .u8 => return self.getInternal(Matcher, key, header, u8),
+ .u16 => return self.getInternal(Matcher, key, header, u16),
+ .u32 => return self.getInternal(Matcher, key, header, u32),
+ .usize => return self.getInternal(Matcher, key, header, usize),
}
}
@@ -903,9 +912,15 @@ pub fn ArrayHashMapUnmanaged(
unreachable;
}
- fn getInternal(self: Self, key: K, header: *IndexHeader, comptime I: type) ?usize {
+ fn getInternal(
+ self: Self,
+ comptime Matcher: type,
+ key: Matcher.Key,
+ header: *IndexHeader,
+ comptime I: type,
+ ) ?usize {
const indexes = header.indexes(I);
- const h = hash(key);
+ const h = Matcher.hash2(key);
const start_index = header.constrainIndex(h);
var roll_over: usize = 0;
while (roll_over <= header.max_distance_from_start_index) : (roll_over += 1) {
@@ -916,7 +931,7 @@ pub fn ArrayHashMapUnmanaged(
const entry = &self.entries.items[index.entry_index];
const hash_match = if (store_hash) h == entry.hash else true;
- if (hash_match and eql(key, entry.key))
+ if (hash_match and Matcher.eql2(key, entry.key))
return index.entry_index;
}
return null;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment