authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-06-02 14:40:27-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-06-02 14:41:45-04:00
logbae0c9b554105092297330c8beec977fe3468da6
tree22d41ba905804f606bfdb3b11769957aa13485aa
parent40a1cfed538c0a19993c3f5497d1ef18d46dac2b

std.HashMap: allow ensureCapacity with a zero parameter


1 files changed, 4 insertions(+), 0 deletions(-)

lib/std/hash_map.zig+4
...@@ -169,6 +169,7 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u3...@@ -169,6 +169,7 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u3
169 /// Increases capacity so that the hash map will be at most169 /// Increases capacity so that the hash map will be at most
170 /// 60% full when expected_count items are put into it170 /// 60% full when expected_count items are put into it
171 pub fn ensureCapacity(self: *Self, expected_count: usize) !void {171 pub fn ensureCapacity(self: *Self, expected_count: usize) !void {
172 if (expected_count == 0) return;
172 const optimized_capacity = optimizedCapacity(expected_count);173 const optimized_capacity = optimizedCapacity(expected_count);
173 return self.ensureCapacityExact(optimized_capacity);174 return self.ensureCapacityExact(optimized_capacity);
174 }175 }
...@@ -473,6 +474,9 @@ test "iterator hash map" {...@@ -473,6 +474,9 @@ test "iterator hash map" {
473 var reset_map = AutoHashMap(i32, i32).init(std.testing.allocator);474 var reset_map = AutoHashMap(i32, i32).init(std.testing.allocator);
474 defer reset_map.deinit();475 defer reset_map.deinit();
475476
477 // test ensureCapacity with a 0 parameter
478 try reset_map.ensureCapacity(0);
479
476 try reset_map.putNoClobber(0, 11);480 try reset_map.putNoClobber(0, 11);
477 try reset_map.putNoClobber(1, 22);481 try reset_map.putNoClobber(1, 22);
478 try reset_map.putNoClobber(2, 33);482 try reset_map.putNoClobber(2, 33);