Skip to content

Commit

Permalink
HashMap.getOrPutAssumeCapacityAdapted should set key to undefined (#9138
Browse files Browse the repository at this point in the history
)

* std.hash_map.HashMap: getOrPutAssumeCapacityAdapted should set key to undefined

* add test for std.hash_map.HashMap.getOrPutAdapted
  • Loading branch information
Hadron67 authored Jun 18, 2021
1 parent 6ce8440 commit 1f29b75
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion lib/std/hash_map.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1236,7 +1236,7 @@ pub fn HashMapUnmanaged(
metadata[0].fill(fingerprint);
const new_key = &self.keys()[idx];
const new_value = &self.values()[idx];
new_key.* = key;
new_key.* = undefined;
new_value.* = undefined;
self.size += 1;

Expand Down Expand Up @@ -1884,6 +1884,52 @@ test "std.hash_map clone" {
}
}

test "std.hash_map getOrPutAdapted" {
const AdaptedContext = struct {
fn eql(self: @This(), adapted_key: []const u8, test_key: u64) bool {
return std.fmt.parseInt(u64, adapted_key, 10) catch unreachable == test_key;
}
fn hash(self: @This(), adapted_key: []const u8) u64 {
const key = std.fmt.parseInt(u64, adapted_key, 10) catch unreachable;
return (AutoContext(u64){}).hash(key);
}
};
var map = AutoHashMap(u64, u64).init(testing.allocator);
defer map.deinit();

const keys = [_][]const u8{
"1231",
"4564",
"7894",
"1132",
"65235",
"95462",
"0112305",
"00658",
"0",
"2",
};

var real_keys: [keys.len]u64 = undefined;

inline for (keys) |key_str, i| {
const result = try map.getOrPutAdapted(key_str, AdaptedContext{});
try testing.expect(!result.found_existing);
real_keys[i] = std.fmt.parseInt(u64, key_str, 10) catch unreachable;
result.key_ptr.* = real_keys[i];
result.value_ptr.* = i * 2;
}

try testing.expectEqual(map.count(), keys.len);

inline for (keys) |key_str, i| {
const result = try map.getOrPutAdapted(key_str, AdaptedContext{});
try testing.expect(result.found_existing);
try testing.expectEqual(real_keys[i], result.key_ptr.*);
try testing.expectEqual(@as(u64, i) * 2, result.value_ptr.*);
}
}

test "compile everything" {
std.testing.refAllDecls(AutoHashMap(i32, i32));
std.testing.refAllDecls(StringHashMap([]const u8));
Expand Down

0 comments on commit 1f29b75

Please sign in to comment.