commit 65fe99e18a89e92b6b82f798411e1e13e21df511 (tree)
parent 842b74d7ea3d4c5b4780f52c6dc8bce21d679fe1
Author: Saurabh Mishra <saurabh.m@proton.me>
Date: Fri, 3 Apr 2026 22:20:46 +0200
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>
Diffstat:
2 files changed, 18 insertions(+), 16 deletions(-)
diff --git a/lib/std/priority_dequeue.zig b/lib/std/priority_dequeue.zig
@@ -40,8 +40,9 @@ pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compar
}
/// Free memory used by the dequeue.
- pub fn deinit(self: Self, allocator: Allocator) void {
+ pub fn deinit(self: *Self, allocator: Allocator) void {
allocator.free(self.items);
+ self.* = undefined;
}
/// Insert a new element, maintaining priority.
@@ -77,7 +78,7 @@ pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compar
return 1 == @clz(index +% 1) & 1;
}
- fn nextIsMinLayer(self: Self) bool {
+ fn nextIsMinLayer(self: *const Self) bool {
return isMinLayer(self.len);
}
@@ -86,7 +87,7 @@ pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compar
min_layer: bool,
};
- fn getStartForSiftUp(self: Self, child: T, index: usize) StartIndexAndLayer {
+ fn getStartForSiftUp(self: *const Self, child: T, index: usize) StartIndexAndLayer {
const child_index = index;
const parent_index = parentIndex(child_index);
const parent = self.items[parent_index];
@@ -136,20 +137,20 @@ pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compar
/// Look at the smallest element in the dequeue. Returns
/// `null` if empty.
- pub fn peekMin(self: *Self) ?T {
+ pub fn peekMin(self: *const Self) ?T {
return if (self.len > 0) self.items[0] else null;
}
/// Look at the largest element in the dequeue. Returns
/// `null` if empty.
- pub fn peekMax(self: *Self) ?T {
+ pub fn peekMax(self: *const Self) ?T {
if (self.len == 0) return null;
if (self.len == 1) return self.items[0];
if (self.len == 2) return self.items[1];
return self.bestItemAtIndices(1, 2, .gt).item;
}
- fn maxIndex(self: Self) ?usize {
+ fn maxIndex(self: *const Self) ?usize {
if (self.len == 0) return null;
if (self.len == 1) return 0;
if (self.len == 2) return 1;
@@ -261,14 +262,14 @@ pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compar
index: usize,
};
- fn getItem(self: Self, index: usize) ItemAndIndex {
+ fn getItem(self: *const Self, index: usize) ItemAndIndex {
return .{
.item = self.items[index],
.index = index,
};
}
- fn bestItem(self: Self, item1: ItemAndIndex, item2: ItemAndIndex, target_order: Order) ItemAndIndex {
+ fn bestItem(self: *const Self, item1: ItemAndIndex, item2: ItemAndIndex, target_order: Order) ItemAndIndex {
if (compareFn(self.context, item1.item, item2.item) == target_order) {
return item1;
} else {
@@ -276,13 +277,13 @@ pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compar
}
}
- fn bestItemAtIndices(self: Self, index1: usize, index2: usize, target_order: Order) ItemAndIndex {
+ fn bestItemAtIndices(self: *const Self, index1: usize, index2: usize, target_order: Order) ItemAndIndex {
const item1 = self.getItem(index1);
const item2 = self.getItem(index2);
return self.bestItem(item1, item2, target_order);
}
- fn bestDescendent(self: Self, first_child_index: usize, first_grandchild_index: usize, target_order: Order) ItemAndIndex {
+ fn bestDescendent(self: *const Self, first_child_index: usize, first_grandchild_index: usize, target_order: Order) ItemAndIndex {
const second_child_index = first_child_index + 1;
if (first_grandchild_index >= self.len) {
// 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
}
/// Return the number of elements remaining in the dequeue
- pub fn count(self: Self) usize {
+ pub fn count(self: *const Self) usize {
return self.len;
}
/// Return the number of elements that can be added to the
/// dequeue before more memory is allocated.
- pub fn capacity(self: Self) usize {
+ pub fn capacity(self: *const Self) usize {
return self.items.len;
}
diff --git a/lib/std/priority_queue.zig b/lib/std/priority_queue.zig
@@ -41,6 +41,7 @@ pub fn PriorityQueue(comptime T: type, comptime Context: type, comptime compareF
/// Free memory used by the queue.
pub fn deinit(self: *Self, allocator: Allocator) void {
allocator.free(self.allocatedSlice());
+ self.* = undefined;
}
/// Insert a new element, maintaining priority.
@@ -78,7 +79,7 @@ pub fn PriorityQueue(comptime T: type, comptime Context: type, comptime compareF
/// Look at the highest priority element in the queue. Returns
/// `null` if empty.
- pub fn peek(self: *Self) ?T {
+ pub fn peek(self: *const Self) ?T {
return if (self.items.len > 0) self.items[0] else null;
}
@@ -117,19 +118,19 @@ pub fn PriorityQueue(comptime T: type, comptime Context: type, comptime compareF
/// Return the number of elements remaining in the priority
/// queue.
- pub fn count(self: Self) usize {
+ pub fn count(self: *const Self) usize {
return self.items.len;
}
/// Return the number of elements that can be added to the
/// queue before more memory is allocated.
- pub fn capacity(self: Self) usize {
+ pub fn capacity(self: *const Self) usize {
return self.cap;
}
/// Returns a slice of all the items plus the extra capacity, whose memory
/// contents are `undefined`.
- fn allocatedSlice(self: Self) []T {
+ fn allocatedSlice(self: *const Self) []T {
// `items.len` is the length, not the capacity.
return self.items.ptr[0..self.cap];
}