aboutsummaryrefslogtreecommitdiff
path: root/lib/std/priority_dequeue.zig
diff options
context:
space:
mode:
authorRyan Liptak <squeek502@hotmail.com>2021-09-16 17:21:19 -0700
committerIsaac Freund <ifreund@ifreund.xyz>2021-09-19 13:52:56 +0200
commitfeeb25908bebd5d09cf05128fad7d7c1a8a803a1 (patch)
tree030e1e9a32c10f7c0428b6c208e5c1446fc2cf28 /lib/std/priority_dequeue.zig
parent2be3b1d2bfd4bab61a481226fd1d714512ea2f44 (diff)
downloadzig-feeb25908bebd5d09cf05128fad7d7c1a8a803a1.tar.gz
zig-feeb25908bebd5d09cf05128fad7d7c1a8a803a1.zip
std.PriorityDequeue: ensureUnusedCapacity and ensureTotalCapacity
Same as c8ae581fef6506a8234cdba1355ba7f0f449031a, but for PriorityDequeue.
Diffstat (limited to 'lib/std/priority_dequeue.zig')
-rw-r--r--lib/std/priority_dequeue.zig19
1 files changed, 14 insertions, 5 deletions
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) {