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: e78b1b810fd1
Choose a base ref
...
head repository: ziglang/zig
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 5f1aa3505d38
Choose a head ref
  • 3 commits
  • 2 files changed
  • 2 contributors

Commits on Jul 13, 2018

  1. Copy the full SHA
    a1cafa6 View commit details
  2. Copy the full SHA
    fe98a2d View commit details

Commits on Jul 14, 2018

  1. Merge pull request #1232 from BarabasGitHub/fix-array-list-insert

    Fix array list insert
    andrewrk authored Jul 14, 2018

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    5f1aa35 View commit details
Showing with 39 additions and 4 deletions.
  1. +21 −4 std/array_list.zig
  2. +18 −0 std/mem.zig
25 changes: 21 additions & 4 deletions std/array_list.zig
Original file line number Diff line number Diff line change
@@ -85,15 +85,15 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type {
try self.ensureCapacity(self.len + 1);
self.len += 1;

mem.copy(T, self.items[n + 1 .. self.len], self.items[n .. self.len - 1]);
mem.copyBackwards(T, self.items[n + 1 .. self.len], self.items[n .. self.len - 1]);
self.items[n] = item;
}

pub fn insertSlice(self: *Self, n: usize, items: []align(A) const T) !void {
try self.ensureCapacity(self.len + items.len);
self.len += items.len;

mem.copy(T, self.items[n + items.len .. self.len], self.items[n .. self.len - items.len]);
mem.copyBackwards(T, self.items[n + items.len .. self.len], self.items[n .. self.len - items.len]);
mem.copy(T, self.items[n .. n + items.len], items);
}

@@ -266,19 +266,36 @@ test "insert ArrayList test" {
defer list.deinit();

try list.append(1);
try list.append(2);
try list.append(3);
try list.insert(0, 5);
assert(list.items[0] == 5);
assert(list.items[1] == 1);
assert(list.items[2] == 2);
assert(list.items[3] == 3);
}

test "insertSlice ArrayList test" {
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.insertSlice(1, []const i32{
9,
8,
});
assert(list.items[0] == 5);
assert(list.items[0] == 1);
assert(list.items[1] == 9);
assert(list.items[2] == 8);
assert(list.items[3] == 2);
assert(list.items[4] == 3);
assert(list.items[5] == 4);

const items = []const i32{1};
try list.insertSlice(0, items[0..0]);
assert(list.items[0] == 5);
assert(list.len == 6);
assert(list.items[0] == 1);
}
18 changes: 18 additions & 0 deletions std/mem.zig
Original file line number Diff line number Diff line change
@@ -125,6 +125,7 @@ pub const Allocator = struct {

/// Copy all of source into dest at position 0.
/// dest.len must be >= source.len.
/// dest.ptr must be <= src.ptr.
pub fn copy(comptime T: type, dest: []T, source: []const T) void {
// TODO instead of manually doing this check for the whole array
// and turning off runtime safety, the compiler should detect loops like
@@ -135,6 +136,23 @@ pub fn copy(comptime T: type, dest: []T, source: []const T) void {
dest[i] = s;
}

/// Copy all of source into dest at position 0.
/// dest.len must be >= source.len.
/// dest.ptr must be >= src.ptr.
pub fn copyBackwards(comptime T: type, dest: []T, source: []const T) void {
// TODO instead of manually doing this check for the whole array
// and turning off runtime safety, the compiler should detect loops like
// this and automatically omit safety checks for loops
@setRuntimeSafety(false);
assert(dest.len >= source.len);
var i = source.len;
while(i > 0){
i -= 1;
dest[i] = source[i];
}
}


pub fn set(comptime T: type, dest: []T, value: T) void {
for (dest) |*d|
d.* = value;