diff options
| author | Chris Boesch <48591413+chrboesch@users.noreply.github.com> | 2022-12-29 00:24:57 +0100 |
|---|---|---|
| committer | Veikka Tuominen <git@vexu.eu> | 2023-01-03 12:48:20 +0200 |
| commit | 1de96a2cc4bf2d1be1404690d6a867eb0127d533 (patch) | |
| tree | f0956e852abf33b9397044efe52e027eb5961c70 /lib/std/array_list.zig | |
| parent | 8094fa5d48c48fe7b6993e1577209811de7a0e9b (diff) | |
| download | zig-1de96a2cc4bf2d1be1404690d6a867eb0127d533.tar.gz zig-1de96a2cc4bf2d1be1404690d6a867eb0127d533.zip | |
Add the two functions 'getLast' and 'getLastOrNull' to ArrayListAligned/ArrayListAlignedUnmanaged.
Diffstat (limited to 'lib/std/array_list.zig')
| -rw-r--r-- | lib/std/array_list.zig | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/lib/std/array_list.zig b/lib/std/array_list.zig index c6df2a66eb..852c81f140 100644 --- a/lib/std/array_list.zig +++ b/lib/std/array_list.zig @@ -468,6 +468,20 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { pub fn unusedCapacitySlice(self: Self) Slice { return self.allocatedSlice()[self.items.len..]; } + + /// Return the last element from the list. + /// Asserts the list has at least one item. + pub fn getLast(self: *Self) T { + const val = self.items[self.items.len - 1]; + return val; + } + + /// Return the last element from the list, or + /// return `null` if list is empty. + pub fn getLastOrNull(self: *Self) ?T { + if (self.items.len == 0) return null; + return self.getLast(); + } }; } @@ -913,6 +927,20 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ pub fn unusedCapacitySlice(self: Self) Slice { return self.allocatedSlice()[self.items.len..]; } + + /// Return the last element from the list. + /// Asserts the list has at least one item. + pub fn getLast(self: *Self) T { + const val = self.items[self.items.len - 1]; + return val; + } + + /// Return the last element from the list, or + /// return `null` if list is empty. + pub fn getLastOrNull(self: *Self) ?T { + if (self.items.len == 0) return null; + return self.getLast(); + } }; } |
