| ... | ... | @@ -108,6 +108,17 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 108 | 108 | return result[0 .. result.len - 1 :sentinel]; |
| 109 | 109 | } |
| 110 | 110 | |
| 111 | /// Creates a copy of this ArrayList, using the same allocator. |
| 112 | pub fn clone(self: *Self) !Self { |
| 113 | const items_copy = try self.allocator.alloc(T, self.capacity); |
| 114 | mem.copy(T, items_copy, self.items); |
| 115 | return Self{ |
| 116 | .items = items_copy, |
| 117 | .capacity = self.capacity, |
| 118 | .allocator = self.allocator, |
| 119 | }; |
| 120 | } |
| 121 | |
| 111 | 122 | /// Insert `item` at index `n` by moving `list[n .. list.len]` to make room. |
| 112 | 123 | /// This operation is O(N). |
| 113 | 124 | pub fn insert(self: *Self, n: usize, item: T) !void { |
| ... | ... | @@ -489,6 +500,16 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 489 | 500 | return result[0 .. result.len - 1 :sentinel]; |
| 490 | 501 | } |
| 491 | 502 | |
| 503 | /// Creates a copy of this ArrayList. |
| 504 | pub fn clone(self: *Self, allocator: Allocator) !Self { |
| 505 | const items_copy = try allocator.alloc(T, self.capacity); |
| 506 | mem.copy(T, items_copy, self.items); |
| 507 | return Self{ |
| 508 | .items = items_copy, |
| 509 | .capacity = self.capacity, |
| 510 | }; |
| 511 | } |
| 512 | |
| 492 | 513 | /// Insert `item` at index `n`. Moves `list[n .. list.len]` |
| 493 | 514 | /// to higher indices to make room. |
| 494 | 515 | /// This operation is O(N). |
| ... | ... | @@ -802,6 +823,53 @@ test "std.ArrayList/ArrayListUnmanaged.initCapacity" { |
| 802 | 823 | } |
| 803 | 824 | } |
| 804 | 825 | |
| 826 | test "std.ArrayList/ArrayListUnmanaged.clone" { |
| 827 | const a = testing.allocator; |
| 828 | { |
| 829 | var array = ArrayList(i32).init(a); |
| 830 | try array.append(-1); |
| 831 | try array.append(3); |
| 832 | try array.append(5); |
| 833 | |
| 834 | const cloned = try array.clone(); |
| 835 | defer cloned.deinit(); |
| 836 | |
| 837 | var i: usize = 0; |
| 838 | while (i < array.items.len) : (i += 1) { |
| 839 | try testing.expectEqual(array.items[i], cloned.items[i]); |
| 840 | } |
| 841 | try testing.expectEqual(array.capacity, cloned.capacity); |
| 842 | try testing.expectEqual(array.allocator, cloned.allocator); |
| 843 | |
| 844 | array.deinit(); |
| 845 | |
| 846 | try testing.expectEqual(cloned.items[0], -1); |
| 847 | try testing.expectEqual(cloned.items[1], 3); |
| 848 | try testing.expectEqual(cloned.items[2], 5); |
| 849 | } |
| 850 | { |
| 851 | var array = ArrayListUnmanaged(i32){}; |
| 852 | try array.append(a, -1); |
| 853 | try array.append(a, 3); |
| 854 | try array.append(a, 5); |
| 855 | |
| 856 | var cloned = try array.clone(a); |
| 857 | defer cloned.deinit(a); |
| 858 | |
| 859 | var i: usize = 0; |
| 860 | while (i < array.items.len) : (i += 1) { |
| 861 | try testing.expectEqual(array.items[i], cloned.items[i]); |
| 862 | } |
| 863 | try testing.expectEqual(array.capacity, cloned.capacity); |
| 864 | |
| 865 | array.deinit(a); |
| 866 | |
| 867 | try testing.expectEqual(cloned.items[0], -1); |
| 868 | try testing.expectEqual(cloned.items[1], 3); |
| 869 | try testing.expectEqual(cloned.items[2], 5); |
| 870 | } |
| 871 | } |
| 872 | |
| 805 | 873 | test "std.ArrayList/ArrayListUnmanaged.basic" { |
| 806 | 874 | const a = testing.allocator; |
| 807 | 875 | { |