add set function to arraylist

so you can set a value without growing the underlying buffer,
with range safety checks
This commit is contained in:
Arthur Elliott 2018-06-07 10:00:27 -04:00 committed by Andrew Kelley
parent 9046b5eac0
commit e0092ee4a5

View File

@ -44,6 +44,11 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type {
return l.toSliceConst()[n];
}
pub fn set(self: *const Self, n: usize, item: *const T) !void {
if (n >= self.len) return error.OutOfBounds;
self.items[n] = item.*;
}
pub fn count(self: *const Self) usize {
return self.len;
}
@ -162,6 +167,11 @@ test "basic ArrayList test" {
var list = ArrayList(i32).init(debug.global_allocator);
defer list.deinit();
// setting on empty list is out of bounds
list.set(0, 1) catch |err| {
assert(err == error.OutOfBounds);
};
{
var i: usize = 0;
while (i < 10) : (i += 1) {
@ -200,6 +210,20 @@ test "basic ArrayList test" {
list.appendSlice([]const i32{}) catch unreachable;
assert(list.len == 9);
// can only set on indices < self.len
list.set(7, 33) catch unreachable;
list.set(8, 42) catch unreachable;
list.set(9, 99) catch |err| {
assert(err == error.OutOfBounds);
};
list.set(10, 123) catch |err| {
assert(err == error.OutOfBounds);
};
assert(list.pop() == 42);
assert(list.pop() == 33);
}
test "iterator ArrayList test" {