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