Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 14 additions & 16 deletions lib/std/array_list.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand All @@ -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
Expand Down