aboutsummaryrefslogtreecommitdiff
path: root/lib/std/array_list.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2023-06-12 22:50:50 -0700
committerGitHub <noreply@github.com>2023-06-12 22:50:50 -0700
commit529ef75101e21bd45402c516138343b4770238eb (patch)
tree56c925bd7df84e5f223c31a7c8fa90606c8e2dc9 /lib/std/array_list.zig
parent1e7dcaa3ae57294ab5998b44a8c13ccc5019e7ea (diff)
parent2ad073ec6d4e2be967f18c9907844404a7eed42e (diff)
downloadzig-529ef75101e21bd45402c516138343b4770238eb.tar.gz
zig-529ef75101e21bd45402c516138343b4770238eb.zip
Merge pull request #15569 from ziglang/intern-pool-3
Use InternPool for all types and constant values
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.