| ... | @@ -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 most | 169 | /// Increases capacity so that the hash map will be at most |
| 170 | /// 60% full when expected_count items are put into it | 170 | /// 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(); |
| 475 | | 476 | |
| | 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); |