| ... | @@ -42,7 +42,7 @@ pub fn PriorityQueue(comptime T: type) type { | ... | @@ -42,7 +42,7 @@ pub fn PriorityQueue(comptime T: type) type { |
| 42 | | 42 | |
| 43 | /// Insert a new element, maintaining priority. | 43 | /// Insert a new element, maintaining priority. |
| 44 | pub fn add(self: *Self, elem: T) !void { | 44 | pub fn add(self: *Self, elem: T) !void { |
| 45 | try ensureCapacity(self, self.len + 1); | 45 | try self.ensureUnusedCapacity(1); |
| 46 | addUnchecked(self, elem); | 46 | addUnchecked(self, elem); |
| 47 | } | 47 | } |
| 48 | | 48 | |
| ... | @@ -69,7 +69,7 @@ pub fn PriorityQueue(comptime T: type) type { | ... | @@ -69,7 +69,7 @@ pub fn PriorityQueue(comptime T: type) type { |
| 69 | | 69 | |
| 70 | /// Add each element in `items` to the queue. | 70 | /// Add each element in `items` to the queue. |
| 71 | pub fn addSlice(self: *Self, items: []const T) !void { | 71 | pub fn addSlice(self: *Self, items: []const T) !void { |
| 72 | try self.ensureCapacity(self.len + items.len); | 72 | try self.ensureUnusedCapacity(items.len); |
| 73 | for (items) |e| { | 73 | for (items) |e| { |
| 74 | self.addUnchecked(e); | 74 | self.addUnchecked(e); |
| 75 | } | 75 | } |
| ... | @@ -175,7 +175,11 @@ pub fn PriorityQueue(comptime T: type) type { | ... | @@ -175,7 +175,11 @@ pub fn PriorityQueue(comptime T: type) type { |
| 175 | return queue; | 175 | return queue; |
| 176 | } | 176 | } |
| 177 | | 177 | |
| 178 | pub fn ensureCapacity(self: *Self, new_capacity: usize) !void { | 178 | /// Deprecated: call `ensureUnusedCapacity` or `ensureTotalCapacity`. |
| | 179 | pub const ensureCapacity = ensureTotalCapacity; |
| | 180 | |
| | 181 | /// Ensure that the queue can fit at least `new_capacity` items. |
| | 182 | pub fn ensureTotalCapacity(self: *Self, new_capacity: usize) !void { |
| 179 | var better_capacity = self.capacity(); | 183 | var better_capacity = self.capacity(); |
| 180 | if (better_capacity >= new_capacity) return; | 184 | if (better_capacity >= new_capacity) return; |
| 181 | while (true) { | 185 | while (true) { |
| ... | @@ -185,6 +189,11 @@ pub fn PriorityQueue(comptime T: type) type { | ... | @@ -185,6 +189,11 @@ pub fn PriorityQueue(comptime T: type) type { |
| 185 | self.items = try self.allocator.realloc(self.items, better_capacity); | 189 | self.items = try self.allocator.realloc(self.items, better_capacity); |
| 186 | } | 190 | } |
| 187 | | 191 | |
| | 192 | /// Ensure that the queue can fit at least `additional_count` **more** item. |
| | 193 | pub fn ensureUnusedCapacity(self: *Self, additional_count: usize) !void { |
| | 194 | return self.ensureTotalCapacity(self.len + additional_count); |
| | 195 | } |
| | 196 | |
| 188 | /// Reduce allocated capacity to `new_len`. | 197 | /// Reduce allocated capacity to `new_len`. |
| 189 | pub fn shrinkAndFree(self: *Self, new_len: usize) void { | 198 | pub fn shrinkAndFree(self: *Self, new_len: usize) void { |
| 190 | assert(new_len <= self.items.len); | 199 | assert(new_len <= self.items.len); |
| ... | @@ -483,7 +492,7 @@ test "std.PriorityQueue: shrinkAndFree" { | ... | @@ -483,7 +492,7 @@ test "std.PriorityQueue: shrinkAndFree" { |
| 483 | var queue = PQ.init(testing.allocator, lessThan); | 492 | var queue = PQ.init(testing.allocator, lessThan); |
| 484 | defer queue.deinit(); | 493 | defer queue.deinit(); |
| 485 | | 494 | |
| 486 | try queue.ensureCapacity(4); | 495 | try queue.ensureTotalCapacity(4); |
| 487 | try expect(queue.capacity() >= 4); | 496 | try expect(queue.capacity() >= 4); |
| 488 | | 497 | |
| 489 | try queue.add(1); | 498 | try queue.add(1); |