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...@@ -40,8 +40,9 @@ pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compar
40 }40 }
4141
42 /// Free memory used by the dequeue.42 /// Free memory used by the dequeue.
43 pub fn deinit(self: Self, allocator: Allocator) void {43 pub fn deinit(self: *Self, allocator: Allocator) void {
44 allocator.free(self.items);44 allocator.free(self.items);
45 self.* = undefined;
45 }46 }
4647
47 /// Insert a new element, maintaining priority.48 /// Insert a new element, maintaining priority.
...@@ -77,7 +78,7 @@ pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compar...@@ -77,7 +78,7 @@ pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compar
77 return 1 == @clz(index +% 1) & 1;78 return 1 == @clz(index +% 1) & 1;
78 }79 }
7980
80 fn nextIsMinLayer(self: Self) bool {81 fn nextIsMinLayer(self: *const Self) bool {
81 return isMinLayer(self.len);82 return isMinLayer(self.len);
82 }83 }
8384
...@@ -86,7 +87,7 @@ pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compar...@@ -86,7 +87,7 @@ pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compar
86 min_layer: bool,87 min_layer: bool,
87 };88 };
8889
89 fn getStartForSiftUp(self: Self, child: T, index: usize) StartIndexAndLayer {90 fn getStartForSiftUp(self: *const Self, child: T, index: usize) StartIndexAndLayer {
90 const child_index = index;91 const child_index = index;
91 const parent_index = parentIndex(child_index);92 const parent_index = parentIndex(child_index);
92 const parent = self.items[parent_index];93 const parent = self.items[parent_index];
...@@ -136,20 +137,20 @@ pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compar...@@ -136,20 +137,20 @@ pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compar
136137
137 /// Look at the smallest element in the dequeue. Returns138 /// Look at the smallest element in the dequeue. Returns
138 /// `null` if empty.139 /// `null` if empty.
139 pub fn peekMin(self: *Self) ?T {140 pub fn peekMin(self: *const Self) ?T {
140 return if (self.len > 0) self.items[0] else null;141 return if (self.len > 0) self.items[0] else null;
141 }142 }
142143
143 /// Look at the largest element in the dequeue. Returns144 /// Look at the largest element in the dequeue. Returns
144 /// `null` if empty.145 /// `null` if empty.
145 pub fn peekMax(self: *Self) ?T {146 pub fn peekMax(self: *const Self) ?T {
146 if (self.len == 0) return null;147 if (self.len == 0) return null;
147 if (self.len == 1) return self.items[0];148 if (self.len == 1) return self.items[0];
148 if (self.len == 2) return self.items[1];149 if (self.len == 2) return self.items[1];
149 return self.bestItemAtIndices(1, 2, .gt).item;150 return self.bestItemAtIndices(1, 2, .gt).item;
150 }151 }
151152
152 fn maxIndex(self: Self) ?usize {153 fn maxIndex(self: *const Self) ?usize {
153 if (self.len == 0) return null;154 if (self.len == 0) return null;
154 if (self.len == 1) return 0;155 if (self.len == 1) return 0;
155 if (self.len == 2) return 1;156 if (self.len == 2) return 1;
...@@ -261,14 +262,14 @@ pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compar...@@ -261,14 +262,14 @@ pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compar
261 index: usize,262 index: usize,
262 };263 };
263264
264 fn getItem(self: Self, index: usize) ItemAndIndex {265 fn getItem(self: *const Self, index: usize) ItemAndIndex {
265 return .{266 return .{
266 .item = self.items[index],267 .item = self.items[index],
267 .index = index,268 .index = index,
268 };269 };
269 }270 }
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 {
272 if (compareFn(self.context, item1.item, item2.item) == target_order) {273 if (compareFn(self.context, item1.item, item2.item) == target_order) {
273 return item1;274 return item1;
274 } else {275 } else {
...@@ -276,13 +277,13 @@ pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compar...@@ -276,13 +277,13 @@ pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compar
276 }277 }
277 }278 }
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 {
280 const item1 = self.getItem(index1);281 const item1 = self.getItem(index1);
281 const item2 = self.getItem(index2);282 const item2 = self.getItem(index2);
282 return self.bestItem(item1, item2, target_order);283 return self.bestItem(item1, item2, target_order);
283 }284 }
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 {
286 const second_child_index = first_child_index + 1;287 const second_child_index = first_child_index + 1;
287 if (first_grandchild_index >= self.len) {288 if (first_grandchild_index >= self.len) {
288 // No grandchildren, find the best child (second may not exist)289 // 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...@@ -314,13 +315,13 @@ pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compar
314 }315 }
315316
316 /// Return the number of elements remaining in the dequeue317 /// Return the number of elements remaining in the dequeue
317 pub fn count(self: Self) usize {318 pub fn count(self: *const Self) usize {
318 return self.len;319 return self.len;
319 }320 }
320321
321 /// Return the number of elements that can be added to the322 /// Return the number of elements that can be added to the
322 /// dequeue before more memory is allocated.323 /// dequeue before more memory is allocated.
323 pub fn capacity(self: Self) usize {324 pub fn capacity(self: *const Self) usize {
324 return self.items.len;325 return self.items.len;
325 }326 }
326327
lib/std/priority_queue.zig+5-4
...@@ -41,6 +41,7 @@ pub fn PriorityQueue(comptime T: type, comptime Context: type, comptime compareF...@@ -41,6 +41,7 @@ pub fn PriorityQueue(comptime T: type, comptime Context: type, comptime compareF
41 /// Free memory used by the queue.41 /// Free memory used by the queue.
42 pub fn deinit(self: *Self, allocator: Allocator) void {42 pub fn deinit(self: *Self, allocator: Allocator) void {
43 allocator.free(self.allocatedSlice());43 allocator.free(self.allocatedSlice());
44 self.* = undefined;
44 }45 }
4546
46 /// Insert a new element, maintaining priority.47 /// Insert a new element, maintaining priority.
...@@ -78,7 +79,7 @@ pub fn PriorityQueue(comptime T: type, comptime Context: type, comptime compareF...@@ -78,7 +79,7 @@ pub fn PriorityQueue(comptime T: type, comptime Context: type, comptime compareF
7879
79 /// Look at the highest priority element in the queue. Returns80 /// Look at the highest priority element in the queue. Returns
80 /// `null` if empty.81 /// `null` if empty.
81 pub fn peek(self: *Self) ?T {82 pub fn peek(self: *const Self) ?T {
82 return if (self.items.len > 0) self.items[0] else null;83 return if (self.items.len > 0) self.items[0] else null;
83 }84 }
8485
...@@ -117,19 +118,19 @@ pub fn PriorityQueue(comptime T: type, comptime Context: type, comptime compareF...@@ -117,19 +118,19 @@ pub fn PriorityQueue(comptime T: type, comptime Context: type, comptime compareF
117118
118 /// Return the number of elements remaining in the priority119 /// Return the number of elements remaining in the priority
119 /// queue.120 /// queue.
120 pub fn count(self: Self) usize {121 pub fn count(self: *const Self) usize {
121 return self.items.len;122 return self.items.len;
122 }123 }
123124
124 /// Return the number of elements that can be added to the125 /// Return the number of elements that can be added to the
125 /// queue before more memory is allocated.126 /// queue before more memory is allocated.
126 pub fn capacity(self: Self) usize {127 pub fn capacity(self: *const Self) usize {
127 return self.cap;128 return self.cap;
128 }129 }
129130
130 /// Returns a slice of all the items plus the extra capacity, whose memory131 /// Returns a slice of all the items plus the extra capacity, whose memory
131 /// contents are `undefined`.132 /// contents are `undefined`.
132 fn allocatedSlice(self: Self) []T {133 fn allocatedSlice(self: *const Self) []T {
133 // `items.len` is the length, not the capacity.134 // `items.len` is the length, not the capacity.
134 return self.items.ptr[0..self.cap];135 return self.items.ptr[0..self.cap];
135 }136 }