| ... | ... | @@ -814,3 +814,22 @@ pub fn sum(comptime T: type, slice: []const T) T { |
| 814 | 814 | for (slice) |item| res += item; |
| 815 | 815 | return res; |
| 816 | 816 | } |
| 817 | |
| 818 | pub fn RingBuffer(comptime T: type, comptime capacity: usize) type { |
| 819 | return struct { |
| 820 | items: [capacity]T = undefined, |
| 821 | len: usize = 0, |
| 822 | comptime capacity: usize = capacity, |
| 823 | |
| 824 | const Self = @This(); |
| 825 | |
| 826 | pub fn append(self: *Self, new_item: T) void { |
| 827 | if (self.len == self.capacity) { |
| 828 | for (1..self.len) |i| self.items[i - 1] = self.items[i]; |
| 829 | self.len -= 1; |
| 830 | } |
| 831 | self.items[self.len] = new_item; |
| 832 | self.len += 1; |
| 833 | } |
| 834 | }; |
| 835 | } |