| ... | ... | @@ -63,7 +63,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 63 | 63 | /// Deinitialize with `deinit` or use `toOwnedSlice`. |
| 64 | 64 | pub fn initCapacity(allocator: Allocator, num: usize) Allocator.Error!Self { |
| 65 | 65 | var self = Self.init(allocator); |
| 66 | | try self.ensureTotalCapacityPrecise(num); |
| 66 | try self.reserveTotalPrecise(num); |
| 67 | 67 | return self; |
| 68 | 68 | } |
| 69 | 69 | |
| ... | ... | @@ -127,7 +127,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 127 | 127 | /// The caller owns the returned memory. Empties this ArrayList. |
| 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 | | try self.ensureTotalCapacityPrecise(self.items.len + 1); |
| 130 | try self.reserveTotalPrecise(self.items.len + 1); |
| 131 | 131 | self.appendReserved(sentinel); |
| 132 | 132 | const result = try self.toOwnedSlice(); |
| 133 | 133 | return result[0 .. result.len - 1 :sentinel]; |
| ... | ... | @@ -193,8 +193,8 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 193 | 193 | return addManyAtReserved(self, index, count); |
| 194 | 194 | } |
| 195 | 195 | |
| 196 | | // Make a new allocation, avoiding `ensureTotalCapacity` in order |
| 197 | | // to avoid extra memory copies. |
| 196 | // Make a new allocation, avoiding extra memory copies by avoiding |
| 197 | // `reserveTotal`. |
| 198 | 198 | const new_memory = try self.allocator.alignedAlloc(T, alignment, new_capacity); |
| 199 | 199 | const to_move = self.items[index..]; |
| 200 | 200 | @memcpy(new_memory[0..index], self.items[0..index]); |
| ... | ... | @@ -314,7 +314,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 314 | 314 | /// memory as necessary. |
| 315 | 315 | /// Invalidates element pointers if additional memory is needed. |
| 316 | 316 | pub fn appendSlice(self: *Self, items: []const T) Allocator.Error!void { |
| 317 | | try self.ensureUnusedCapacity(items.len); |
| 317 | try self.reserveUnused(items.len); |
| 318 | 318 | self.appendSliceReserved(items); |
| 319 | 319 | } |
| 320 | 320 | |
| ... | ... | @@ -337,7 +337,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 337 | 337 | /// `appendSlice` instead would be a compile error. |
| 338 | 338 | /// Invalidates element pointers if additional memory is needed. |
| 339 | 339 | pub fn appendUnalignedSlice(self: *Self, items: []align(1) const T) Allocator.Error!void { |
| 340 | | try self.ensureUnusedCapacity(items.len); |
| 340 | try self.reserveUnused(items.len); |
| 341 | 341 | self.appendUnalignedSliceReserved(items); |
| 342 | 342 | } |
| 343 | 343 | |
| ... | ... | @@ -425,7 +425,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 425 | 425 | /// Additional elements contain the value `undefined`. |
| 426 | 426 | /// Invalidates element pointers if additional memory is needed. |
| 427 | 427 | pub fn resize(self: *Self, new_len: usize) Allocator.Error!void { |
| 428 | | try self.ensureTotalCapacity(new_len); |
| 428 | try self.reserveTotal(new_len); |
| 429 | 429 | self.items.len = new_len; |
| 430 | 430 | } |
| 431 | 431 | |
| ... | ... | @@ -458,10 +458,13 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 458 | 458 | self.capacity = 0; |
| 459 | 459 | } |
| 460 | 460 | |
| 461 | /// Deprecated. To be removed after 0.14.0 is tagged. |
| 462 | pub const ensureTotalCapacity = reserveTotal; |
| 463 | |
| 461 | 464 | /// If the current capacity is less than `new_capacity`, this function will |
| 462 | 465 | /// modify the array so that it can hold at least `new_capacity` items. |
| 463 | 466 | /// Invalidates element pointers if additional memory is needed. |
| 464 | | pub fn ensureTotalCapacity(self: *Self, new_capacity: usize) Allocator.Error!void { |
| 467 | pub fn reserveTotal(self: *Self, new_capacity: usize) Allocator.Error!void { |
| 465 | 468 | if (@sizeOf(T) == 0) { |
| 466 | 469 | self.capacity = math.maxInt(usize); |
| 467 | 470 | return; |
| ... | ... | @@ -470,13 +473,16 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 470 | 473 | if (self.capacity >= new_capacity) return; |
| 471 | 474 | |
| 472 | 475 | const better_capacity = growCapacity(self.capacity, new_capacity); |
| 473 | | return self.ensureTotalCapacityPrecise(better_capacity); |
| 476 | return self.reserveTotalPrecise(better_capacity); |
| 474 | 477 | } |
| 475 | 478 | |
| 479 | /// Deprecated. To be removed after 0.14.0 is tagged. |
| 480 | pub const ensureTotalCapacityPrecise = reserveTotalPrecise; |
| 481 | |
| 476 | 482 | /// If the current capacity is less than `new_capacity`, this function will |
| 477 | 483 | /// modify the array so that it can hold exactly `new_capacity` items. |
| 478 | 484 | /// Invalidates element pointers if additional memory is needed. |
| 479 | | pub fn ensureTotalCapacityPrecise(self: *Self, new_capacity: usize) Allocator.Error!void { |
| 485 | pub fn reserveTotalPrecise(self: *Self, new_capacity: usize) Allocator.Error!void { |
| 480 | 486 | if (@sizeOf(T) == 0) { |
| 481 | 487 | self.capacity = math.maxInt(usize); |
| 482 | 488 | return; |
| ... | ... | @@ -501,10 +507,13 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 501 | 507 | } |
| 502 | 508 | } |
| 503 | 509 | |
| 510 | /// Deprecated. To be removed after 0.14.0 is tagged. |
| 511 | pub const ensureUnusedCapacity = reserveUnused; |
| 512 | |
| 504 | 513 | /// Modify the array so that it can hold at least `additional_count` **more** items. |
| 505 | 514 | /// Invalidates element pointers if additional memory is needed. |
| 506 | | pub fn ensureUnusedCapacity(self: *Self, additional_count: usize) Allocator.Error!void { |
| 507 | | return self.ensureTotalCapacity(try addOrOom(self.items.len, additional_count)); |
| 515 | pub fn reserveUnused(self: *Self, additional_count: usize) Allocator.Error!void { |
| 516 | return self.reserveTotal(try addOrOom(self.items.len, additional_count)); |
| 508 | 517 | } |
| 509 | 518 | |
| 510 | 519 | /// Increases the array's length to match the full capacity that is already allocated. |
| ... | ... | @@ -519,7 +528,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 519 | 528 | pub fn addOne(self: *Self) Allocator.Error!*T { |
| 520 | 529 | // This can never overflow because `self.items` can never occupy the whole address space |
| 521 | 530 | const newlen = self.items.len + 1; |
| 522 | | try self.ensureTotalCapacity(newlen); |
| 531 | try self.reserveTotal(newlen); |
| 523 | 532 | return self.addOneReserved(); |
| 524 | 533 | } |
| 525 | 534 | |
| ... | ... | @@ -687,7 +696,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 687 | 696 | /// Deinitialize with `deinit` or use `toOwnedSlice`. |
| 688 | 697 | pub fn initCapacity(allocator: Allocator, num: usize) Allocator.Error!Self { |
| 689 | 698 | var self = Self{}; |
| 690 | | try self.ensureTotalCapacityPrecise(allocator, num); |
| 699 | try self.reserveTotalPrecise(allocator, num); |
| 691 | 700 | return self; |
| 692 | 701 | } |
| 693 | 702 | |
| ... | ... | @@ -754,7 +763,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 754 | 763 | /// The caller owns the returned memory. ArrayList becomes empty. |
| 755 | 764 | pub fn toOwnedSliceSentinel(self: *Self, allocator: Allocator, comptime sentinel: T) Allocator.Error!SentinelSlice(sentinel) { |
| 756 | 765 | // This addition can never overflow because `self.items` can never occupy the whole address space |
| 757 | | try self.ensureTotalCapacityPrecise(allocator, self.items.len + 1); |
| 766 | try self.reserveTotalPrecise(allocator, self.items.len + 1); |
| 758 | 767 | self.appendReserved(sentinel); |
| 759 | 768 | const result = try self.toOwnedSlice(allocator); |
| 760 | 769 | return result[0 .. result.len - 1 :sentinel]; |
| ... | ... | @@ -954,7 +963,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 954 | 963 | /// memory as necessary. |
| 955 | 964 | /// Invalidates element pointers if additional memory is needed. |
| 956 | 965 | pub fn appendSlice(self: *Self, allocator: Allocator, items: []const T) Allocator.Error!void { |
| 957 | | try self.ensureUnusedCapacity(allocator, items.len); |
| 966 | try self.reserveUnused(allocator, items.len); |
| 958 | 967 | self.appendSliceReserved(items); |
| 959 | 968 | } |
| 960 | 969 | |
| ... | ... | @@ -976,7 +985,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 976 | 985 | /// be a compile error. |
| 977 | 986 | /// Invalidates element pointers if additional memory is needed. |
| 978 | 987 | pub fn appendUnalignedSlice(self: *Self, allocator: Allocator, items: []align(1) const T) Allocator.Error!void { |
| 979 | | try self.ensureUnusedCapacity(allocator, items.len); |
| 988 | try self.reserveUnused(allocator, items.len); |
| 980 | 989 | self.appendUnalignedSliceReserved(items); |
| 981 | 990 | } |
| 982 | 991 | |
| ... | ... | @@ -1049,6 +1058,9 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 1049 | 1058 | @memset(self.items[old_len..self.items.len], value); |
| 1050 | 1059 | } |
| 1051 | 1060 | |
| 1061 | /// Deprecated. To be removed after 0.14.0 is tagged. |
| 1062 | pub const appendNTimesAssumeCapacity = appendNTimesReserved; |
| 1063 | |
| 1052 | 1064 | /// Append a value to the list `n` times. |
| 1053 | 1065 | /// Never invalidates element pointers. |
| 1054 | 1066 | /// The function is inline so that a comptime-known `value` parameter will |
| ... | ... | @@ -1065,7 +1077,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 1065 | 1077 | /// Additional elements contain the value `undefined`. |
| 1066 | 1078 | /// Invalidates element pointers if additional memory is needed. |
| 1067 | 1079 | pub fn resize(self: *Self, allocator: Allocator, new_len: usize) Allocator.Error!void { |
| 1068 | | try self.ensureTotalCapacity(allocator, new_len); |
| 1080 | try self.reserveTotal(allocator, new_len); |
| 1069 | 1081 | self.items.len = new_len; |
| 1070 | 1082 | } |
| 1071 | 1083 | |
| ... | ... | @@ -1122,20 +1134,26 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 1122 | 1134 | self.capacity = 0; |
| 1123 | 1135 | } |
| 1124 | 1136 | |
| 1137 | /// Deprecated. To be removed after 0.14.0 is tagged. |
| 1138 | pub const ensureTotalCapacity = reserveTotal; |
| 1139 | |
| 1125 | 1140 | /// If the current capacity is less than `new_capacity`, this function will |
| 1126 | 1141 | /// modify the array so that it can hold at least `new_capacity` items. |
| 1127 | 1142 | /// Invalidates element pointers if additional memory is needed. |
| 1128 | | pub fn ensureTotalCapacity(self: *Self, allocator: Allocator, new_capacity: usize) Allocator.Error!void { |
| 1143 | pub fn reserveTotal(self: *Self, allocator: Allocator, new_capacity: usize) Allocator.Error!void { |
| 1129 | 1144 | if (self.capacity >= new_capacity) return; |
| 1130 | 1145 | |
| 1131 | 1146 | const better_capacity = growCapacity(self.capacity, new_capacity); |
| 1132 | | return self.ensureTotalCapacityPrecise(allocator, better_capacity); |
| 1147 | return self.reserveTotalPrecise(allocator, better_capacity); |
| 1133 | 1148 | } |
| 1134 | 1149 | |
| 1150 | /// Deprecated. To be removed after 0.14.0 is tagged. |
| 1151 | pub const ensureTotalCapacityPrecise = reserveTotalPrecise; |
| 1152 | |
| 1135 | 1153 | /// If the current capacity is less than `new_capacity`, this function will |
| 1136 | 1154 | /// modify the array so that it can hold exactly `new_capacity` items. |
| 1137 | 1155 | /// Invalidates element pointers if additional memory is needed. |
| 1138 | | pub fn ensureTotalCapacityPrecise(self: *Self, allocator: Allocator, new_capacity: usize) Allocator.Error!void { |
| 1156 | pub fn reserveTotalPrecise(self: *Self, allocator: Allocator, new_capacity: usize) Allocator.Error!void { |
| 1139 | 1157 | if (@sizeOf(T) == 0) { |
| 1140 | 1158 | self.capacity = math.maxInt(usize); |
| 1141 | 1159 | return; |
| ... | ... | @@ -1160,14 +1178,13 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 1160 | 1178 | } |
| 1161 | 1179 | } |
| 1162 | 1180 | |
| 1181 | /// Deprecated. To be removed after 0.14.0 is tagged. |
| 1182 | pub const ensureUnusedCapacity = reserveUnused; |
| 1183 | |
| 1163 | 1184 | /// Modify the array so that it can hold at least `additional_count` **more** items. |
| 1164 | 1185 | /// Invalidates element pointers if additional memory is needed. |
| 1165 | | pub fn ensureUnusedCapacity( |
| 1166 | | self: *Self, |
| 1167 | | allocator: Allocator, |
| 1168 | | additional_count: usize, |
| 1169 | | ) Allocator.Error!void { |
| 1170 | | return self.ensureTotalCapacity(allocator, try addOrOom(self.items.len, additional_count)); |
| 1186 | pub fn reserveUnused(self: *Self, allocator: Allocator, additional_count: usize) Allocator.Error!void { |
| 1187 | return self.reserveTotal(allocator, try addOrOom(self.items.len, additional_count)); |
| 1171 | 1188 | } |
| 1172 | 1189 | |
| 1173 | 1190 | /// Increases the array's length to match the full capacity that is already allocated. |
| ... | ... | @@ -1182,7 +1199,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 1182 | 1199 | pub fn addOne(self: *Self, allocator: Allocator) Allocator.Error!*T { |
| 1183 | 1200 | // This can never overflow because `self.items` can never occupy the whole address space |
| 1184 | 1201 | const newlen = self.items.len + 1; |
| 1185 | | try self.ensureTotalCapacity(allocator, newlen); |
| 1202 | try self.reserveTotal(allocator, newlen); |
| 1186 | 1203 | return self.addOneReserved(); |
| 1187 | 1204 | } |
| 1188 | 1205 | |
| ... | ... | @@ -2077,7 +2094,7 @@ test "addManyAsArray" { |
| 2077 | 2094 | defer list.deinit(); |
| 2078 | 2095 | |
| 2079 | 2096 | (try list.addManyAsArray(4)).* = "aoeu".*; |
| 2080 | | try list.ensureTotalCapacity(8); |
| 2097 | try list.reserveTotal(8); |
| 2081 | 2098 | list.addManyAsArrayReserved(4).* = "asdf".*; |
| 2082 | 2099 | |
| 2083 | 2100 | try testing.expectEqualSlices(u8, list.items, "aoeuasdf"); |
| ... | ... | @@ -2087,7 +2104,7 @@ test "addManyAsArray" { |
| 2087 | 2104 | defer list.deinit(a); |
| 2088 | 2105 | |
| 2089 | 2106 | (try list.addManyAsArray(a, 4)).* = "aoeu".*; |
| 2090 | | try list.ensureTotalCapacity(a, 8); |
| 2107 | try list.reserveTotal(a, 8); |
| 2091 | 2108 | list.addManyAsArrayReserved(4).* = "asdf".*; |
| 2092 | 2109 | |
| 2093 | 2110 | try testing.expectEqualSlices(u8, list.items, "aoeuasdf"); |
| ... | ... | @@ -2304,7 +2321,7 @@ test "return OutOfMemory when capacity would exceed maximum usize integer value" |
| 2304 | 2321 | try testing.expectError(error.OutOfMemory, list.addManyAsArray(a, 2)); |
| 2305 | 2322 | try testing.expectError(error.OutOfMemory, list.addManyAsSlice(a, 2)); |
| 2306 | 2323 | try testing.expectError(error.OutOfMemory, list.insertSlice(a, 0, items)); |
| 2307 | | try testing.expectError(error.OutOfMemory, list.ensureUnusedCapacity(a, 2)); |
| 2324 | try testing.expectError(error.OutOfMemory, list.reserveUnused(a, 2)); |
| 2308 | 2325 | } |
| 2309 | 2326 | |
| 2310 | 2327 | { |
| ... | ... | @@ -2322,7 +2339,7 @@ test "return OutOfMemory when capacity would exceed maximum usize integer value" |
| 2322 | 2339 | try testing.expectError(error.OutOfMemory, list.addManyAsArray(2)); |
| 2323 | 2340 | try testing.expectError(error.OutOfMemory, list.addManyAsSlice(2)); |
| 2324 | 2341 | try testing.expectError(error.OutOfMemory, list.insertSlice(0, items)); |
| 2325 | | try testing.expectError(error.OutOfMemory, list.ensureUnusedCapacity(2)); |
| 2342 | try testing.expectError(error.OutOfMemory, list.reserveUnused(2)); |
| 2326 | 2343 | } |
| 2327 | 2344 | } |
| 2328 | 2345 | |