aboutsummaryrefslogtreecommitdiff
path: root/lib/std/hash_map.zig
diff options
context:
space:
mode:
Diffstat (limited to 'lib/std/hash_map.zig')
-rw-r--r--lib/std/hash_map.zig36
1 files changed, 34 insertions, 2 deletions
diff --git a/lib/std/hash_map.zig b/lib/std/hash_map.zig
index 06071f0a0b..644429f871 100644
--- a/lib/std/hash_map.zig
+++ b/lib/std/hash_map.zig
@@ -92,6 +92,34 @@ pub fn hashString(s: []const u8) u64 {
return std.hash.Wyhash.hash(0, s);
}
+pub const StringIndexContext = struct {
+ bytes: *std.ArrayListUnmanaged(u8),
+
+ pub fn eql(self: @This(), a: u32, b: u32) bool {
+ _ = self;
+ return a == b;
+ }
+
+ pub fn hash(self: @This(), x: u32) u64 {
+ const x_slice = mem.spanZ(@ptrCast([*:0]const u8, self.bytes.items.ptr) + x);
+ return hashString(x_slice);
+ }
+};
+
+pub const StringIndexAdapter = struct {
+ bytes: *std.ArrayListUnmanaged(u8),
+
+ pub fn eql(self: @This(), a_slice: []const u8, b: u32) bool {
+ const b_slice = mem.spanZ(@ptrCast([*:0]const u8, self.bytes.items.ptr) + b);
+ return mem.eql(u8, a_slice, b_slice);
+ }
+
+ pub fn hash(self: @This(), adapted_key: []const u8) u64 {
+ _ = self;
+ return hashString(adapted_key);
+ }
+};
+
/// Deprecated use `default_max_load_percentage`
pub const DefaultMaxLoadPercentage = default_max_load_percentage;
@@ -622,8 +650,12 @@ pub fn HashMap(
return other.promoteContext(self.allocator, new_ctx);
}
- /// Creates a copy of this map, using a specified allocator and context
- pub fn cloneWithAllocatorAndContext(new_allocator: *Allocator, new_ctx: anytype) !HashMap(K, V, @TypeOf(new_ctx), max_load_percentage) {
+ /// Creates a copy of this map, using a specified allocator and context.
+ pub fn cloneWithAllocatorAndContext(
+ self: Self,
+ new_allocator: *Allocator,
+ new_ctx: anytype,
+ ) !HashMap(K, V, @TypeOf(new_ctx), max_load_percentage) {
var other = try self.unmanaged.cloneContext(new_allocator, new_ctx);
return other.promoteContext(new_allocator, new_ctx);
}