| ... | @@ -43,13 +43,13 @@ pub fn PriorityDequeue(comptime T: type) type { | ... | @@ -43,13 +43,13 @@ pub fn PriorityDequeue(comptime T: type) type { |
| 43 | | 43 | |
| 44 | /// Insert a new element, maintaining priority. | 44 | /// Insert a new element, maintaining priority. |
| 45 | pub fn add(self: *Self, elem: T) !void { | 45 | pub fn add(self: *Self, elem: T) !void { |
| 46 | try ensureCapacity(self, self.len + 1); | 46 | try self.ensureUnusedCapacity(1); |
| 47 | addUnchecked(self, elem); | 47 | addUnchecked(self, elem); |
| 48 | } | 48 | } |
| 49 | | 49 | |
| 50 | /// Add each element in `items` to the dequeue. | 50 | /// Add each element in `items` to the dequeue. |
| 51 | pub fn addSlice(self: *Self, items: []const T) !void { | 51 | pub fn addSlice(self: *Self, items: []const T) !void { |
| 52 | try self.ensureCapacity(self.len + items.len); | 52 | try self.ensureUnusedCapacity(items.len); |
| 53 | for (items) |e| { | 53 | for (items) |e| { |
| 54 | self.addUnchecked(e); | 54 | self.addUnchecked(e); |
| 55 | } | 55 | } |
| ... | @@ -359,7 +359,11 @@ pub fn PriorityDequeue(comptime T: type) type { | ... | @@ -359,7 +359,11 @@ pub fn PriorityDequeue(comptime T: type) type { |
| 359 | return queue; | 359 | return queue; |
| 360 | } | 360 | } |
| 361 | | 361 | |
| 362 | pub fn ensureCapacity(self: *Self, new_capacity: usize) !void { | 362 | /// Deprecated: call `ensureUnusedCapacity` or `ensureTotalCapacity`. |
| | 363 | pub const ensureCapacity = ensureTotalCapacity; |
| | 364 | |
| | 365 | /// Ensure that the dequeue can fit at least `new_capacity` items. |
| | 366 | pub fn ensureTotalCapacity(self: *Self, new_capacity: usize) !void { |
| 363 | var better_capacity = self.capacity(); | 367 | var better_capacity = self.capacity(); |
| 364 | if (better_capacity >= new_capacity) return; | 368 | if (better_capacity >= new_capacity) return; |
| 365 | while (true) { | 369 | while (true) { |
| ... | @@ -369,6 +373,11 @@ pub fn PriorityDequeue(comptime T: type) type { | ... | @@ -369,6 +373,11 @@ pub fn PriorityDequeue(comptime T: type) type { |
| 369 | self.items = try self.allocator.realloc(self.items, better_capacity); | 373 | self.items = try self.allocator.realloc(self.items, better_capacity); |
| 370 | } | 374 | } |
| 371 | | 375 | |
| | 376 | /// Ensure that the dequeue can fit at least `additional_count` **more** items. |
| | 377 | pub fn ensureUnusedCapacity(self: *Self, additional_count: usize) !void { |
| | 378 | return self.ensureTotalCapacity(self.len + additional_count); |
| | 379 | } |
| | 380 | |
| 372 | /// Reduce allocated capacity to `new_len`. | 381 | /// Reduce allocated capacity to `new_len`. |
| 373 | pub fn shrinkAndFree(self: *Self, new_len: usize) void { | 382 | pub fn shrinkAndFree(self: *Self, new_len: usize) void { |
| 374 | assert(new_len <= self.items.len); | 383 | assert(new_len <= self.items.len); |
| ... | @@ -824,7 +833,7 @@ test "std.PriorityDequeue: shrinkAndFree" { | ... | @@ -824,7 +833,7 @@ test "std.PriorityDequeue: shrinkAndFree" { |
| 824 | var queue = PDQ.init(testing.allocator, lessThanComparison); | 833 | var queue = PDQ.init(testing.allocator, lessThanComparison); |
| 825 | defer queue.deinit(); | 834 | defer queue.deinit(); |
| 826 | | 835 | |
| 827 | try queue.ensureCapacity(4); | 836 | try queue.ensureTotalCapacity(4); |
| 828 | try expect(queue.capacity() >= 4); | 837 | try expect(queue.capacity() >= 4); |
| 829 | | 838 | |
| 830 | try queue.add(1); | 839 | try queue.add(1); |
| ... | @@ -940,7 +949,7 @@ fn fuzzTestMinMax(rng: *std.rand.Random, queue_size: usize) !void { | ... | @@ -940,7 +949,7 @@ fn fuzzTestMinMax(rng: *std.rand.Random, queue_size: usize) !void { |
| 940 | | 949 | |
| 941 | fn generateRandomSlice(allocator: *std.mem.Allocator, rng: *std.rand.Random, size: usize) ![]u32 { | 950 | fn generateRandomSlice(allocator: *std.mem.Allocator, rng: *std.rand.Random, size: usize) ![]u32 { |
| 942 | var array = std.ArrayList(u32).init(allocator); | 951 | var array = std.ArrayList(u32).init(allocator); |
| 943 | try array.ensureCapacity(size); | 952 | try array.ensureTotalCapacity(size); |
| 944 | | 953 | |
| 945 | var i: usize = 0; | 954 | var i: usize = 0; |
| 946 | while (i < size) : (i += 1) { | 955 | while (i < size) : (i += 1) { |