Skip to content

Commit 6fe1993

Browse files
committed
fix ArrayHashMap setKey when store_hash=true
1 parent f87b443 commit 6fe1993

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

lib/std/array_hash_map.zig

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,7 +1351,7 @@ pub fn ArrayHashMapUnmanaged(
13511351
pub fn setKeyContext(self: *Self, gpa: Allocator, index: usize, new_key: K, ctx: Context) Oom!void {
13521352
const key_ptr = &self.entries.items(.key)[index];
13531353
key_ptr.* = new_key;
1354-
if (store_hash) self.entries.items(.hash)[index].* = checkedHash(ctx, key_ptr.*);
1354+
if (store_hash) self.entries.items(.hash)[index] = checkedHash(ctx, key_ptr.*);
13551355
try rebuildIndex(self, gpa, undefined);
13561356
}
13571357

@@ -2550,10 +2550,26 @@ test "0 sized key and 0 sized value" {
25502550
try testing.expectEqual(map.get(0), null);
25512551
}
25522552

2553-
test "setKey" {
2553+
test "setKey storehash true" {
25542554
const gpa = std.testing.allocator;
25552555

2556-
var map: AutoArrayHashMapUnmanaged(i32, i32) = .empty;
2556+
var map: ArrayHashMapUnmanaged(i32, i32, AutoContext(i32), true) = .empty;
2557+
defer map.deinit(gpa);
2558+
2559+
try map.put(gpa, 12, 34);
2560+
try map.put(gpa, 56, 78);
2561+
2562+
try map.setKey(gpa, 0, 42);
2563+
try testing.expectEqual(2, map.count());
2564+
try testing.expectEqual(false, map.contains(12));
2565+
try testing.expectEqual(34, map.get(42));
2566+
try testing.expectEqual(78, map.get(56));
2567+
}
2568+
2569+
test "setKey storehash false" {
2570+
const gpa = std.testing.allocator;
2571+
2572+
var map: ArrayHashMapUnmanaged(i32, i32, AutoContext(i32), false) = .empty;
25572573
defer map.deinit(gpa);
25582574

25592575
try map.put(gpa, 12, 34);

0 commit comments

Comments
 (0)