Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix ArrayHashMap setKey when store_hash=true #22968

Merged
merged 1 commit into from
Feb 22, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions lib/std/array_hash_map.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1351,7 +1351,7 @@ pub fn ArrayHashMapUnmanaged(
pub fn setKeyContext(self: *Self, gpa: Allocator, index: usize, new_key: K, ctx: Context) Oom!void {
const key_ptr = &self.entries.items(.key)[index];
key_ptr.* = new_key;
if (store_hash) self.entries.items(.hash)[index].* = checkedHash(ctx, key_ptr.*);
if (store_hash) self.entries.items(.hash)[index] = checkedHash(ctx, key_ptr.*);
try rebuildIndex(self, gpa, undefined);
}

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

test "setKey" {
test "setKey storehash true" {
const gpa = std.testing.allocator;

var map: AutoArrayHashMapUnmanaged(i32, i32) = .empty;
var map: ArrayHashMapUnmanaged(i32, i32, AutoContext(i32), true) = .empty;
defer map.deinit(gpa);

try map.put(gpa, 12, 34);
try map.put(gpa, 56, 78);

try map.setKey(gpa, 0, 42);
try testing.expectEqual(2, map.count());
try testing.expectEqual(false, map.contains(12));
try testing.expectEqual(34, map.get(42));
try testing.expectEqual(78, map.get(56));
}

test "setKey storehash false" {
const gpa = std.testing.allocator;

var map: ArrayHashMapUnmanaged(i32, i32, AutoContext(i32), false) = .empty;
defer map.deinit(gpa);

try map.put(gpa, 12, 34);
Expand Down
Loading