ArrayList: remove old (before span) API

This commit is contained in:
xackus 2020-04-11 21:06:56 +02:00 committed by Andrew Kelley
parent 3c34c313cf
commit dbc00e2424
7 changed files with 29 additions and 97 deletions

View file

@ -136,14 +136,14 @@ pub fn build(b: *Builder) !void {
}
fn dependOnLib(b: *Builder, lib_exe_obj: var, dep: LibraryDep) void {
for (dep.libdirs.toSliceConst()) |lib_dir| {
for (dep.libdirs.items) |lib_dir| {
lib_exe_obj.addLibPath(lib_dir);
}
const lib_dir = fs.path.join(
b.allocator,
&[_][]const u8{ dep.prefix, "lib" },
) catch unreachable;
for (dep.system_libs.toSliceConst()) |lib| {
for (dep.system_libs.items) |lib| {
const static_bare_name = if (mem.eql(u8, lib, "curses"))
@as([]const u8, "libncurses.a")
else
@ -159,10 +159,10 @@ fn dependOnLib(b: *Builder, lib_exe_obj: var, dep: LibraryDep) void {
lib_exe_obj.linkSystemLibrary(lib);
}
}
for (dep.libs.toSliceConst()) |lib| {
for (dep.libs.items) |lib| {
lib_exe_obj.addObjectFile(lib);
}
for (dep.includes.toSliceConst()) |include_path| {
for (dep.includes.items) |include_path| {
lib_exe_obj.addIncludeDir(include_path);
}
}

View file

@ -57,37 +57,13 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type {
return self.items;
}
/// Deprecated: use `items` field directly.
pub fn toSlice(self: Self) Slice {
return self.items;
}
/// Deprecated: use `items` field directly.
pub fn toSliceConst(self: Self) SliceConst {
return self.items;
}
/// Deprecated: use `list.items[i]`.
pub fn at(self: Self, i: usize) T {
return self.items[i];
}
/// Deprecated: use `&list.items[i]`.
pub fn ptrAt(self: Self, i: usize) *T {
return &self.items[i];
}
/// Deprecated: use `if (i >= list.items.len) return error.OutOfBounds else list.items[i] = item`.
pub fn setOrError(self: Self, i: usize, item: T) !void {
if (i >= self.items.len) return error.OutOfBounds;
self.items[i] = item;
}
/// Deprecated: use `list.items[i] = item`.
pub fn set(self: *Self, i: usize, item: T) void {
assert(i < self.items.len);
self.items[i] = item;
}
pub const toSlice = @compileError("deprecated: use `items` field directly");
pub const toSliceConst = @compileError("deprecated: use `items` field directly");
pub const at = @compileError("deprecated: use `list.items[i]`");
pub const ptrAt = @compileError("deprecated: use `&list.items[i]`");
pub const setOrError = @compileError("deprecated: use `if (i >= list.items.len) return error.OutOfBounds else list.items[i] = item`");
pub const set = @compileError("deprecated: use `list.items[i] = item`");
pub const swapRemoveOrError = @compileError("deprecated: use `if (i >= list.items.len) return error.OutOfBounds else list.swapRemove(i)`");
/// ArrayList takes ownership of the passed in slice. The slice must have been
/// allocated with `allocator`.
@ -167,12 +143,6 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type {
return old_item;
}
/// Deprecated: use `if (i >= list.items.len) return error.OutOfBounds else list.swapRemove(i)`.
pub fn swapRemoveOrError(self: *Self, i: usize) !T {
if (i >= self.items.len) return error.OutOfBounds;
return self.swapRemove(i);
}
/// Append the slice of items to the list. Allocates more
/// memory as necessary.
pub fn appendSlice(self: *Self, items: SliceConst) !void {
@ -308,9 +278,6 @@ test "std.ArrayList.basic" {
var list = ArrayList(i32).init(testing.allocator);
defer list.deinit();
// setting on empty list is out of bounds
testing.expectError(error.OutOfBounds, list.setOrError(0, 1));
{
var i: usize = 0;
while (i < 10) : (i += 1) {
@ -329,10 +296,6 @@ test "std.ArrayList.basic" {
testing.expect(v == @intCast(i32, i + 1));
}
for (list.toSliceConst()) |v, i| {
testing.expect(v == @intCast(i32, i + 1));
}
testing.expect(list.pop() == 10);
testing.expect(list.items.len == 9);
@ -347,11 +310,8 @@ test "std.ArrayList.basic" {
testing.expect(list.items.len == 9);
// can only set on indices < self.items.len
list.set(7, 33);
list.set(8, 42);
testing.expectError(error.OutOfBounds, list.setOrError(9, 99));
testing.expectError(error.OutOfBounds, list.setOrError(10, 123));
list.items[7] = 33;
list.items[8] = 42;
testing.expect(list.pop() == 42);
testing.expect(list.pop() == 33);
@ -428,34 +388,6 @@ test "std.ArrayList.swapRemove" {
testing.expect(list.items.len == 4);
}
test "std.ArrayList.swapRemoveOrError" {
var list = ArrayList(i32).init(testing.allocator);
defer list.deinit();
// Test just after initialization
testing.expectError(error.OutOfBounds, list.swapRemoveOrError(0));
// Test after adding one item and remote it
try list.append(1);
testing.expect((try list.swapRemoveOrError(0)) == 1);
testing.expectError(error.OutOfBounds, list.swapRemoveOrError(0));
// Test after adding two items and remote both
try list.append(1);
try list.append(2);
testing.expect((try list.swapRemoveOrError(1)) == 2);
testing.expect((try list.swapRemoveOrError(0)) == 1);
testing.expectError(error.OutOfBounds, list.swapRemoveOrError(0));
// Test out of bounds with one item
try list.append(1);
testing.expectError(error.OutOfBounds, list.swapRemoveOrError(1));
// Test out of bounds with two items
try list.append(2);
testing.expectError(error.OutOfBounds, list.swapRemoveOrError(2));
}
test "std.ArrayList.insert" {
var list = ArrayList(i32).init(testing.allocator);
defer list.deinit();

View file

@ -158,7 +158,7 @@ const FileLockTestContext = struct {
fn run_lock_file_test(contexts: []FileLockTestContext) !void {
var threads = std.ArrayList(*std.Thread).init(std.testing.allocator);
defer {
for (threads.toSlice()) |thread| {
for (threads.items) |thread| {
thread.wait();
}
threads.deinit();

View file

@ -200,7 +200,7 @@ pub const Headers = struct {
var i = dex.items.len;
while (i > 0) {
i -= 1;
const data_index = dex.at(i);
const data_index = dex.items[i];
const removed = self.data.orderedRemove(data_index);
assert(mem.eql(u8, removed.name, name));
removed.deinit();
@ -260,7 +260,7 @@ pub const Headers = struct {
/// Access the header at the specified index.
pub fn at(self: Self, i: usize) HeaderEntry {
return self.data.at(i);
return self.data.items[i];
}
/// Returns a list of indices containing headers with the given name.
@ -280,7 +280,7 @@ pub const Headers = struct {
const buf = try allocator.alloc(HeaderEntry, dex.items.len);
var n: usize = 0;
for (dex.span()) |idx| {
buf[n] = self.data.at(idx);
buf[n] = self.data.items[idx];
n += 1;
}
return buf;
@ -303,18 +303,18 @@ pub const Headers = struct {
const total_len = blk: {
var sum: usize = dex.items.len - 1; // space for separator(s)
for (dex.span()) |idx|
sum += self.data.at(idx).value.len;
sum += self.data.items[idx].value.len;
break :blk sum;
};
const buf = try allocator.alloc(u8, total_len);
errdefer allocator.free(buf);
const first_value = self.data.at(dex.at(0)).value;
const first_value = self.data.items[dex.items[0]].value;
mem.copy(u8, buf, first_value);
var buf_index: usize = first_value.len;
for (dex.toSlice()[1..]) |idx| {
const value = self.data.at(idx).value;
for (dex.items[1..]) |idx| {
const value = self.data.items[idx].value;
buf[buf_index] = ',';
buf_index += 1;
mem.copy(u8, buf[buf_index..], value);
@ -342,7 +342,7 @@ pub const Headers = struct {
}
pub fn sort(self: *Self) void {
std.sort.sort(HeaderEntry, self.data.toSlice(), HeaderEntry.compare);
std.sort.sort(HeaderEntry, self.data.items, HeaderEntry.compare);
self.rebuild_index();
}

View file

@ -1879,7 +1879,7 @@ pub const Parser = struct {
return ValueTree{
.arena = arena,
.root = p.stack.at(0),
.root = p.stack.items[0],
};
}
@ -2168,7 +2168,7 @@ test "json.parser.dynamic" {
const array_of_object = image.Object.get("ArrayOfObject").?.value;
testing.expect(array_of_object.Array.items.len == 1);
const obj0 = array_of_object.Array.at(0).Object.get("n").?.value;
const obj0 = array_of_object.Array.items[0].Object.get("n").?.value;
testing.expect(mem.eql(u8, obj0.String, "m"));
const double = image.Object.get("double").?.value;
@ -2222,8 +2222,8 @@ test "write json then parse it" {
testing.expect(tree.root.Object.get("f").?.value.Bool == false);
testing.expect(tree.root.Object.get("t").?.value.Bool == true);
testing.expect(tree.root.Object.get("int").?.value.Integer == 1234);
testing.expect(tree.root.Object.get("array").?.value.Array.at(0).Null == {});
testing.expect(tree.root.Object.get("array").?.value.Array.at(1).Float == 12.34);
testing.expect(tree.root.Object.get("array").?.value.Array.items[0].Null == {});
testing.expect(tree.root.Object.get("array").?.value.Array.items[1].Float == 12.34);
testing.expect(mem.eql(u8, tree.root.Object.get("str").?.value.String, "hello"));
}
@ -2247,7 +2247,7 @@ test "integer after float has proper type" {
\\ "ints": [1, 2, 3]
\\}
);
std.testing.expect(json.Object.getValue("ints").?.Array.at(0) == .Integer);
std.testing.expect(json.Object.getValue("ints").?.Array.items[0] == .Integer);
}
test "escaped characters" {

View file

@ -281,7 +281,7 @@ pub const LibCInstallation = struct {
var path_i: usize = 0;
while (path_i < search_paths.items.len) : (path_i += 1) {
// search in reverse order
const search_path_untrimmed = search_paths.at(search_paths.items.len - path_i - 1);
const search_path_untrimmed = search_paths.items[search_paths.items.len - path_i - 1];
const search_path = std.mem.trimLeft(u8, search_path_untrimmed, " ");
var search_dir = fs.cwd().openDir(search_path, .{}) catch |err| switch (err) {
error.FileNotFound,

View file

@ -880,7 +880,7 @@ pub const CompileErrorContext = struct {
var i: usize = 0;
ok = while (err_iter.next()) |line| : (i += 1) {
if (i >= self.case.expected_errors.items.len) break false;
const expected = self.case.expected_errors.at(i);
const expected = self.case.expected_errors.items[i];
if (mem.indexOf(u8, line, expected) == null) break false;
continue;
} else true;