Skip to content
Merged
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
24 changes: 8 additions & 16 deletions lib/std/multi_array_list.zig
Original file line number Diff line number Diff line change
Expand Up @@ -263,23 +263,15 @@ pub fn MultiArrayList(comptime T: type) type {
return index;
}

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

/// Remove and return the last element from the list, or
/// return `null` if list is empty.
/// Invalidates pointers to fields of the removed element, if any.
pub fn popOrNull(self: *Self) ?T {
if (self.len == 0) return null;
return self.pop();
}

/// Inserts an item into an ordered list. Shifts all elements
/// after and including the specified index back by one and
/// sets the given index to the specified element. May reallocate
Expand Down Expand Up @@ -685,11 +677,11 @@ test "basic usage" {
.b = "xnopyt",
.c = 'd',
});
try testing.expectEqualStrings("xnopyt", list.pop().b);
try testing.expectEqual(@as(?u8, 'c'), if (list.popOrNull()) |elem| elem.c else null);
try testing.expectEqual(@as(u32, 2), list.pop().a);
try testing.expectEqual(@as(u8, 'a'), list.pop().c);
try testing.expectEqual(@as(?Foo, null), list.popOrNull());
try testing.expectEqualStrings("xnopyt", list.pop().?.b);
try testing.expectEqual(@as(?u8, 'c'), if (list.pop()) |elem| elem.c else null);
try testing.expectEqual(@as(u32, 2), list.pop().?.a);
try testing.expectEqual(@as(u8, 'a'), list.pop().?.c);
try testing.expectEqual(@as(?Foo, null), list.pop());

list.clearRetainingCapacity();
try testing.expectEqual(0, list.len);
Expand Down
Loading