ona/source/coral/list.zig

135 lines
3.1 KiB
Zig
Raw Normal View History

2023-05-23 02:08:34 +02:00
const io = @import("./io.zig");
const math = @import("./math.zig");
2023-07-10 02:10:56 +02:00
pub const ByteStack = Stack(io.Byte);
2023-05-28 03:19:46 +02:00
pub fn Stack(comptime Value: type) type {
2023-05-23 02:08:34 +02:00
return struct {
2023-06-23 00:46:48 +02:00
allocator: io.Allocator,
2023-07-10 02:10:56 +02:00
capacity: usize,
values: []Value,
2023-05-23 02:08:34 +02:00
const Self = @This();
pub fn clear(self: *Self) void {
self.values = self.values[0 .. 0];
}
pub fn drop(self: *Self, amount: usize) bool {
if (amount > self.values.len) {
return false;
}
self.values = self.values[0 .. self.values.len - amount];
return true;
}
2023-07-10 02:10:56 +02:00
pub fn free(self: *Self) void {
2023-06-02 23:43:53 +02:00
if (self.capacity == 0) {
return;
}
2023-07-10 02:10:56 +02:00
self.allocator.deallocate(self.values.ptr[0 .. self.capacity]);
2023-05-23 02:08:34 +02:00
2023-06-02 23:43:53 +02:00
self.values = &.{};
2023-05-23 02:08:34 +02:00
}
2023-06-23 00:46:48 +02:00
pub fn grow(self: *Self, growth_amount: usize) io.AllocationError!void {
2023-05-23 02:08:34 +02:00
const grown_capacity = self.capacity + growth_amount;
2023-07-10 02:10:56 +02:00
const buffer = try self.allocator.reallocate(null, @sizeOf(Value) * grown_capacity);
2023-05-23 02:08:34 +02:00
2023-07-10 02:10:56 +02:00
errdefer self.allocator.deallocate(buffer);
2023-05-23 02:08:34 +02:00
2023-06-02 23:43:53 +02:00
if (self.capacity != 0) {
2023-07-10 02:10:56 +02:00
io.copy(buffer, io.bytes_of(self.values));
self.allocator.deallocate(self.values.ptr[0 .. self.capacity]);
2023-06-02 23:43:53 +02:00
}
2023-05-23 02:08:34 +02:00
2023-07-10 02:10:56 +02:00
self.values = @as([*]Value, @ptrCast(@alignCast(buffer)))[0 .. self.values.len];
2023-05-23 02:08:34 +02:00
self.capacity = grown_capacity;
}
2023-07-10 02:10:56 +02:00
pub fn make(allocator: io.Allocator) Self {
return .{
.allocator = allocator,
.capacity = 0,
.values = &.{},
};
}
pub fn pack(self: *Self) io.AllocationError!void {
const packed_size = self.values.len;
const buffer = try self.allocator.reallocate(null, @sizeOf(Value) * self.values.len);
io.copy(buffer, io.bytes_of(self.values));
if (self.capacity != 0) {
self.allocator.deallocate(self.values.ptr[0 .. self.capacity]);
}
self.values = @as([*]Value, @ptrCast(@alignCast(buffer)))[0 .. packed_size];
self.capacity = packed_size;
}
pub fn peek(self: Self) ?Value {
if (self.values.len == 0) {
return null;
}
return self.values[self.values.len - 1];
}
2023-05-28 03:19:46 +02:00
pub fn pop(self: *Self) ?Value {
if (self.values.len == 0) {
return null;
}
const last_index = self.values.len - 1;
defer self.values = self.values[0 .. last_index];
return self.values[last_index];
}
pub fn push_all(self: *Self, values: []const Value) io.AllocationError!void {
const new_length = self.values.len + values.len;
if (new_length > self.capacity) {
try self.grow(values.len + values.len);
}
const offset_index = self.values.len;
self.values = self.values.ptr[0 .. new_length];
for (0 .. values.len) |index| {
self.values[offset_index + index] = values[index];
}
}
2023-06-23 00:46:48 +02:00
pub fn push_one(self: *Self, value: Value) io.AllocationError!void {
2023-05-23 02:08:34 +02:00
if (self.values.len == self.capacity) {
2023-06-23 00:46:48 +02:00
try self.grow(math.max(1, self.capacity));
2023-05-23 02:08:34 +02:00
}
const offset_index = self.values.len;
self.values = self.values.ptr[0 .. self.values.len + 1];
self.values[offset_index] = value;
}
};
}
pub fn stack_as_writer(self: *ByteStack) io.Writer {
return io.Writer.bind(ByteStack, self, write_stack);
}
fn write_stack(stack: *ByteStack, bytes: []const io.Byte) ?usize {
stack.push_all(bytes) catch return null;
return bytes.len;
}