| ... | @@ -50,19 +50,19 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { | ... | @@ -50,19 +50,19 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 50 | } | 50 | } |
| 51 | | 51 | |
| 52 | /// Deinitialize with `deinit` or use `toOwnedSlice`. | 52 | /// Deinitialize with `deinit` or use `toOwnedSlice`. |
| 53 | pub fn init(allocator: Allocator) Self { | 53 | pub fn init(gpa: Allocator) Self { |
| 54 | return Self{ | 54 | return Self{ |
| 55 | .items = &[_]T{}, | 55 | .items = &[_]T{}, |
| 56 | .capacity = 0, | 56 | .capacity = 0, |
| 57 | .allocator = allocator, | 57 | .allocator = gpa, |
| 58 | }; | 58 | }; |
| 59 | } | 59 | } |
| 60 | | 60 | |
| 61 | /// Initialize with capacity to hold `num` elements. | 61 | /// Initialize with capacity to hold `num` elements. |
| 62 | /// The resulting capacity will equal `num` exactly. | 62 | /// The resulting capacity will equal `num` exactly. |
| 63 | /// Deinitialize with `deinit` or use `toOwnedSlice`. | 63 | /// Deinitialize with `deinit` or use `toOwnedSlice`. |
| 64 | pub fn initCapacity(allocator: Allocator, num: usize) Allocator.Error!Self { | 64 | pub fn initCapacity(gpa: Allocator, num: usize) Allocator.Error!Self { |
| 65 | var self = Self.init(allocator); | 65 | var self = Self.init(gpa); |
| 66 | try self.ensureTotalCapacityPrecise(num); | 66 | try self.ensureTotalCapacityPrecise(num); |
| 67 | return self; | 67 | return self; |
| 68 | } | 68 | } |
| ... | @@ -75,24 +75,24 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { | ... | @@ -75,24 +75,24 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 75 | } | 75 | } |
| 76 | | 76 | |
| 77 | /// ArrayList takes ownership of the passed in slice. The slice must have been | 77 | /// ArrayList takes ownership of the passed in slice. The slice must have been |
| 78 | /// allocated with `allocator`. | 78 | /// allocated with `gpa`. |
| 79 | /// Deinitialize with `deinit` or use `toOwnedSlice`. | 79 | /// Deinitialize with `deinit` or use `toOwnedSlice`. |
| 80 | pub fn fromOwnedSlice(allocator: Allocator, slice: Slice) Self { | 80 | pub fn fromOwnedSlice(gpa: Allocator, slice: Slice) Self { |
| 81 | return Self{ | 81 | return Self{ |
| 82 | .items = slice, | 82 | .items = slice, |
| 83 | .capacity = slice.len, | 83 | .capacity = slice.len, |
| 84 | .allocator = allocator, | 84 | .allocator = gpa, |
| 85 | }; | 85 | }; |
| 86 | } | 86 | } |
| 87 | | 87 | |
| 88 | /// ArrayList takes ownership of the passed in slice. The slice must have been | 88 | /// ArrayList takes ownership of the passed in slice. The slice must have been |
| 89 | /// allocated with `allocator`. | 89 | /// allocated with `gpa`. |
| 90 | /// Deinitialize with `deinit` or use `toOwnedSlice`. | 90 | /// Deinitialize with `deinit` or use `toOwnedSlice`. |
| 91 | pub fn fromOwnedSliceSentinel(allocator: Allocator, comptime sentinel: T, slice: [:sentinel]T) Self { | 91 | pub fn fromOwnedSliceSentinel(gpa: Allocator, comptime sentinel: T, slice: [:sentinel]T) Self { |
| 92 | return Self{ | 92 | return Self{ |
| 93 | .items = slice, | 93 | .items = slice, |
| 94 | .capacity = slice.len + 1, | 94 | .capacity = slice.len + 1, |
| 95 | .allocator = allocator, | 95 | .allocator = gpa, |
| 96 | }; | 96 | }; |
| 97 | } | 97 | } |
| 98 | | 98 | |
| ... | @@ -646,9 +646,9 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ | ... | @@ -646,9 +646,9 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 646 | /// Initialize with capacity to hold `num` elements. | 646 | /// Initialize with capacity to hold `num` elements. |
| 647 | /// The resulting capacity will equal `num` exactly. | 647 | /// The resulting capacity will equal `num` exactly. |
| 648 | /// Deinitialize with `deinit` or use `toOwnedSlice`. | 648 | /// Deinitialize with `deinit` or use `toOwnedSlice`. |
| 649 | pub fn initCapacity(allocator: Allocator, num: usize) Allocator.Error!Self { | 649 | pub fn initCapacity(gpa: Allocator, num: usize) Allocator.Error!Self { |
| 650 | var self = Self{}; | 650 | var self = Self{}; |
| 651 | try self.ensureTotalCapacityPrecise(allocator, num); | 651 | try self.ensureTotalCapacityPrecise(gpa, num); |
| 652 | return self; | 652 | return self; |
| 653 | } | 653 | } |
| 654 | | 654 | |
| ... | @@ -664,19 +664,18 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ | ... | @@ -664,19 +664,18 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 664 | } | 664 | } |
| 665 | | 665 | |
| 666 | /// Release all allocated memory. | 666 | /// Release all allocated memory. |
| 667 | pub fn deinit(self: *Self, allocator: Allocator) void { | 667 | pub fn deinit(self: *Self, gpa: Allocator) void { |
| 668 | allocator.free(self.allocatedSlice()); | 668 | gpa.free(self.allocatedSlice()); |
| 669 | self.* = undefined; | 669 | self.* = undefined; |
| 670 | } | 670 | } |
| 671 | | 671 | |
| 672 | /// Convert this list into an analogous memory-managed one. | 672 | /// Convert this list into an analogous memory-managed one. |
| 673 | /// The returned list has ownership of the underlying memory. | 673 | /// The returned list has ownership of the underlying memory. |
| 674 | pub fn toManaged(self: *Self, allocator: Allocator) ArrayListAligned(T, alignment) { | 674 | pub fn toManaged(self: *Self, gpa: Allocator) ArrayListAligned(T, alignment) { |
| 675 | return .{ .items = self.items, .capacity = self.capacity, .allocator = allocator }; | 675 | return .{ .items = self.items, .capacity = self.capacity, .allocator = gpa }; |
| 676 | } | 676 | } |
| 677 | | 677 | |
| 678 | /// ArrayListUnmanaged takes ownership of the passed in slice. The slice must have been | 678 | /// ArrayListUnmanaged takes ownership of the passed in slice. |
| 679 | /// allocated with `allocator`. | | |
| 680 | /// Deinitialize with `deinit` or use `toOwnedSlice`. | 679 | /// Deinitialize with `deinit` or use `toOwnedSlice`. |
| 681 | pub fn fromOwnedSlice(slice: Slice) Self { | 680 | pub fn fromOwnedSlice(slice: Slice) Self { |
| 682 | return Self{ | 681 | return Self{ |
| ... | @@ -685,8 +684,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ | ... | @@ -685,8 +684,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 685 | }; | 684 | }; |
| 686 | } | 685 | } |
| 687 | | 686 | |
| 688 | /// ArrayListUnmanaged takes ownership of the passed in slice. The slice must have been | 687 | /// ArrayListUnmanaged takes ownership of the passed in slice. |
| 689 | /// allocated with `allocator`. | | |
| 690 | /// Deinitialize with `deinit` or use `toOwnedSlice`. | 688 | /// Deinitialize with `deinit` or use `toOwnedSlice`. |
| 691 | pub fn fromOwnedSliceSentinel(comptime sentinel: T, slice: [:sentinel]T) Self { | 689 | pub fn fromOwnedSliceSentinel(comptime sentinel: T, slice: [:sentinel]T) Self { |
| 692 | return Self{ | 690 | return Self{ |
| ... | @@ -697,31 +695,31 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ | ... | @@ -697,31 +695,31 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 697 | | 695 | |
| 698 | /// The caller owns the returned memory. Empties this ArrayList. | 696 | /// The caller owns the returned memory. Empties this ArrayList. |
| 699 | /// Its capacity is cleared, making deinit() safe but unnecessary to call. | 697 | /// Its capacity is cleared, making deinit() safe but unnecessary to call. |
| 700 | pub fn toOwnedSlice(self: *Self, allocator: Allocator) Allocator.Error!Slice { | 698 | pub fn toOwnedSlice(self: *Self, gpa: Allocator) Allocator.Error!Slice { |
| 701 | const old_memory = self.allocatedSlice(); | 699 | const old_memory = self.allocatedSlice(); |
| 702 | if (allocator.remap(old_memory, self.items.len)) |new_items| { | 700 | if (gpa.remap(old_memory, self.items.len)) |new_items| { |
| 703 | self.* = .empty; | 701 | self.* = .empty; |
| 704 | return new_items; | 702 | return new_items; |
| 705 | } | 703 | } |
| 706 | | 704 | |
| 707 | const new_memory = try allocator.alignedAlloc(T, alignment, self.items.len); | 705 | const new_memory = try gpa.alignedAlloc(T, alignment, self.items.len); |
| 708 | @memcpy(new_memory, self.items); | 706 | @memcpy(new_memory, self.items); |
| 709 | self.clearAndFree(allocator); | 707 | self.clearAndFree(gpa); |
| 710 | return new_memory; | 708 | return new_memory; |
| 711 | } | 709 | } |
| 712 | | 710 | |
| 713 | /// The caller owns the returned memory. ArrayList becomes empty. | 711 | /// The caller owns the returned memory. ArrayList becomes empty. |
| 714 | pub fn toOwnedSliceSentinel(self: *Self, allocator: Allocator, comptime sentinel: T) Allocator.Error!SentinelSlice(sentinel) { | 712 | pub fn toOwnedSliceSentinel(self: *Self, gpa: Allocator, comptime sentinel: T) Allocator.Error!SentinelSlice(sentinel) { |
| 715 | // This addition can never overflow because `self.items` can never occupy the whole address space | 713 | // This addition can never overflow because `self.items` can never occupy the whole address space |
| 716 | try self.ensureTotalCapacityPrecise(allocator, self.items.len + 1); | 714 | try self.ensureTotalCapacityPrecise(gpa, self.items.len + 1); |
| 717 | self.appendAssumeCapacity(sentinel); | 715 | self.appendAssumeCapacity(sentinel); |
| 718 | const result = try self.toOwnedSlice(allocator); | 716 | const result = try self.toOwnedSlice(gpa); |
| 719 | return result[0 .. result.len - 1 :sentinel]; | 717 | return result[0 .. result.len - 1 :sentinel]; |
| 720 | } | 718 | } |
| 721 | | 719 | |
| 722 | /// Creates a copy of this ArrayList. | 720 | /// Creates a copy of this ArrayList. |
| 723 | pub fn clone(self: Self, allocator: Allocator) Allocator.Error!Self { | 721 | pub fn clone(self: Self, gpa: Allocator) Allocator.Error!Self { |
| 724 | var cloned = try Self.initCapacity(allocator, self.capacity); | 722 | var cloned = try Self.initCapacity(gpa, self.capacity); |
| 725 | cloned.appendSliceAssumeCapacity(self.items); | 723 | cloned.appendSliceAssumeCapacity(self.items); |
| 726 | return cloned; | 724 | return cloned; |
| 727 | } | 725 | } |
| ... | @@ -731,8 +729,8 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ | ... | @@ -731,8 +729,8 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 731 | /// This operation is O(N). | 729 | /// This operation is O(N). |
| 732 | /// Invalidates element pointers if additional memory is needed. | 730 | /// Invalidates element pointers if additional memory is needed. |
| 733 | /// Asserts that the index is in bounds or equal to the length. | 731 | /// Asserts that the index is in bounds or equal to the length. |
| 734 | pub fn insert(self: *Self, allocator: Allocator, i: usize, item: T) Allocator.Error!void { | 732 | pub fn insert(self: *Self, gpa: Allocator, i: usize, item: T) Allocator.Error!void { |
| 735 | const dst = try self.addManyAt(allocator, i, 1); | 733 | const dst = try self.addManyAt(gpa, i, 1); |
| 736 | dst[0] = item; | 734 | dst[0] = item; |
| 737 | } | 735 | } |
| 738 | | 736 | |
| ... | @@ -759,11 +757,11 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ | ... | @@ -759,11 +757,11 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 759 | /// Asserts that the index is in bounds or equal to the length. | 757 | /// Asserts that the index is in bounds or equal to the length. |
| 760 | pub fn addManyAt( | 758 | pub fn addManyAt( |
| 761 | self: *Self, | 759 | self: *Self, |
| 762 | allocator: Allocator, | 760 | gpa: Allocator, |
| 763 | index: usize, | 761 | index: usize, |
| 764 | count: usize, | 762 | count: usize, |
| 765 | ) Allocator.Error![]T { | 763 | ) Allocator.Error![]T { |
| 766 | var managed = self.toManaged(allocator); | 764 | var managed = self.toManaged(gpa); |
| 767 | defer self.* = managed.moveToUnmanaged(); | 765 | defer self.* = managed.moveToUnmanaged(); |
| 768 | return managed.addManyAt(index, count); | 766 | return managed.addManyAt(index, count); |
| 769 | } | 767 | } |
| ... | @@ -795,12 +793,12 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ | ... | @@ -795,12 +793,12 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 795 | /// Asserts that the index is in bounds or equal to the length. | 793 | /// Asserts that the index is in bounds or equal to the length. |
| 796 | pub fn insertSlice( | 794 | pub fn insertSlice( |
| 797 | self: *Self, | 795 | self: *Self, |
| 798 | allocator: Allocator, | 796 | gpa: Allocator, |
| 799 | index: usize, | 797 | index: usize, |
| 800 | items: []const T, | 798 | items: []const T, |
| 801 | ) Allocator.Error!void { | 799 | ) Allocator.Error!void { |
| 802 | const dst = try self.addManyAt( | 800 | const dst = try self.addManyAt( |
| 803 | allocator, | 801 | gpa, |
| 804 | index, | 802 | index, |
| 805 | items.len, | 803 | items.len, |
| 806 | ); | 804 | ); |
| ... | @@ -812,7 +810,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ | ... | @@ -812,7 +810,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 812 | /// Asserts that the range is in bounds. | 810 | /// Asserts that the range is in bounds. |
| 813 | pub fn replaceRange( | 811 | pub fn replaceRange( |
| 814 | self: *Self, | 812 | self: *Self, |
| 815 | allocator: Allocator, | 813 | gpa: Allocator, |
| 816 | start: usize, | 814 | start: usize, |
| 817 | len: usize, | 815 | len: usize, |
| 818 | new_items: []const T, | 816 | new_items: []const T, |
| ... | @@ -823,7 +821,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ | ... | @@ -823,7 +821,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 823 | const first = new_items[0..range.len]; | 821 | const first = new_items[0..range.len]; |
| 824 | const rest = new_items[range.len..]; | 822 | const rest = new_items[range.len..]; |
| 825 | @memcpy(range[0..first.len], first); | 823 | @memcpy(range[0..first.len], first); |
| 826 | try self.insertSlice(allocator, after_range, rest); | 824 | try self.insertSlice(gpa, after_range, rest); |
| 827 | } else { | 825 | } else { |
| 828 | self.replaceRangeAssumeCapacity(start, len, new_items); | 826 | self.replaceRangeAssumeCapacity(start, len, new_items); |
| 829 | } | 827 | } |
| ... | @@ -859,8 +857,8 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ | ... | @@ -859,8 +857,8 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 859 | | 857 | |
| 860 | /// Extend the list by 1 element. Allocates more memory as necessary. | 858 | /// Extend the list by 1 element. Allocates more memory as necessary. |
| 861 | /// Invalidates element pointers if additional memory is needed. | 859 | /// Invalidates element pointers if additional memory is needed. |
| 862 | pub fn append(self: *Self, allocator: Allocator, item: T) Allocator.Error!void { | 860 | pub fn append(self: *Self, gpa: Allocator, item: T) Allocator.Error!void { |
| 863 | const new_item_ptr = try self.addOne(allocator); | 861 | const new_item_ptr = try self.addOne(gpa); |
| 864 | new_item_ptr.* = item; | 862 | new_item_ptr.* = item; |
| 865 | } | 863 | } |
| 866 | | 864 | |
| ... | @@ -899,8 +897,8 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ | ... | @@ -899,8 +897,8 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 899 | /// Append the slice of items to the list. Allocates more | 897 | /// Append the slice of items to the list. Allocates more |
| 900 | /// memory as necessary. | 898 | /// memory as necessary. |
| 901 | /// Invalidates element pointers if additional memory is needed. | 899 | /// Invalidates element pointers if additional memory is needed. |
| 902 | pub fn appendSlice(self: *Self, allocator: Allocator, items: []const T) Allocator.Error!void { | 900 | pub fn appendSlice(self: *Self, gpa: Allocator, items: []const T) Allocator.Error!void { |
| 903 | try self.ensureUnusedCapacity(allocator, items.len); | 901 | try self.ensureUnusedCapacity(gpa, items.len); |
| 904 | self.appendSliceAssumeCapacity(items); | 902 | self.appendSliceAssumeCapacity(items); |
| 905 | } | 903 | } |
| 906 | | 904 | |
| ... | @@ -918,8 +916,8 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ | ... | @@ -918,8 +916,8 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 918 | /// memory as necessary. Only call this function if a call to `appendSlice` instead would | 916 | /// memory as necessary. Only call this function if a call to `appendSlice` instead would |
| 919 | /// be a compile error. | 917 | /// be a compile error. |
| 920 | /// Invalidates element pointers if additional memory is needed. | 918 | /// Invalidates element pointers if additional memory is needed. |
| 921 | pub fn appendUnalignedSlice(self: *Self, allocator: Allocator, items: []align(1) const T) Allocator.Error!void { | 919 | pub fn appendUnalignedSlice(self: *Self, gpa: Allocator, items: []align(1) const T) Allocator.Error!void { |
| 922 | try self.ensureUnusedCapacity(allocator, items.len); | 920 | try self.ensureUnusedCapacity(gpa, items.len); |
| 923 | self.appendUnalignedSliceAssumeCapacity(items); | 921 | self.appendUnalignedSliceAssumeCapacity(items); |
| 924 | } | 922 | } |
| 925 | | 923 | |
| ... | @@ -947,8 +945,8 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ | ... | @@ -947,8 +945,8 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 947 | std.io.Writer(WriterContext, Allocator.Error, appendWrite); | 945 | std.io.Writer(WriterContext, Allocator.Error, appendWrite); |
| 948 | | 946 | |
| 949 | /// Initializes a Writer which will append to the list. | 947 | /// Initializes a Writer which will append to the list. |
| 950 | pub fn writer(self: *Self, allocator: Allocator) Writer { | 948 | pub fn writer(self: *Self, gpa: Allocator) Writer { |
| 951 | return .{ .context = .{ .self = self, .allocator = allocator } }; | 949 | return .{ .context = .{ .self = self, .allocator = gpa } }; |
| 952 | } | 950 | } |
| 953 | | 951 | |
| 954 | /// Same as `append` except it returns the number of bytes written, | 952 | /// Same as `append` except it returns the number of bytes written, |
| ... | @@ -983,9 +981,9 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ | ... | @@ -983,9 +981,9 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 983 | /// Invalidates element pointers if additional memory is needed. | 981 | /// Invalidates element pointers if additional memory is needed. |
| 984 | /// The function is inline so that a comptime-known `value` parameter will | 982 | /// The function is inline so that a comptime-known `value` parameter will |
| 985 | /// have a more optimal memset codegen in case it has a repeated byte pattern. | 983 | /// have a more optimal memset codegen in case it has a repeated byte pattern. |
| 986 | pub inline fn appendNTimes(self: *Self, allocator: Allocator, value: T, n: usize) Allocator.Error!void { | 984 | pub inline fn appendNTimes(self: *Self, gpa: Allocator, value: T, n: usize) Allocator.Error!void { |
| 987 | const old_len = self.items.len; | 985 | const old_len = self.items.len; |
| 988 | try self.resize(allocator, try addOrOom(old_len, n)); | 986 | try self.resize(gpa, try addOrOom(old_len, n)); |
| 989 | @memset(self.items[old_len..self.items.len], value); | 987 | @memset(self.items[old_len..self.items.len], value); |
| 990 | } | 988 | } |
| 991 | | 989 | |
| ... | @@ -1004,15 +1002,15 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ | ... | @@ -1004,15 +1002,15 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 1004 | /// Adjust the list length to `new_len`. | 1002 | /// Adjust the list length to `new_len`. |
| 1005 | /// Additional elements contain the value `undefined`. | 1003 | /// Additional elements contain the value `undefined`. |
| 1006 | /// Invalidates element pointers if additional memory is needed. | 1004 | /// Invalidates element pointers if additional memory is needed. |
| 1007 | pub fn resize(self: *Self, allocator: Allocator, new_len: usize) Allocator.Error!void { | 1005 | pub fn resize(self: *Self, gpa: Allocator, new_len: usize) Allocator.Error!void { |
| 1008 | try self.ensureTotalCapacity(allocator, new_len); | 1006 | try self.ensureTotalCapacity(gpa, new_len); |
| 1009 | self.items.len = new_len; | 1007 | self.items.len = new_len; |
| 1010 | } | 1008 | } |
| 1011 | | 1009 | |
| 1012 | /// Reduce allocated capacity to `new_len`. | 1010 | /// Reduce allocated capacity to `new_len`. |
| 1013 | /// May invalidate element pointers. | 1011 | /// May invalidate element pointers. |
| 1014 | /// Asserts that the new length is less than or equal to the previous length. | 1012 | /// Asserts that the new length is less than or equal to the previous length. |
| 1015 | pub fn shrinkAndFree(self: *Self, allocator: Allocator, new_len: usize) void { | 1013 | pub fn shrinkAndFree(self: *Self, gpa: Allocator, new_len: usize) void { |
| 1016 | assert(new_len <= self.items.len); | 1014 | assert(new_len <= self.items.len); |
| 1017 | | 1015 | |
| 1018 | if (@sizeOf(T) == 0) { | 1016 | if (@sizeOf(T) == 0) { |
| ... | @@ -1021,13 +1019,13 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ | ... | @@ -1021,13 +1019,13 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 1021 | } | 1019 | } |
| 1022 | | 1020 | |
| 1023 | const old_memory = self.allocatedSlice(); | 1021 | const old_memory = self.allocatedSlice(); |
| 1024 | if (allocator.remap(old_memory, new_len)) |new_items| { | 1022 | if (gpa.remap(old_memory, new_len)) |new_items| { |
| 1025 | self.capacity = new_items.len; | 1023 | self.capacity = new_items.len; |
| 1026 | self.items = new_items; | 1024 | self.items = new_items; |
| 1027 | return; | 1025 | return; |
| 1028 | } | 1026 | } |
| 1029 | | 1027 | |
| 1030 | const new_memory = allocator.alignedAlloc(T, alignment, new_len) catch |e| switch (e) { | 1028 | const new_memory = gpa.alignedAlloc(T, alignment, new_len) catch |e| switch (e) { |
| 1031 | error.OutOfMemory => { | 1029 | error.OutOfMemory => { |
| 1032 | // No problem, capacity is still correct then. | 1030 | // No problem, capacity is still correct then. |
| 1033 | self.items.len = new_len; | 1031 | self.items.len = new_len; |
| ... | @@ -1036,7 +1034,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ | ... | @@ -1036,7 +1034,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 1036 | }; | 1034 | }; |
| 1037 | | 1035 | |
| 1038 | @memcpy(new_memory, self.items[0..new_len]); | 1036 | @memcpy(new_memory, self.items[0..new_len]); |
| 1039 | allocator.free(old_memory); | 1037 | gpa.free(old_memory); |
| 1040 | self.items = new_memory; | 1038 | self.items = new_memory; |
| 1041 | self.capacity = new_memory.len; | 1039 | self.capacity = new_memory.len; |
| 1042 | } | 1040 | } |
| ... | @@ -1056,8 +1054,8 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ | ... | @@ -1056,8 +1054,8 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 1056 | } | 1054 | } |
| 1057 | | 1055 | |
| 1058 | /// Invalidates all element pointers. | 1056 | /// Invalidates all element pointers. |
| 1059 | pub fn clearAndFree(self: *Self, allocator: Allocator) void { | 1057 | pub fn clearAndFree(self: *Self, gpa: Allocator) void { |
| 1060 | allocator.free(self.allocatedSlice()); | 1058 | gpa.free(self.allocatedSlice()); |
| 1061 | self.items.len = 0; | 1059 | self.items.len = 0; |
| 1062 | self.capacity = 0; | 1060 | self.capacity = 0; |
| 1063 | } | 1061 | } |
| ... | @@ -1073,7 +1071,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ | ... | @@ -1073,7 +1071,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 1073 | /// If the current capacity is less than `new_capacity`, this function will | 1071 | /// If the current capacity is less than `new_capacity`, this function will |
| 1074 | /// modify the array so that it can hold exactly `new_capacity` items. | 1072 | /// modify the array so that it can hold exactly `new_capacity` items. |
| 1075 | /// Invalidates element pointers if additional memory is needed. | 1073 | /// Invalidates element pointers if additional memory is needed. |
| 1076 | pub fn ensureTotalCapacityPrecise(self: *Self, allocator: Allocator, new_capacity: usize) Allocator.Error!void { | 1074 | pub fn ensureTotalCapacityPrecise(self: *Self, gpa: Allocator, new_capacity: usize) Allocator.Error!void { |
| 1077 | if (@sizeOf(T) == 0) { | 1075 | if (@sizeOf(T) == 0) { |
| 1078 | self.capacity = math.maxInt(usize); | 1076 | self.capacity = math.maxInt(usize); |
| 1079 | return; | 1077 | return; |
| ... | @@ -1087,13 +1085,13 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ | ... | @@ -1087,13 +1085,13 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 1087 | // the allocator implementation would pointlessly copy our | 1085 | // the allocator implementation would pointlessly copy our |
| 1088 | // extra capacity. | 1086 | // extra capacity. |
| 1089 | const old_memory = self.allocatedSlice(); | 1087 | const old_memory = self.allocatedSlice(); |
| 1090 | if (allocator.remap(old_memory, new_capacity)) |new_memory| { | 1088 | if (gpa.remap(old_memory, new_capacity)) |new_memory| { |
| 1091 | self.items.ptr = new_memory.ptr; | 1089 | self.items.ptr = new_memory.ptr; |
| 1092 | self.capacity = new_memory.len; | 1090 | self.capacity = new_memory.len; |
| 1093 | } else { | 1091 | } else { |
| 1094 | const new_memory = try allocator.alignedAlloc(T, alignment, new_capacity); | 1092 | const new_memory = try gpa.alignedAlloc(T, alignment, new_capacity); |
| 1095 | @memcpy(new_memory[0..self.items.len], self.items); | 1093 | @memcpy(new_memory[0..self.items.len], self.items); |
| 1096 | allocator.free(old_memory); | 1094 | gpa.free(old_memory); |
| 1097 | self.items.ptr = new_memory.ptr; | 1095 | self.items.ptr = new_memory.ptr; |
| 1098 | self.capacity = new_memory.len; | 1096 | self.capacity = new_memory.len; |
| 1099 | } | 1097 | } |
| ... | @@ -1103,10 +1101,10 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ | ... | @@ -1103,10 +1101,10 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 1103 | /// Invalidates element pointers if additional memory is needed. | 1101 | /// Invalidates element pointers if additional memory is needed. |
| 1104 | pub fn ensureUnusedCapacity( | 1102 | pub fn ensureUnusedCapacity( |
| 1105 | self: *Self, | 1103 | self: *Self, |
| 1106 | allocator: Allocator, | 1104 | gpa: Allocator, |
| 1107 | additional_count: usize, | 1105 | additional_count: usize, |
| 1108 | ) Allocator.Error!void { | 1106 | ) Allocator.Error!void { |
| 1109 | return self.ensureTotalCapacity(allocator, try addOrOom(self.items.len, additional_count)); | 1107 | return self.ensureTotalCapacity(gpa, try addOrOom(self.items.len, additional_count)); |
| 1110 | } | 1108 | } |
| 1111 | | 1109 | |
| 1112 | /// Increases the array's length to match the full capacity that is already allocated. | 1110 | /// Increases the array's length to match the full capacity that is already allocated. |
| ... | @@ -1118,10 +1116,10 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ | ... | @@ -1118,10 +1116,10 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 1118 | | 1116 | |
| 1119 | /// Increase length by 1, returning pointer to the new item. | 1117 | /// Increase length by 1, returning pointer to the new item. |
| 1120 | /// The returned element pointer becomes invalid when the list is resized. | 1118 | /// The returned element pointer becomes invalid when the list is resized. |
| 1121 | pub fn addOne(self: *Self, allocator: Allocator) Allocator.Error!*T { | 1119 | pub fn addOne(self: *Self, gpa: Allocator) Allocator.Error!*T { |
| 1122 | // This can never overflow because `self.items` can never occupy the whole address space | 1120 | // This can never overflow because `self.items` can never occupy the whole address space |
| 1123 | const newlen = self.items.len + 1; | 1121 | const newlen = self.items.len + 1; |
| 1124 | try self.ensureTotalCapacity(allocator, newlen); | 1122 | try self.ensureTotalCapacity(gpa, newlen); |
| 1125 | return self.addOneAssumeCapacity(); | 1123 | return self.addOneAssumeCapacity(); |
| 1126 | } | 1124 | } |
| 1127 | | 1125 | |
| ... | @@ -1139,9 +1137,9 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ | ... | @@ -1139,9 +1137,9 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 1139 | /// Resize the array, adding `n` new elements, which have `undefined` values. | 1137 | /// Resize the array, adding `n` new elements, which have `undefined` values. |
| 1140 | /// The return value is an array pointing to the newly allocated elements. | 1138 | /// The return value is an array pointing to the newly allocated elements. |
| 1141 | /// The returned pointer becomes invalid when the list is resized. | 1139 | /// The returned pointer becomes invalid when the list is resized. |
| 1142 | pub fn addManyAsArray(self: *Self, allocator: Allocator, comptime n: usize) Allocator.Error!*[n]T { | 1140 | pub fn addManyAsArray(self: *Self, gpa: Allocator, comptime n: usize) Allocator.Error!*[n]T { |
| 1143 | const prev_len = self.items.len; | 1141 | const prev_len = self.items.len; |
| 1144 | try self.resize(allocator, try addOrOom(self.items.len, n)); | 1142 | try self.resize(gpa, try addOrOom(self.items.len, n)); |
| 1145 | return self.items[prev_len..][0..n]; | 1143 | return self.items[prev_len..][0..n]; |
| 1146 | } | 1144 | } |
| 1147 | | 1145 | |
| ... | @@ -1161,9 +1159,9 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ | ... | @@ -1161,9 +1159,9 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 1161 | /// The return value is a slice pointing to the newly allocated elements. | 1159 | /// The return value is a slice pointing to the newly allocated elements. |
| 1162 | /// The returned pointer becomes invalid when the list is resized. | 1160 | /// The returned pointer becomes invalid when the list is resized. |
| 1163 | /// Resizes list if `self.capacity` is not large enough. | 1161 | /// Resizes list if `self.capacity` is not large enough. |
| 1164 | pub fn addManyAsSlice(self: *Self, allocator: Allocator, n: usize) Allocator.Error![]T { | 1162 | pub fn addManyAsSlice(self: *Self, gpa: Allocator, n: usize) Allocator.Error![]T { |
| 1165 | const prev_len = self.items.len; | 1163 | const prev_len = self.items.len; |
| 1166 | try self.resize(allocator, try addOrOom(self.items.len, n)); | 1164 | try self.resize(gpa, try addOrOom(self.items.len, n)); |
| 1167 | return self.items[prev_len..][0..n]; | 1165 | return self.items[prev_len..][0..n]; |
| 1168 | } | 1166 | } |
| 1169 | | 1167 | |