Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std.MultiArrayList: add clear methods #21695

Merged
merged 1 commit into from
Oct 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions lib/std/multi_array_list.zig
Original file line number Diff line number Diff line change
Expand Up @@ -346,11 +346,8 @@ pub fn MultiArrayList(comptime T: type) type {
/// If `new_len` is greater than zero, this may fail to reduce the capacity,
/// but the data remains intact and the length is updated to new_len.
pub fn shrinkAndFree(self: *Self, gpa: Allocator, new_len: usize) void {
if (new_len == 0) {
gpa.free(self.allocatedBytes());
self.* = .{};
return;
}
if (new_len == 0) return clearAndFree(self, gpa);

assert(new_len <= self.capacity);
assert(new_len <= self.len);

Expand Down Expand Up @@ -391,13 +388,23 @@ pub fn MultiArrayList(comptime T: type) type {
self.* = other;
}

pub fn clearAndFree(self: *Self, gpa: Allocator) void {
gpa.free(self.allocatedBytes());
self.* = .{};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.* = .{};
self.* = .empty;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, of course. Well, it will at the latest be fixed when the field defaults are removed, after 0.14.0 is tagged.

}

/// Reduce length to `new_len`.
/// Invalidates pointers to elements `items[new_len..]`.
/// Keeps capacity the same.
pub fn shrinkRetainingCapacity(self: *Self, new_len: usize) void {
self.len = new_len;
}

/// Invalidates all element pointers.
pub fn clearRetainingCapacity(self: *Self) void {
self.len = 0;
}

/// Modify the array so that it can hold at least `new_capacity` items.
/// Implements super-linear growth to achieve amortized O(1) append operations.
/// Invalidates pointers if additional memory is needed.
Expand Down Expand Up @@ -677,6 +684,14 @@ test "basic usage" {
try testing.expectEqual(@as(u32, 2), list.pop().a);
try testing.expectEqual(@as(u8, 'a'), list.pop().c);
try testing.expectEqual(@as(?Foo, null), list.popOrNull());

list.clearRetainingCapacity();
try testing.expectEqual(0, list.len);
try testing.expect(list.capacity > 0);

list.clearAndFree(ally);
try testing.expectEqual(0, list.len);
try testing.expectEqual(0, list.capacity);
}

// This was observed to fail on aarch64 with LLVM 11, when the capacityInBytes
Expand Down
Loading