From 35c5611f0707a46f6314a7b4e4597ff56bfead18 Mon Sep 17 00:00:00 2001 From: Justus Klausecker Date: Wed, 11 Feb 2026 00:41:04 +0100 Subject: [PATCH] std.Deque: add `*Ptr` variants of getter functions This makes it practical to store large items or items that are meant to be mutable directly inside of the deque. It is the responsibility of the user to stop using the returned pointers after calling a function that could invalidate them. --- lib/std/deque.zig | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/lib/std/deque.zig b/lib/std/deque.zig index 267b8a0afe4a7c40cb0250c0afbeb0b55bb61bc1..6874ebe394117a38d838818eff0b8f520aae36d0 100644 --- a/lib/std/deque.zig +++ b/lib/std/deque.zig @@ -180,12 +180,24 @@ pub fn Deque(comptime T: type) type { return deque.buffer[deque.head]; } + /// Return pointer to the first item in the deque or null if empty. + pub fn frontPtr(deque: *const Self) ?*T { + if (deque.len == 0) return null; + return &deque.buffer[deque.head]; + } + /// Return the last item in the deque or null if empty. pub fn back(deque: *const Self) ?T { if (deque.len == 0) return null; return deque.buffer[deque.bufferIndex(deque.len - 1)]; } + /// Return the last item in the deque or null if empty. + pub fn backPtr(deque: *const Self) ?*T { + if (deque.len == 0) return null; + return &deque.buffer[deque.bufferIndex(deque.len - 1)]; + } + /// Return the item at the given index in the deque. /// /// The first item in the queue is at index 0. @@ -196,6 +208,16 @@ pub fn Deque(comptime T: type) type { return deque.buffer[deque.bufferIndex(index)]; } + /// Return pointer to the item at the given index in the deque. + /// + /// The first item in the queue is at index 0. + /// + /// Asserts that the index is in-bounds. + pub fn atPtr(deque: *const Self, index: usize) *T { + assert(index < deque.len); + return &deque.buffer[deque.bufferIndex(index)]; + } + /// Remove and return the first item in the deque or null if empty. pub fn popFront(deque: *Self) ?T { if (deque.len == 0) return null; -- 2.54.0