-
Notifications
You must be signed in to change notification settings - Fork 0
Ringbuff #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@@ -86,3 +86,171 @@ const MXCErrors = enum(c_int) { | |||
E_NOT_SUPPORTED = -17, | |||
E_FAIL = -255, | |||
}; | |||
|
|||
pub fn RingBuffer(comptime T: type, comptime capacity: usize) type { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move the RingBuffer
into its own file
pub fn peek(self: *Self) !T { | ||
if (self.isEmpty()) { | ||
return error.BufferEmpty; | ||
} | ||
|
||
return self.buffer[self.head]; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think peek
should return an error. A ?T
feels more natural to me
@@ -86,3 +86,171 @@ const MXCErrors = enum(c_int) { | |||
E_NOT_SUPPORTED = -17, | |||
E_FAIL = -255, | |||
}; | |||
|
|||
pub fn RingBuffer(comptime T: type, comptime capacity: usize) type { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if comptime capacity: usize
or comptime_int
is more idiomatic zig, but I guess this is fine if it works
head: usize = 0, | ||
tail: usize = 0, | ||
count: usize = 0, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fun fact: you can use std.math.IntFittingRange(comptime from: comptime_int, comptime to: comptime_int)
which "Returns the smallest integer type that can hold both from and to."
This optimization is definitely unnecessary in this case but it is cool :)
return error.BufferFull; | ||
} | ||
|
||
self.buffer[self.tail] = item; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To me the head
is what advances and is what you push to...not the tail
. Layer 4's ring buffer at least uses the convention that the head
is in front
test "ring buf underflow" { | ||
var rb = RingBuffer(u8, 3).init(); | ||
|
||
try std.testing.expectError(error.BufferEmpty, rb.pop()); | ||
try std.testing.expectError(error.BufferEmpty, rb.peek()); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Weird name for this test
|
||
pub fn init() Self { | ||
return Self{ | ||
.buffer = undefined, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
buffer
can be initialized as a default value along with the others in this struct
025eed6
to
776a82f
Compare
This is for an old version |
Generic ring buffer with peek and tests.