authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-19 15:02:37-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-19 15:02:37-07:00
log22015c1b3bfcf816faf10ea0fc152c4686efb363
tree0e4995242081c594810a7ebc5122aaeae45b1eb4
parent27d4bea9a478e9c544329869d076b0e505d0e382

std.MultiArrayList: ensureUnusedCapacity/ensureTotalCapacity

Same as c8ae581fef6506a8234cdba1355ba7f0f449031a, but for MultiArrayList.

1 files changed, 16 insertions(+), 7 deletions(-)

lib/std/multi_array_list.zig+16-7
......@@ -147,7 +147,7 @@ pub fn MultiArrayList(comptime S: type) type {
147147
148148 /// Extend the list by 1 element. Allocates more memory as necessary.
149149 pub fn append(self: *Self, gpa: *Allocator, elem: S) !void {
150 try self.ensureCapacity(gpa, self.len + 1);
150 try self.ensureUnusedCapacity(gpa, 1);
151151 self.appendAssumeCapacity(elem);
152152 }
153153
......@@ -162,7 +162,7 @@ pub fn MultiArrayList(comptime S: type) type {
162162 /// Adjust the list's length to `new_len`.
163163 /// Does not initialize added items, if any.
164164 pub fn resize(self: *Self, gpa: *Allocator, new_len: usize) !void {
165 try self.ensureCapacity(gpa, new_len);
165 try self.ensureTotalCapacity(gpa, new_len);
166166 self.len = new_len;
167167 }
168168
......@@ -224,10 +224,13 @@ pub fn MultiArrayList(comptime S: type) type {
224224 self.len = new_len;
225225 }
226226
227 /// Deprecated: call `ensureUnusedCapacity` or `ensureTotalCapacity`.
228 pub const ensureCapacity = ensureTotalCapacity;
229
227230 /// Modify the array so that it can hold at least `new_capacity` items.
228231 /// Implements super-linear growth to achieve amortized O(1) append operations.
229232 /// Invalidates pointers if additional memory is needed.
230 pub fn ensureCapacity(self: *Self, gpa: *Allocator, new_capacity: usize) !void {
233 pub fn ensureTotalCapacity(self: *Self, gpa: *Allocator, new_capacity: usize) !void {
231234 var better_capacity = self.capacity;
232235 if (better_capacity >= new_capacity) return;
233236
......@@ -239,6 +242,12 @@ pub fn MultiArrayList(comptime S: type) type {
239242 return self.setCapacity(gpa, better_capacity);
240243 }
241244
245 /// Modify the array so that it can hold at least `additional_count` **more** items.
246 /// Invalidates pointers if additional memory is needed.
247 pub fn ensureUnusedCapacity(self: *Self, gpa: *Allocator, additional_count: usize) !void {
248 return self.ensureTotalCapacity(gpa, self.len + additional_count);
249 }
250
242251 /// Modify the array so that it can hold exactly `new_capacity` items.
243252 /// Invalidates pointers if additional memory is needed.
244253 /// `new_capacity` must be greater or equal to `len`.
......@@ -305,7 +314,7 @@ test "basic usage" {
305314
306315 testing.expectEqual(@as(usize, 0), list.items(.a).len);
307316
308 try list.ensureCapacity(ally, 2);
317 try list.ensureTotalCapacity(ally, 2);
309318
310319 list.appendAssumeCapacity(.{
311320 .a = 1,
......@@ -382,7 +391,7 @@ test "regression test for @reduce bug" {
382391 }){};
383392 defer list.deinit(ally);
384393
385 try list.ensureCapacity(ally, 20);
394 try list.ensureTotalCapacity(ally, 20);
386395
387396 try list.append(ally, .{ .tag = .keyword_const, .start = 0 });
388397 try list.append(ally, .{ .tag = .identifier, .start = 6 });
......@@ -462,7 +471,7 @@ test "ensure capacity on empty list" {
462471 var list = MultiArrayList(Foo){};
463472 defer list.deinit(ally);
464473
465 try list.ensureCapacity(ally, 2);
474 try list.ensureTotalCapacity(ally, 2);
466475 list.appendAssumeCapacity(.{ .a = 1, .b = 2 });
467476 list.appendAssumeCapacity(.{ .a = 3, .b = 4 });
468477
......@@ -477,7 +486,7 @@ test "ensure capacity on empty list" {
477486 testing.expectEqualSlices(u8, &[_]u8{ 6, 8 }, list.items(.b));
478487
479488 list.len = 0;
480 try list.ensureCapacity(ally, 16);
489 try list.ensureTotalCapacity(ally, 16);
481490
482491 list.appendAssumeCapacity(.{ .a = 9, .b = 10 });
483492 list.appendAssumeCapacity(.{ .a = 11, .b = 12 });