2017-12-23 19:08:53 -08:00
|
|
|
const std = @import("index.zig");
|
|
|
|
const debug = std.debug;
|
2016-08-11 22:25:13 -07:00
|
|
|
const assert = debug.assert;
|
2017-12-23 19:08:53 -08:00
|
|
|
const mem = std.mem;
|
2016-05-08 01:34:00 -07:00
|
|
|
const Allocator = mem.Allocator;
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
pub fn ArrayList(comptime T: type) type {
|
2017-12-10 16:40:46 -08:00
|
|
|
return AlignedArrayList(T, @alignOf(T));
|
|
|
|
}
|
|
|
|
|
2018-04-30 22:53:04 -07:00
|
|
|
pub fn AlignedArrayList(comptime T: type, comptime A: u29) type {
|
2017-12-21 21:50:30 -08:00
|
|
|
return struct {
|
2016-12-18 16:40:26 -08:00
|
|
|
const Self = this;
|
2016-07-24 18:35:50 -07:00
|
|
|
|
2017-02-28 00:07:11 -08:00
|
|
|
/// Use toSlice instead of slicing this directly, because if you don't
|
|
|
|
/// specify the end position of the slice, this will potentially give
|
|
|
|
/// you uninitialized memory.
|
2017-12-10 16:40:46 -08:00
|
|
|
items: []align(A) T,
|
2016-12-18 16:40:26 -08:00
|
|
|
len: usize,
|
|
|
|
allocator: &Allocator,
|
2016-05-08 01:34:00 -07:00
|
|
|
|
2017-10-11 07:16:13 -07:00
|
|
|
/// Deinitialize with `deinit` or use `toOwnedSlice`.
|
2018-01-25 01:10:11 -08:00
|
|
|
pub fn init(allocator: &Allocator) Self {
|
2018-04-30 22:53:04 -07:00
|
|
|
return Self{
|
2017-12-10 16:40:46 -08:00
|
|
|
.items = []align(A) 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
|
|
|
}
|
2016-05-08 01:34:00 -07:00
|
|
|
|
2018-05-04 14:48:14 -07:00
|
|
|
pub fn deinit(l: &const Self) void {
|
2017-01-22 21:11:21 -08:00
|
|
|
l.allocator.free(l.items);
|
2016-12-18 16:40:26 -08:00
|
|
|
}
|
2016-09-22 23:00:23 -07:00
|
|
|
|
2018-05-04 14:48:14 -07:00
|
|
|
pub fn toSlice(l: &const Self) []align(A) T {
|
2017-05-19 07:39:59 -07:00
|
|
|
return l.items[0..l.len];
|
2017-02-28 00:07:11 -08:00
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
pub fn toSliceConst(l: &const Self) []align(A) const T {
|
2017-05-19 07:39:59 -07:00
|
|
|
return l.items[0..l.len];
|
2016-12-18 16:40:26 -08:00
|
|
|
}
|
2016-05-08 01:34:00 -07:00
|
|
|
|
2018-02-08 08:22:31 -08:00
|
|
|
pub fn at(l: &const Self, n: usize) T {
|
|
|
|
return l.toSliceConst()[n];
|
|
|
|
}
|
|
|
|
|
2018-05-03 06:54:33 -07:00
|
|
|
pub fn count(self: &const Self) usize {
|
|
|
|
return self.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`.
|
2018-01-25 01:10:11 -08:00
|
|
|
pub fn fromOwnedSlice(allocator: &Allocator, slice: []align(A) T) Self {
|
2018-04-30 22:53:04 -07: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.
|
2018-01-25 01:10:11 -08:00
|
|
|
pub fn toOwnedSlice(self: &Self) []align(A) T {
|
2017-10-11 07:16:13 -07:00
|
|
|
const allocator = self.allocator;
|
2017-12-10 16:40:46 -08:00
|
|
|
const result = allocator.alignedShrink(T, A, 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;
|
|
|
|
}
|
|
|
|
|
2018-02-08 17:45:26 -08:00
|
|
|
pub fn insert(l: &Self, n: usize, item: &const T) !void {
|
2018-02-08 08:22:31 -08:00
|
|
|
try l.ensureCapacity(l.len + 1);
|
|
|
|
l.len += 1;
|
|
|
|
|
2018-04-30 22:53:04 -07:00
|
|
|
mem.copy(T, l.items[n + 1..l.len], l.items[n..l.len - 1]);
|
|
|
|
l.items[n] = item.*;
|
2018-02-08 08:22:31 -08:00
|
|
|
}
|
|
|
|
|
2018-02-08 17:45:26 -08:00
|
|
|
pub fn insertSlice(l: &Self, n: usize, items: []align(A) const T) !void {
|
2018-02-08 08:22:31 -08:00
|
|
|
try l.ensureCapacity(l.len + items.len);
|
|
|
|
l.len += items.len;
|
|
|
|
|
2018-04-30 22:53:04 -07:00
|
|
|
mem.copy(T, l.items[n + items.len..l.len], l.items[n..l.len - items.len]);
|
|
|
|
mem.copy(T, l.items[n..n + items.len], items);
|
2018-02-08 08:22:31 -08:00
|
|
|
}
|
|
|
|
|
2018-01-31 19:48:40 -08:00
|
|
|
pub fn append(l: &Self, item: &const T) !void {
|
2018-01-07 13:51:46 -08:00
|
|
|
const new_item_ptr = try l.addOne();
|
2018-04-30 22:53:04 -07:00
|
|
|
new_item_ptr.* = item.*;
|
2016-12-18 16:40:26 -08:00
|
|
|
}
|
2016-08-11 22:25:13 -07:00
|
|
|
|
2018-01-31 19:48:40 -08:00
|
|
|
pub fn appendSlice(l: &Self, items: []align(A) const T) !void {
|
2018-01-07 13:51:46 -08:00
|
|
|
try l.ensureCapacity(l.len + items.len);
|
2017-09-09 10:49:40 -07:00
|
|
|
mem.copy(T, l.items[l.len..], items);
|
|
|
|
l.len += items.len;
|
|
|
|
}
|
|
|
|
|
2018-01-31 19:48:40 -08:00
|
|
|
pub fn resize(l: &Self, new_len: usize) !void {
|
2018-01-07 13:51:46 -08:00
|
|
|
try l.ensureCapacity(new_len);
|
2016-12-18 16:40:26 -08:00
|
|
|
l.len = new_len;
|
|
|
|
}
|
2016-05-08 01:34:00 -07:00
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
pub fn shrink(l: &Self, new_len: usize) void {
|
2017-04-20 22:56:12 -07:00
|
|
|
assert(new_len <= l.len);
|
|
|
|
l.len = new_len;
|
|
|
|
}
|
|
|
|
|
2018-01-31 19:48:40 -08:00
|
|
|
pub fn ensureCapacity(l: &Self, new_capacity: usize) !void {
|
2016-12-18 16:40:26 -08:00
|
|
|
var better_capacity = l.items.len;
|
|
|
|
if (better_capacity >= new_capacity) return;
|
|
|
|
while (true) {
|
|
|
|
better_capacity += better_capacity / 2 + 8;
|
|
|
|
if (better_capacity >= new_capacity) break;
|
|
|
|
}
|
2018-01-07 13:51:46 -08:00
|
|
|
l.items = try l.allocator.alignedRealloc(T, A, l.items, better_capacity);
|
2016-05-08 01:34:00 -07:00
|
|
|
}
|
2017-02-28 00:07:11 -08:00
|
|
|
|
2018-01-31 19:48:40 -08:00
|
|
|
pub fn addOne(l: &Self) !&T {
|
2017-02-28 00:07:11 -08:00
|
|
|
const new_length = l.len + 1;
|
2018-01-07 13:51:46 -08:00
|
|
|
try l.ensureCapacity(new_length);
|
2017-02-28 00:07:11 -08:00
|
|
|
const result = &l.items[l.len];
|
|
|
|
l.len = new_length;
|
|
|
|
return result;
|
|
|
|
}
|
2017-04-19 23:26:36 -07:00
|
|
|
|
2018-01-25 01:10:11 -08: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
|
|
|
|
2018-01-25 01:10:11 -08: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();
|
|
|
|
}
|
2018-05-03 06:54:33 -07:00
|
|
|
|
|
|
|
pub const Iterator = struct {
|
|
|
|
list: &const Self,
|
|
|
|
// how many items have we returned
|
|
|
|
count: usize,
|
|
|
|
|
|
|
|
pub fn next(it: &Iterator) ?T {
|
|
|
|
if (it.count >= it.list.len) return null;
|
|
|
|
const val = it.list.at(it.count);
|
|
|
|
it.count += 1;
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn reset(it: &Iterator) void {
|
|
|
|
it.count = 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-05-04 14:48:14 -07:00
|
|
|
pub fn iterator(self: &const Self) Iterator {
|
2018-05-26 15:16:39 -07:00
|
|
|
return Iterator{
|
|
|
|
.list = self,
|
|
|
|
.count = 0,
|
|
|
|
};
|
2018-05-03 06:54:33 -07:00
|
|
|
}
|
2017-12-21 21:50:30 -08:00
|
|
|
};
|
2016-05-08 01:34:00 -07:00
|
|
|
}
|
|
|
|
|
2017-05-04 11:05:06 -07:00
|
|
|
test "basic ArrayList test" {
|
2017-11-10 11:02:45 -08:00
|
|
|
var list = ArrayList(i32).init(debug.global_allocator);
|
2016-05-08 01:34:00 -07:00
|
|
|
defer list.deinit();
|
|
|
|
|
2018-04-30 22:53:04 -07:00
|
|
|
{
|
|
|
|
var i: usize = 0;
|
|
|
|
while (i < 10) : (i += 1) {
|
|
|
|
list.append(i32(i + 1)) catch unreachable;
|
|
|
|
}
|
|
|
|
}
|
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) {
|
|
|
|
assert(list.items[i] == i32(i + 1));
|
|
|
|
}
|
|
|
|
}
|
2017-04-19 23:26:36 -07:00
|
|
|
|
2018-05-04 14:48:14 -07:00
|
|
|
for (list.toSlice()) |v, i| {
|
|
|
|
assert(v == i32(i + 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (list.toSliceConst()) |v, i| {
|
|
|
|
assert(v == i32(i + 1));
|
|
|
|
}
|
|
|
|
|
2017-04-19 23:26:36 -07:00
|
|
|
assert(list.pop() == 10);
|
|
|
|
assert(list.len == 9);
|
2017-09-09 10:49:40 -07:00
|
|
|
|
2018-04-30 22:53:04 -07:00
|
|
|
list.appendSlice([]const i32{
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
3,
|
|
|
|
}) catch unreachable;
|
2017-09-09 10:49:40 -07:00
|
|
|
assert(list.len == 12);
|
|
|
|
assert(list.pop() == 3);
|
|
|
|
assert(list.pop() == 2);
|
|
|
|
assert(list.pop() == 1);
|
|
|
|
assert(list.len == 9);
|
|
|
|
|
2018-04-30 22:53:04 -07:00
|
|
|
list.appendSlice([]const i32{}) catch unreachable;
|
2017-09-09 10:49:40 -07:00
|
|
|
assert(list.len == 9);
|
2016-05-08 01:34:00 -07:00
|
|
|
}
|
2018-02-08 08:22:31 -08:00
|
|
|
|
2018-05-03 06:54:33 -07:00
|
|
|
test "iterator 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);
|
|
|
|
|
2018-05-26 15:16:39 -07:00
|
|
|
var count: i32 = 0;
|
2018-05-03 06:54:33 -07:00
|
|
|
var it = list.iterator();
|
|
|
|
while (it.next()) |next| {
|
|
|
|
assert(next == count + 1);
|
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(count == 3);
|
|
|
|
assert(it.next() == null);
|
|
|
|
it.reset();
|
|
|
|
count = 0;
|
|
|
|
while (it.next()) |next| {
|
|
|
|
assert(next == count + 1);
|
|
|
|
count += 1;
|
|
|
|
if (count == 2) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
it.reset();
|
2018-05-26 15:16:39 -07:00
|
|
|
assert(??it.next() == 1);
|
2018-05-03 06:54:33 -07:00
|
|
|
}
|
|
|
|
|
2018-02-08 08:22:31 -08:00
|
|
|
test "insert ArrayList test" {
|
|
|
|
var list = ArrayList(i32).init(debug.global_allocator);
|
|
|
|
defer list.deinit();
|
|
|
|
|
|
|
|
try list.append(1);
|
|
|
|
try list.insert(0, 5);
|
|
|
|
assert(list.items[0] == 5);
|
|
|
|
assert(list.items[1] == 1);
|
|
|
|
|
2018-04-30 22:53:04 -07:00
|
|
|
try list.insertSlice(1, []const i32{
|
|
|
|
9,
|
|
|
|
8,
|
|
|
|
});
|
2018-02-08 08:22:31 -08:00
|
|
|
assert(list.items[0] == 5);
|
|
|
|
assert(list.items[1] == 9);
|
|
|
|
assert(list.items[2] == 8);
|
|
|
|
|
2018-04-30 22:53:04 -07:00
|
|
|
const items = []const i32{1};
|
2018-02-08 08:22:31 -08:00
|
|
|
try list.insertSlice(0, items[0..0]);
|
|
|
|
assert(list.items[0] == 5);
|
|
|
|
}
|