authorgravatar for john.schmidt.h@gmail.comJohn Schmidt <john.schmidt.h@gmail.com> 2021-12-13 00:00:04+01:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-01-27 19:35:08+02:00
log554734f9f81fb16201e1c3826c601d1a0a3d7c91
treeef3b7dc575a69aebd060a98ac8ba2e340745dec5
parent3e6952ad16625197d569b99e2097ccc63172e201

Add ArrayList.clone

This is a common operation and matches the API on other stdlib collections like HashMap and MultiArrayList.

1 files changed, 68 insertions(+), 0 deletions(-)

lib/std/array_list.zig+68
......@@ -108,6 +108,17 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type {
108108 return result[0 .. result.len - 1 :sentinel];
109109 }
110110
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
111122 /// Insert `item` at index `n` by moving `list[n .. list.len]` to make room.
112123 /// This operation is O(N).
113124 pub fn insert(self: *Self, n: usize, item: T) !void {
......@@ -489,6 +500,16 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
489500 return result[0 .. result.len - 1 :sentinel];
490501 }
491502
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
492513 /// Insert `item` at index `n`. Moves `list[n .. list.len]`
493514 /// to higher indices to make room.
494515 /// This operation is O(N).
......@@ -802,6 +823,53 @@ test "std.ArrayList/ArrayListUnmanaged.initCapacity" {
802823 }
803824}
804825
826test "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
805873test "std.ArrayList/ArrayListUnmanaged.basic" {
806874 const a = testing.allocator;
807875 {