mirror of
https://github.com/ziglang/zig.git
synced 2025-12-06 06:13:07 +00:00
Add the two functions 'getLast' and 'getLastOrNull' to ArrayListAligned/ArrayListAlignedUnmanaged.
This commit is contained in:
parent
8094fa5d48
commit
1de96a2cc4
@ -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();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user