From bae0c9b554105092297330c8beec977fe3468da6 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 2 Jun 2020 14:40:27 -0400 Subject: [PATCH] std.HashMap: allow ensureCapacity with a zero parameter --- lib/std/hash_map.zig | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/std/hash_map.zig b/lib/std/hash_map.zig index c3ffa94c78..bcd4280153 100644 --- a/lib/std/hash_map.zig +++ b/lib/std/hash_map.zig @@ -169,6 +169,7 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u3 /// Increases capacity so that the hash map will be at most /// 60% full when expected_count items are put into it pub fn ensureCapacity(self: *Self, expected_count: usize) !void { + if (expected_count == 0) return; const optimized_capacity = optimizedCapacity(expected_count); return self.ensureCapacityExact(optimized_capacity); } @@ -473,6 +474,9 @@ test "iterator hash map" { var reset_map = AutoHashMap(i32, i32).init(std.testing.allocator); defer reset_map.deinit(); + // test ensureCapacity with a 0 parameter + try reset_map.ensureCapacity(0); + try reset_map.putNoClobber(0, 11); try reset_map.putNoClobber(1, 22); try reset_map.putNoClobber(2, 33);