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
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 168 additions & 0 deletions shared/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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

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

return struct {
const Self = @This();

buffer: [capacity]T,
head: usize = 0,
tail: usize = 0,
count: usize = 0,
Comment on lines +95 to +97
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 :)


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

};
}

pub fn isEmpty(self: *Self) bool {
return self.count == 0;
}

pub fn isFull(self: *Self) bool {
return self.count == capacity;
}

pub fn push(self: *Self, item: T) !void {
if (self.isFull()) {
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

self.tail = @mod((self.tail + 1), capacity);
self.count += 1;
}

pub fn pop(self: *Self) !T {
if (self.isEmpty()) {
return error.BufferEmpty;
}

const item = self.buffer[self.head];
self.head = @mod((self.head) + 1, capacity);
self.count -= 1;
return item;
}

pub fn peek(self: *Self) !T {
if (self.isEmpty()) {
return error.BufferEmpty;
}

return self.buffer[self.head];
}
Comment on lines +134 to +140
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

};
}

test "ring buf simple operations" {
var rb = RingBuffer(i32, 4).init();

try rb.push(1);
try rb.push(2);
try rb.push(3);

try std.testing.expectEqual(@as(usize, 3), rb.count);
try std.testing.expectEqual(@as(i32, 1), try rb.pop());
try std.testing.expectEqual(@as(i32, 2), try rb.pop());

try rb.push(4);
try rb.push(5);

try std.testing.expectEqual(@as(usize, 3), rb.count);
try std.testing.expectEqual(@as(i32, 3), try rb.pop());
}

test "ring buf wrap around" {
var rb = RingBuffer(i32, 3).init();

try rb.push(1);
try rb.push(2);
try rb.push(3);

try std.testing.expectError(error.BufferFull, rb.push(4));

try std.testing.expectEqual(@as(i32, 1), try rb.pop());
try rb.push(4);

try std.testing.expectEqual(@as(i32, 2), try rb.pop());
try std.testing.expectEqual(@as(i32, 3), try rb.pop());
try std.testing.expectEqual(@as(i32, 4), try rb.pop());
}

test "ring buf isEmpty and isFull checks" {
var rb = RingBuffer(u8, 2).init();

try std.testing.expect(rb.isEmpty());
try std.testing.expect(!rb.isFull());

try rb.push(10);
try std.testing.expect(!rb.isEmpty());
try std.testing.expect(!rb.isFull());

try rb.push(20);
try std.testing.expect(!rb.isEmpty());
try std.testing.expect(rb.isFull());
}

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


test "ring buf peek operation" {
var rb = RingBuffer(u8, 3).init();

try rb.push(42);
try std.testing.expectEqual(@as(u8, 42), try rb.peek());
try std.testing.expectEqual(@as(u8, 42), try rb.pop());

try rb.push(100);
try rb.push(101);
try std.testing.expectEqual(@as(u8, 100), try rb.peek());
try std.testing.expectEqual(@as(u8, 100), try rb.pop());
try std.testing.expectEqual(@as(u8, 101), try rb.peek());
}

test "ring buf mix push and pop" {
var rb = RingBuffer(i32, 3).init();

try rb.push(1);
try rb.push(2);
try std.testing.expectEqual(@as(i32, 1), try rb.pop());

try rb.push(3);
try rb.push(4);
try std.testing.expectEqual(@as(i32, 2), try rb.pop());
try std.testing.expectEqual(@as(i32, 3), try rb.pop());

try rb.push(5);
try rb.push(6);
try std.testing.expectEqual(@as(i32, 4), try rb.pop());
try std.testing.expectEqual(@as(i32, 5), try rb.pop());
}

const TestStruct = struct {
id: u32,
name: []const u8,
};

test "ring buf with struct values" {
var rb = RingBuffer(TestStruct, 3).init();

const item1 = TestStruct{ .id = 1, .name = "Alice" };
const item2 = TestStruct{ .id = 2, .name = "Bob" };

try rb.push(item1);
try rb.push(item2);

const result1 = try rb.pop();
try std.testing.expectEqual(@as(u32, 1), result1.id);
try std.testing.expectEqualStrings("Alice", result1.name);

const result2 = try rb.pop();
try std.testing.expectEqual(@as(u32, 2), result2.id);
try std.testing.expectEqualStrings("Bob", result2.name);

try std.testing.expect(rb.isEmpty());
}