地圖 (Jido) is a lightweight Unix TUI file explorer designed for speed and simplicity.
1const std = @import("std");
2
3pub fn CircularStack(comptime T: type, comptime capacity: usize) type {
4 return struct {
5 const Self = @This();
6
7 head: usize = 0,
8 count: usize = 0,
9 buf: [capacity]T = undefined,
10
11 pub fn init() Self {
12 return Self{};
13 }
14
15 pub fn reset(self: *Self) void {
16 self.head = 0;
17 self.count = 0;
18 }
19
20 pub fn push(self: *Self, v: T) ?T {
21 const prev_elem = if (self.count == capacity) self.buf[self.head] else null;
22
23 self.buf[self.head] = v;
24 self.head = (self.head + 1) % capacity;
25 if (self.count != capacity) self.count += 1;
26
27 return prev_elem;
28 }
29
30 pub fn pop(self: *Self) ?T {
31 if (self.count == 0) return null;
32
33 self.head = (self.head - 1) % capacity;
34 const value = self.buf[self.head];
35 self.count -= 1;
36 return value;
37 }
38 };
39}