Commit f04007028f

John Schmidt <john.schmidt.h@gmail.com>
2021-12-13 15:01:05
Implement clone with initCapacity and appendSliceAssumeCapacity
`ArrayList.ensureTotalCapacityPrecise` uses `Allocator.reallocAtLeast` under the hood, which can return more than `new_capacity` bytes if `alignment != @alignOf(T)`. This implementation of `clone` assures that the case of `ensureTotalCapacityPrecise` is handled correctly. Thanks @Vexu and @squeek502 for pointing this out.
1 parent d86b866
Changed files (1)
lib/std/array_list.zig
@@ -110,14 +110,9 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type {
 
         /// Creates a copy of this ArrayList, using the same allocator.
         pub fn clone(self: *Self) !Self {
-            var items_copy = try self.allocator.alloc(T, self.capacity);
-            mem.copy(T, items_copy, self.items);
-            items_copy.len = self.items.len;
-            return Self{
-                .items = items_copy,
-                .capacity = self.capacity,
-                .allocator = self.allocator,
-            };
+            var cloned = try ArrayList(T).initCapacity(self.allocator, self.capacity);
+            cloned.appendSliceAssumeCapacity(self.items);
+            return cloned;
         }
 
         /// Insert `item` at index `n` by moving `list[n .. list.len]` to make room.
@@ -503,13 +498,9 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
 
         /// Creates a copy of this ArrayList.
         pub fn clone(self: *Self, allocator: Allocator) !Self {
-            var items_copy = try allocator.alloc(T, self.capacity);
-            mem.copy(T, items_copy, self.items);
-            items_copy.len = self.items.len;
-            return Self{
-                .items = items_copy,
-                .capacity = self.capacity,
-            };
+            var cloned = try ArrayListUnmanaged(T).initCapacity(allocator, self.capacity);
+            cloned.appendSliceAssumeCapacity(self.items);
+            return cloned;
         }
 
         /// Insert `item` at index `n`. Moves `list[n .. list.len]`
@@ -837,8 +828,8 @@ test "std.ArrayList/ArrayListUnmanaged.clone" {
         defer cloned.deinit();
 
         try testing.expectEqualSlices(i32, array.items, cloned.items);
-        try testing.expectEqual(array.capacity, cloned.capacity);
         try testing.expectEqual(array.allocator, cloned.allocator);
+        try testing.expect(cloned.capacity >= array.capacity);
 
         array.deinit();
 
@@ -856,7 +847,7 @@ test "std.ArrayList/ArrayListUnmanaged.clone" {
         defer cloned.deinit(a);
 
         try testing.expectEqualSlices(i32, array.items, cloned.items);
-        try testing.expectEqual(array.capacity, cloned.capacity);
+        try testing.expect(cloned.capacity >= array.capacity);
 
         array.deinit(a);