Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std.BoundedArray: popOrNull() -> pop() [v2] #22723

Merged
merged 3 commits into from
Feb 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions doc/langref/test_switch_dispatch_loop.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ fn evaluate(initial_stack: []const i32, code: []const Instruction) !i32 {
// Because all code after `continue` is unreachable, this branch does
// not provide a result.
.add => {
try stack.append(stack.pop() + stack.pop());
try stack.append(stack.pop().? + stack.pop().?);

ip += 1;
continue :vm code[ip];
},
.mul => {
try stack.append(stack.pop() * stack.pop());
try stack.append(stack.pop().? * stack.pop().?);

ip += 1;
continue :vm code[ip];
},
.end => stack.pop(),
.end => stack.pop().?,
};
}

Expand Down
24 changes: 9 additions & 15 deletions lib/std/bounded_array.zig
Original file line number Diff line number Diff line change
Expand Up @@ -134,20 +134,14 @@ pub fn BoundedArrayAligned(
return self.slice()[prev_len..][0..n];
}

/// Remove and return the last element from the slice.
/// Asserts the slice has at least one item.
pub fn pop(self: *Self) T {
/// Remove and return the last element from the slice, or return `null` if the slice is empty.
pub fn pop(self: *Self) ?T {
if (self.len == 0) return null;
const item = self.get(self.len - 1);
self.len -= 1;
return item;
}

/// Remove and return the last element from the slice, or
/// return `null` if the slice is empty.
pub fn popOrNull(self: *Self) ?T {
return if (self.len == 0) null else self.pop();
}

/// Return a slice of only the extra capacity after items.
/// This can be useful for writing directly into it.
/// Note that such an operation must be followed up with a
Expand Down Expand Up @@ -229,7 +223,7 @@ pub fn BoundedArrayAligned(
/// This operation is O(N).
pub fn orderedRemove(self: *Self, i: usize) T {
const newlen = self.len - 1;
if (newlen == i) return self.pop();
if (newlen == i) return self.pop().?;
const old_item = self.get(i);
for (self.slice()[i..newlen], 0..) |*b, j| b.* = self.get(i + 1 + j);
self.set(newlen, undefined);
Expand All @@ -241,9 +235,9 @@ pub fn BoundedArrayAligned(
/// The empty slot is filled from the end of the slice.
/// This operation is O(1).
pub fn swapRemove(self: *Self, i: usize) T {
if (self.len - 1 == i) return self.pop();
if (self.len - 1 == i) return self.pop().?;
const old_item = self.get(i);
self.set(i, self.pop());
self.set(i, self.pop().?);
return old_item;
}

Expand Down Expand Up @@ -339,8 +333,8 @@ test BoundedArray {
try testing.expectEqual(a.pop(), 0xff);

try a.resize(1);
try testing.expectEqual(a.popOrNull(), 0);
try testing.expectEqual(a.popOrNull(), null);
try testing.expectEqual(a.pop(), 0);
try testing.expectEqual(a.pop(), null);
var unused = a.unusedCapacitySlice();
@memset(unused[0..8], 2);
unused[8] = 3;
Expand Down Expand Up @@ -397,7 +391,7 @@ test BoundedArray {
try testing.expectEqual(added_slice.len, 3);
try testing.expectEqual(a.len, 36);

while (a.popOrNull()) |_| {}
while (a.pop()) |_| {}
const w = a.writer();
const s = "hello, this is a test string";
try w.writeAll(s);
Expand Down
Loading