| ... | @@ -40,6 +40,14 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type { | ... | @@ -40,6 +40,14 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type { |
| 40 | .allocator = allocator, | 40 | .allocator = allocator, |
| 41 | }; | 41 | }; |
| 42 | } | 42 | } |
| | 43 | |
| | 44 | /// Initialize with capacity to hold at least num elements. |
| | 45 | /// Deinitialize with `deinit` or use `toOwnedSlice`. |
| | 46 | pub fn initCapacity(allocator: *Allocator, num: usize) !Self { |
| | 47 | var self = Self.init(allocator); |
| | 48 | try self.ensureCapacity(num); |
| | 49 | return self; |
| | 50 | } |
| 43 | | 51 | |
| 44 | /// Release all allocated memory. | 52 | /// Release all allocated memory. |
| 45 | pub fn deinit(self: Self) void { | 53 | pub fn deinit(self: Self) void { |
| ... | @@ -271,6 +279,15 @@ test "std.ArrayList.init" { | ... | @@ -271,6 +279,15 @@ test "std.ArrayList.init" { |
| 271 | testing.expect(list.capacity() == 0); | 279 | testing.expect(list.capacity() == 0); |
| 272 | } | 280 | } |
| 273 | | 281 | |
| | 282 | test "std.ArrayList.initCapacity" { |
| | 283 | var bytes: [1024]u8 = undefined; |
| | 284 | const allocator = &std.heap.FixedBufferAllocator.init(bytes[0..]).allocator; |
| | 285 | var list = try ArrayList(i8).initCapacity(allocator, 200); |
| | 286 | defer list.deinit(); |
| | 287 | testing.expect(list.count() == 0); |
| | 288 | testing.expect(list.capacity() >= 200); |
| | 289 | } |
| | 290 | |
| 274 | test "std.ArrayList.basic" { | 291 | test "std.ArrayList.basic" { |
| 275 | var bytes: [1024]u8 = undefined; | 292 | var bytes: [1024]u8 = undefined; |
| 276 | const allocator = &std.heap.FixedBufferAllocator.init(bytes[0..]).allocator; | 293 | const allocator = &std.heap.FixedBufferAllocator.init(bytes[0..]).allocator; |