| ... | @@ -468,6 +468,20 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { | ... | @@ -468,6 +468,20 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 468 | pub fn unusedCapacitySlice(self: Self) Slice { | 468 | pub fn unusedCapacitySlice(self: Self) Slice { |
| 469 | return self.allocatedSlice()[self.items.len..]; | 469 | return self.allocatedSlice()[self.items.len..]; |
| 470 | } | 470 | } |
| | 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 | } |
| 471 | }; | 485 | }; |
| 472 | } | 486 | } |
| 473 | | 487 | |
| ... | @@ -913,6 +927,20 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ | ... | @@ -913,6 +927,20 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 913 | pub fn unusedCapacitySlice(self: Self) Slice { | 927 | pub fn unusedCapacitySlice(self: Self) Slice { |
| 914 | return self.allocatedSlice()[self.items.len..]; | 928 | return self.allocatedSlice()[self.items.len..]; |
| 915 | } | 929 | } |
| | 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 | } |
| 916 | }; | 944 | }; |
| 917 | } | 945 | } |
| 918 | | 946 | |