| ... | @@ -4,12 +4,17 @@ const extras = @import("./lib.zig"); | ... | @@ -4,12 +4,17 @@ const extras = @import("./lib.zig"); |
| 4 | | 4 | |
| 5 | pub fn RingBuffer(comptime T: type, comptime capacity: usize) type { | 5 | pub fn RingBuffer(comptime T: type, comptime capacity: usize) type { |
| 6 | return struct { | 6 | return struct { |
| 7 | items: [capacity]T = undefined, | 7 | items: [capacity]T, |
| 8 | len: usize = 0, | 8 | len: usize, |
| 9 | comptime capacity: usize = capacity, | 9 | comptime capacity: usize = capacity, |
| 10 | | 10 | |
| 11 | const Self = @This(); | 11 | const Self = @This(); |
| 12 | | 12 | |
| | 13 | pub const empty: Self = .{ |
| | 14 | .items = undefined, |
| | 15 | .len = 0, |
| | 16 | }; |
| | 17 | |
| 13 | pub fn append(self: *Self, new_item: T) void { | 18 | pub fn append(self: *Self, new_item: T) void { |
| 14 | if (self.len == self.capacity) { | 19 | if (self.len == self.capacity) { |
| 15 | for (1..self.len) |i| self.items[i - 1] = self.items[i]; | 20 | for (1..self.len) |i| self.items[i - 1] = self.items[i]; |
| ... | @@ -18,6 +23,14 @@ pub fn RingBuffer(comptime T: type, comptime capacity: usize) type { | ... | @@ -18,6 +23,14 @@ pub fn RingBuffer(comptime T: type, comptime capacity: usize) type { |
| 18 | self.items[self.len] = new_item; | 23 | self.items[self.len] = new_item; |
| 19 | self.len += 1; | 24 | self.len += 1; |
| 20 | } | 25 | } |
| | 26 | |
| | 27 | pub fn slice(self: *Self) []T { |
| | 28 | return self.items[0..self.len]; |
| | 29 | } |
| | 30 | |
| | 31 | pub fn rest(self: *Self) []T { |
| | 32 | return self.items[self.len..]; |
| | 33 | } |
| 21 | }; | 34 | }; |
| 22 | } | 35 | } |
| 23 | | 36 | |