地圖 (Jido) is a lightweight Unix TUI file explorer designed for speed and simplicity.
at v0.4.0 35 lines 876 B view raw
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) void { 21 self.buf[self.head] = v; 22 self.head = (self.head + 1) % capacity; 23 if (self.count != capacity) self.count += 1; 24 } 25 26 pub fn pop(self: *Self) ?T { 27 if (self.count == 0) return null; 28 29 self.head = (self.head - 1) % capacity; 30 const value = self.buf[self.head]; 31 self.count -= 1; 32 return value; 33 } 34 }; 35}