authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-05-06 12:50:53-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-05-06 12:50:53-07:00
log426e4c784cee76d3711bb17fb53ef062e3b0f072
tree971893fdae5cb232d2aa1b13ed82c522a78e46cc
parent17067e0e6b6d28623b47b639853abc69a83b620a

std.ArrayHashMap: ensureUnusedCapacity and ensureTotalCapacity

Same as 22015c1b3bfcf816faf10ea0fc152c4686efb363, but for ArrayHashMap.

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

lib/std/array_hash_map.zig+28-4
...@@ -158,10 +158,20 @@ pub fn ArrayHashMap(...@@ -158,10 +158,20 @@ pub fn ArrayHashMap(
158 return self.unmanaged.getOrPutValue(self.allocator, key, value);158 return self.unmanaged.getOrPutValue(self.allocator, key, value);
159 }159 }
160160
161 /// Deprecated: call `ensureUnusedCapacity` or `ensureTotalCapacity`.
162 pub const ensureCapacity = ensureTotalCapacity;
163
161 /// Increases capacity, guaranteeing that insertions up until the164 /// Increases capacity, guaranteeing that insertions up until the
162 /// `expected_count` will not cause an allocation, and therefore cannot fail.165 /// `expected_count` will not cause an allocation, and therefore cannot fail.
163 pub fn ensureCapacity(self: *Self, new_capacity: usize) !void {166 pub fn ensureTotalCapacity(self: *Self, new_capacity: usize) !void {
164 return self.unmanaged.ensureCapacity(self.allocator, new_capacity);167 return self.unmanaged.ensureTotalCapacity(self.allocator, new_capacity);
168 }
169
170 /// Increases capacity, guaranteeing that insertions up until
171 /// `additional_count` **more** items will not cause an allocation, and
172 /// therefore cannot fail.
173 pub fn ensureUnusedCapacity(self: *Self, additional_count: usize) !void {
174 return self.unmanaged.ensureUnusedCapacity(self.allocator, additional_count);
165 }175 }
166176
167 /// Returns the number of total elements which may be present before it is177 /// Returns the number of total elements which may be present before it is
...@@ -472,10 +482,13 @@ pub fn ArrayHashMapUnmanaged(...@@ -472,10 +482,13 @@ pub fn ArrayHashMapUnmanaged(
472 return res.entry;482 return res.entry;
473 }483 }
474484
485 /// Deprecated: call `ensureUnusedCapacity` or `ensureTotalCapacity`.
486 pub const ensureCapacity = ensureTotalCapacity;
487
475 /// Increases capacity, guaranteeing that insertions up until the488 /// Increases capacity, guaranteeing that insertions up until the
476 /// `expected_count` will not cause an allocation, and therefore cannot fail.489 /// `expected_count` will not cause an allocation, and therefore cannot fail.
477 pub fn ensureCapacity(self: *Self, allocator: *Allocator, new_capacity: usize) !void {490 pub fn ensureTotalCapacity(self: *Self, allocator: *Allocator, new_capacity: usize) !void {
478 try self.entries.ensureCapacity(allocator, new_capacity);491 try self.entries.ensureTotalCapacity(allocator, new_capacity);
479 if (new_capacity <= linear_scan_max) return;492 if (new_capacity <= linear_scan_max) return;
480493
481 // Ensure that the indexes will be at most 60% full if494 // Ensure that the indexes will be at most 60% full if
...@@ -501,6 +514,17 @@ pub fn ArrayHashMapUnmanaged(...@@ -501,6 +514,17 @@ pub fn ArrayHashMapUnmanaged(
501 }514 }
502 }515 }
503516
517 /// Increases capacity, guaranteeing that insertions up until
518 /// `additional_count` **more** items will not cause an allocation, and
519 /// therefore cannot fail.
520 pub fn ensureUnusedCapacity(
521 self: *Self,
522 allocator: *Allocator,
523 additional_capacity: usize,
524 ) !void {
525 return self.ensureTotalCapacity(allocator, self.count() + additional_capacity);
526 }
527
504 /// Returns the number of total elements which may be present before it is528 /// Returns the number of total elements which may be present before it is
505 /// no longer guaranteed that no allocations will be performed.529 /// no longer guaranteed that no allocations will be performed.
506 pub fn capacity(self: Self) usize {530 pub fn capacity(self: Self) usize {