diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2021-05-06 12:50:53 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2021-05-06 12:50:53 -0700 |
| commit | 426e4c784cee76d3711bb17fb53ef062e3b0f072 (patch) | |
| tree | 971893fdae5cb232d2aa1b13ed82c522a78e46cc /lib/std/array_hash_map.zig | |
| parent | 17067e0e6b6d28623b47b639853abc69a83b620a (diff) | |
| download | zig-426e4c784cee76d3711bb17fb53ef062e3b0f072.tar.gz zig-426e4c784cee76d3711bb17fb53ef062e3b0f072.zip | |
std.ArrayHashMap: ensureUnusedCapacity and ensureTotalCapacity
Same as 22015c1b3bfcf816faf10ea0fc152c4686efb363, but for ArrayHashMap.
Diffstat (limited to 'lib/std/array_hash_map.zig')
| -rw-r--r-- | lib/std/array_hash_map.zig | 32 |
1 files changed, 28 insertions, 4 deletions
diff --git a/lib/std/array_hash_map.zig b/lib/std/array_hash_map.zig index 1dc4110c75..80493bb2a7 100644 --- a/lib/std/array_hash_map.zig +++ b/lib/std/array_hash_map.zig @@ -158,10 +158,20 @@ pub fn ArrayHashMap( return self.unmanaged.getOrPutValue(self.allocator, key, value); } + /// Deprecated: call `ensureUnusedCapacity` or `ensureTotalCapacity`. + pub const ensureCapacity = ensureTotalCapacity; + /// Increases capacity, guaranteeing that insertions up until the /// `expected_count` will not cause an allocation, and therefore cannot fail. - pub fn ensureCapacity(self: *Self, new_capacity: usize) !void { - return self.unmanaged.ensureCapacity(self.allocator, new_capacity); + pub fn ensureTotalCapacity(self: *Self, new_capacity: usize) !void { + return self.unmanaged.ensureTotalCapacity(self.allocator, new_capacity); + } + + /// Increases capacity, guaranteeing that insertions up until + /// `additional_count` **more** items will not cause an allocation, and + /// therefore cannot fail. + pub fn ensureUnusedCapacity(self: *Self, additional_count: usize) !void { + return self.unmanaged.ensureUnusedCapacity(self.allocator, additional_count); } /// Returns the number of total elements which may be present before it is @@ -472,10 +482,13 @@ pub fn ArrayHashMapUnmanaged( return res.entry; } + /// Deprecated: call `ensureUnusedCapacity` or `ensureTotalCapacity`. + pub const ensureCapacity = ensureTotalCapacity; + /// Increases capacity, guaranteeing that insertions up until the /// `expected_count` will not cause an allocation, and therefore cannot fail. - pub fn ensureCapacity(self: *Self, allocator: *Allocator, new_capacity: usize) !void { - try self.entries.ensureCapacity(allocator, new_capacity); + pub fn ensureTotalCapacity(self: *Self, allocator: *Allocator, new_capacity: usize) !void { + try self.entries.ensureTotalCapacity(allocator, new_capacity); if (new_capacity <= linear_scan_max) return; // Ensure that the indexes will be at most 60% full if @@ -501,6 +514,17 @@ pub fn ArrayHashMapUnmanaged( } } + /// Increases capacity, guaranteeing that insertions up until + /// `additional_count` **more** items will not cause an allocation, and + /// therefore cannot fail. + pub fn ensureUnusedCapacity( + self: *Self, + allocator: *Allocator, + additional_capacity: usize, + ) !void { + return self.ensureTotalCapacity(allocator, self.count() + additional_capacity); + } + /// Returns the number of total elements which may be present before it is /// no longer guaranteed that no allocations will be performed. pub fn capacity(self: Self) usize { |
