| ... | @@ -50,7 +50,6 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { | ... | @@ -50,7 +50,6 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 50 | allocator: *Allocator, | 50 | allocator: *Allocator, |
| 51 | | 51 | |
| 52 | pub const Slice = if (alignment) |a| ([]align(a) T) else []T; | 52 | pub const Slice = if (alignment) |a| ([]align(a) T) else []T; |
| 53 | pub const SliceConst = if (alignment) |a| ([]align(a) const T) else []const T; | | |
| 54 | | 53 | |
| 55 | /// Deinitialize with `deinit` or use `toOwnedSlice`. | 54 | /// Deinitialize with `deinit` or use `toOwnedSlice`. |
| 56 | pub fn init(allocator: *Allocator) Self { | 55 | pub fn init(allocator: *Allocator) Self { |
| ... | @@ -141,7 +140,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { | ... | @@ -141,7 +140,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 141 | | 140 | |
| 142 | /// Insert slice `items` at index `i` by moving `list[i .. list.len]` to make room. | 141 | /// Insert slice `items` at index `i` by moving `list[i .. list.len]` to make room. |
| 143 | /// This operation is O(N). | 142 | /// This operation is O(N). |
| 144 | pub fn insertSlice(self: *Self, i: usize, items: SliceConst) !void { | 143 | pub fn insertSlice(self: *Self, i: usize, items: []const T) !void { |
| 145 | try self.ensureCapacity(self.items.len + items.len); | 144 | try self.ensureCapacity(self.items.len + items.len); |
| 146 | self.items.len += items.len; | 145 | self.items.len += items.len; |
| 147 | | 146 | |
| ... | @@ -153,7 +152,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { | ... | @@ -153,7 +152,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 153 | /// Grows list if `len < new_items.len`. | 152 | /// Grows list if `len < new_items.len`. |
| 154 | /// Shrinks list if `len > new_items.len`. | 153 | /// Shrinks list if `len > new_items.len`. |
| 155 | /// Invalidates pointers if this ArrayList is resized. | 154 | /// Invalidates pointers if this ArrayList is resized. |
| 156 | pub fn replaceRange(self: *Self, start: usize, len: usize, new_items: SliceConst) !void { | 155 | pub fn replaceRange(self: *Self, start: usize, len: usize, new_items: []const T) !void { |
| 157 | const after_range = start + len; | 156 | const after_range = start + len; |
| 158 | const range = self.items[start..after_range]; | 157 | const range = self.items[start..after_range]; |
| 159 | | 158 | |
| ... | @@ -220,14 +219,14 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { | ... | @@ -220,14 +219,14 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 220 | | 219 | |
| 221 | /// Append the slice of items to the list. Allocates more | 220 | /// Append the slice of items to the list. Allocates more |
| 222 | /// memory as necessary. | 221 | /// memory as necessary. |
| 223 | pub fn appendSlice(self: *Self, items: SliceConst) !void { | 222 | pub fn appendSlice(self: *Self, items: []const T) !void { |
| 224 | try self.ensureCapacity(self.items.len + items.len); | 223 | try self.ensureCapacity(self.items.len + items.len); |
| 225 | self.appendSliceAssumeCapacity(items); | 224 | self.appendSliceAssumeCapacity(items); |
| 226 | } | 225 | } |
| 227 | | 226 | |
| 228 | /// Append the slice of items to the list, asserting the capacity is already | 227 | /// Append the slice of items to the list, asserting the capacity is already |
| 229 | /// enough to store the new items. **Does not** invalidate pointers. | 228 | /// enough to store the new items. **Does not** invalidate pointers. |
| 230 | pub fn appendSliceAssumeCapacity(self: *Self, items: SliceConst) void { | 229 | pub fn appendSliceAssumeCapacity(self: *Self, items: []const T) void { |
| 231 | const oldlen = self.items.len; | 230 | const oldlen = self.items.len; |
| 232 | const newlen = self.items.len + items.len; | 231 | const newlen = self.items.len + items.len; |
| 233 | self.items.len = newlen; | 232 | self.items.len = newlen; |
| ... | @@ -429,7 +428,6 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ | ... | @@ -429,7 +428,6 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 429 | capacity: usize = 0, | 428 | capacity: usize = 0, |
| 430 | | 429 | |
| 431 | pub const Slice = if (alignment) |a| ([]align(a) T) else []T; | 430 | pub const Slice = if (alignment) |a| ([]align(a) T) else []T; |
| 432 | pub const SliceConst = if (alignment) |a| ([]align(a) const T) else []const T; | | |
| 433 | | 431 | |
| 434 | /// Initialize with capacity to hold at least num elements. | 432 | /// Initialize with capacity to hold at least num elements. |
| 435 | /// Deinitialize with `deinit` or use `toOwnedSlice`. | 433 | /// Deinitialize with `deinit` or use `toOwnedSlice`. |
| ... | @@ -483,7 +481,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ | ... | @@ -483,7 +481,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 483 | /// Insert slice `items` at index `i`. Moves `list[i .. list.len]` to | 481 | /// Insert slice `items` at index `i`. Moves `list[i .. list.len]` to |
| 484 | /// higher indicices make room. | 482 | /// higher indicices make room. |
| 485 | /// This operation is O(N). | 483 | /// This operation is O(N). |
| 486 | pub fn insertSlice(self: *Self, allocator: *Allocator, i: usize, items: SliceConst) !void { | 484 | pub fn insertSlice(self: *Self, allocator: *Allocator, i: usize, items: []const T) !void { |
| 487 | try self.ensureCapacity(allocator, self.items.len + items.len); | 485 | try self.ensureCapacity(allocator, self.items.len + items.len); |
| 488 | self.items.len += items.len; | 486 | self.items.len += items.len; |
| 489 | | 487 | |
| ... | @@ -495,7 +493,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ | ... | @@ -495,7 +493,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 495 | /// Grows list if `len < new_items.len`. | 493 | /// Grows list if `len < new_items.len`. |
| 496 | /// Shrinks list if `len > new_items.len` | 494 | /// Shrinks list if `len > new_items.len` |
| 497 | /// Invalidates pointers if this ArrayList is resized. | 495 | /// Invalidates pointers if this ArrayList is resized. |
| 498 | pub fn replaceRange(self: *Self, allocator: *Allocator, start: usize, len: usize, new_items: SliceConst) !void { | 496 | pub fn replaceRange(self: *Self, allocator: *Allocator, start: usize, len: usize, new_items: []const T) !void { |
| 499 | var managed = self.toManaged(allocator); | 497 | var managed = self.toManaged(allocator); |
| 500 | try managed.replaceRange(start, len, new_items); | 498 | try managed.replaceRange(start, len, new_items); |
| 501 | self.* = managed.toUnmanaged(); | 499 | self.* = managed.toUnmanaged(); |
| ... | @@ -543,14 +541,14 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ | ... | @@ -543,14 +541,14 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 543 | | 541 | |
| 544 | /// Append the slice of items to the list. Allocates more | 542 | /// Append the slice of items to the list. Allocates more |
| 545 | /// memory as necessary. | 543 | /// memory as necessary. |
| 546 | pub fn appendSlice(self: *Self, allocator: *Allocator, items: SliceConst) !void { | 544 | pub fn appendSlice(self: *Self, allocator: *Allocator, items: []const T) !void { |
| 547 | try self.ensureCapacity(allocator, self.items.len + items.len); | 545 | try self.ensureCapacity(allocator, self.items.len + items.len); |
| 548 | self.appendSliceAssumeCapacity(items); | 546 | self.appendSliceAssumeCapacity(items); |
| 549 | } | 547 | } |
| 550 | | 548 | |
| 551 | /// Append the slice of items to the list, asserting the capacity is enough | 549 | /// Append the slice of items to the list, asserting the capacity is enough |
| 552 | /// to store the new items. | 550 | /// to store the new items. |
| 553 | pub fn appendSliceAssumeCapacity(self: *Self, items: SliceConst) void { | 551 | pub fn appendSliceAssumeCapacity(self: *Self, items: []const T) void { |
| 554 | const oldlen = self.items.len; | 552 | const oldlen = self.items.len; |
| 555 | const newlen = self.items.len + items.len; | 553 | const newlen = self.items.len + items.len; |
| 556 | | 554 | |
| ... | @@ -1128,15 +1126,31 @@ test "std.ArrayList/ArrayListUnmanaged: ArrayList(T) of struct T" { | ... | @@ -1128,15 +1126,31 @@ test "std.ArrayList/ArrayListUnmanaged: ArrayList(T) of struct T" { |
| 1128 | } | 1126 | } |
| 1129 | } | 1127 | } |
| 1130 | | 1128 | |
| 1131 | test "std.ArrayList(u8) implements writer" { | 1129 | test "std.ArrayList(u8)/ArrayListAligned implements writer" { |
| 1132 | var buffer = ArrayList(u8).init(std.testing.allocator); | 1130 | const a = testing.allocator; |
| 1133 | defer buffer.deinit(); | 1131 | |
| | 1132 | { |
| | 1133 | var buffer = ArrayList(u8).init(a); |
| | 1134 | defer buffer.deinit(); |
| | 1135 | |
| | 1136 | const x: i32 = 42; |
| | 1137 | const y: i32 = 1234; |
| | 1138 | try buffer.writer().print("x: {}\ny: {}\n", .{ x, y }); |
| 1134 | | 1139 | |
| 1135 | const x: i32 = 42; | 1140 | testing.expectEqualSlices(u8, "x: 42\ny: 1234\n", buffer.items); |
| 1136 | const y: i32 = 1234; | 1141 | } |
| 1137 | try buffer.writer().print("x: {}\ny: {}\n", .{ x, y }); | 1142 | { |
| | 1143 | var list = ArrayListAligned(u8, 2).init(a); |
| | 1144 | defer list.deinit(); |
| 1138 | | 1145 | |
| 1139 | testing.expectEqualSlices(u8, "x: 42\ny: 1234\n", buffer.items); | 1146 | const writer = list.writer(); |
| | 1147 | try writer.writeAll("a"); |
| | 1148 | try writer.writeAll("bc"); |
| | 1149 | try writer.writeAll("d"); |
| | 1150 | try writer.writeAll("efg"); |
| | 1151 | |
| | 1152 | testing.expectEqualSlices(u8, list.items, "abcdefg"); |
| | 1153 | } |
| 1140 | } | 1154 | } |
| 1141 | | 1155 | |
| 1142 | test "std.ArrayList/ArrayListUnmanaged.shrink still sets length on error.OutOfMemory" { | 1156 | test "std.ArrayList/ArrayListUnmanaged.shrink still sets length on error.OutOfMemory" { |
| ... | @@ -1167,18 +1181,6 @@ test "std.ArrayList/ArrayListUnmanaged.shrink still sets length on error.OutOfMe | ... | @@ -1167,18 +1181,6 @@ test "std.ArrayList/ArrayListUnmanaged.shrink still sets length on error.OutOfMe |
| 1167 | } | 1181 | } |
| 1168 | } | 1182 | } |
| 1169 | | 1183 | |
| 1170 | test "std.ArrayList.writer" { | | |
| 1171 | var list = ArrayList(u8).init(std.testing.allocator); | | |
| 1172 | defer list.deinit(); | | |
| 1173 | | | |
| 1174 | const writer = list.writer(); | | |
| 1175 | try writer.writeAll("a"); | | |
| 1176 | try writer.writeAll("bc"); | | |
| 1177 | try writer.writeAll("d"); | | |
| 1178 | try writer.writeAll("efg"); | | |
| 1179 | testing.expectEqualSlices(u8, list.items, "abcdefg"); | | |
| 1180 | } | | |
| 1181 | | | |
| 1182 | test "std.ArrayList/ArrayListUnmanaged.addManyAsArray" { | 1184 | test "std.ArrayList/ArrayListUnmanaged.addManyAsArray" { |
| 1183 | const a = std.testing.allocator; | 1185 | const a = std.testing.allocator; |
| 1184 | { | 1186 | { |
| ... | @@ -1226,3 +1228,27 @@ test "std.ArrayList/ArrayListUnmanaged.toOwnedSliceSentinel" { | ... | @@ -1226,3 +1228,27 @@ test "std.ArrayList/ArrayListUnmanaged.toOwnedSliceSentinel" { |
| 1226 | testing.expectEqualStrings(result, mem.spanZ(result.ptr)); | 1228 | testing.expectEqualStrings(result, mem.spanZ(result.ptr)); |
| 1227 | } | 1229 | } |
| 1228 | } | 1230 | } |
| | 1231 | |
| | 1232 | test "ArrayListAligned/ArrayListAlignedUnmanaged accepts unaligned slices" { |
| | 1233 | const a = testing.allocator; |
| | 1234 | { |
| | 1235 | var list = std.ArrayListAligned(u8, 8).init(a); |
| | 1236 | defer list.deinit(); |
| | 1237 | |
| | 1238 | try list.appendSlice(&.{ 0, 1, 2, 3 }); |
| | 1239 | try list.insertSlice(2, &.{ 4, 5, 6, 7 }); |
| | 1240 | try list.replaceRange(1, 3, &.{ 8, 9 }); |
| | 1241 | |
| | 1242 | testing.expectEqualSlices(u8, list.items, &.{ 0, 8, 9, 6, 7, 2, 3 }); |
| | 1243 | } |
| | 1244 | { |
| | 1245 | var list = std.ArrayListAlignedUnmanaged(u8, 8){}; |
| | 1246 | defer list.deinit(a); |
| | 1247 | |
| | 1248 | try list.appendSlice(a, &.{ 0, 1, 2, 3 }); |
| | 1249 | try list.insertSlice(a, 2, &.{ 4, 5, 6, 7 }); |
| | 1250 | try list.replaceRange(a, 1, 3, &.{ 8, 9 }); |
| | 1251 | |
| | 1252 | testing.expectEqualSlices(u8, list.items, &.{ 0, 8, 9, 6, 7, 2, 3 }); |
| | 1253 | } |
| | 1254 | } |