aboutsummaryrefslogtreecommitdiff
path: root/std/array_list.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2019-08-28 16:59:35 -0400
committerAndrew Kelley <andrew@ziglang.org>2019-08-28 16:59:35 -0400
commit928ce5e326ccd1b7456002c28f34975b5bc4b13f (patch)
tree5706376fe74103223511f5a4954ce2dff0b08d0e /std/array_list.zig
parent2f3ad48c9b35ccac377e710a4eee6b35f60b119f (diff)
parente8a9caa3dd0aef7b745b8197afecec90598b2cdd (diff)
downloadzig-928ce5e326ccd1b7456002c28f34975b5bc4b13f.tar.gz
zig-928ce5e326ccd1b7456002c28f34975b5bc4b13f.zip
Merge remote-tracking branch 'origin/master' into llvm9
Diffstat (limited to 'std/array_list.zig')
-rw-r--r--std/array_list.zig21
1 files changed, 12 insertions, 9 deletions
diff --git a/std/array_list.zig b/std/array_list.zig
index ee282145bd..a527d818d6 100644
--- a/std/array_list.zig
+++ b/std/array_list.zig
@@ -6,20 +6,23 @@ const mem = std.mem;
const Allocator = mem.Allocator;
pub fn ArrayList(comptime T: type) type {
- return AlignedArrayList(T, @alignOf(T));
+ return AlignedArrayList(T, null);
}
-pub fn AlignedArrayList(comptime T: type, comptime A: u29) type {
+pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type {
return struct {
const Self = @This();
/// Use toSlice instead of slicing this directly, because if you don't
/// specify the end position of the slice, this will potentially give
/// you uninitialized memory.
- items: []align(A) T,
+ items: Slice,
len: usize,
allocator: *Allocator,
+ pub const Slice = if (alignment) |a| ([]align(a) T) else []T;
+ pub const SliceConst = if (alignment) |a| ([]align(a) const T) else []const T;
+
/// Deinitialize with `deinit` or use `toOwnedSlice`.
pub fn init(allocator: *Allocator) Self {
return Self{
@@ -33,11 +36,11 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type {
self.allocator.free(self.items);
}
- pub fn toSlice(self: Self) []align(A) T {
+ pub fn toSlice(self: Self) Slice {
return self.items[0..self.len];
}
- pub fn toSliceConst(self: Self) []align(A) const T {
+ pub fn toSliceConst(self: Self) SliceConst {
return self.items[0..self.len];
}
@@ -69,7 +72,7 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type {
/// ArrayList takes ownership of the passed in slice. The slice must have been
/// allocated with `allocator`.
/// Deinitialize with `deinit` or use `toOwnedSlice`.
- pub fn fromOwnedSlice(allocator: *Allocator, slice: []align(A) T) Self {
+ pub fn fromOwnedSlice(allocator: *Allocator, slice: Slice) Self {
return Self{
.items = slice,
.len = slice.len,
@@ -78,7 +81,7 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type {
}
/// The caller owns the returned memory. ArrayList becomes empty.
- pub fn toOwnedSlice(self: *Self) []align(A) T {
+ pub fn toOwnedSlice(self: *Self) Slice {
const allocator = self.allocator;
const result = allocator.shrink(self.items, self.len);
self.* = init(allocator);
@@ -93,7 +96,7 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type {
self.items[n] = item;
}
- pub fn insertSlice(self: *Self, n: usize, items: []align(A) const T) !void {
+ pub fn insertSlice(self: *Self, n: usize, items: SliceConst) !void {
try self.ensureCapacity(self.len + items.len);
self.len += items.len;
@@ -141,7 +144,7 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type {
return self.swapRemove(i);
}
- pub fn appendSlice(self: *Self, items: []align(A) const T) !void {
+ pub fn appendSlice(self: *Self, items: SliceConst) !void {
try self.ensureCapacity(self.len + items.len);
mem.copy(T, self.items[self.len..], items);
self.len += items.len;