zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 35c5611f0707a46f6314a7b4e4597ff56bfead18 (tree)
parent f36d0573cd0f80b271b9f07a7215fa237d14ae9a
Author: Justus Klausecker <justus@klausecker.de>
Date:   Wed, 11 Feb 2026 00:41:04 +0100

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.

Diffstat:
Mlib/std/deque.zig | 22++++++++++++++++++++++
1 file changed, 22 insertions(+), 0 deletions(-)

diff --git 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;