about summary refs log tree commit diff
path: root/aoc/2021/10/Stack.zig
diff options
context:
space:
mode:
authorNguyễn Gia Phong <mcsinyx@disroot.org>2021-12-10 21:09:19 +0700
committerNguyễn Gia Phong <mcsinyx@disroot.org>2021-12-10 21:09:19 +0700
commit9d041894e485f3dcd93a952266273a25f6614f07 (patch)
tree0613e0975b71e9f98ddb11bf7e0ba39054f8a741 /aoc/2021/10/Stack.zig
parentf53ac491b1ec74ad9c44d20065517467c1877190 (diff)
downloadcp-9d041894e485f3dcd93a952266273a25f6614f07.tar.gz
[aoc/2021/10] Get rid of bullshit
Diffstat (limited to 'aoc/2021/10/Stack.zig')
-rw-r--r--aoc/2021/10/Stack.zig35
1 files changed, 35 insertions, 0 deletions
diff --git a/aoc/2021/10/Stack.zig b/aoc/2021/10/Stack.zig
new file mode 100644
index 0000000..1aab8eb
--- /dev/null
+++ b/aoc/2021/10/Stack.zig
@@ -0,0 +1,35 @@
+const Allocator = @import("std").mem.Allocator;
+const Self = @This();
+
+allocator: *Allocator,
+memory: []u8,
+len: usize,
+
+pub fn alloc(allocator: *Allocator, size: usize) !Self {
+    return Self{
+        .allocator = allocator,
+        .memory = try allocator.alloc(u8, size),
+        .len = 0,
+    };
+}
+
+pub fn free(self: *Self) void {
+    self.allocator.free(self.memory);
+    self.* = undefined;
+}
+
+pub fn push(self: *Self, node: u8) void {
+    self.memory[self.len] = node;
+    self.len += 1;
+}
+
+pub fn pop(self: *Self) ?u8 {
+    if (self.len < 1)
+        return null;
+    self.len -= 1;
+    return self.memory[self.len];
+}
+
+pub fn reset(self: *Self) void {
+    self.len = 0;
+}