-
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
Ringbuff #3
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if |
||
return struct { | ||
const Self = @This(); | ||
|
||
buffer: [capacity]T, | ||
head: usize = 0, | ||
tail: usize = 0, | ||
count: usize = 0, | ||
Comment on lines
+95
to
+97
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fun fact: you can use This optimization is definitely unnecessary in this case but it is cool :) |
||
|
||
pub fn init() Self { | ||
return Self{ | ||
.buffer = undefined, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
}; | ||
} | ||
|
||
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To me the |
||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think |
||
}; | ||
} | ||
|
||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()); | ||
} |
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