authorgravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2021-07-12 18:32:02+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-07-12 11:31:36-07:00
log03156e589939993bba339162d27d24fd511601c6
tree3752699ba2a0a2bd10938f9eb4b191b11a72820d
parent3063f0a5ed373947badd0af056db310283c76e37

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.

1 files changed, 14 insertions(+), 1 deletions(-)

lib/std/hash_map.zig+14-1
......@@ -848,7 +848,7 @@ pub fn HashMapUnmanaged(
848848 return ensureUnusedCapacityContext(self, allocator, additional_size, undefined);
849849 }
850850 pub fn ensureUnusedCapacityContext(self: *Self, allocator: *Allocator, additional_size: Size, ctx: Context) !void {
851 return ensureTotalCapacityContext(self, allocator, self.capacity() + additional_size, ctx);
851 return ensureTotalCapacityContext(self, allocator, self.count() + additional_size, ctx);
852852 }
853853
854854 pub fn clearRetainingCapacity(self: *Self) void {
......@@ -1956,6 +1956,19 @@ test "std.hash_map getOrPutAdapted" {
19561956 }
19571957}
19581958
1959test "std.hash_map ensureUnusedCapacity" {
1960 var map = AutoHashMap(u64, u64).init(testing.allocator);
1961 defer map.deinit();
1962
1963 try map.ensureUnusedCapacity(32);
1964 const capacity = map.capacity();
1965 try map.ensureUnusedCapacity(32);
1966
1967 // Repeated ensureUnusedCapacity() calls with no insertions between
1968 // should not change the capacity.
1969 try testing.expectEqual(capacity, map.capacity());
1970}
1971
19591972test "compile everything" {
19601973 std.testing.refAllDecls(AutoHashMap(i32, i32));
19611974 std.testing.refAllDecls(StringHashMap([]const u8));