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 18:48:20 +0700
committerNguyễn Gia Phong <mcsinyx@disroot.org>2021-12-10 18:48:20 +0700
commitf53ac491b1ec74ad9c44d20065517467c1877190 (patch)
tree33494f5bd7d8c72dade242c6862cb1bc7f095ab3 /aoc/2021/10/stack.zig
parent1606066809130d544aaf722d617a1df5670602cb (diff)
downloadcp-f53ac491b1ec74ad9c44d20065517467c1877190.tar.gz
[aoc/2021] Finish day 10
Diffstat (limited to 'aoc/2021/10/stack.zig')
-rw-r--r--aoc/2021/10/stack.zig39
1 files changed, 39 insertions, 0 deletions
diff --git a/aoc/2021/10/stack.zig b/aoc/2021/10/stack.zig
new file mode 100644
index 0000000..1002d61
--- /dev/null
+++ b/aoc/2021/10/stack.zig
@@ -0,0 +1,39 @@
+const Allocator = @import("std").mem.Allocator;
+
+pub fn Stack(comptime T: type) type {
+    return struct {
+        allocator: *Allocator,
+        memory: []T,
+        len: usize,
+
+        const Self = @This();
+
+        pub fn alloc(allocator: *Allocator, size: usize) !Self {
+            return Self{
+                .allocator = allocator,
+                .memory = try allocator.alloc(T, size),
+                .len = 0,
+            };
+        }
+
+        pub fn free(self: *Self) void {
+            self.allocator.free(self.memory);
+            self.* = undefined;
+        }
+
+        pub fn push(self: *Self, node: T) void {
+            self.memory[self.len] = node;
+            self.len += 1;
+        }
+
+        pub fn pop(self: *Self) T {
+            self.len -= 1;
+            return self.memory[self.len];
+        }
+
+        pub fn reset(self: *Self) void {
+            self.len = 0;
+        }
+    };
+}
+