authorgravatar for 48591413+chrboesch@users.noreply.github.comChris Boesch <48591413+chrboesch@users.noreply.github.com> 2022-12-29 00:24:57+01:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2023-01-03 12:48:20+02:00
log1de96a2cc4bf2d1be1404690d6a867eb0127d533
treef0956e852abf33b9397044efe52e027eb5961c70
parent8094fa5d48c48fe7b6993e1577209811de7a0e9b

Add the two functions 'getLast' and 'getLastOrNull' to ArrayListAligned/ArrayListAlignedUnmanaged.


1 files changed, 28 insertions(+), 0 deletions(-)

lib/std/array_list.zig+28
......@@ -468,6 +468,20 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type {
468468 pub fn unusedCapacitySlice(self: Self) Slice {
469469 return self.allocatedSlice()[self.items.len..];
470470 }
471
472 /// Return the last element from the list.
473 /// Asserts the list has at least one item.
474 pub fn getLast(self: *Self) T {
475 const val = self.items[self.items.len - 1];
476 return val;
477 }
478
479 /// Return the last element from the list, or
480 /// return `null` if list is empty.
481 pub fn getLastOrNull(self: *Self) ?T {
482 if (self.items.len == 0) return null;
483 return self.getLast();
484 }
471485 };
472486}
473487
......@@ -913,6 +927,20 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
913927 pub fn unusedCapacitySlice(self: Self) Slice {
914928 return self.allocatedSlice()[self.items.len..];
915929 }
930
931 /// Return the last element from the list.
932 /// Asserts the list has at least one item.
933 pub fn getLast(self: *Self) T {
934 const val = self.items[self.items.len - 1];
935 return val;
936 }
937
938 /// Return the last element from the list, or
939 /// return `null` if list is empty.
940 pub fn getLastOrNull(self: *Self) ?T {
941 if (self.items.len == 0) return null;
942 return self.getLast();
943 }
916944 };
917945}
918946