2019-03-02 13:46:04 -08:00
|
|
|
const std = @import("std.zig");
|
2017-12-23 19:08:53 -08:00
|
|
|
const debug = std.debug;
|
2016-08-11 22:25:13 -07:00
|
|
|
const assert = debug.assert;
|
2019-02-08 15:18:47 -08:00
|
|
|
const testing = std.testing;
|
2017-12-23 19:08:53 -08:00
|
|
|
const mem = std.mem;
|
2016-05-08 01:34:00 -07:00
|
|
|
const Allocator = mem.Allocator;
|
|
|
|
|
2020-03-06 15:01:20 -08:00
|
|
|
/// A contiguous, growable list of items in memory.
|
|
|
|
/// This is a wrapper around an array of T values. Initialize with `init`.
|
2018-01-25 01:10:11 -08:00
|
|
|
pub fn ArrayList(comptime T: type) type {
|
2019-08-26 08:23:25 -07:00
|
|
|
return AlignedArrayList(T, null);
|
2017-12-10 16:40:46 -08:00
|
|
|
}
|
|
|
|
|
2019-08-26 08:23:25 -07:00
|
|
|
pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type {
|
2019-08-30 11:53:44 -07:00
|
|
|
if (alignment) |a| {
|
|
|
|
if (a == @alignOf(T)) {
|
|
|
|
return AlignedArrayList(T, null);
|
|
|
|
}
|
|
|
|
}
|
2018-11-13 05:08:37 -08:00
|
|
|
return struct {
|
2018-09-13 13:34:33 -07:00
|
|
|
const Self = @This();
|
2016-07-24 18:35:50 -07:00
|
|
|
|
2020-03-06 15:01:20 -08:00
|
|
|
/// Use `span` instead of slicing this directly, because if you don't
|
2017-02-28 00:07:11 -08:00
|
|
|
/// specify the end position of the slice, this will potentially give
|
|
|
|
/// you uninitialized memory.
|
2019-08-26 08:23:25 -07:00
|
|
|
items: Slice,
|
2016-12-18 16:40:26 -08:00
|
|
|
len: usize,
|
2018-05-31 07:56:59 -07:00
|
|
|
allocator: *Allocator,
|
2016-05-08 01:34:00 -07:00
|
|
|
|
2019-08-26 08:23:25 -07:00
|
|
|
pub const Slice = if (alignment) |a| ([]align(a) T) else []T;
|
|
|
|
pub const SliceConst = if (alignment) |a| ([]align(a) const T) else []const T;
|
|
|
|
|
2017-10-11 07:16:13 -07:00
|
|
|
/// Deinitialize with `deinit` or use `toOwnedSlice`.
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn init(allocator: *Allocator) Self {
|
2018-11-13 05:08:37 -08:00
|
|
|
return Self{
|
2019-11-27 00:30:39 -08:00
|
|
|
.items = &[_]T{},
|
2016-12-18 16:40:26 -08:00
|
|
|
.len = 0,
|
|
|
|
.allocator = allocator,
|
2017-12-21 21:50:30 -08:00
|
|
|
};
|
2016-09-22 23:00:23 -07:00
|
|
|
}
|
2019-12-09 12:56:19 -08:00
|
|
|
|
2019-11-23 19:54:33 -08:00
|
|
|
/// Initialize with capacity to hold at least num elements.
|
|
|
|
/// Deinitialize with `deinit` or use `toOwnedSlice`.
|
|
|
|
pub fn initCapacity(allocator: *Allocator, num: usize) !Self {
|
|
|
|
var self = Self.init(allocator);
|
|
|
|
try self.ensureCapacity(num);
|
|
|
|
return self;
|
|
|
|
}
|
2016-05-08 01:34:00 -07:00
|
|
|
|
2019-10-28 00:57:23 -07:00
|
|
|
/// Release all allocated memory.
|
2018-06-15 10:49:39 -07:00
|
|
|
pub fn deinit(self: Self) void {
|
2018-06-09 09:03:11 -07:00
|
|
|
self.allocator.free(self.items);
|
2016-12-18 16:40:26 -08:00
|
|
|
}
|
2016-09-22 23:00:23 -07:00
|
|
|
|
2019-10-28 00:57:23 -07:00
|
|
|
/// Return contents as a slice. Only valid while the list
|
|
|
|
/// doesn't change size.
|
2020-03-06 15:01:20 -08:00
|
|
|
pub fn span(self: var) @TypeOf(self.items[0..self.len]) {
|
2018-06-09 09:03:11 -07:00
|
|
|
return self.items[0..self.len];
|
2017-02-28 00:07:11 -08:00
|
|
|
}
|
|
|
|
|
2020-03-06 15:01:20 -08:00
|
|
|
/// Deprecated: use `span`.
|
|
|
|
pub fn toSlice(self: Self) Slice {
|
|
|
|
return self.span();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Deprecated: use `span`.
|
2019-08-26 08:23:25 -07:00
|
|
|
pub fn toSliceConst(self: Self) SliceConst {
|
2020-03-06 15:01:20 -08:00
|
|
|
return self.span();
|
2016-12-18 16:40:26 -08:00
|
|
|
}
|
2016-05-08 01:34:00 -07:00
|
|
|
|
2020-03-06 15:01:20 -08:00
|
|
|
/// Deprecated: use `span()[i]`.
|
2018-07-14 07:01:45 -07:00
|
|
|
pub fn at(self: Self, i: usize) T {
|
2020-03-06 15:01:20 -08:00
|
|
|
return self.span()[i];
|
2018-02-08 08:22:31 -08:00
|
|
|
}
|
|
|
|
|
2020-03-06 15:01:20 -08:00
|
|
|
/// Deprecated: use `&span()[i]`.
|
2019-11-12 00:14:38 -08:00
|
|
|
pub fn ptrAt(self: Self, i: usize) *T {
|
2020-03-06 15:01:20 -08:00
|
|
|
return &self.span()[i];
|
2019-11-12 00:14:38 -08:00
|
|
|
}
|
|
|
|
|
2020-03-06 15:01:20 -08:00
|
|
|
/// Deprecated: use `if (i >= list.len) return error.OutOfBounds else span()[i] = item`.
|
2018-06-15 10:49:39 -07:00
|
|
|
pub fn setOrError(self: Self, i: usize, item: T) !void {
|
2018-06-09 09:03:11 -07:00
|
|
|
if (i >= self.len) return error.OutOfBounds;
|
2018-06-15 10:49:39 -07:00
|
|
|
self.items[i] = item;
|
2018-06-09 09:03:11 -07:00
|
|
|
}
|
|
|
|
|
2020-03-06 15:01:20 -08:00
|
|
|
/// Deprecated: use `list.span()[i] = item`.
|
2018-06-15 10:49:39 -07:00
|
|
|
pub fn set(self: *Self, i: usize, item: T) void {
|
2018-06-09 09:03:11 -07:00
|
|
|
assert(i < self.len);
|
2018-06-15 10:49:39 -07:00
|
|
|
self.items[i] = item;
|
2018-06-07 07:00:27 -07:00
|
|
|
}
|
|
|
|
|
2019-10-28 00:57:23 -07:00
|
|
|
/// Return the maximum number of items the list can hold
|
|
|
|
/// without allocating more memory.
|
2018-09-09 03:54:00 -07:00
|
|
|
pub fn capacity(self: Self) usize {
|
|
|
|
return self.items.len;
|
|
|
|
}
|
|
|
|
|
2017-10-11 07:16:13 -07:00
|
|
|
/// ArrayList takes ownership of the passed in slice. The slice must have been
|
|
|
|
/// allocated with `allocator`.
|
|
|
|
/// Deinitialize with `deinit` or use `toOwnedSlice`.
|
2019-08-26 08:23:25 -07:00
|
|
|
pub fn fromOwnedSlice(allocator: *Allocator, slice: Slice) Self {
|
2018-11-13 05:08:37 -08:00
|
|
|
return Self{
|
2017-10-11 07:16:13 -07:00
|
|
|
.items = slice,
|
|
|
|
.len = slice.len,
|
|
|
|
.allocator = allocator,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The caller owns the returned memory. ArrayList becomes empty.
|
2019-08-26 08:23:25 -07:00
|
|
|
pub fn toOwnedSlice(self: *Self) Slice {
|
2017-10-11 07:16:13 -07:00
|
|
|
const allocator = self.allocator;
|
2019-03-15 14:47:47 -07:00
|
|
|
const result = allocator.shrink(self.items, self.len);
|
2018-04-30 22:53:04 -07:00
|
|
|
self.* = init(allocator);
|
2017-10-11 07:16:13 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-12-10 12:08:10 -08:00
|
|
|
/// Insert `item` at index `n`. Moves `list[n .. list.len]`
|
2019-10-28 00:57:23 -07:00
|
|
|
/// to make room.
|
2018-06-15 10:49:39 -07:00
|
|
|
pub fn insert(self: *Self, n: usize, item: T) !void {
|
2018-06-09 09:03:11 -07:00
|
|
|
try self.ensureCapacity(self.len + 1);
|
|
|
|
self.len += 1;
|
2018-02-08 08:22:31 -08:00
|
|
|
|
2018-07-13 14:01:21 -07:00
|
|
|
mem.copyBackwards(T, self.items[n + 1 .. self.len], self.items[n .. self.len - 1]);
|
2018-06-15 10:49:39 -07:00
|
|
|
self.items[n] = item;
|
2018-02-08 08:22:31 -08:00
|
|
|
}
|
|
|
|
|
2020-03-06 15:01:20 -08:00
|
|
|
/// Insert slice `items` at index `i`. Moves
|
|
|
|
/// `list[i .. list.len]` to make room.
|
|
|
|
/// This operation is O(N).
|
|
|
|
pub fn insertSlice(self: *Self, i: usize, items: SliceConst) !void {
|
2018-06-09 09:03:11 -07:00
|
|
|
try self.ensureCapacity(self.len + items.len);
|
|
|
|
self.len += items.len;
|
2018-02-08 08:22:31 -08:00
|
|
|
|
2020-03-06 15:01:20 -08:00
|
|
|
mem.copyBackwards(T, self.items[i + items.len .. self.len], self.items[i .. self.len - items.len]);
|
|
|
|
mem.copy(T, self.items[i .. i + items.len], items);
|
2018-02-08 08:22:31 -08:00
|
|
|
}
|
|
|
|
|
2020-03-06 15:01:20 -08:00
|
|
|
/// Extend the list by 1 element. Allocates more memory as necessary.
|
2018-06-15 10:49:39 -07:00
|
|
|
pub fn append(self: *Self, item: T) !void {
|
2018-06-09 09:03:11 -07:00
|
|
|
const new_item_ptr = try self.addOne();
|
2018-06-15 10:49:39 -07:00
|
|
|
new_item_ptr.* = item;
|
2016-12-18 16:40:26 -08:00
|
|
|
}
|
2018-07-14 07:01:45 -07:00
|
|
|
|
2019-10-28 00:57:23 -07:00
|
|
|
/// Extend the list by 1 element, but asserting `self.capacity`
|
|
|
|
/// is sufficient to hold an additional item.
|
2018-09-09 03:54:00 -07:00
|
|
|
pub fn appendAssumeCapacity(self: *Self, item: T) void {
|
|
|
|
const new_item_ptr = self.addOneAssumeCapacity();
|
|
|
|
new_item_ptr.* = item;
|
|
|
|
}
|
|
|
|
|
2020-03-06 15:01:20 -08:00
|
|
|
/// Remove the element at index `i` from the list and return its value.
|
|
|
|
/// Asserts the array has at least one item.
|
|
|
|
/// This operation is O(N).
|
2019-04-12 03:23:44 -07:00
|
|
|
pub fn orderedRemove(self: *Self, i: usize) T {
|
|
|
|
const newlen = self.len - 1;
|
|
|
|
if (newlen == i) return self.pop();
|
|
|
|
|
|
|
|
const old_item = self.at(i);
|
|
|
|
for (self.items[i..newlen]) |*b, j| b.* = self.items[i + 1 + j];
|
|
|
|
self.items[newlen] = undefined;
|
|
|
|
self.len = newlen;
|
|
|
|
return old_item;
|
|
|
|
}
|
|
|
|
|
2018-07-11 10:52:30 -07:00
|
|
|
/// Removes the element at the specified index and returns it.
|
2018-07-14 07:01:45 -07:00
|
|
|
/// The empty slot is filled from the end of the list.
|
2020-03-06 15:01:20 -08:00
|
|
|
/// This operation is O(1).
|
2018-07-14 07:01:45 -07:00
|
|
|
pub fn swapRemove(self: *Self, i: usize) T {
|
|
|
|
if (self.len - 1 == i) return self.pop();
|
|
|
|
|
2020-03-06 15:01:20 -08:00
|
|
|
const slice = self.span();
|
2018-07-14 07:01:45 -07:00
|
|
|
const old_item = slice[i];
|
|
|
|
slice[i] = self.pop();
|
2018-07-11 10:52:30 -07:00
|
|
|
return old_item;
|
|
|
|
}
|
2018-07-14 07:01:45 -07:00
|
|
|
|
2020-03-06 15:01:20 -08:00
|
|
|
/// Deprecated: use `if (i >= list.len) return error.OutOfBounds else list.swapRemove(i)`.
|
2018-07-17 07:29:42 -07:00
|
|
|
pub fn swapRemoveOrError(self: *Self, i: usize) !T {
|
|
|
|
if (i >= self.len) return error.OutOfBounds;
|
|
|
|
return self.swapRemove(i);
|
|
|
|
}
|
|
|
|
|
2019-10-28 00:57:23 -07:00
|
|
|
/// Append the slice of items to the list. Allocates more
|
|
|
|
/// memory as necessary.
|
2019-08-26 08:23:25 -07:00
|
|
|
pub fn appendSlice(self: *Self, items: SliceConst) !void {
|
2018-06-09 09:03:11 -07:00
|
|
|
try self.ensureCapacity(self.len + items.len);
|
|
|
|
mem.copy(T, self.items[self.len..], items);
|
|
|
|
self.len += items.len;
|
2017-09-09 10:49:40 -07:00
|
|
|
}
|
|
|
|
|
2020-04-01 08:56:39 -07:00
|
|
|
/// Same as `append` except it returns the number of bytes written, which is always the same
|
|
|
|
/// as `m.len`. The purpose of this function existing is to match `std.io.OutStream` API.
|
|
|
|
/// This function may be called only when `T` is `u8`.
|
|
|
|
fn appendWrite(self: *Self, m: []const u8) !usize {
|
|
|
|
try self.appendSlice(m);
|
|
|
|
return m.len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Initializes an OutStream which will append to the list.
|
|
|
|
/// This function may be called only when `T` is `u8`.
|
|
|
|
pub fn outStream(self: *Self) std.io.OutStream(*Self, error{OutOfMemory}, appendWrite) {
|
|
|
|
return .{ .context = self };
|
|
|
|
}
|
2020-03-01 00:13:26 -08:00
|
|
|
|
2020-04-01 08:56:39 -07:00
|
|
|
/// Append a value to the list `n` times.
|
|
|
|
/// Allocates more memory as necessary.
|
2020-02-19 08:33:35 -08:00
|
|
|
pub fn appendNTimes(self: *Self, value: T, n: usize) !void {
|
|
|
|
const old_len = self.len;
|
|
|
|
try self.resize(self.len + n);
|
|
|
|
mem.set(T, self.items[old_len..self.len], value);
|
|
|
|
}
|
|
|
|
|
2020-04-01 08:56:39 -07:00
|
|
|
/// Adjust the list's length to `new_len`.
|
|
|
|
/// Does not initialize added items if any.
|
2018-06-09 09:03:11 -07:00
|
|
|
pub fn resize(self: *Self, new_len: usize) !void {
|
|
|
|
try self.ensureCapacity(new_len);
|
|
|
|
self.len = new_len;
|
2016-12-18 16:40:26 -08:00
|
|
|
}
|
2016-05-08 01:34:00 -07:00
|
|
|
|
2019-10-28 00:57:23 -07:00
|
|
|
/// Reduce allocated capacity to `new_len`.
|
2020-03-06 15:01:20 -08:00
|
|
|
/// Invalidates element pointers.
|
2018-06-09 09:03:11 -07:00
|
|
|
pub fn shrink(self: *Self, new_len: usize) void {
|
|
|
|
assert(new_len <= self.len);
|
|
|
|
self.len = new_len;
|
2019-03-15 14:47:47 -07:00
|
|
|
self.items = self.allocator.realloc(self.items, new_len) catch |e| switch (e) {
|
|
|
|
error.OutOfMemory => return, // no problem, capacity is still correct then.
|
|
|
|
};
|
2017-04-20 22:56:12 -07:00
|
|
|
}
|
|
|
|
|
2018-06-09 09:03:11 -07:00
|
|
|
pub fn ensureCapacity(self: *Self, new_capacity: usize) !void {
|
2018-09-09 03:54:00 -07:00
|
|
|
var better_capacity = self.capacity();
|
2016-12-18 16:40:26 -08:00
|
|
|
if (better_capacity >= new_capacity) return;
|
|
|
|
while (true) {
|
|
|
|
better_capacity += better_capacity / 2 + 8;
|
|
|
|
if (better_capacity >= new_capacity) break;
|
|
|
|
}
|
2019-03-15 14:47:47 -07:00
|
|
|
self.items = try self.allocator.realloc(self.items, better_capacity);
|
2016-05-08 01:34:00 -07:00
|
|
|
}
|
2017-02-28 00:07:11 -08:00
|
|
|
|
2020-03-06 15:01:20 -08:00
|
|
|
/// Increases the array's length to match the full capacity that is already allocated.
|
|
|
|
/// The new elements have `undefined` values. This operation does not invalidate any
|
|
|
|
/// element pointers.
|
|
|
|
pub fn expandToCapacity(self: *Self) void {
|
|
|
|
self.len = self.items.len;
|
|
|
|
}
|
|
|
|
|
2019-10-28 00:57:23 -07:00
|
|
|
/// Increase length by 1, returning pointer to the new item.
|
2020-03-06 15:01:20 -08:00
|
|
|
/// The returned pointer becomes invalid when the list is resized.
|
2018-06-09 09:03:11 -07:00
|
|
|
pub fn addOne(self: *Self) !*T {
|
|
|
|
const new_length = self.len + 1;
|
|
|
|
try self.ensureCapacity(new_length);
|
2018-09-09 03:54:00 -07:00
|
|
|
return self.addOneAssumeCapacity();
|
|
|
|
}
|
|
|
|
|
2020-03-06 15:01:20 -08:00
|
|
|
/// Increase length by 1, returning pointer to the new item.
|
|
|
|
/// Asserts that there is already space for the new item without allocating more.
|
|
|
|
/// The returned pointer becomes invalid when the list is resized.
|
2018-09-09 03:54:00 -07:00
|
|
|
pub fn addOneAssumeCapacity(self: *Self) *T {
|
2019-12-10 12:08:10 -08:00
|
|
|
assert(self.len < self.capacity());
|
2018-06-09 09:03:11 -07:00
|
|
|
const result = &self.items[self.len];
|
2018-09-09 03:54:00 -07:00
|
|
|
self.len += 1;
|
2017-02-28 00:07:11 -08:00
|
|
|
return result;
|
|
|
|
}
|
2017-04-19 23:26:36 -07:00
|
|
|
|
2020-03-06 15:01:20 -08:00
|
|
|
/// Remove and return the last element from the list.
|
|
|
|
/// Asserts the list has at least one item.
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn pop(self: *Self) T {
|
2017-04-19 23:26:36 -07:00
|
|
|
self.len -= 1;
|
|
|
|
return self.items[self.len];
|
|
|
|
}
|
2017-12-10 16:40:46 -08:00
|
|
|
|
2020-03-06 15:01:20 -08:00
|
|
|
/// Remove and return the last element from the list.
|
|
|
|
/// If the list is empty, returns `null`.
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn popOrNull(self: *Self) ?T {
|
2018-04-30 22:53:04 -07:00
|
|
|
if (self.len == 0) return null;
|
2017-12-10 16:40:46 -08:00
|
|
|
return self.pop();
|
|
|
|
}
|
2017-12-21 21:50:30 -08:00
|
|
|
};
|
2016-05-08 01:34:00 -07:00
|
|
|
}
|
|
|
|
|
2018-09-09 03:54:00 -07:00
|
|
|
test "std.ArrayList.init" {
|
2020-01-31 17:06:50 -08:00
|
|
|
var list = ArrayList(i32).init(testing.allocator);
|
2018-09-09 03:54:00 -07:00
|
|
|
defer list.deinit();
|
|
|
|
|
2019-12-10 12:08:10 -08:00
|
|
|
testing.expect(list.len == 0);
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(list.capacity() == 0);
|
2018-09-09 03:54:00 -07:00
|
|
|
}
|
|
|
|
|
2019-11-23 19:54:33 -08:00
|
|
|
test "std.ArrayList.initCapacity" {
|
2020-01-31 17:06:50 -08:00
|
|
|
var list = try ArrayList(i8).initCapacity(testing.allocator, 200);
|
2019-11-23 19:54:33 -08:00
|
|
|
defer list.deinit();
|
2019-12-10 12:08:10 -08:00
|
|
|
testing.expect(list.len == 0);
|
2019-11-23 19:54:33 -08:00
|
|
|
testing.expect(list.capacity() >= 200);
|
|
|
|
}
|
|
|
|
|
2018-07-17 07:28:08 -07:00
|
|
|
test "std.ArrayList.basic" {
|
2020-01-31 17:06:50 -08:00
|
|
|
var list = ArrayList(i32).init(testing.allocator);
|
2016-05-08 01:34:00 -07:00
|
|
|
defer list.deinit();
|
|
|
|
|
2018-06-09 09:03:11 -07:00
|
|
|
// setting on empty list is out of bounds
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expectError(error.OutOfBounds, list.setOrError(0, 1));
|
2018-06-07 07:00:27 -07:00
|
|
|
|
2018-04-30 22:53:04 -07:00
|
|
|
{
|
|
|
|
var i: usize = 0;
|
|
|
|
while (i < 10) : (i += 1) {
|
2018-06-16 23:57:07 -07:00
|
|
|
list.append(@intCast(i32, i + 1)) catch unreachable;
|
2018-04-30 22:53:04 -07:00
|
|
|
}
|
|
|
|
}
|
2016-05-08 01:34:00 -07:00
|
|
|
|
2018-04-30 22:53:04 -07:00
|
|
|
{
|
|
|
|
var i: usize = 0;
|
|
|
|
while (i < 10) : (i += 1) {
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(list.items[i] == @intCast(i32, i + 1));
|
2018-04-30 22:53:04 -07:00
|
|
|
}
|
|
|
|
}
|
2017-04-19 23:26:36 -07:00
|
|
|
|
2020-03-06 15:01:20 -08:00
|
|
|
for (list.span()) |v, i| {
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(v == @intCast(i32, i + 1));
|
2018-05-04 14:48:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for (list.toSliceConst()) |v, i| {
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(v == @intCast(i32, i + 1));
|
2018-05-04 14:48:14 -07:00
|
|
|
}
|
|
|
|
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(list.pop() == 10);
|
|
|
|
testing.expect(list.len == 9);
|
2017-09-09 10:49:40 -07:00
|
|
|
|
2019-11-27 00:30:39 -08:00
|
|
|
list.appendSlice(&[_]i32{ 1, 2, 3 }) catch unreachable;
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(list.len == 12);
|
|
|
|
testing.expect(list.pop() == 3);
|
|
|
|
testing.expect(list.pop() == 2);
|
|
|
|
testing.expect(list.pop() == 1);
|
|
|
|
testing.expect(list.len == 9);
|
2017-09-09 10:49:40 -07:00
|
|
|
|
2019-11-27 00:30:39 -08:00
|
|
|
list.appendSlice(&[_]i32{}) catch unreachable;
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(list.len == 9);
|
2018-06-09 09:03:11 -07:00
|
|
|
|
2018-06-07 07:00:27 -07:00
|
|
|
// can only set on indices < self.len
|
2018-06-09 09:03:11 -07:00
|
|
|
list.set(7, 33);
|
|
|
|
list.set(8, 42);
|
|
|
|
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expectError(error.OutOfBounds, list.setOrError(9, 99));
|
|
|
|
testing.expectError(error.OutOfBounds, list.setOrError(10, 123));
|
2018-06-07 07:00:27 -07:00
|
|
|
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(list.pop() == 42);
|
|
|
|
testing.expect(list.pop() == 33);
|
2016-05-08 01:34:00 -07:00
|
|
|
}
|
2018-02-08 08:22:31 -08:00
|
|
|
|
2020-02-19 08:33:35 -08:00
|
|
|
test "std.ArrayList.appendNTimes" {
|
|
|
|
var list = ArrayList(i32).init(testing.allocator);
|
|
|
|
defer list.deinit();
|
|
|
|
|
|
|
|
try list.appendNTimes(2, 10);
|
|
|
|
testing.expectEqual(@as(usize, 10), list.len);
|
2020-03-06 15:01:20 -08:00
|
|
|
for (list.span()) |element| {
|
2020-02-19 08:33:35 -08:00
|
|
|
testing.expectEqual(@as(i32, 2), element);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
test "std.ArrayList.appendNTimes with failing allocator" {
|
|
|
|
var list = ArrayList(i32).init(testing.failing_allocator);
|
|
|
|
defer list.deinit();
|
|
|
|
testing.expectError(error.OutOfMemory, list.appendNTimes(2, 10));
|
|
|
|
}
|
|
|
|
|
2019-04-12 03:23:44 -07:00
|
|
|
test "std.ArrayList.orderedRemove" {
|
2020-01-29 19:22:01 -08:00
|
|
|
var list = ArrayList(i32).init(testing.allocator);
|
2019-04-12 03:23:44 -07:00
|
|
|
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
|
2019-11-06 20:25:57 -08:00
|
|
|
testing.expectEqual(@as(i32, 4), list.orderedRemove(3));
|
|
|
|
testing.expectEqual(@as(i32, 5), list.at(3));
|
|
|
|
testing.expectEqual(@as(usize, 6), list.len);
|
2019-04-12 03:23:44 -07:00
|
|
|
|
|
|
|
//remove from end
|
2019-11-06 20:25:57 -08:00
|
|
|
testing.expectEqual(@as(i32, 7), list.orderedRemove(5));
|
|
|
|
testing.expectEqual(@as(usize, 5), list.len);
|
2019-04-12 03:23:44 -07:00
|
|
|
|
|
|
|
//remove from front
|
2019-11-06 20:25:57 -08:00
|
|
|
testing.expectEqual(@as(i32, 1), list.orderedRemove(0));
|
|
|
|
testing.expectEqual(@as(i32, 2), list.at(0));
|
|
|
|
testing.expectEqual(@as(usize, 4), list.len);
|
2019-04-12 03:23:44 -07:00
|
|
|
}
|
|
|
|
|
2018-07-14 07:01:45 -07:00
|
|
|
test "std.ArrayList.swapRemove" {
|
2020-01-29 19:22:01 -08:00
|
|
|
var list = ArrayList(i32).init(testing.allocator);
|
2018-07-11 10:52:30 -07:00
|
|
|
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);
|
2018-07-14 07:01:45 -07:00
|
|
|
|
2018-07-11 10:52:30 -07:00
|
|
|
//remove from middle
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(list.swapRemove(3) == 4);
|
|
|
|
testing.expect(list.at(3) == 7);
|
|
|
|
testing.expect(list.len == 6);
|
2018-07-14 07:01:45 -07:00
|
|
|
|
2018-07-11 10:52:30 -07:00
|
|
|
//remove from end
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(list.swapRemove(5) == 6);
|
|
|
|
testing.expect(list.len == 5);
|
2018-07-14 07:01:45 -07:00
|
|
|
|
2018-07-11 10:52:30 -07:00
|
|
|
//remove from front
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(list.swapRemove(0) == 1);
|
|
|
|
testing.expect(list.at(0) == 5);
|
|
|
|
testing.expect(list.len == 4);
|
2018-07-11 10:52:30 -07:00
|
|
|
}
|
|
|
|
|
2018-07-17 07:29:42 -07:00
|
|
|
test "std.ArrayList.swapRemoveOrError" {
|
2020-01-29 19:22:01 -08:00
|
|
|
var list = ArrayList(i32).init(testing.allocator);
|
2018-07-17 07:29:42 -07:00
|
|
|
defer list.deinit();
|
|
|
|
|
|
|
|
// Test just after initialization
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expectError(error.OutOfBounds, list.swapRemoveOrError(0));
|
2018-07-17 07:29:42 -07:00
|
|
|
|
|
|
|
// Test after adding one item and remote it
|
|
|
|
try list.append(1);
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect((try list.swapRemoveOrError(0)) == 1);
|
|
|
|
testing.expectError(error.OutOfBounds, list.swapRemoveOrError(0));
|
2018-07-17 07:29:42 -07:00
|
|
|
|
|
|
|
// Test after adding two items and remote both
|
|
|
|
try list.append(1);
|
|
|
|
try list.append(2);
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect((try list.swapRemoveOrError(1)) == 2);
|
|
|
|
testing.expect((try list.swapRemoveOrError(0)) == 1);
|
|
|
|
testing.expectError(error.OutOfBounds, list.swapRemoveOrError(0));
|
2018-07-17 07:29:42 -07:00
|
|
|
|
|
|
|
// Test out of bounds with one item
|
|
|
|
try list.append(1);
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expectError(error.OutOfBounds, list.swapRemoveOrError(1));
|
2018-07-17 07:29:42 -07:00
|
|
|
|
|
|
|
// Test out of bounds with two items
|
|
|
|
try list.append(2);
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expectError(error.OutOfBounds, list.swapRemoveOrError(2));
|
2018-07-17 07:29:42 -07:00
|
|
|
}
|
|
|
|
|
2018-07-17 07:28:08 -07:00
|
|
|
test "std.ArrayList.insert" {
|
2020-01-29 19:22:01 -08:00
|
|
|
var list = ArrayList(i32).init(testing.allocator);
|
2018-02-08 08:22:31 -08:00
|
|
|
defer list.deinit();
|
|
|
|
|
|
|
|
try list.append(1);
|
2018-07-13 13:35:34 -07:00
|
|
|
try list.append(2);
|
|
|
|
try list.append(3);
|
2018-02-08 08:22:31 -08:00
|
|
|
try list.insert(0, 5);
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(list.items[0] == 5);
|
|
|
|
testing.expect(list.items[1] == 1);
|
|
|
|
testing.expect(list.items[2] == 2);
|
|
|
|
testing.expect(list.items[3] == 3);
|
2018-07-13 13:35:34 -07:00
|
|
|
}
|
|
|
|
|
2018-07-17 07:28:08 -07:00
|
|
|
test "std.ArrayList.insertSlice" {
|
2020-01-29 19:22:01 -08:00
|
|
|
var list = ArrayList(i32).init(testing.allocator);
|
2018-07-13 13:35:34 -07:00
|
|
|
defer list.deinit();
|
2018-02-08 08:22:31 -08:00
|
|
|
|
2018-07-13 13:35:34 -07:00
|
|
|
try list.append(1);
|
|
|
|
try list.append(2);
|
|
|
|
try list.append(3);
|
|
|
|
try list.append(4);
|
2019-11-27 00:30:39 -08:00
|
|
|
try list.insertSlice(1, &[_]i32{ 9, 8 });
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(list.items[0] == 1);
|
|
|
|
testing.expect(list.items[1] == 9);
|
|
|
|
testing.expect(list.items[2] == 8);
|
|
|
|
testing.expect(list.items[3] == 2);
|
|
|
|
testing.expect(list.items[4] == 3);
|
|
|
|
testing.expect(list.items[5] == 4);
|
2018-02-08 08:22:31 -08:00
|
|
|
|
2019-06-09 16:24:24 -07:00
|
|
|
const items = [_]i32{1};
|
2018-02-08 08:22:31 -08:00
|
|
|
try list.insertSlice(0, items[0..0]);
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(list.len == 6);
|
|
|
|
testing.expect(list.items[0] == 1);
|
2018-02-08 08:22:31 -08:00
|
|
|
}
|
2018-12-20 05:49:09 -08:00
|
|
|
|
|
|
|
const Item = struct {
|
|
|
|
integer: i32,
|
|
|
|
sub_items: ArrayList(Item),
|
|
|
|
};
|
|
|
|
|
|
|
|
test "std.ArrayList: ArrayList(T) of struct T" {
|
2020-01-29 19:22:01 -08:00
|
|
|
var root = Item{ .integer = 1, .sub_items = ArrayList(Item).init(testing.allocator) };
|
2020-01-29 11:18:04 -08:00
|
|
|
defer root.sub_items.deinit();
|
2020-01-29 19:22:01 -08:00
|
|
|
try root.sub_items.append(Item{ .integer = 42, .sub_items = ArrayList(Item).init(testing.allocator) });
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(root.sub_items.items[0].integer == 42);
|
2018-12-20 05:49:09 -08:00
|
|
|
}
|
2020-03-01 00:13:26 -08:00
|
|
|
|
|
|
|
test "std.ArrayList(u8) implements outStream" {
|
|
|
|
var buffer = ArrayList(u8).init(std.testing.allocator);
|
|
|
|
defer buffer.deinit();
|
|
|
|
|
|
|
|
const x: i32 = 42;
|
|
|
|
const y: i32 = 1234;
|
|
|
|
try buffer.outStream().print("x: {}\ny: {}\n", .{ x, y });
|
|
|
|
|
|
|
|
testing.expectEqualSlices(u8, "x: 42\ny: 1234\n", buffer.span());
|
|
|
|
}
|