diff options
Diffstat (limited to 'lib/std/array_list.zig')
| -rw-r--r-- | lib/std/array_list.zig | 47 |
1 files changed, 16 insertions, 31 deletions
diff --git a/lib/std/array_list.zig b/lib/std/array_list.zig index 55b6e3cc40..11419f975a 100644 --- a/lib/std/array_list.zig +++ b/lib/std/array_list.zig @@ -289,10 +289,10 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { /// Asserts that the list is not empty. /// Asserts that the index is in bounds. pub fn swapRemove(self: *Self, i: usize) T { - if (self.items.len - 1 == i) return self.pop(); + if (self.items.len - 1 == i) return self.pop().?; const old_item = self.items[i]; - self.items[i] = self.pop(); + self.items[i] = self.pop().?; return old_item; } @@ -555,23 +555,15 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { return self.items[prev_len..][0..n]; } - /// Remove and return the last element from the list. - /// Invalidates element pointers to the removed element. - /// Asserts that the list is not empty. - pub fn pop(self: *Self) T { + /// Remove and return the last element from the list, or return `null` if list is empty. + /// Invalidates element pointers to the removed element, if any. + pub fn pop(self: *Self) ?T { + if (self.items.len == 0) return null; const val = self.items[self.items.len - 1]; self.items.len -= 1; return val; } - /// Remove and return the last element from the list, or - /// return `null` if list is empty. - /// Invalidates element pointers to the removed element, if any. - pub fn popOrNull(self: *Self) ?T { - if (self.items.len == 0) return null; - return self.pop(); - } - /// Returns a slice of all the items plus the extra capacity, whose memory /// contents are `undefined`. pub fn allocatedSlice(self: Self) Slice { @@ -897,10 +889,10 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ /// Asserts that the list is not empty. /// Asserts that the index is in bounds. pub fn swapRemove(self: *Self, i: usize) T { - if (self.items.len - 1 == i) return self.pop(); + if (self.items.len - 1 == i) return self.pop().?; const old_item = self.items[i]; - self.items[i] = self.pop(); + self.items[i] = self.pop().?; return old_item; } @@ -1190,22 +1182,15 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ } /// Remove and return the last element from the list. + /// If the list is empty, returns `null`. /// Invalidates pointers to last element. - /// Asserts that the list is not empty. - pub fn pop(self: *Self) T { + pub fn pop(self: *Self) ?T { + if (self.items.len == 0) return null; const val = self.items[self.items.len - 1]; self.items.len -= 1; return val; } - /// Remove and return the last element from the list. - /// If the list is empty, returns `null`. - /// Invalidates pointers to last element. - pub fn popOrNull(self: *Self) ?T { - if (self.items.len == 0) return null; - return self.pop(); - } - /// Returns a slice of all the items plus the extra capacity, whose memory /// contents are `undefined`. pub fn allocatedSlice(self: Self) Slice { @@ -2184,7 +2169,7 @@ test "ArrayList(u0)" { try testing.expectEqual(count, 3); } -test "ArrayList(?u32).popOrNull()" { +test "ArrayList(?u32).pop()" { const a = testing.allocator; var list = ArrayList(?u32).init(a); @@ -2195,10 +2180,10 @@ test "ArrayList(?u32).popOrNull()" { try list.append(2); try testing.expectEqual(list.items.len, 3); - try testing.expect(list.popOrNull().? == @as(u32, 2)); - try testing.expect(list.popOrNull().? == @as(u32, 1)); - try testing.expect(list.popOrNull().? == null); - try testing.expect(list.popOrNull() == null); + try testing.expect(list.pop().? == @as(u32, 2)); + try testing.expect(list.pop().? == @as(u32, 1)); + try testing.expect(list.pop().? == null); + try testing.expect(list.pop() == null); } test "ArrayList(u32).getLast()" { |
