diff --git a/lib/std/priority_dequeue.zig b/lib/std/priority_dequeue.zig index d154f5df5e..5bde0a36d0 100644 --- a/lib/std/priority_dequeue.zig +++ b/lib/std/priority_dequeue.zig @@ -43,13 +43,13 @@ pub fn PriorityDequeue(comptime T: type) type { /// Insert a new element, maintaining priority. pub fn add(self: *Self, elem: T) !void { - try ensureCapacity(self, self.len + 1); + try self.ensureUnusedCapacity(1); addUnchecked(self, elem); } /// Add each element in `items` to the dequeue. pub fn addSlice(self: *Self, items: []const T) !void { - try self.ensureCapacity(self.len + items.len); + try self.ensureUnusedCapacity(items.len); for (items) |e| { self.addUnchecked(e); } @@ -359,7 +359,11 @@ pub fn PriorityDequeue(comptime T: type) type { return queue; } - pub fn ensureCapacity(self: *Self, new_capacity: usize) !void { + /// Deprecated: call `ensureUnusedCapacity` or `ensureTotalCapacity`. + pub const ensureCapacity = ensureTotalCapacity; + + /// Ensure that the dequeue can fit at least `new_capacity` items. + pub fn ensureTotalCapacity(self: *Self, new_capacity: usize) !void { var better_capacity = self.capacity(); if (better_capacity >= new_capacity) return; while (true) { @@ -369,6 +373,11 @@ pub fn PriorityDequeue(comptime T: type) type { self.items = try self.allocator.realloc(self.items, better_capacity); } + /// Ensure that the dequeue can fit at least `additional_count` **more** items. + pub fn ensureUnusedCapacity(self: *Self, additional_count: usize) !void { + return self.ensureTotalCapacity(self.len + additional_count); + } + /// Reduce allocated capacity to `new_len`. pub fn shrinkAndFree(self: *Self, new_len: usize) void { assert(new_len <= self.items.len); @@ -824,7 +833,7 @@ test "std.PriorityDequeue: shrinkAndFree" { var queue = PDQ.init(testing.allocator, lessThanComparison); defer queue.deinit(); - try queue.ensureCapacity(4); + try queue.ensureTotalCapacity(4); try expect(queue.capacity() >= 4); try queue.add(1); @@ -940,7 +949,7 @@ fn fuzzTestMinMax(rng: *std.rand.Random, queue_size: usize) !void { fn generateRandomSlice(allocator: *std.mem.Allocator, rng: *std.rand.Random, size: usize) ![]u32 { var array = std.ArrayList(u32).init(allocator); - try array.ensureCapacity(size); + try array.ensureTotalCapacity(size); var i: usize = 0; while (i < size) : (i += 1) {