ona/source/coral/list.zig

77 lines
1.7 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];
}
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 = &.{},
};
}
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];
}
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;
}
};
}