aboutsummaryrefslogtreecommitdiff
path: root/lib/std/array_list.zig
diff options
context:
space:
mode:
authorJacob Young <jacobly0@users.noreply.github.com>2023-05-22 07:58:02 -0400
committerAndrew Kelley <andrew@ziglang.org>2023-06-10 20:47:54 -0700
commit6e0de1d11694a58745da76d601ebab7562feed09 (patch)
treecd6cf352788d8f47bad97a95e4390dd3f6a309c5 /lib/std/array_list.zig
parent5555bdca047f8dbf8d7adfa8f248f5ce9b692b9e (diff)
downloadzig-6e0de1d11694a58745da76d601ebab7562feed09.tar.gz
zig-6e0de1d11694a58745da76d601ebab7562feed09.zip
InternPool: port most of value tags
Diffstat (limited to 'lib/std/array_list.zig')
-rw-r--r--lib/std/array_list.zig44
1 files changed, 44 insertions, 0 deletions
diff --git a/lib/std/array_list.zig b/lib/std/array_list.zig
index bbfa588d6d..c2a2486dfa 100644
--- a/lib/std/array_list.zig
+++ b/lib/std/array_list.zig
@@ -459,6 +459,28 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type {
return self.items[prev_len..][0..n];
}
+ /// Resize the array, adding `n` new elements, which have `undefined` values.
+ /// The return value is a slice pointing to the newly allocated elements.
+ /// The returned pointer becomes invalid when the list is resized.
+ /// Resizes list if `self.capacity` is not large enough.
+ pub fn addManyAsSlice(self: *Self, n: usize) Allocator.Error![]T {
+ const prev_len = self.items.len;
+ try self.resize(self.items.len + n);
+ return self.items[prev_len..][0..n];
+ }
+
+ /// Resize the array, adding `n` new elements, which have `undefined` values.
+ /// The return value is a slice pointing to the newly allocated elements.
+ /// Asserts that there is already space for the new item without allocating more.
+ /// **Does not** invalidate element pointers.
+ /// The returned pointer becomes invalid when the list is resized.
+ pub fn addManyAsSliceAssumeCapacity(self: *Self, n: usize) []T {
+ assert(self.items.len + n <= self.capacity);
+ const prev_len = self.items.len;
+ self.items.len += n;
+ return self.items[prev_len..][0..n];
+ }
+
/// Remove and return the last element from the list.
/// Asserts the list has at least one item.
/// Invalidates pointers to the removed element.
@@ -949,6 +971,28 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
return self.items[prev_len..][0..n];
}
+ /// Resize the array, adding `n` new elements, which have `undefined` values.
+ /// The return value is a slice pointing to the newly allocated elements.
+ /// The returned pointer becomes invalid when the list is resized.
+ /// Resizes list if `self.capacity` is not large enough.
+ pub fn addManyAsSlice(self: *Self, allocator: Allocator, n: usize) Allocator.Error![]T {
+ const prev_len = self.items.len;
+ try self.resize(allocator, self.items.len + n);
+ return self.items[prev_len..][0..n];
+ }
+
+ /// Resize the array, adding `n` new elements, which have `undefined` values.
+ /// The return value is a slice pointing to the newly allocated elements.
+ /// Asserts that there is already space for the new item without allocating more.
+ /// **Does not** invalidate element pointers.
+ /// The returned pointer becomes invalid when the list is resized.
+ pub fn addManyAsSliceAssumeCapacity(self: *Self, n: usize) []T {
+ assert(self.items.len + n <= self.capacity);
+ const prev_len = self.items.len;
+ self.items.len += n;
+ return self.items[prev_len..][0..n];
+ }
+
/// Remove and return the last element from the list.
/// Asserts the list has at least one item.
/// Invalidates pointers to last element.