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 {...@@ -147,7 +147,7 @@ pub fn MultiArrayList(comptime S: type) type {
147147
148 /// Extend the list by 1 element. Allocates more memory as necessary.148 /// Extend the list by 1 element. Allocates more memory as necessary.
149 pub fn append(self: *Self, gpa: *Allocator, elem: S) !void {149 pub fn append(self: *Self, gpa: *Allocator, elem: S) !void {
150 try self.ensureCapacity(gpa, self.len + 1);150 try self.ensureUnusedCapacity(gpa, 1);
151 self.appendAssumeCapacity(elem);151 self.appendAssumeCapacity(elem);
152 }152 }
153153
...@@ -162,7 +162,7 @@ pub fn MultiArrayList(comptime S: type) type {...@@ -162,7 +162,7 @@ pub fn MultiArrayList(comptime S: type) type {
162 /// Adjust the list's length to `new_len`.162 /// Adjust the list's length to `new_len`.
163 /// Does not initialize added items, if any.163 /// Does not initialize added items, if any.
164 pub fn resize(self: *Self, gpa: *Allocator, new_len: usize) !void {164 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);
166 self.len = new_len;166 self.len = new_len;
167 }167 }
168168
...@@ -224,10 +224,13 @@ pub fn MultiArrayList(comptime S: type) type {...@@ -224,10 +224,13 @@ pub fn MultiArrayList(comptime S: type) type {
224 self.len = new_len;224 self.len = new_len;
225 }225 }
226226
227 /// Deprecated: call `ensureUnusedCapacity` or `ensureTotalCapacity`.
228 pub const ensureCapacity = ensureTotalCapacity;
229
227 /// Modify the array so that it can hold at least `new_capacity` items.230 /// Modify the array so that it can hold at least `new_capacity` items.
228 /// Implements super-linear growth to achieve amortized O(1) append operations.231 /// Implements super-linear growth to achieve amortized O(1) append operations.
229 /// Invalidates pointers if additional memory is needed.232 /// 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 {
231 var better_capacity = self.capacity;234 var better_capacity = self.capacity;
232 if (better_capacity >= new_capacity) return;235 if (better_capacity >= new_capacity) return;
233236
...@@ -239,6 +242,12 @@ pub fn MultiArrayList(comptime S: type) type {...@@ -239,6 +242,12 @@ pub fn MultiArrayList(comptime S: type) type {
239 return self.setCapacity(gpa, better_capacity);242 return self.setCapacity(gpa, better_capacity);
240 }243 }
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
242 /// Modify the array so that it can hold exactly `new_capacity` items.251 /// Modify the array so that it can hold exactly `new_capacity` items.
243 /// Invalidates pointers if additional memory is needed.252 /// Invalidates pointers if additional memory is needed.
244 /// `new_capacity` must be greater or equal to `len`.253 /// `new_capacity` must be greater or equal to `len`.
...@@ -305,7 +314,7 @@ test "basic usage" {...@@ -305,7 +314,7 @@ test "basic usage" {
305314
306 testing.expectEqual(@as(usize, 0), list.items(.a).len);315 testing.expectEqual(@as(usize, 0), list.items(.a).len);
307316
308 try list.ensureCapacity(ally, 2);317 try list.ensureTotalCapacity(ally, 2);
309318
310 list.appendAssumeCapacity(.{319 list.appendAssumeCapacity(.{
311 .a = 1,320 .a = 1,
...@@ -382,7 +391,7 @@ test "regression test for @reduce bug" {...@@ -382,7 +391,7 @@ test "regression test for @reduce bug" {
382 }){};391 }){};
383 defer list.deinit(ally);392 defer list.deinit(ally);
384393
385 try list.ensureCapacity(ally, 20);394 try list.ensureTotalCapacity(ally, 20);
386395
387 try list.append(ally, .{ .tag = .keyword_const, .start = 0 });396 try list.append(ally, .{ .tag = .keyword_const, .start = 0 });
388 try list.append(ally, .{ .tag = .identifier, .start = 6 });397 try list.append(ally, .{ .tag = .identifier, .start = 6 });
...@@ -462,7 +471,7 @@ test "ensure capacity on empty list" {...@@ -462,7 +471,7 @@ test "ensure capacity on empty list" {
462 var list = MultiArrayList(Foo){};471 var list = MultiArrayList(Foo){};
463 defer list.deinit(ally);472 defer list.deinit(ally);
464473
465 try list.ensureCapacity(ally, 2);474 try list.ensureTotalCapacity(ally, 2);
466 list.appendAssumeCapacity(.{ .a = 1, .b = 2 });475 list.appendAssumeCapacity(.{ .a = 1, .b = 2 });
467 list.appendAssumeCapacity(.{ .a = 3, .b = 4 });476 list.appendAssumeCapacity(.{ .a = 3, .b = 4 });
468477
...@@ -477,7 +486,7 @@ test "ensure capacity on empty list" {...@@ -477,7 +486,7 @@ test "ensure capacity on empty list" {
477 testing.expectEqualSlices(u8, &[_]u8{ 6, 8 }, list.items(.b));486 testing.expectEqualSlices(u8, &[_]u8{ 6, 8 }, list.items(.b));
478487
479 list.len = 0;488 list.len = 0;
480 try list.ensureCapacity(ally, 16);489 try list.ensureTotalCapacity(ally, 16);
481490
482 list.appendAssumeCapacity(.{ .a = 9, .b = 10 });491 list.appendAssumeCapacity(.{ .a = 9, .b = 10 });
483 list.appendAssumeCapacity(.{ .a = 11, .b = 12 });492 list.appendAssumeCapacity(.{ .a = 11, .b = 12 });