authorgravatar for 4678790+dweiller@users.noreply.github.comDominic <4678790+dweiller@users.noreply.github.com> 2023-01-27 23:50:23+11:00
committergravatar for 4678790+dweiller@users.noreply.github.comDominic <4678790+dweiller@users.noreply.github.com> 2023-02-20 09:09:06+11:00
log3c06e2e7d0de92c0674c16ae23e1462a3acbe718
tree37236bb1ab40749476159229bc992a72c3456fa7
parent1e5b8be5099d976628aaa3fc4208a0bbd45c7700

std.compress.zstandard: add doc comments for RingBuffer


1 files changed, 25 insertions(+), 4 deletions(-)

lib/std/compress/zstandard/RingBuffer.zig+25-4
......@@ -1,8 +1,8 @@
11//! This ring buffer stores read and write indices while being able to utilise the full
22//! backing slice by incrementing the indices modulo twice the slice's length and reducing
3//! indices modulo the slice's length on slice access. This means that the bit of information
4//! distinguishing whether the buffer is full or empty in an implementation utilising
5//! and extra flag is stored in difference of the indices.
3//! indices modulo the slice's length on slice access. This means that whether the ring buffer
4//! if full or empty can be distinguised by looking at the different between the read and write
5//! indices without adding an extra boolean flag or having to reserve a slot in the buffer.
66
77const assert = @import("std").debug.assert;
88
......@@ -12,33 +12,45 @@ data: []u8,
1212read_index: usize,
1313write_index: usize,
1414
15/// Returns `index` modulo the length of the backing slice.
1516pub fn mask(self: RingBuffer, index: usize) usize {
1617 return index % self.data.len;
1718}
1819
20/// Returns `index` module twice the length of the backing slice.
1921pub fn mask2(self: RingBuffer, index: usize) usize {
2022 return index % (2 * self.data.len);
2123}
2224
25/// Write `byte` into the ring buffer. Returns `error.Full` if the ring
26/// buffer is full.
2327pub fn write(self: *RingBuffer, byte: u8) !void {
2428 if (self.isFull()) return error.Full;
2529 self.writeAssumeCapacity(byte);
2630}
2731
32/// Write `byte` into the ring buffer. If the ring buffer is full, the
33/// oldest byte is overwritten.
2834pub fn writeAssumeCapacity(self: *RingBuffer, byte: u8) void {
2935 self.data[self.mask(self.write_index)] = byte;
3036 self.write_index = self.mask2(self.write_index + 1);
3137}
3238
39/// Write `bytes` into the ring bufffer. Returns `error.Full` if the ring
40/// buffer does not have enough space, without writing any data.
3341pub fn writeSlice(self: *RingBuffer, bytes: []const u8) !void {
3442 if (self.len() + bytes.len > self.data.len) return error.Full;
3543 self.writeSliceAssumeCapacity(bytes);
3644}
3745
46/// Write `bytes` into the ring buffer. If there is not enough space, older
47/// bytes will be overwritten.
3848pub fn writeSliceAssumeCapacity(self: *RingBuffer, bytes: []const u8) void {
3949 for (bytes) |b| self.writeAssumeCapacity(b);
4050}
4151
52/// Consume a byte from the ring buffer and return it. Returns `null` if the
53/// ring buffer is empty.
4254pub fn read(self: *RingBuffer) ?u8 {
4355 if (self.isEmpty()) return null;
4456 const byte = self.data[self.mask(self.read_index)];
......@@ -46,24 +58,32 @@ pub fn read(self: *RingBuffer) ?u8 {
4658 return byte;
4759}
4860
61/// Returns `true` if the ring buffer is empty and `false` otherwise.
4962pub fn isEmpty(self: RingBuffer) bool {
5063 return self.write_index == self.read_index;
5164}
5265
66/// Returns `true` if the ring buffer is full and `false` otherwise.
5367pub fn isFull(self: RingBuffer) bool {
5468 return self.mask2(self.write_index + self.data.len) == self.read_index;
5569}
5670
71/// Returns the length
5772pub fn len(self: RingBuffer) usize {
5873 const adjusted_write_index = self.write_index + @boolToInt(self.write_index < self.read_index) * 2 * self.data.len;
5974 return adjusted_write_index - self.read_index;
6075}
6176
62const Slice = struct {
77/// A `Slice` represents a region of a ring buffer. The region is split into two
78/// sections as the ring buffer data will not be contiguous if the desired region
79/// wraps to the start of the backing slice.
80pub const Slice = struct {
6381 first: []u8,
6482 second: []u8,
6583};
6684
85/// Returns a `Slice` for the region of the ring buffer staring at `self.mask(start_unmasked)`
86/// with the specified length.
6787pub fn sliceAt(self: RingBuffer, start_unmasked: usize, length: usize) Slice {
6888 assert(length <= self.data.len);
6989 const slice1_start = self.mask(start_unmasked);
......@@ -76,6 +96,7 @@ pub fn sliceAt(self: RingBuffer, start_unmasked: usize, length: usize) Slice {
7696 };
7797}
7898
99/// Returns a `Slice` for the last `length` bytes written to the ring buffer.
79100pub fn sliceLast(self: RingBuffer, length: usize) Slice {
80101 return self.sliceAt(self.write_index + self.data.len - length, length);
81102}