Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: ziglang/zig
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 317ed57cb1e2
Choose a base ref
...
head repository: ziglang/zig
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 2a719ee6c598
Choose a head ref
  • 3 commits
  • 1 file changed
  • 2 contributors

Commits on Jul 14, 2018

  1. Added remove to ArrayList

    tgschultz authored and andrewrk committed Jul 14, 2018
    Copy the full SHA
    a0c1498 View commit details
  2. Copy the full SHA
    b44332f View commit details
  3. Copy the full SHA
    2a719ee View commit details
Showing with 49 additions and 2 deletions.
  1. +49 −2 std/array_list.zig
51 changes: 49 additions & 2 deletions std/array_list.zig
Original file line number Diff line number Diff line change
@@ -41,8 +41,8 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type {
return self.items[0..self.len];
}

pub fn at(self: Self, n: usize) T {
return self.toSliceConst()[n];
pub fn at(self: Self, i: usize) T {
return self.toSliceConst()[i];
}

/// Sets the value at index `i`, or returns `error.OutOfBounds` if
@@ -102,6 +102,26 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type {
new_item_ptr.* = item;
}

/// Removes the element at the specified index and returns it.
/// The empty slot is filled from the end of the list.
pub fn swapRemove(self: *Self, i: usize) T {
if (self.len - 1 == i) return self.pop();

const slice = self.toSlice();
const old_item = slice[i];
slice[i] = self.pop();
return old_item;
}

pub fn removeOrError(self: *Self, n: usize) !T {
if (n >= self.len) return error.OutOfBounds;
if (self.len - 1 == n) return self.pop();

var old_item = self.at(n);
try self.setOrError(n, self.pop());
return old_item;
}

pub fn appendSlice(self: *Self, items: []align(A) const T) !void {
try self.ensureCapacity(self.len + items.len);
mem.copy(T, self.items[self.len..], items);
@@ -232,6 +252,33 @@ test "basic ArrayList test" {
assert(list.pop() == 33);
}

test "std.ArrayList.swapRemove" {
var list = ArrayList(i32).init(debug.global_allocator);
defer list.deinit();

try list.append(1);
try list.append(2);
try list.append(3);
try list.append(4);
try list.append(5);
try list.append(6);
try list.append(7);

//remove from middle
assert(list.swapRemove(3) == 4);
assert(list.at(3) == 7);
assert(list.len == 6);

//remove from end
assert(list.swapRemove(5) == 6);
assert(list.len == 5);

//remove from front
assert(list.swapRemove(0) == 1);
assert(list.at(0) == 5);
assert(list.len == 4);
}

test "iterator ArrayList test" {
var list = ArrayList(i32).init(debug.global_allocator);
defer list.deinit();