authorgravatar for 102751849+Fri3dNstuff@users.noreply.github.comFri3dNstuff <102751849+Fri3dNstuff@users.noreply.github.com> 2025-10-12 05:04:32+03:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2025-10-11 19:04:32-07:00
log87c18945c207c77b5bc84123a7ba043b848bbabb
treea404891d51874c7892a0f1522645846e2ed7c523
parent95242cc43132c7b6aeb2f7171389075859aba008
signaturebadge-check Signed by PGP key B5690EEEBB952194

std.ArrayList: swapRemove set removed element to undefined (#25514)


1 files changed, 14 insertions(+), 16 deletions(-)

lib/std/array_list.zig+14-16
...@@ -277,14 +277,13 @@ pub fn AlignedManaged(comptime T: type, comptime alignment: ?mem.Alignment) type...@@ -277,14 +277,13 @@ pub fn AlignedManaged(comptime T: type, comptime alignment: ?mem.Alignment) type
277 /// The empty slot is filled from the end of the list.277 /// The empty slot is filled from the end of the list.
278 /// This operation is O(1).278 /// This operation is O(1).
279 /// This may not preserve item order. Use `orderedRemove` if you need to preserve order.279 /// This may not preserve item order. Use `orderedRemove` if you need to preserve order.
280 /// Asserts that the list is not empty.
281 /// Asserts that the index is in bounds.280 /// Asserts that the index is in bounds.
282 pub fn swapRemove(self: *Self, i: usize) T {281 pub fn swapRemove(self: *Self, i: usize) T {
283 if (self.items.len - 1 == i) return self.pop().?;282 const val = self.items[i];
284283 self.items[i] = self.items[self.items.len - 1];
285 const old_item = self.items[i];284 self.items[self.items.len - 1] = undefined;
286 self.items[i] = self.pop().?;285 self.items.len -= 1;
287 return old_item;286 return val;
288 }287 }
289288
290 /// Append the slice of items to the list. Allocates more289 /// 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...@@ -522,6 +521,7 @@ pub fn AlignedManaged(comptime T: type, comptime alignment: ?mem.Alignment) type
522 pub fn pop(self: *Self) ?T {521 pub fn pop(self: *Self) ?T {
523 if (self.items.len == 0) return null;522 if (self.items.len == 0) return null;
524 const val = self.items[self.items.len - 1];523 const val = self.items[self.items.len - 1];
524 self.items[self.items.len - 1] = undefined;
525 self.items.len -= 1;525 self.items.len -= 1;
526 return val;526 return val;
527 }527 }
...@@ -544,8 +544,7 @@ pub fn AlignedManaged(comptime T: type, comptime alignment: ?mem.Alignment) type...@@ -544,8 +544,7 @@ pub fn AlignedManaged(comptime T: type, comptime alignment: ?mem.Alignment) type
544 /// Returns the last element from the list.544 /// Returns the last element from the list.
545 /// Asserts that the list is not empty.545 /// Asserts that the list is not empty.
546 pub fn getLast(self: Self) T {546 pub fn getLast(self: Self) T {
547 const val = self.items[self.items.len - 1];547 return self.items[self.items.len - 1];
548 return val;
549 }548 }
550549
551 /// Returns the last element from the list, or `null` if list is empty.550 /// 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 {...@@ -956,14 +955,13 @@ pub fn Aligned(comptime T: type, comptime alignment: ?mem.Alignment) type {
956 /// The empty slot is filled from the end of the list.955 /// The empty slot is filled from the end of the list.
957 /// Invalidates pointers to last element.956 /// Invalidates pointers to last element.
958 /// This operation is O(1).957 /// This operation is O(1).
959 /// Asserts that the list is not empty.
960 /// Asserts that the index is in bounds.958 /// Asserts that the index is in bounds.
961 pub fn swapRemove(self: *Self, i: usize) T {959 pub fn swapRemove(self: *Self, i: usize) T {
962 if (self.items.len - 1 == i) return self.pop().?;960 const val = self.items[i];
963961 self.items[i] = self.items[self.items.len - 1];
964 const old_item = self.items[i];962 self.items[self.items.len - 1] = undefined;
965 self.items[i] = self.pop().?;963 self.items.len -= 1;
966 return old_item;964 return val;
967 }965 }
968966
969 /// Append the slice of items to the list. Allocates more967 /// 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 {...@@ -1327,6 +1325,7 @@ pub fn Aligned(comptime T: type, comptime alignment: ?mem.Alignment) type {
1327 pub fn pop(self: *Self) ?T {1325 pub fn pop(self: *Self) ?T {
1328 if (self.items.len == 0) return null;1326 if (self.items.len == 0) return null;
1329 const val = self.items[self.items.len - 1];1327 const val = self.items[self.items.len - 1];
1328 self.items[self.items.len - 1] = undefined;
1330 self.items.len -= 1;1329 self.items.len -= 1;
1331 return val;1330 return val;
1332 }1331 }
...@@ -1348,8 +1347,7 @@ pub fn Aligned(comptime T: type, comptime alignment: ?mem.Alignment) type {...@@ -1348,8 +1347,7 @@ pub fn Aligned(comptime T: type, comptime alignment: ?mem.Alignment) type {
1348 /// Return the last element from the list.1347 /// Return the last element from the list.
1349 /// Asserts that the list is not empty.1348 /// Asserts that the list is not empty.
1350 pub fn getLast(self: Self) T {1349 pub fn getLast(self: Self) T {
1351 const val = self.items[self.items.len - 1];1350 return self.items[self.items.len - 1];
1352 return val;
1353 }1351 }
13541352
1355 /// Return the last element from the list, or1353 /// Return the last element from the list, or