authorgravatar for saurabh.m@proton.meSaurabh Mishra <saurabh.m@proton.me> 2026-04-03 22:20:46+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-04-03 22:20:46+02:00
log65fe99e18a89e92b6b82f798411e1e13e21df511
tree402433276dec0c35f2cbd75a774b99018c4eee3c
parent842b74d7ea3d4c5b4780f52c6dc8bce21d679fe1

priority queue and dequeue: use `*const Self` in read-only methods and fix `deinit` (#31712)

Read-only methods `peek` in priority queue, and `peekMin` and `peekMax` in priority dequeue use `self: *const Self` instead of `self: *Self`. Reviewed-on: https://codeberg.org/ziglang/zig/pulls/31712 Reviewed-by: Andrew Kelley <andrew@ziglang.org> Co-authored-by: Saurabh Mishra <saurabh.m@proton.me> Co-committed-by: Saurabh Mishra <saurabh.m@proton.me>

2 files changed, 18 insertions(+), 16 deletions(-)

lib/std/priority_dequeue.zig+13-12
......@@ -40,8 +40,9 @@ pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compar
4040 }
4141
4242 /// Free memory used by the dequeue.
43 pub fn deinit(self: Self, allocator: Allocator) void {
43 pub fn deinit(self: *Self, allocator: Allocator) void {
4444 allocator.free(self.items);
45 self.* = undefined;
4546 }
4647
4748 /// Insert a new element, maintaining priority.
......@@ -77,7 +78,7 @@ pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compar
7778 return 1 == @clz(index +% 1) & 1;
7879 }
7980
80 fn nextIsMinLayer(self: Self) bool {
81 fn nextIsMinLayer(self: *const Self) bool {
8182 return isMinLayer(self.len);
8283 }
8384
......@@ -86,7 +87,7 @@ pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compar
8687 min_layer: bool,
8788 };
8889
89 fn getStartForSiftUp(self: Self, child: T, index: usize) StartIndexAndLayer {
90 fn getStartForSiftUp(self: *const Self, child: T, index: usize) StartIndexAndLayer {
9091 const child_index = index;
9192 const parent_index = parentIndex(child_index);
9293 const parent = self.items[parent_index];
......@@ -136,20 +137,20 @@ pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compar
136137
137138 /// Look at the smallest element in the dequeue. Returns
138139 /// `null` if empty.
139 pub fn peekMin(self: *Self) ?T {
140 pub fn peekMin(self: *const Self) ?T {
140141 return if (self.len > 0) self.items[0] else null;
141142 }
142143
143144 /// Look at the largest element in the dequeue. Returns
144145 /// `null` if empty.
145 pub fn peekMax(self: *Self) ?T {
146 pub fn peekMax(self: *const Self) ?T {
146147 if (self.len == 0) return null;
147148 if (self.len == 1) return self.items[0];
148149 if (self.len == 2) return self.items[1];
149150 return self.bestItemAtIndices(1, 2, .gt).item;
150151 }
151152
152 fn maxIndex(self: Self) ?usize {
153 fn maxIndex(self: *const Self) ?usize {
153154 if (self.len == 0) return null;
154155 if (self.len == 1) return 0;
155156 if (self.len == 2) return 1;
......@@ -261,14 +262,14 @@ pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compar
261262 index: usize,
262263 };
263264
264 fn getItem(self: Self, index: usize) ItemAndIndex {
265 fn getItem(self: *const Self, index: usize) ItemAndIndex {
265266 return .{
266267 .item = self.items[index],
267268 .index = index,
268269 };
269270 }
270271
271 fn bestItem(self: Self, item1: ItemAndIndex, item2: ItemAndIndex, target_order: Order) ItemAndIndex {
272 fn bestItem(self: *const Self, item1: ItemAndIndex, item2: ItemAndIndex, target_order: Order) ItemAndIndex {
272273 if (compareFn(self.context, item1.item, item2.item) == target_order) {
273274 return item1;
274275 } else {
......@@ -276,13 +277,13 @@ pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compar
276277 }
277278 }
278279
279 fn bestItemAtIndices(self: Self, index1: usize, index2: usize, target_order: Order) ItemAndIndex {
280 fn bestItemAtIndices(self: *const Self, index1: usize, index2: usize, target_order: Order) ItemAndIndex {
280281 const item1 = self.getItem(index1);
281282 const item2 = self.getItem(index2);
282283 return self.bestItem(item1, item2, target_order);
283284 }
284285
285 fn bestDescendent(self: Self, first_child_index: usize, first_grandchild_index: usize, target_order: Order) ItemAndIndex {
286 fn bestDescendent(self: *const Self, first_child_index: usize, first_grandchild_index: usize, target_order: Order) ItemAndIndex {
286287 const second_child_index = first_child_index + 1;
287288 if (first_grandchild_index >= self.len) {
288289 // No grandchildren, find the best child (second may not exist)
......@@ -314,13 +315,13 @@ pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compar
314315 }
315316
316317 /// Return the number of elements remaining in the dequeue
317 pub fn count(self: Self) usize {
318 pub fn count(self: *const Self) usize {
318319 return self.len;
319320 }
320321
321322 /// Return the number of elements that can be added to the
322323 /// dequeue before more memory is allocated.
323 pub fn capacity(self: Self) usize {
324 pub fn capacity(self: *const Self) usize {
324325 return self.items.len;
325326 }
326327
lib/std/priority_queue.zig+5-4
......@@ -41,6 +41,7 @@ pub fn PriorityQueue(comptime T: type, comptime Context: type, comptime compareF
4141 /// Free memory used by the queue.
4242 pub fn deinit(self: *Self, allocator: Allocator) void {
4343 allocator.free(self.allocatedSlice());
44 self.* = undefined;
4445 }
4546
4647 /// Insert a new element, maintaining priority.
......@@ -78,7 +79,7 @@ pub fn PriorityQueue(comptime T: type, comptime Context: type, comptime compareF
7879
7980 /// Look at the highest priority element in the queue. Returns
8081 /// `null` if empty.
81 pub fn peek(self: *Self) ?T {
82 pub fn peek(self: *const Self) ?T {
8283 return if (self.items.len > 0) self.items[0] else null;
8384 }
8485
......@@ -117,19 +118,19 @@ pub fn PriorityQueue(comptime T: type, comptime Context: type, comptime compareF
117118
118119 /// Return the number of elements remaining in the priority
119120 /// queue.
120 pub fn count(self: Self) usize {
121 pub fn count(self: *const Self) usize {
121122 return self.items.len;
122123 }
123124
124125 /// Return the number of elements that can be added to the
125126 /// queue before more memory is allocated.
126 pub fn capacity(self: Self) usize {
127 pub fn capacity(self: *const Self) usize {
127128 return self.cap;
128129 }
129130
130131 /// Returns a slice of all the items plus the extra capacity, whose memory
131132 /// contents are `undefined`.
132 fn allocatedSlice(self: Self) []T {
133 fn allocatedSlice(self: *const Self) []T {
133134 // `items.len` is the length, not the capacity.
134135 return self.items.ptr[0..self.cap];
135136 }