Skip to content

Commit 87c1894

Browse files
authored
std.ArrayList: swapRemove set removed element to undefined (#25514)
1 parent 95242cc commit 87c1894

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

lib/std/array_list.zig

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -277,14 +277,13 @@ pub fn AlignedManaged(comptime T: type, comptime alignment: ?mem.Alignment) type
277277
/// The empty slot is filled from the end of the list.
278278
/// This operation is O(1).
279279
/// This may not preserve item order. Use `orderedRemove` if you need to preserve order.
280-
/// Asserts that the list is not empty.
281280
/// Asserts that the index is in bounds.
282281
pub fn swapRemove(self: *Self, i: usize) T {
283-
if (self.items.len - 1 == i) return self.pop().?;
284-
285-
const old_item = self.items[i];
286-
self.items[i] = self.pop().?;
287-
return old_item;
282+
const val = self.items[i];
283+
self.items[i] = self.items[self.items.len - 1];
284+
self.items[self.items.len - 1] = undefined;
285+
self.items.len -= 1;
286+
return val;
288287
}
289288

290289
/// 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
522521
pub fn pop(self: *Self) ?T {
523522
if (self.items.len == 0) return null;
524523
const val = self.items[self.items.len - 1];
524+
self.items[self.items.len - 1] = undefined;
525525
self.items.len -= 1;
526526
return val;
527527
}
@@ -544,8 +544,7 @@ pub fn AlignedManaged(comptime T: type, comptime alignment: ?mem.Alignment) type
544544
/// Returns the last element from the list.
545545
/// Asserts that the list is not empty.
546546
pub fn getLast(self: Self) T {
547-
const val = self.items[self.items.len - 1];
548-
return val;
547+
return self.items[self.items.len - 1];
549548
}
550549

551550
/// 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 {
956955
/// The empty slot is filled from the end of the list.
957956
/// Invalidates pointers to last element.
958957
/// This operation is O(1).
959-
/// Asserts that the list is not empty.
960958
/// Asserts that the index is in bounds.
961959
pub fn swapRemove(self: *Self, i: usize) T {
962-
if (self.items.len - 1 == i) return self.pop().?;
963-
964-
const old_item = self.items[i];
965-
self.items[i] = self.pop().?;
966-
return old_item;
960+
const val = self.items[i];
961+
self.items[i] = self.items[self.items.len - 1];
962+
self.items[self.items.len - 1] = undefined;
963+
self.items.len -= 1;
964+
return val;
967965
}
968966

969967
/// 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 {
13271325
pub fn pop(self: *Self) ?T {
13281326
if (self.items.len == 0) return null;
13291327
const val = self.items[self.items.len - 1];
1328+
self.items[self.items.len - 1] = undefined;
13301329
self.items.len -= 1;
13311330
return val;
13321331
}
@@ -1348,8 +1347,7 @@ pub fn Aligned(comptime T: type, comptime alignment: ?mem.Alignment) type {
13481347
/// Return the last element from the list.
13491348
/// Asserts that the list is not empty.
13501349
pub fn getLast(self: Self) T {
1351-
const val = self.items[self.items.len - 1];
1352-
return val;
1350+
return self.items[self.items.len - 1];
13531351
}
13541352

13551353
/// Return the last element from the list, or

0 commit comments

Comments
 (0)