authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-07-07 00:38:10-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-07-07 00:38:10-07:00
log298a65ff4b4efc46c877309be22550f023f49758
tree592894e1e0af7356b50ae6ab68e337f3915b1849
parent6ba843ee0fee75490655544bb9442f20e271cc2c

std.HashMap: add ensureUnusedCapacity and ensureTotalCapacity

and deprecated ensureCapacity. This matches the pattern set by ArrayList and ArrayHashMap already.

1 files changed, 26 insertions(+), 6 deletions(-)

lib/std/hash_map.zig+26-6
...@@ -483,10 +483,20 @@ pub fn HashMap(...@@ -483,10 +483,20 @@ pub fn HashMap(
483 return self.unmanaged.getOrPutValueContext(self.allocator, key, value, self.ctx);483 return self.unmanaged.getOrPutValueContext(self.allocator, key, value, self.ctx);
484 }484 }
485485
486 /// Deprecated: call `ensureUnusedCapacity` or `ensureTotalCapacity`.
487 pub const ensureCapacity = ensureTotalCapacity;
488
486 /// Increases capacity, guaranteeing that insertions up until the489 /// Increases capacity, guaranteeing that insertions up until the
487 /// `expected_count` will not cause an allocation, and therefore cannot fail.490 /// `expected_count` will not cause an allocation, and therefore cannot fail.
488 pub fn ensureCapacity(self: *Self, expected_count: Size) !void {491 pub fn ensureTotalCapacity(self: *Self, expected_count: Size) !void {
489 return self.unmanaged.ensureCapacityContext(self.allocator, expected_count, self.ctx);492 return self.unmanaged.ensureTotalCapacityContext(self.allocator, expected_count, self.ctx);
493 }
494
495 /// Increases capacity, guaranteeing that insertions up until
496 /// `additional_count` **more** items will not cause an allocation, and
497 /// therefore cannot fail.
498 pub fn ensureUnusedCapacity(self: *Self, additional_count: Size) !void {
499 return self.unmanaged.ensureUnusedCapacityContext(self.allocator, additional_count, self.ctx);
490 }500 }
491501
492 /// Returns the number of total elements which may be present before it is502 /// Returns the number of total elements which may be present before it is
...@@ -821,16 +831,26 @@ pub fn HashMapUnmanaged(...@@ -821,16 +831,26 @@ pub fn HashMapUnmanaged(
821 return new_cap;831 return new_cap;
822 }832 }
823833
824 pub fn ensureCapacity(self: *Self, allocator: *Allocator, new_size: Size) !void {834 /// Deprecated: call `ensureUnusedCapacity` or `ensureTotalCapacity`.
835 pub const ensureCapacity = ensureTotalCapacity;
836
837 pub fn ensureTotalCapacity(self: *Self, allocator: *Allocator, new_size: Size) !void {
825 if (@sizeOf(Context) != 0)838 if (@sizeOf(Context) != 0)
826 @compileError("Cannot infer context " ++ @typeName(Context) ++ ", call ensureCapacityContext instead.");839 @compileError("Cannot infer context " ++ @typeName(Context) ++ ", call ensureTotalCapacityContext instead.");
827 return ensureCapacityContext(self, allocator, new_size, undefined);840 return ensureTotalCapacityContext(self, allocator, new_size, undefined);
828 }841 }
829 pub fn ensureCapacityContext(self: *Self, allocator: *Allocator, new_size: Size, ctx: Context) !void {842 pub fn ensureTotalCapacityContext(self: *Self, allocator: *Allocator, new_size: Size, ctx: Context) !void {
830 if (new_size > self.size)843 if (new_size > self.size)
831 try self.growIfNeeded(allocator, new_size - self.size, ctx);844 try self.growIfNeeded(allocator, new_size - self.size, ctx);
832 }845 }
833846
847 pub fn ensureUnusedCapacity(self: *Self, allocator: *Allocator, additional_size: Size) !void {
848 return ensureUnusedCapacityContext(self, allocator, additional_size, undefined);
849 }
850 pub fn ensureUnusedCapacityContext(self: *Self, allocator: *Allocator, additional_size: Size, ctx: Context) !void {
851 return ensureTotalCapacityContext(self, allocator, self.capacity() + additional_size, ctx);
852 }
853
834 pub fn clearRetainingCapacity(self: *Self) void {854 pub fn clearRetainingCapacity(self: *Self) void {
835 if (self.metadata) |_| {855 if (self.metadata) |_| {
836 self.initMetadatas();856 self.initMetadatas();