Commit 426e4c784c

Andrew Kelley <andrew@ziglang.org>
2021-05-06 21:50:53
std.ArrayHashMap: ensureUnusedCapacity and ensureTotalCapacity
Same as 22015c1b3bfcf816faf10ea0fc152c4686efb363, but for ArrayHashMap.
1 parent 17067e0
Changed files (1)
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 {