| ... | ... | @@ -6,20 +6,23 @@ const mem = std.mem; |
| 6 | 6 | const Allocator = mem.Allocator; |
| 7 | 7 | |
| 8 | 8 | pub fn ArrayList(comptime T: type) type { |
| 9 | | return AlignedArrayList(T, @alignOf(T)); |
| 9 | return AlignedArrayList(T, null); |
| 10 | 10 | } |
| 11 | 11 | |
| 12 | | pub fn AlignedArrayList(comptime T: type, comptime A: u29) type { |
| 12 | pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type { |
| 13 | 13 | return struct { |
| 14 | 14 | const Self = @This(); |
| 15 | 15 | |
| 16 | 16 | /// Use toSlice instead of slicing this directly, because if you don't |
| 17 | 17 | /// specify the end position of the slice, this will potentially give |
| 18 | 18 | /// you uninitialized memory. |
| 19 | | items: []align(A) T, |
| 19 | items: Slice, |
| 20 | 20 | len: usize, |
| 21 | 21 | allocator: *Allocator, |
| 22 | 22 | |
| 23 | pub const Slice = if (alignment) |a| ([]align(a) T) else []T; |
| 24 | pub const SliceConst = if (alignment) |a| ([]align(a) const T) else []const T; |
| 25 | |
| 23 | 26 | /// Deinitialize with `deinit` or use `toOwnedSlice`. |
| 24 | 27 | pub fn init(allocator: *Allocator) Self { |
| 25 | 28 | return Self{ |
| ... | ... | @@ -33,11 +36,11 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type { |
| 33 | 36 | self.allocator.free(self.items); |
| 34 | 37 | } |
| 35 | 38 | |
| 36 | | pub fn toSlice(self: Self) []align(A) T { |
| 39 | pub fn toSlice(self: Self) Slice { |
| 37 | 40 | return self.items[0..self.len]; |
| 38 | 41 | } |
| 39 | 42 | |
| 40 | | pub fn toSliceConst(self: Self) []align(A) const T { |
| 43 | pub fn toSliceConst(self: Self) SliceConst { |
| 41 | 44 | return self.items[0..self.len]; |
| 42 | 45 | } |
| 43 | 46 | |
| ... | ... | @@ -69,7 +72,7 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type { |
| 69 | 72 | /// ArrayList takes ownership of the passed in slice. The slice must have been |
| 70 | 73 | /// allocated with `allocator`. |
| 71 | 74 | /// Deinitialize with `deinit` or use `toOwnedSlice`. |
| 72 | | pub fn fromOwnedSlice(allocator: *Allocator, slice: []align(A) T) Self { |
| 75 | pub fn fromOwnedSlice(allocator: *Allocator, slice: Slice) Self { |
| 73 | 76 | return Self{ |
| 74 | 77 | .items = slice, |
| 75 | 78 | .len = slice.len, |
| ... | ... | @@ -78,7 +81,7 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type { |
| 78 | 81 | } |
| 79 | 82 | |
| 80 | 83 | /// The caller owns the returned memory. ArrayList becomes empty. |
| 81 | | pub fn toOwnedSlice(self: *Self) []align(A) T { |
| 84 | pub fn toOwnedSlice(self: *Self) Slice { |
| 82 | 85 | const allocator = self.allocator; |
| 83 | 86 | const result = allocator.shrink(self.items, self.len); |
| 84 | 87 | self.* = init(allocator); |
| ... | ... | @@ -93,7 +96,7 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type { |
| 93 | 96 | self.items[n] = item; |
| 94 | 97 | } |
| 95 | 98 | |
| 96 | | pub fn insertSlice(self: *Self, n: usize, items: []align(A) const T) !void { |
| 99 | pub fn insertSlice(self: *Self, n: usize, items: SliceConst) !void { |
| 97 | 100 | try self.ensureCapacity(self.len + items.len); |
| 98 | 101 | self.len += items.len; |
| 99 | 102 | |
| ... | ... | @@ -141,7 +144,7 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type { |
| 141 | 144 | return self.swapRemove(i); |
| 142 | 145 | } |
| 143 | 146 | |
| 144 | | pub fn appendSlice(self: *Self, items: []align(A) const T) !void { |
| 147 | pub fn appendSlice(self: *Self, items: SliceConst) !void { |
| 145 | 148 | try self.ensureCapacity(self.len + items.len); |
| 146 | 149 | mem.copy(T, self.items[self.len..], items); |
| 147 | 150 | self.len += items.len; |