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(
483483 return self.unmanaged.getOrPutValueContext(self.allocator, key, value, self.ctx);
484484 }
485485
486 /// Deprecated: call `ensureUnusedCapacity` or `ensureTotalCapacity`.
487 pub const ensureCapacity = ensureTotalCapacity;
488
486489 /// Increases capacity, guaranteeing that insertions up until the
487490 /// `expected_count` will not cause an allocation, and therefore cannot fail.
488 pub fn ensureCapacity(self: *Self, expected_count: Size) !void {
489 return self.unmanaged.ensureCapacityContext(self.allocator, expected_count, self.ctx);
491 pub fn ensureTotalCapacity(self: *Self, expected_count: Size) !void {
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);
490500 }
491501
492502 /// Returns the number of total elements which may be present before it is
......@@ -821,16 +831,26 @@ pub fn HashMapUnmanaged(
821831 return new_cap;
822832 }
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 {
825838 if (@sizeOf(Context) != 0)
826 @compileError("Cannot infer context " ++ @typeName(Context) ++ ", call ensureCapacityContext instead.");
827 return ensureCapacityContext(self, allocator, new_size, undefined);
839 @compileError("Cannot infer context " ++ @typeName(Context) ++ ", call ensureTotalCapacityContext instead.");
840 return ensureTotalCapacityContext(self, allocator, new_size, undefined);
828841 }
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 {
830843 if (new_size > self.size)
831844 try self.growIfNeeded(allocator, new_size - self.size, ctx);
832845 }
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
834854 pub fn clearRetainingCapacity(self: *Self) void {
835855 if (self.metadata) |_| {
836856 self.initMetadatas();