| ... | ... | @@ -128,7 +128,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 128 | 128 | pub fn toOwnedSliceSentinel(self: *Self, comptime sentinel: T) Allocator.Error!SentinelSlice(sentinel) { |
| 129 | 129 | // This addition can never overflow because `self.items` can never occupy the whole address space |
| 130 | 130 | try self.ensureTotalCapacityPrecise(self.items.len + 1); |
| 131 | | self.appendAssumeCapacity(sentinel); |
| 131 | self.appendReserved(sentinel); |
| 132 | 132 | const result = try self.toOwnedSlice(); |
| 133 | 133 | return result[0 .. result.len - 1 :sentinel]; |
| 134 | 134 | } |
| ... | ... | @@ -136,7 +136,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 136 | 136 | /// Creates a copy of this ArrayList, using the same allocator. |
| 137 | 137 | pub fn clone(self: Self) Allocator.Error!Self { |
| 138 | 138 | var cloned = try Self.initCapacity(self.allocator, self.capacity); |
| 139 | | cloned.appendSliceAssumeCapacity(self.items); |
| 139 | cloned.appendSliceReserved(self.items); |
| 140 | 140 | return cloned; |
| 141 | 141 | } |
| 142 | 142 | |
| ... | ... | @@ -150,13 +150,16 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 150 | 150 | dst[0] = item; |
| 151 | 151 | } |
| 152 | 152 | |
| 153 | /// Deprecated. To be removed after 0.14.0 is tagged. |
| 154 | pub const insertAssumeCapacity = insertReserved; |
| 155 | |
| 153 | 156 | /// Insert `item` at index `i`. Moves `list[i .. list.len]` to higher indices to make room. |
| 154 | 157 | /// If `i` is equal to the length of the list this operation is |
| 155 | | /// equivalent to appendAssumeCapacity. |
| 158 | /// equivalent to `appendReserved`. |
| 156 | 159 | /// This operation is O(N). |
| 157 | 160 | /// Asserts that there is enough capacity for the new item. |
| 158 | 161 | /// Asserts that the index is in bounds or equal to the length. |
| 159 | | pub fn insertAssumeCapacity(self: *Self, i: usize, item: T) void { |
| 162 | pub fn insertReserved(self: *Self, i: usize, item: T) void { |
| 160 | 163 | assert(self.items.len < self.capacity); |
| 161 | 164 | self.items.len += 1; |
| 162 | 165 | |
| ... | ... | @@ -176,7 +179,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 176 | 179 | const new_len = try addOrOom(self.items.len, count); |
| 177 | 180 | |
| 178 | 181 | if (self.capacity >= new_len) |
| 179 | | return addManyAtAssumeCapacity(self, index, count); |
| 182 | return addManyAtReserved(self, index, count); |
| 180 | 183 | |
| 181 | 184 | // Here we avoid copying allocated but unused bytes by |
| 182 | 185 | // attempting a resize in place, and falling back to allocating |
| ... | ... | @@ -187,7 +190,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 187 | 190 | const old_memory = self.allocatedSlice(); |
| 188 | 191 | if (self.allocator.resize(old_memory, new_capacity)) { |
| 189 | 192 | self.capacity = new_capacity; |
| 190 | | return addManyAtAssumeCapacity(self, index, count); |
| 193 | return addManyAtReserved(self, index, count); |
| 191 | 194 | } |
| 192 | 195 | |
| 193 | 196 | // Make a new allocation, avoiding `ensureTotalCapacity` in order |
| ... | ... | @@ -204,6 +207,9 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 204 | 207 | return new_memory[index..][0..count]; |
| 205 | 208 | } |
| 206 | 209 | |
| 210 | /// Deprecated. To be removed after 0.14.0 is tagged. |
| 211 | pub const addManyAtAssumeCapacity = addManyAtReserved; |
| 212 | |
| 207 | 213 | /// Add `count` new elements at position `index`, which have |
| 208 | 214 | /// `undefined` values. Returns a slice pointing to the newly allocated |
| 209 | 215 | /// elements, which becomes invalid after various `ArrayList` |
| ... | ... | @@ -212,7 +218,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 212 | 218 | /// Invalidates pre-existing pointers to elements at and after `index`, but |
| 213 | 219 | /// does not invalidate any before that. |
| 214 | 220 | /// Asserts that the index is in bounds or equal to the length. |
| 215 | | pub fn addManyAtAssumeCapacity(self: *Self, index: usize, count: usize) []T { |
| 221 | pub fn addManyAtReserved(self: *Self, index: usize, count: usize) []T { |
| 216 | 222 | const new_len = self.items.len + count; |
| 217 | 223 | assert(self.capacity >= new_len); |
| 218 | 224 | const to_move = self.items[index..]; |
| ... | ... | @@ -247,13 +253,16 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 247 | 253 | return unmanaged.replaceRange(self.allocator, start, len, new_items); |
| 248 | 254 | } |
| 249 | 255 | |
| 256 | /// Deprecated. To be removed after 0.14.0 is tagged. |
| 257 | pub const replaceRangeAssumeCapacity = replaceRangeReserved; |
| 258 | |
| 250 | 259 | /// Grows or shrinks the list as necessary. |
| 251 | 260 | /// Never invalidates element pointers. |
| 252 | 261 | /// Asserts the capacity is enough for additional items. |
| 253 | | pub fn replaceRangeAssumeCapacity(self: *Self, start: usize, len: usize, new_items: []const T) void { |
| 262 | pub fn replaceRangeReserved(self: *Self, start: usize, len: usize, new_items: []const T) void { |
| 254 | 263 | var unmanaged = self.moveToUnmanaged(); |
| 255 | 264 | defer self.* = unmanaged.toManaged(self.allocator); |
| 256 | | return unmanaged.replaceRangeAssumeCapacity(start, len, new_items); |
| 265 | return unmanaged.replaceRangeReserved(start, len, new_items); |
| 257 | 266 | } |
| 258 | 267 | |
| 259 | 268 | /// Extends the list by 1 element. Allocates more memory as necessary. |
| ... | ... | @@ -263,11 +272,14 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 263 | 272 | new_item_ptr.* = item; |
| 264 | 273 | } |
| 265 | 274 | |
| 275 | /// Deprecated. To be removed after 0.14.0 is tagged. |
| 276 | pub const appendAssumeCapacity = appendReserved; |
| 277 | |
| 266 | 278 | /// Extends the list by 1 element. |
| 267 | 279 | /// Never invalidates element pointers. |
| 268 | 280 | /// Asserts that the list can hold one additional item. |
| 269 | | pub fn appendAssumeCapacity(self: *Self, item: T) void { |
| 270 | | const new_item_ptr = self.addOneAssumeCapacity(); |
| 281 | pub fn appendReserved(self: *Self, item: T) void { |
| 282 | const new_item_ptr = self.addOneReserved(); |
| 271 | 283 | new_item_ptr.* = item; |
| 272 | 284 | } |
| 273 | 285 | |
| ... | ... | @@ -280,7 +292,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 280 | 292 | /// Asserts that the list is not empty. |
| 281 | 293 | pub fn orderedRemove(self: *Self, i: usize) T { |
| 282 | 294 | const old_item = self.items[i]; |
| 283 | | self.replaceRangeAssumeCapacity(i, 1, &.{}); |
| 295 | self.replaceRangeReserved(i, 1, &.{}); |
| 284 | 296 | return old_item; |
| 285 | 297 | } |
| 286 | 298 | |
| ... | ... | @@ -303,13 +315,16 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 303 | 315 | /// Invalidates element pointers if additional memory is needed. |
| 304 | 316 | pub fn appendSlice(self: *Self, items: []const T) Allocator.Error!void { |
| 305 | 317 | try self.ensureUnusedCapacity(items.len); |
| 306 | | self.appendSliceAssumeCapacity(items); |
| 318 | self.appendSliceReserved(items); |
| 307 | 319 | } |
| 308 | 320 | |
| 321 | /// Deprecated. To be removed after 0.14.0 is tagged. |
| 322 | pub const appendSliceAssumeCapacity = appendSliceReserved; |
| 323 | |
| 309 | 324 | /// Append the slice of items to the list. |
| 310 | 325 | /// Never invalidates element pointers. |
| 311 | 326 | /// Asserts that the list can hold the additional items. |
| 312 | | pub fn appendSliceAssumeCapacity(self: *Self, items: []const T) void { |
| 327 | pub fn appendSliceReserved(self: *Self, items: []const T) void { |
| 313 | 328 | const old_len = self.items.len; |
| 314 | 329 | const new_len = old_len + items.len; |
| 315 | 330 | assert(new_len <= self.capacity); |
| ... | ... | @@ -323,16 +338,19 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 323 | 338 | /// Invalidates element pointers if additional memory is needed. |
| 324 | 339 | pub fn appendUnalignedSlice(self: *Self, items: []align(1) const T) Allocator.Error!void { |
| 325 | 340 | try self.ensureUnusedCapacity(items.len); |
| 326 | | self.appendUnalignedSliceAssumeCapacity(items); |
| 341 | self.appendUnalignedSliceReserved(items); |
| 327 | 342 | } |
| 328 | 343 | |
| 344 | /// Deprecated. To be removed after 0.14.0 is tagged. |
| 345 | pub const appendUnalignedSliceAssumeCapacity = appendUnalignedSliceReserved; |
| 346 | |
| 329 | 347 | /// Append the slice of items to the list. |
| 330 | 348 | /// Never invalidates element pointers. |
| 331 | 349 | /// This function is only needed when calling |
| 332 | | /// `appendSliceAssumeCapacity` instead would be a compile error due to the |
| 350 | /// `appendSliceReserved` instead would be a compile error due to the |
| 333 | 351 | /// alignment of the `items` parameter. |
| 334 | 352 | /// Asserts that the list can hold the additional items. |
| 335 | | pub fn appendUnalignedSliceAssumeCapacity(self: *Self, items: []align(1) const T) void { |
| 353 | pub fn appendUnalignedSliceReserved(self: *Self, items: []align(1) const T) void { |
| 336 | 354 | const old_len = self.items.len; |
| 337 | 355 | const new_len = old_len + items.len; |
| 338 | 356 | assert(new_len <= self.capacity); |
| ... | ... | @@ -373,7 +391,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 373 | 391 | if (m.len > available_capacity) |
| 374 | 392 | return error.OutOfMemory; |
| 375 | 393 | |
| 376 | | self.appendSliceAssumeCapacity(m); |
| 394 | self.appendSliceReserved(m); |
| 377 | 395 | return m.len; |
| 378 | 396 | } |
| 379 | 397 | |
| ... | ... | @@ -388,12 +406,15 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 388 | 406 | @memset(self.items[old_len..self.items.len], value); |
| 389 | 407 | } |
| 390 | 408 | |
| 409 | /// Deprecated. To be removed after 0.14.0 is tagged. |
| 410 | pub const appendNTimesAssumeCapacity = appendNTimesReserved; |
| 411 | |
| 391 | 412 | /// Append a value to the list `n` times. |
| 392 | 413 | /// Never invalidates element pointers. |
| 393 | 414 | /// The function is inline so that a comptime-known `value` parameter will |
| 394 | 415 | /// have a more optimal memset codegen in case it has a repeated byte pattern. |
| 395 | 416 | /// Asserts that the list can hold the additional items. |
| 396 | | pub inline fn appendNTimesAssumeCapacity(self: *Self, value: T, n: usize) void { |
| 417 | pub inline fn appendNTimesReserved(self: *Self, value: T, n: usize) void { |
| 397 | 418 | const new_len = self.items.len + n; |
| 398 | 419 | assert(new_len <= self.capacity); |
| 399 | 420 | @memset(self.items.ptr[self.items.len..new_len], value); |
| ... | ... | @@ -499,14 +520,17 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 499 | 520 | // This can never overflow because `self.items` can never occupy the whole address space |
| 500 | 521 | const newlen = self.items.len + 1; |
| 501 | 522 | try self.ensureTotalCapacity(newlen); |
| 502 | | return self.addOneAssumeCapacity(); |
| 523 | return self.addOneReserved(); |
| 503 | 524 | } |
| 504 | 525 | |
| 526 | /// Deprecated. To be removed after 0.14.0 is tagged. |
| 527 | pub const addOneAssumeCapacity = addOneReserved; |
| 528 | |
| 505 | 529 | /// Increase length by 1, returning pointer to the new item. |
| 506 | 530 | /// The returned pointer becomes invalid when the list is resized. |
| 507 | 531 | /// Never invalidates element pointers. |
| 508 | 532 | /// Asserts that the list can hold one additional item. |
| 509 | | pub fn addOneAssumeCapacity(self: *Self) *T { |
| 533 | pub fn addOneReserved(self: *Self) *T { |
| 510 | 534 | assert(self.items.len < self.capacity); |
| 511 | 535 | self.items.len += 1; |
| 512 | 536 | return &self.items[self.items.len - 1]; |
| ... | ... | @@ -522,12 +546,15 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 522 | 546 | return self.items[prev_len..][0..n]; |
| 523 | 547 | } |
| 524 | 548 | |
| 549 | /// Deprecated. To be removed after 0.14.0 is tagged. |
| 550 | pub const addManyAsArrayAssumeCapacity = addManyAsArrayReserved; |
| 551 | |
| 525 | 552 | /// Resize the array, adding `n` new elements, which have `undefined` values. |
| 526 | 553 | /// The return value is an array pointing to the newly allocated elements. |
| 527 | 554 | /// Never invalidates element pointers. |
| 528 | 555 | /// The returned pointer becomes invalid when the list is resized. |
| 529 | 556 | /// Asserts that the list can hold the additional items. |
| 530 | | pub fn addManyAsArrayAssumeCapacity(self: *Self, comptime n: usize) *[n]T { |
| 557 | pub fn addManyAsArrayReserved(self: *Self, comptime n: usize) *[n]T { |
| 531 | 558 | assert(self.items.len + n <= self.capacity); |
| 532 | 559 | const prev_len = self.items.len; |
| 533 | 560 | self.items.len += n; |
| ... | ... | @@ -544,12 +571,15 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 544 | 571 | return self.items[prev_len..][0..n]; |
| 545 | 572 | } |
| 546 | 573 | |
| 574 | /// Deprecated. To be removed after 0.14.0 is tagged. |
| 575 | pub const addManyAsSliceAssumeCapacity = addManyAsSliceReserved; |
| 576 | |
| 547 | 577 | /// Resize the array, adding `n` new elements, which have `undefined` values. |
| 548 | 578 | /// The return value is a slice pointing to the newly allocated elements. |
| 549 | 579 | /// Never invalidates element pointers. |
| 550 | 580 | /// The returned pointer becomes invalid when the list is resized. |
| 551 | 581 | /// Asserts that the list can hold the additional items. |
| 552 | | pub fn addManyAsSliceAssumeCapacity(self: *Self, n: usize) []T { |
| 582 | pub fn addManyAsSliceReserved(self: *Self, n: usize) []T { |
| 553 | 583 | assert(self.items.len + n <= self.capacity); |
| 554 | 584 | const prev_len = self.items.len; |
| 555 | 585 | self.items.len += n; |
| ... | ... | @@ -725,7 +755,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 725 | 755 | pub fn toOwnedSliceSentinel(self: *Self, allocator: Allocator, comptime sentinel: T) Allocator.Error!SentinelSlice(sentinel) { |
| 726 | 756 | // This addition can never overflow because `self.items` can never occupy the whole address space |
| 727 | 757 | try self.ensureTotalCapacityPrecise(allocator, self.items.len + 1); |
| 728 | | self.appendAssumeCapacity(sentinel); |
| 758 | self.appendReserved(sentinel); |
| 729 | 759 | const result = try self.toOwnedSlice(allocator); |
| 730 | 760 | return result[0 .. result.len - 1 :sentinel]; |
| 731 | 761 | } |
| ... | ... | @@ -733,7 +763,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 733 | 763 | /// Creates a copy of this ArrayList. |
| 734 | 764 | pub fn clone(self: Self, allocator: Allocator) Allocator.Error!Self { |
| 735 | 765 | var cloned = try Self.initCapacity(allocator, self.capacity); |
| 736 | | cloned.appendSliceAssumeCapacity(self.items); |
| 766 | cloned.appendSliceReserved(self.items); |
| 737 | 767 | return cloned; |
| 738 | 768 | } |
| 739 | 769 | |
| ... | ... | @@ -747,12 +777,15 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 747 | 777 | dst[0] = item; |
| 748 | 778 | } |
| 749 | 779 | |
| 780 | /// Deprecated. To be removed after 0.14.0 is tagged. |
| 781 | pub const insertAssumeCapacity = insertReserved; |
| 782 | |
| 750 | 783 | /// Insert `item` at index `i`. Moves `list[i .. list.len]` to higher indices to make room. |
| 751 | 784 | /// If in` is equal to the length of the list this operation is equivalent to append. |
| 752 | 785 | /// This operation is O(N). |
| 753 | 786 | /// Asserts that the list has capacity for one additional item. |
| 754 | 787 | /// Asserts that the index is in bounds or equal to the length. |
| 755 | | pub fn insertAssumeCapacity(self: *Self, i: usize, item: T) void { |
| 788 | pub fn insertReserved(self: *Self, i: usize, item: T) void { |
| 756 | 789 | assert(self.items.len < self.capacity); |
| 757 | 790 | self.items.len += 1; |
| 758 | 791 | |
| ... | ... | @@ -779,6 +812,9 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 779 | 812 | return managed.addManyAt(index, count); |
| 780 | 813 | } |
| 781 | 814 | |
| 815 | /// Deprecated. To be removed after 0.14.0 is tagged. |
| 816 | pub const addManyAtAssumeCapacity = addManyAtReserved; |
| 817 | |
| 782 | 818 | /// Add `count` new elements at position `index`, which have |
| 783 | 819 | /// `undefined` values. Returns a slice pointing to the newly allocated |
| 784 | 820 | /// elements, which becomes invalid after various `ArrayList` |
| ... | ... | @@ -787,7 +823,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 787 | 823 | /// does not invalidate any before that. |
| 788 | 824 | /// Asserts that the list has capacity for the additional items. |
| 789 | 825 | /// Asserts that the index is in bounds or equal to the length. |
| 790 | | pub fn addManyAtAssumeCapacity(self: *Self, index: usize, count: usize) []T { |
| 826 | pub fn addManyAtReserved(self: *Self, index: usize, count: usize) []T { |
| 791 | 827 | const new_len = self.items.len + count; |
| 792 | 828 | assert(self.capacity >= new_len); |
| 793 | 829 | const to_move = self.items[index..]; |
| ... | ... | @@ -836,14 +872,17 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 836 | 872 | @memcpy(range[0..first.len], first); |
| 837 | 873 | try self.insertSlice(allocator, after_range, rest); |
| 838 | 874 | } else { |
| 839 | | self.replaceRangeAssumeCapacity(start, len, new_items); |
| 875 | self.replaceRangeReserved(start, len, new_items); |
| 840 | 876 | } |
| 841 | 877 | } |
| 842 | 878 | |
| 879 | /// Deprecated. To be removed after 0.14.0 is tagged. |
| 880 | pub const replaceRangeAssumeCapacity = replaceRangeReserved; |
| 881 | |
| 843 | 882 | /// Grows or shrinks the list as necessary. |
| 844 | 883 | /// Never invalidates element pointers. |
| 845 | 884 | /// Asserts the capacity is enough for additional items. |
| 846 | | pub fn replaceRangeAssumeCapacity(self: *Self, start: usize, len: usize, new_items: []const T) void { |
| 885 | pub fn replaceRangeReserved(self: *Self, start: usize, len: usize, new_items: []const T) void { |
| 847 | 886 | const after_range = start + len; |
| 848 | 887 | const range = self.items[start..after_range]; |
| 849 | 888 | |
| ... | ... | @@ -853,7 +892,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 853 | 892 | const first = new_items[0..range.len]; |
| 854 | 893 | const rest = new_items[range.len..]; |
| 855 | 894 | @memcpy(range[0..first.len], first); |
| 856 | | const dst = self.addManyAtAssumeCapacity(after_range, rest.len); |
| 895 | const dst = self.addManyAtReserved(after_range, rest.len); |
| 857 | 896 | @memcpy(dst, rest); |
| 858 | 897 | } else { |
| 859 | 898 | const extra = range.len - new_items.len; |
| ... | ... | @@ -875,11 +914,14 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 875 | 914 | new_item_ptr.* = item; |
| 876 | 915 | } |
| 877 | 916 | |
| 917 | /// Deprecated. To be removed after 0.14.0 is tagged. |
| 918 | pub const appendAssumeCapacity = appendReserved; |
| 919 | |
| 878 | 920 | /// Extend the list by 1 element. |
| 879 | 921 | /// Never invalidates element pointers. |
| 880 | 922 | /// Asserts that the list can hold one additional item. |
| 881 | | pub fn appendAssumeCapacity(self: *Self, item: T) void { |
| 882 | | const new_item_ptr = self.addOneAssumeCapacity(); |
| 923 | pub fn appendReserved(self: *Self, item: T) void { |
| 924 | const new_item_ptr = self.addOneReserved(); |
| 883 | 925 | new_item_ptr.* = item; |
| 884 | 926 | } |
| 885 | 927 | |
| ... | ... | @@ -890,7 +932,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 890 | 932 | /// Asserts that the index is in bounds. |
| 891 | 933 | pub fn orderedRemove(self: *Self, i: usize) T { |
| 892 | 934 | const old_item = self.items[i]; |
| 893 | | self.replaceRangeAssumeCapacity(i, 1, &.{}); |
| 935 | self.replaceRangeReserved(i, 1, &.{}); |
| 894 | 936 | return old_item; |
| 895 | 937 | } |
| 896 | 938 | |
| ... | ... | @@ -913,12 +955,15 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 913 | 955 | /// Invalidates element pointers if additional memory is needed. |
| 914 | 956 | pub fn appendSlice(self: *Self, allocator: Allocator, items: []const T) Allocator.Error!void { |
| 915 | 957 | try self.ensureUnusedCapacity(allocator, items.len); |
| 916 | | self.appendSliceAssumeCapacity(items); |
| 958 | self.appendSliceReserved(items); |
| 917 | 959 | } |
| 918 | 960 | |
| 961 | /// Deprecated. To be removed after 0.14.0 is tagged. |
| 962 | pub const appendSliceAssumeCapacity = appendSliceReserved; |
| 963 | |
| 919 | 964 | /// Append the slice of items to the list. |
| 920 | 965 | /// Asserts that the list can hold the additional items. |
| 921 | | pub fn appendSliceAssumeCapacity(self: *Self, items: []const T) void { |
| 966 | pub fn appendSliceReserved(self: *Self, items: []const T) void { |
| 922 | 967 | const old_len = self.items.len; |
| 923 | 968 | const new_len = old_len + items.len; |
| 924 | 969 | assert(new_len <= self.capacity); |
| ... | ... | @@ -932,14 +977,17 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 932 | 977 | /// Invalidates element pointers if additional memory is needed. |
| 933 | 978 | pub fn appendUnalignedSlice(self: *Self, allocator: Allocator, items: []align(1) const T) Allocator.Error!void { |
| 934 | 979 | try self.ensureUnusedCapacity(allocator, items.len); |
| 935 | | self.appendUnalignedSliceAssumeCapacity(items); |
| 980 | self.appendUnalignedSliceReserved(items); |
| 936 | 981 | } |
| 937 | 982 | |
| 983 | /// Deprecated. To be removed after 0.14.0 is tagged. |
| 984 | pub const appendUnalignedSliceAssumeCapacity = appendUnalignedSliceReserved; |
| 985 | |
| 938 | 986 | /// Append an unaligned slice of items to the list. |
| 939 | | /// Only call this function if a call to `appendSliceAssumeCapacity` |
| 987 | /// Only call this function if a call to `appendSliceReserved` |
| 940 | 988 | /// instead would be a compile error. |
| 941 | 989 | /// Asserts that the list can hold the additional items. |
| 942 | | pub fn appendUnalignedSliceAssumeCapacity(self: *Self, items: []align(1) const T) void { |
| 990 | pub fn appendUnalignedSliceReserved(self: *Self, items: []align(1) const T) void { |
| 943 | 991 | const old_len = self.items.len; |
| 944 | 992 | const new_len = old_len + items.len; |
| 945 | 993 | assert(new_len <= self.capacity); |
| ... | ... | @@ -986,7 +1034,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 986 | 1034 | if (m.len > available_capacity) |
| 987 | 1035 | return error.OutOfMemory; |
| 988 | 1036 | |
| 989 | | self.appendSliceAssumeCapacity(m); |
| 1037 | self.appendSliceReserved(m); |
| 990 | 1038 | return m.len; |
| 991 | 1039 | } |
| 992 | 1040 | |
| ... | ... | @@ -1006,7 +1054,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 1006 | 1054 | /// The function is inline so that a comptime-known `value` parameter will |
| 1007 | 1055 | /// have better memset codegen in case it has a repeated byte pattern. |
| 1008 | 1056 | /// Asserts that the list can hold the additional items. |
| 1009 | | pub inline fn appendNTimesAssumeCapacity(self: *Self, value: T, n: usize) void { |
| 1057 | pub inline fn appendNTimesReserved(self: *Self, value: T, n: usize) void { |
| 1010 | 1058 | const new_len = self.items.len + n; |
| 1011 | 1059 | assert(new_len <= self.capacity); |
| 1012 | 1060 | @memset(self.items.ptr[self.items.len..new_len], value); |
| ... | ... | @@ -1135,14 +1183,17 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 1135 | 1183 | // This can never overflow because `self.items` can never occupy the whole address space |
| 1136 | 1184 | const newlen = self.items.len + 1; |
| 1137 | 1185 | try self.ensureTotalCapacity(allocator, newlen); |
| 1138 | | return self.addOneAssumeCapacity(); |
| 1186 | return self.addOneReserved(); |
| 1139 | 1187 | } |
| 1140 | 1188 | |
| 1189 | /// Deprecated. To be removed after 0.14.0 is tagged. |
| 1190 | pub const addOneAssumeCapacity = addOneReserved; |
| 1191 | |
| 1141 | 1192 | /// Increase length by 1, returning pointer to the new item. |
| 1142 | 1193 | /// Never invalidates element pointers. |
| 1143 | 1194 | /// The returned element pointer becomes invalid when the list is resized. |
| 1144 | 1195 | /// Asserts that the list can hold one additional item. |
| 1145 | | pub fn addOneAssumeCapacity(self: *Self) *T { |
| 1196 | pub fn addOneReserved(self: *Self) *T { |
| 1146 | 1197 | assert(self.items.len < self.capacity); |
| 1147 | 1198 | |
| 1148 | 1199 | self.items.len += 1; |
| ... | ... | @@ -1158,12 +1209,15 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 1158 | 1209 | return self.items[prev_len..][0..n]; |
| 1159 | 1210 | } |
| 1160 | 1211 | |
| 1212 | /// Deprecated. To be removed after 0.14.0 is tagged. |
| 1213 | pub const addManyAsArrayAssumeCapacity = addManyAsArrayReserved; |
| 1214 | |
| 1161 | 1215 | /// Resize the array, adding `n` new elements, which have `undefined` values. |
| 1162 | 1216 | /// The return value is an array pointing to the newly allocated elements. |
| 1163 | 1217 | /// Never invalidates element pointers. |
| 1164 | 1218 | /// The returned pointer becomes invalid when the list is resized. |
| 1165 | 1219 | /// Asserts that the list can hold the additional items. |
| 1166 | | pub fn addManyAsArrayAssumeCapacity(self: *Self, comptime n: usize) *[n]T { |
| 1220 | pub fn addManyAsArrayReserved(self: *Self, comptime n: usize) *[n]T { |
| 1167 | 1221 | assert(self.items.len + n <= self.capacity); |
| 1168 | 1222 | const prev_len = self.items.len; |
| 1169 | 1223 | self.items.len += n; |
| ... | ... | @@ -1180,12 +1234,15 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 1180 | 1234 | return self.items[prev_len..][0..n]; |
| 1181 | 1235 | } |
| 1182 | 1236 | |
| 1237 | /// Deprecated. To be removed after 0.14.0 is tagged. |
| 1238 | pub const addManyAsSliceAssumeCapacity = addManyAsSliceReserved; |
| 1239 | |
| 1183 | 1240 | /// Resize the array, adding `n` new elements, which have `undefined` values. |
| 1184 | 1241 | /// The return value is a slice pointing to the newly allocated elements. |
| 1185 | 1242 | /// Never invalidates element pointers. |
| 1186 | 1243 | /// The returned pointer becomes invalid when the list is resized. |
| 1187 | 1244 | /// Asserts that the list can hold the additional items. |
| 1188 | | pub fn addManyAsSliceAssumeCapacity(self: *Self, n: usize) []T { |
| 1245 | pub fn addManyAsSliceReserved(self: *Self, n: usize) []T { |
| 1189 | 1246 | assert(self.items.len + n <= self.capacity); |
| 1190 | 1247 | const prev_len = self.items.len; |
| 1191 | 1248 | self.items.len += n; |
| ... | ... | @@ -1731,7 +1788,7 @@ test "ArrayList.replaceRange" { |
| 1731 | 1788 | } |
| 1732 | 1789 | } |
| 1733 | 1790 | |
| 1734 | | test "ArrayList.replaceRangeAssumeCapacity" { |
| 1791 | test "ArrayList.replaceRangeReserved" { |
| 1735 | 1792 | const a = testing.allocator; |
| 1736 | 1793 | |
| 1737 | 1794 | { |
| ... | ... | @@ -1739,7 +1796,7 @@ test "ArrayList.replaceRangeAssumeCapacity" { |
| 1739 | 1796 | defer list.deinit(); |
| 1740 | 1797 | try list.appendSlice(&[_]i32{ 1, 2, 3, 4, 5 }); |
| 1741 | 1798 | |
| 1742 | | list.replaceRangeAssumeCapacity(1, 0, &[_]i32{ 0, 0, 0 }); |
| 1799 | list.replaceRangeReserved(1, 0, &[_]i32{ 0, 0, 0 }); |
| 1743 | 1800 | |
| 1744 | 1801 | try testing.expectEqualSlices(i32, &[_]i32{ 1, 0, 0, 0, 2, 3, 4, 5 }, list.items); |
| 1745 | 1802 | } |
| ... | ... | @@ -1748,7 +1805,7 @@ test "ArrayList.replaceRangeAssumeCapacity" { |
| 1748 | 1805 | defer list.deinit(); |
| 1749 | 1806 | try list.appendSlice(&[_]i32{ 1, 2, 3, 4, 5 }); |
| 1750 | 1807 | |
| 1751 | | list.replaceRangeAssumeCapacity(1, 1, &[_]i32{ 0, 0, 0 }); |
| 1808 | list.replaceRangeReserved(1, 1, &[_]i32{ 0, 0, 0 }); |
| 1752 | 1809 | |
| 1753 | 1810 | try testing.expectEqualSlices( |
| 1754 | 1811 | i32, |
| ... | ... | @@ -1761,7 +1818,7 @@ test "ArrayList.replaceRangeAssumeCapacity" { |
| 1761 | 1818 | defer list.deinit(); |
| 1762 | 1819 | try list.appendSlice(&[_]i32{ 1, 2, 3, 4, 5 }); |
| 1763 | 1820 | |
| 1764 | | list.replaceRangeAssumeCapacity(1, 2, &[_]i32{ 0, 0, 0 }); |
| 1821 | list.replaceRangeReserved(1, 2, &[_]i32{ 0, 0, 0 }); |
| 1765 | 1822 | |
| 1766 | 1823 | try testing.expectEqualSlices(i32, &[_]i32{ 1, 0, 0, 0, 4, 5 }, list.items); |
| 1767 | 1824 | } |
| ... | ... | @@ -1770,7 +1827,7 @@ test "ArrayList.replaceRangeAssumeCapacity" { |
| 1770 | 1827 | defer list.deinit(); |
| 1771 | 1828 | try list.appendSlice(&[_]i32{ 1, 2, 3, 4, 5 }); |
| 1772 | 1829 | |
| 1773 | | list.replaceRangeAssumeCapacity(1, 3, &[_]i32{ 0, 0, 0 }); |
| 1830 | list.replaceRangeReserved(1, 3, &[_]i32{ 0, 0, 0 }); |
| 1774 | 1831 | |
| 1775 | 1832 | try testing.expectEqualSlices(i32, &[_]i32{ 1, 0, 0, 0, 5 }, list.items); |
| 1776 | 1833 | } |
| ... | ... | @@ -1779,7 +1836,7 @@ test "ArrayList.replaceRangeAssumeCapacity" { |
| 1779 | 1836 | defer list.deinit(); |
| 1780 | 1837 | try list.appendSlice(&[_]i32{ 1, 2, 3, 4, 5 }); |
| 1781 | 1838 | |
| 1782 | | list.replaceRangeAssumeCapacity(1, 4, &[_]i32{ 0, 0, 0 }); |
| 1839 | list.replaceRangeReserved(1, 4, &[_]i32{ 0, 0, 0 }); |
| 1783 | 1840 | |
| 1784 | 1841 | try testing.expectEqualSlices(i32, &[_]i32{ 1, 0, 0, 0 }, list.items); |
| 1785 | 1842 | } |
| ... | ... | @@ -1839,7 +1896,7 @@ test "ArrayListUnmanaged.replaceRange" { |
| 1839 | 1896 | } |
| 1840 | 1897 | } |
| 1841 | 1898 | |
| 1842 | | test "ArrayListUnmanaged.replaceRangeAssumeCapacity" { |
| 1899 | test "ArrayListUnmanaged.replaceRangeReserved" { |
| 1843 | 1900 | const a = testing.allocator; |
| 1844 | 1901 | |
| 1845 | 1902 | { |
| ... | ... | @@ -1847,7 +1904,7 @@ test "ArrayListUnmanaged.replaceRangeAssumeCapacity" { |
| 1847 | 1904 | defer list.deinit(a); |
| 1848 | 1905 | try list.appendSlice(a, &[_]i32{ 1, 2, 3, 4, 5 }); |
| 1849 | 1906 | |
| 1850 | | list.replaceRangeAssumeCapacity(1, 0, &[_]i32{ 0, 0, 0 }); |
| 1907 | list.replaceRangeReserved(1, 0, &[_]i32{ 0, 0, 0 }); |
| 1851 | 1908 | |
| 1852 | 1909 | try testing.expectEqualSlices(i32, &[_]i32{ 1, 0, 0, 0, 2, 3, 4, 5 }, list.items); |
| 1853 | 1910 | } |
| ... | ... | @@ -1856,7 +1913,7 @@ test "ArrayListUnmanaged.replaceRangeAssumeCapacity" { |
| 1856 | 1913 | defer list.deinit(a); |
| 1857 | 1914 | try list.appendSlice(a, &[_]i32{ 1, 2, 3, 4, 5 }); |
| 1858 | 1915 | |
| 1859 | | list.replaceRangeAssumeCapacity(1, 1, &[_]i32{ 0, 0, 0 }); |
| 1916 | list.replaceRangeReserved(1, 1, &[_]i32{ 0, 0, 0 }); |
| 1860 | 1917 | |
| 1861 | 1918 | try testing.expectEqualSlices( |
| 1862 | 1919 | i32, |
| ... | ... | @@ -1869,7 +1926,7 @@ test "ArrayListUnmanaged.replaceRangeAssumeCapacity" { |
| 1869 | 1926 | defer list.deinit(a); |
| 1870 | 1927 | try list.appendSlice(a, &[_]i32{ 1, 2, 3, 4, 5 }); |
| 1871 | 1928 | |
| 1872 | | list.replaceRangeAssumeCapacity(1, 2, &[_]i32{ 0, 0, 0 }); |
| 1929 | list.replaceRangeReserved(1, 2, &[_]i32{ 0, 0, 0 }); |
| 1873 | 1930 | |
| 1874 | 1931 | try testing.expectEqualSlices(i32, &[_]i32{ 1, 0, 0, 0, 4, 5 }, list.items); |
| 1875 | 1932 | } |
| ... | ... | @@ -1878,7 +1935,7 @@ test "ArrayListUnmanaged.replaceRangeAssumeCapacity" { |
| 1878 | 1935 | defer list.deinit(a); |
| 1879 | 1936 | try list.appendSlice(a, &[_]i32{ 1, 2, 3, 4, 5 }); |
| 1880 | 1937 | |
| 1881 | | list.replaceRangeAssumeCapacity(1, 3, &[_]i32{ 0, 0, 0 }); |
| 1938 | list.replaceRangeReserved(1, 3, &[_]i32{ 0, 0, 0 }); |
| 1882 | 1939 | |
| 1883 | 1940 | try testing.expectEqualSlices(i32, &[_]i32{ 1, 0, 0, 0, 5 }, list.items); |
| 1884 | 1941 | } |
| ... | ... | @@ -1887,7 +1944,7 @@ test "ArrayListUnmanaged.replaceRangeAssumeCapacity" { |
| 1887 | 1944 | defer list.deinit(a); |
| 1888 | 1945 | try list.appendSlice(a, &[_]i32{ 1, 2, 3, 4, 5 }); |
| 1889 | 1946 | |
| 1890 | | list.replaceRangeAssumeCapacity(1, 4, &[_]i32{ 0, 0, 0 }); |
| 1947 | list.replaceRangeReserved(1, 4, &[_]i32{ 0, 0, 0 }); |
| 1891 | 1948 | |
| 1892 | 1949 | try testing.expectEqualSlices(i32, &[_]i32{ 1, 0, 0, 0 }, list.items); |
| 1893 | 1950 | } |
| ... | ... | @@ -2021,7 +2078,7 @@ test "addManyAsArray" { |
| 2021 | 2078 | |
| 2022 | 2079 | (try list.addManyAsArray(4)).* = "aoeu".*; |
| 2023 | 2080 | try list.ensureTotalCapacity(8); |
| 2024 | | list.addManyAsArrayAssumeCapacity(4).* = "asdf".*; |
| 2081 | list.addManyAsArrayReserved(4).* = "asdf".*; |
| 2025 | 2082 | |
| 2026 | 2083 | try testing.expectEqualSlices(u8, list.items, "aoeuasdf"); |
| 2027 | 2084 | } |
| ... | ... | @@ -2031,7 +2088,7 @@ test "addManyAsArray" { |
| 2031 | 2088 | |
| 2032 | 2089 | (try list.addManyAsArray(a, 4)).* = "aoeu".*; |
| 2033 | 2090 | try list.ensureTotalCapacity(a, 8); |
| 2034 | | list.addManyAsArrayAssumeCapacity(4).* = "asdf".*; |
| 2091 | list.addManyAsArrayReserved(4).* = "asdf".*; |
| 2035 | 2092 | |
| 2036 | 2093 | try testing.expectEqualSlices(u8, list.items, "aoeuasdf"); |
| 2037 | 2094 | } |