Skip to content

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

Closed
wants to merge 2 commits into from
Closed

Ringbuff #3

wants to merge 2 commits into from

Conversation

jlevere
Copy link
Collaborator

@jlevere jlevere commented Nov 18, 2024

Generic ring buffer with peek and tests.

@jlevere jlevere requested a review from mbund November 18, 2024 19:25
@@ -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 {
Copy link
Collaborator

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

Comment on lines +134 to +140
pub fn peek(self: *Self) !T {
if (self.isEmpty()) {
return error.BufferEmpty;
}

return self.buffer[self.head];
}
Copy link
Collaborator

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 {
Copy link
Collaborator

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

Comment on lines +95 to +97
head: usize = 0,
tail: usize = 0,
count: usize = 0,
Copy link
Collaborator

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;
Copy link
Collaborator

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

Comment on lines +194 to +199
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());
}
Copy link
Collaborator

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,
Copy link
Collaborator

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

@mbund mbund force-pushed the main branch 2 times, most recently from 025eed6 to 776a82f Compare January 17, 2025 20:03
@mbund
Copy link
Collaborator

mbund commented Jan 28, 2025

This is for an old version

@mbund mbund closed this Jan 28, 2025
@mbund mbund deleted the ringbuff branch January 29, 2025 04:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants