Skip to content

Commit 3b39108

Browse files
Fri3dNstuffxtexx
authored andcommitted
std.ArrayList: swapRemove set removed element to undefined (ziglang#25514)
(cherry picked from commit 87c1894)
1 parent 024fff6 commit 3b39108

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
@@ -554,6 +553,7 @@ pub fn AlignedManaged(comptime T: type, comptime alignment: ?mem.Alignment) type
554553
pub fn pop(self: *Self) ?T {
555554
if (self.items.len == 0) return null;
556555
const val = self.items[self.items.len - 1];
556+
self.items[self.items.len - 1] = undefined;
557557
self.items.len -= 1;
558558
return val;
559559
}
@@ -576,8 +576,7 @@ pub fn AlignedManaged(comptime T: type, comptime alignment: ?mem.Alignment) type
576576
/// Returns the last element from the list.
577577
/// Asserts that the list is not empty.
578578
pub fn getLast(self: Self) T {
579-
const val = self.items[self.items.len - 1];
580-
return val;
579+
return self.items[self.items.len - 1];
581580
}
582581

583582
/// Returns the last element from the list, or `null` if list is empty.
@@ -958,14 +957,13 @@ pub fn Aligned(comptime T: type, comptime alignment: ?mem.Alignment) type {
958957
/// The empty slot is filled from the end of the list.
959958
/// Invalidates pointers to last element.
960959
/// This operation is O(1).
961-
/// Asserts that the list is not empty.
962960
/// Asserts that the index is in bounds.
963961
pub fn swapRemove(self: *Self, i: usize) T {
964-
if (self.items.len - 1 == i) return self.pop().?;
965-
966-
const old_item = self.items[i];
967-
self.items[i] = self.pop().?;
968-
return old_item;
962+
const val = self.items[i];
963+
self.items[i] = self.items[self.items.len - 1];
964+
self.items[self.items.len - 1] = undefined;
965+
self.items.len -= 1;
966+
return val;
969967
}
970968

971969
/// Append the slice of items to the list. Allocates more
@@ -1371,6 +1369,7 @@ pub fn Aligned(comptime T: type, comptime alignment: ?mem.Alignment) type {
13711369
pub fn pop(self: *Self) ?T {
13721370
if (self.items.len == 0) return null;
13731371
const val = self.items[self.items.len - 1];
1372+
self.items[self.items.len - 1] = undefined;
13741373
self.items.len -= 1;
13751374
return val;
13761375
}
@@ -1392,8 +1391,7 @@ pub fn Aligned(comptime T: type, comptime alignment: ?mem.Alignment) type {
13921391
/// Return the last element from the list.
13931392
/// Asserts that the list is not empty.
13941393
pub fn getLast(self: Self) T {
1395-
const val = self.items[self.items.len - 1];
1396-
return val;
1394+
return self.items[self.items.len - 1];
13971395
}
13981396

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

0 commit comments

Comments
 (0)