authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2025-02-09 03:46:15-08:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2025-02-09 11:46:15+00:00
log933ba935c5ff85668c82606ba4ff2531c00b2e36
tree3b75388fef0c7d7f33f595f33eaa1795576e15de
parent4e4775d6bd612694540e738e9347a872bcde5036
signaturebadge-check Signed by PGP key B5690EEEBB952194

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


2 files changed, 12 insertions(+), 18 deletions(-)

doc/langref/test_switch_dispatch_loop.zig+3-3
...@@ -15,18 +15,18 @@ fn evaluate(initial_stack: []const i32, code: []const Instruction) !i32 {...@@ -15,18 +15,18 @@ fn evaluate(initial_stack: []const i32, code: []const Instruction) !i32 {
15 // Because all code after `continue` is unreachable, this branch does15 // Because all code after `continue` is unreachable, this branch does
16 // not provide a result.16 // not provide a result.
17 .add => {17 .add => {
18 try stack.append(stack.pop() + stack.pop());18 try stack.append(stack.pop().? + stack.pop().?);
1919
20 ip += 1;20 ip += 1;
21 continue :vm code[ip];21 continue :vm code[ip];
22 },22 },
23 .mul => {23 .mul => {
24 try stack.append(stack.pop() * stack.pop());24 try stack.append(stack.pop().? * stack.pop().?);
2525
26 ip += 1;26 ip += 1;
27 continue :vm code[ip];27 continue :vm code[ip];
28 },28 },
29 .end => stack.pop(),29 .end => stack.pop().?,
30 };30 };
31}31}
3232
lib/std/bounded_array.zig+9-15
...@@ -134,20 +134,14 @@ pub fn BoundedArrayAligned(...@@ -134,20 +134,14 @@ pub fn BoundedArrayAligned(
134 return self.slice()[prev_len..][0..n];134 return self.slice()[prev_len..][0..n];
135 }135 }
136136
137 /// Remove and return the last element from the slice.137 /// Remove and return the last element from the slice, or return `null` if the slice is empty.
138 /// Asserts the slice has at least one item.138 pub fn pop(self: *Self) ?T {
139 pub fn pop(self: *Self) T {139 if (self.len == 0) return null;
140 const item = self.get(self.len - 1);140 const item = self.get(self.len - 1);
141 self.len -= 1;141 self.len -= 1;
142 return item;142 return item;
143 }143 }
144144
145 /// Remove and return the last element from the slice, or
146 /// return `null` if the slice is empty.
147 pub fn popOrNull(self: *Self) ?T {
148 return if (self.len == 0) null else self.pop();
149 }
150
151 /// Return a slice of only the extra capacity after items.145 /// Return a slice of only the extra capacity after items.
152 /// This can be useful for writing directly into it.146 /// This can be useful for writing directly into it.
153 /// Note that such an operation must be followed up with a147 /// Note that such an operation must be followed up with a
...@@ -229,7 +223,7 @@ pub fn BoundedArrayAligned(...@@ -229,7 +223,7 @@ pub fn BoundedArrayAligned(
229 /// This operation is O(N).223 /// This operation is O(N).
230 pub fn orderedRemove(self: *Self, i: usize) T {224 pub fn orderedRemove(self: *Self, i: usize) T {
231 const newlen = self.len - 1;225 const newlen = self.len - 1;
232 if (newlen == i) return self.pop();226 if (newlen == i) return self.pop().?;
233 const old_item = self.get(i);227 const old_item = self.get(i);
234 for (self.slice()[i..newlen], 0..) |*b, j| b.* = self.get(i + 1 + j);228 for (self.slice()[i..newlen], 0..) |*b, j| b.* = self.get(i + 1 + j);
235 self.set(newlen, undefined);229 self.set(newlen, undefined);
...@@ -241,9 +235,9 @@ pub fn BoundedArrayAligned(...@@ -241,9 +235,9 @@ pub fn BoundedArrayAligned(
241 /// The empty slot is filled from the end of the slice.235 /// The empty slot is filled from the end of the slice.
242 /// This operation is O(1).236 /// This operation is O(1).
243 pub fn swapRemove(self: *Self, i: usize) T {237 pub fn swapRemove(self: *Self, i: usize) T {
244 if (self.len - 1 == i) return self.pop();238 if (self.len - 1 == i) return self.pop().?;
245 const old_item = self.get(i);239 const old_item = self.get(i);
246 self.set(i, self.pop());240 self.set(i, self.pop().?);
247 return old_item;241 return old_item;
248 }242 }
249243
...@@ -339,8 +333,8 @@ test BoundedArray {...@@ -339,8 +333,8 @@ test BoundedArray {
339 try testing.expectEqual(a.pop(), 0xff);333 try testing.expectEqual(a.pop(), 0xff);
340334
341 try a.resize(1);335 try a.resize(1);
342 try testing.expectEqual(a.popOrNull(), 0);336 try testing.expectEqual(a.pop(), 0);
343 try testing.expectEqual(a.popOrNull(), null);337 try testing.expectEqual(a.pop(), null);
344 var unused = a.unusedCapacitySlice();338 var unused = a.unusedCapacitySlice();
345 @memset(unused[0..8], 2);339 @memset(unused[0..8], 2);
346 unused[8] = 3;340 unused[8] = 3;
...@@ -397,7 +391,7 @@ test BoundedArray {...@@ -397,7 +391,7 @@ test BoundedArray {
397 try testing.expectEqual(added_slice.len, 3);391 try testing.expectEqual(added_slice.len, 3);
398 try testing.expectEqual(a.len, 36);392 try testing.expectEqual(a.len, 36);
399393
400 while (a.popOrNull()) |_| {}394 while (a.pop()) |_| {}
401 const w = a.writer();395 const w = a.writer();
402 const s = "hello, this is a test string";396 const s = "hello, this is a test string";
403 try w.writeAll(s);397 try w.writeAll(s);