about summary refs log tree commit diff
path: root/aoc/2021/10/Stack.zig
blob: 1aab8eb88efe2637d700b1478f698bf9eda8e652 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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;
}