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

This commit is contained in:
Meghan Denny 2025-02-09 03:46:15 -08:00 committed by GitHub
parent 4e4775d6bd
commit 933ba935c5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 18 deletions

View File

@ -15,18 +15,18 @@ fn evaluate(initial_stack: []const i32, code: []const Instruction) !i32 {
// Because all code after `continue` is unreachable, this branch does // Because all code after `continue` is unreachable, this branch does
// not provide a result. // not provide a result.
.add => { .add => {
try stack.append(stack.pop() + stack.pop()); try stack.append(stack.pop().? + stack.pop().?);
ip += 1; ip += 1;
continue :vm code[ip]; continue :vm code[ip];
}, },
.mul => { .mul => {
try stack.append(stack.pop() * stack.pop()); try stack.append(stack.pop().? * stack.pop().?);
ip += 1; ip += 1;
continue :vm code[ip]; continue :vm code[ip];
}, },
.end => stack.pop(), .end => stack.pop().?,
}; };
} }

View File

@ -134,20 +134,14 @@ pub fn BoundedArrayAligned(
return self.slice()[prev_len..][0..n]; return self.slice()[prev_len..][0..n];
} }
/// Remove and return the last element from the slice. /// Remove and return the last element from the slice, or return `null` if the slice is empty.
/// Asserts the slice has at least one item. pub fn pop(self: *Self) ?T {
pub fn pop(self: *Self) T { if (self.len == 0) return null;
const item = self.get(self.len - 1); const item = self.get(self.len - 1);
self.len -= 1; self.len -= 1;
return item; 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. /// Return a slice of only the extra capacity after items.
/// This can be useful for writing directly into it. /// This can be useful for writing directly into it.
/// Note that such an operation must be followed up with a /// Note that such an operation must be followed up with a
@ -229,7 +223,7 @@ pub fn BoundedArrayAligned(
/// This operation is O(N). /// This operation is O(N).
pub fn orderedRemove(self: *Self, i: usize) T { pub fn orderedRemove(self: *Self, i: usize) T {
const newlen = self.len - 1; const newlen = self.len - 1;
if (newlen == i) return self.pop(); if (newlen == i) return self.pop().?;
const old_item = self.get(i); const old_item = self.get(i);
for (self.slice()[i..newlen], 0..) |*b, j| b.* = self.get(i + 1 + j); for (self.slice()[i..newlen], 0..) |*b, j| b.* = self.get(i + 1 + j);
self.set(newlen, undefined); self.set(newlen, undefined);
@ -241,9 +235,9 @@ pub fn BoundedArrayAligned(
/// The empty slot is filled from the end of the slice. /// The empty slot is filled from the end of the slice.
/// This operation is O(1). /// This operation is O(1).
pub fn swapRemove(self: *Self, i: usize) T { 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); const old_item = self.get(i);
self.set(i, self.pop()); self.set(i, self.pop().?);
return old_item; return old_item;
} }
@ -339,8 +333,8 @@ test BoundedArray {
try testing.expectEqual(a.pop(), 0xff); try testing.expectEqual(a.pop(), 0xff);
try a.resize(1); try a.resize(1);
try testing.expectEqual(a.popOrNull(), 0); try testing.expectEqual(a.pop(), 0);
try testing.expectEqual(a.popOrNull(), null); try testing.expectEqual(a.pop(), null);
var unused = a.unusedCapacitySlice(); var unused = a.unusedCapacitySlice();
@memset(unused[0..8], 2); @memset(unused[0..8], 2);
unused[8] = 3; unused[8] = 3;
@ -397,7 +391,7 @@ test BoundedArray {
try testing.expectEqual(added_slice.len, 3); try testing.expectEqual(added_slice.len, 3);
try testing.expectEqual(a.len, 36); try testing.expectEqual(a.len, 36);
while (a.popOrNull()) |_| {} while (a.pop()) |_| {}
const w = a.writer(); const w = a.writer();
const s = "hello, this is a test string"; const s = "hello, this is a test string";
try w.writeAll(s); try w.writeAll(s);