std/hash_map: fix ensureUnusedCapacity() over-allocating

Currently this function adds the desired unused capactiy to the current
total capacity instead of the current used capactiy.
This commit is contained in:
Isaac Freund
2021-07-12 18:32:02 +02:00
committed by Andrew Kelley
parent 3063f0a5ed
commit 03156e5899

View File

@@ -848,7 +848,7 @@ pub fn HashMapUnmanaged(
return ensureUnusedCapacityContext(self, allocator, additional_size, undefined);
}
pub fn ensureUnusedCapacityContext(self: *Self, allocator: *Allocator, additional_size: Size, ctx: Context) !void {
return ensureTotalCapacityContext(self, allocator, self.capacity() + additional_size, ctx);
return ensureTotalCapacityContext(self, allocator, self.count() + additional_size, ctx);
}
pub fn clearRetainingCapacity(self: *Self) void {
@@ -1956,6 +1956,19 @@ test "std.hash_map getOrPutAdapted" {
}
}
test "std.hash_map ensureUnusedCapacity" {
var map = AutoHashMap(u64, u64).init(testing.allocator);
defer map.deinit();
try map.ensureUnusedCapacity(32);
const capacity = map.capacity();
try map.ensureUnusedCapacity(32);
// Repeated ensureUnusedCapacity() calls with no insertions between
// should not change the capacity.
try testing.expectEqual(capacity, map.capacity());
}
test "compile everything" {
std.testing.refAllDecls(AutoHashMap(i32, i32));
std.testing.refAllDecls(StringHashMap([]const u8));