@@ -277,14 +277,13 @@ pub fn AlignedManaged(comptime T: type, comptime alignment: ?mem.Alignment) type
277
277
/// The empty slot is filled from the end of the list.
278
278
/// This operation is O(1).
279
279
/// This may not preserve item order. Use `orderedRemove` if you need to preserve order.
280
- /// Asserts that the list is not empty.
281
280
/// Asserts that the index is in bounds.
282
281
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 ;
288
287
}
289
288
290
289
/// 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
521
pub fn pop (self : * Self ) ? T {
523
522
if (self .items .len == 0 ) return null ;
524
523
const val = self .items [self .items .len - 1 ];
524
+ self .items [self .items .len - 1 ] = undefined ;
525
525
self .items .len -= 1 ;
526
526
return val ;
527
527
}
@@ -544,8 +544,7 @@ pub fn AlignedManaged(comptime T: type, comptime alignment: ?mem.Alignment) type
544
544
/// Returns the last element from the list.
545
545
/// Asserts that the list is not empty.
546
546
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 ];
549
548
}
550
549
551
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
955
/// The empty slot is filled from the end of the list.
957
956
/// Invalidates pointers to last element.
958
957
/// This operation is O(1).
959
- /// Asserts that the list is not empty.
960
958
/// Asserts that the index is in bounds.
961
959
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 ;
967
965
}
968
966
969
967
/// 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
1325
pub fn pop (self : * Self ) ? T {
1328
1326
if (self .items .len == 0 ) return null ;
1329
1327
const val = self .items [self .items .len - 1 ];
1328
+ self .items [self .items .len - 1 ] = undefined ;
1330
1329
self .items .len -= 1 ;
1331
1330
return val ;
1332
1331
}
@@ -1348,8 +1347,7 @@ pub fn Aligned(comptime T: type, comptime alignment: ?mem.Alignment) type {
1348
1347
/// Return the last element from the list.
1349
1348
/// Asserts that the list is not empty.
1350
1349
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 ];
1353
1351
}
1354
1352
1355
1353
/// Return the last element from the list, or
0 commit comments