Remains the question: how to garantuee zero’s for the part after “len”?
fields are always public so you can zero the buffer yourself @memSet
if you weren’t aware of it before :3
2 Likes
var suffix : std.BoundedArray(u5, 32) = .{
.buffer = [_]u5{0} ** 32,
.len = 0,
};
The 3 extra bits in the u5
will still be set to undefined
, but autoHash
will correctly avoid those bits (I assume autoEql
will also).
Note that it’s still not a full solution to the problem, since e.g. this test will still fail:
test "bounded array auto hash map" {
var map: std.AutoHashMap(std.BoundedArray(u5, 32), void) = .init(std.testing.allocator);
defer map.deinit();
var arr: std.BoundedArray(u5, 32) = .{
.buffer = [_]u5{ 1, 2, 3 } ++ [_]u5{0} ** 29,
.len = 3,
};
try map.putNoClobber(arr, {});
try arr.append(4);
// ...
_ = arr.pop();
try std.testing.expect(map.contains(arr));
}
The only way to make this work in all cases would be to use a custom BoundedArray implementation that sets all elements outside the len
to 0
after every modification to the list, but that’s just pointless extra work to enable using an autoHash
function that isn’t suited to this use case.
1 Like