| ... | @@ -6,20 +6,23 @@ const mem = std.mem; | ... | @@ -6,20 +6,23 @@ const mem = std.mem; |
| 6 | const Allocator = mem.Allocator; | 6 | const Allocator = mem.Allocator; |
| 7 | | 7 | |
| 8 | pub fn ArrayList(comptime T: type) type { | 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 | return struct { | 13 | return struct { |
| 14 | const Self = @This(); | 14 | const Self = @This(); |
| 15 | | 15 | |
| 16 | /// Use toSlice instead of slicing this directly, because if you don't | 16 | /// Use toSlice instead of slicing this directly, because if you don't |
| 17 | /// specify the end position of the slice, this will potentially give | 17 | /// specify the end position of the slice, this will potentially give |
| 18 | /// you uninitialized memory. | 18 | /// you uninitialized memory. |
| 19 | items: []align(A) T, | 19 | items: Slice, |
| 20 | len: usize, | 20 | len: usize, |
| 21 | allocator: *Allocator, | 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 | /// Deinitialize with `deinit` or use `toOwnedSlice`. | 26 | /// Deinitialize with `deinit` or use `toOwnedSlice`. |
| 24 | pub fn init(allocator: *Allocator) Self { | 27 | pub fn init(allocator: *Allocator) Self { |
| 25 | return Self{ | 28 | return Self{ |
| ... | @@ -33,11 +36,11 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type { | ... | @@ -33,11 +36,11 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type { |
| 33 | self.allocator.free(self.items); | 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 | return self.items[0..self.len]; | 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 | return self.items[0..self.len]; | 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,7 +72,7 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type { |
| 69 | /// ArrayList takes ownership of the passed in slice. The slice must have been | 72 | /// ArrayList takes ownership of the passed in slice. The slice must have been |
| 70 | /// allocated with `allocator`. | 73 | /// allocated with `allocator`. |
| 71 | /// Deinitialize with `deinit` or use `toOwnedSlice`. | 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 | return Self{ | 76 | return Self{ |
| 74 | .items = slice, | 77 | .items = slice, |
| 75 | .len = slice.len, | 78 | .len = slice.len, |
| ... | @@ -78,7 +81,7 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type { | ... | @@ -78,7 +81,7 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type { |
| 78 | } | 81 | } |
| 79 | | 82 | |
| 80 | /// The caller owns the returned memory. ArrayList becomes empty. | 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 | const allocator = self.allocator; | 85 | const allocator = self.allocator; |
| 83 | const result = allocator.shrink(self.items, self.len); | 86 | const result = allocator.shrink(self.items, self.len); |
| 84 | self.* = init(allocator); | 87 | self.* = init(allocator); |
| ... | @@ -93,7 +96,7 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type { | ... | @@ -93,7 +96,7 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type { |
| 93 | self.items[n] = item; | 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 | try self.ensureCapacity(self.len + items.len); | 100 | try self.ensureCapacity(self.len + items.len); |
| 98 | self.len += items.len; | 101 | self.len += items.len; |
| 99 | | 102 | |
| ... | @@ -141,7 +144,7 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type { | ... | @@ -141,7 +144,7 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type { |
| 141 | return self.swapRemove(i); | 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 | try self.ensureCapacity(self.len + items.len); | 148 | try self.ensureCapacity(self.len + items.len); |
| 146 | mem.copy(T, self.items[self.len..], items); | 149 | mem.copy(T, self.items[self.len..], items); |
| 147 | self.len += items.len; | 150 | self.len += items.len; |