diff options
| author | Fri3dNstuff <102751849+Fri3dNstuff@users.noreply.github.com> | 2025-10-12 05:04:32 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-10-11 19:04:32 -0700 |
| commit | 87c18945c207c77b5bc84123a7ba043b848bbabb (patch) | |
| tree | a404891d51874c7892a0f1522645846e2ed7c523 /lib/std | |
| parent | 95242cc43132c7b6aeb2f7171389075859aba008 (diff) | |
| download | zig-87c18945c207c77b5bc84123a7ba043b848bbabb.tar.gz zig-87c18945c207c77b5bc84123a7ba043b848bbabb.zip | |
std.ArrayList: swapRemove set removed element to undefined (#25514)
Diffstat (limited to 'lib/std')
| -rw-r--r-- | lib/std/array_list.zig | 30 |
1 files changed, 14 insertions, 16 deletions
diff --git a/lib/std/array_list.zig b/lib/std/array_list.zig index 96a0344442..11202a186e 100644 --- a/lib/std/array_list.zig +++ b/lib/std/array_list.zig @@ -277,14 +277,13 @@ pub fn AlignedManaged(comptime T: type, comptime alignment: ?mem.Alignment) type /// The empty slot is filled from the end of the list. /// This operation is O(1). /// This may not preserve item order. Use `orderedRemove` if you need to preserve order. - /// Asserts that the list is not empty. /// Asserts that the index is in bounds. pub fn swapRemove(self: *Self, i: usize) T { - if (self.items.len - 1 == i) return self.pop().?; - - const old_item = self.items[i]; - self.items[i] = self.pop().?; - return old_item; + const val = self.items[i]; + self.items[i] = self.items[self.items.len - 1]; + self.items[self.items.len - 1] = undefined; + self.items.len -= 1; + return val; } /// Append the slice of items to the list. Allocates more @@ -522,6 +521,7 @@ pub fn AlignedManaged(comptime T: type, comptime alignment: ?mem.Alignment) type pub fn pop(self: *Self) ?T { if (self.items.len == 0) return null; const val = self.items[self.items.len - 1]; + self.items[self.items.len - 1] = undefined; self.items.len -= 1; return val; } @@ -544,8 +544,7 @@ pub fn AlignedManaged(comptime T: type, comptime alignment: ?mem.Alignment) type /// Returns the last element from the list. /// Asserts that the list is not empty. pub fn getLast(self: Self) T { - const val = self.items[self.items.len - 1]; - return val; + return self.items[self.items.len - 1]; } /// Returns the last element from the list, or `null` if list is empty. @@ -956,14 +955,13 @@ pub fn Aligned(comptime T: type, comptime alignment: ?mem.Alignment) type { /// The empty slot is filled from the end of the list. /// Invalidates pointers to last element. /// This operation is O(1). - /// Asserts that the list is not empty. /// Asserts that the index is in bounds. pub fn swapRemove(self: *Self, i: usize) T { - if (self.items.len - 1 == i) return self.pop().?; - - const old_item = self.items[i]; - self.items[i] = self.pop().?; - return old_item; + const val = self.items[i]; + self.items[i] = self.items[self.items.len - 1]; + self.items[self.items.len - 1] = undefined; + self.items.len -= 1; + return val; } /// Append the slice of items to the list. Allocates more @@ -1327,6 +1325,7 @@ pub fn Aligned(comptime T: type, comptime alignment: ?mem.Alignment) type { pub fn pop(self: *Self) ?T { if (self.items.len == 0) return null; const val = self.items[self.items.len - 1]; + self.items[self.items.len - 1] = undefined; self.items.len -= 1; return val; } @@ -1348,8 +1347,7 @@ pub fn Aligned(comptime T: type, comptime alignment: ?mem.Alignment) type { /// Return the last element from the list. /// Asserts that the list is not empty. pub fn getLast(self: Self) T { - const val = self.items[self.items.len - 1]; - return val; + return self.items[self.items.len - 1]; } /// Return the last element from the list, or |
