Skip to content

Commit f394aee

Browse files
author
dweiller
committed
std.RingBuffer: add (non-concurrent) RingBuffer implementation
1 parent 4935173 commit f394aee

File tree

5 files changed

+21
-11
lines changed

5 files changed

+21
-11
lines changed

lib/std/compress/zstandard/RingBuffer.zig renamed to lib/std/RingBuffer.zig

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
//! This ring buffer stores read and write indices while being able to utilise the full
2-
//! 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 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.
1+
//! This ring buffer stores read and write indices while being able to utilise
2+
//! the full backing slice by incrementing the indices modulo twice the slice's
3+
//! length and reducing indices modulo the slice's length on slice access. This
4+
//! means that whether the ring buffer if full or empty can be distinguised by
5+
//! looking at the different between the read and write indices without adding
6+
//! an extra boolean flag or having to reserve a slot in the buffer.
7+
//!
8+
//! This ring buffer has not been implemented with thread safety in mind, and
9+
//! therefore should not be assumed to be suitable for use cases involving
10+
//! separate reader and writer threads.
611

712
const Allocator = @import("std").mem.Allocator;
813
const assert = @import("std").debug.assert;
@@ -72,6 +77,13 @@ pub fn writeSliceAssumeCapacity(self: *RingBuffer, bytes: []const u8) void {
7277
/// ring buffer is empty.
7378
pub fn read(self: *RingBuffer) ?u8 {
7479
if (self.isEmpty()) return null;
80+
return self.readAssumeLength();
81+
}
82+
83+
/// Consume a byte from the ring buffer and return it; asserts that the buffer
84+
/// is not empty.
85+
pub fn readAssumeLength(self: *RingBuffer) u8 {
86+
assert(!self.isEmpty());
7587
const byte = self.data[self.mask(self.read_index)];
7688
self.read_index = self.mask2(self.read_index + 1);
7789
return byte;

lib/std/compress/zstandard.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
const std = @import("std");
22
const Allocator = std.mem.Allocator;
3+
const RingBuffer = std.RingBuffer;
34

45
const types = @import("zstandard/types.zig");
56
pub const frame = types.frame;
67
pub const compressed_block = types.compressed_block;
78

8-
const RingBuffer = @import("zstandard/RingBuffer.zig");
99
pub const decompress = @import("zstandard/decompress.zig");
1010

1111
pub fn Stream(

lib/std/compress/zstandard/decode/block.zig

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const std = @import("std");
22
const assert = std.debug.assert;
3+
const RingBuffer = std.RingBuffer;
34

45
const types = @import("../types.zig");
56
const frame = types.frame;
@@ -8,9 +9,6 @@ const LiteralsSection = types.compressed_block.LiteralsSection;
89
const SequencesSection = types.compressed_block.SequencesSection;
910

1011
const huffman = @import("huffman.zig");
11-
12-
const RingBuffer = @import("../RingBuffer.zig");
13-
1412
const readers = @import("../readers.zig");
1513

1614
const decodeFseTable = @import("fse.zig").decodeFseTable;

lib/std/compress/zstandard/decompress.zig

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const std = @import("std");
22
const assert = std.debug.assert;
33
const Allocator = std.mem.Allocator;
4+
const RingBuffer = std.RingBuffer;
45

56
const types = @import("types.zig");
67
const frame = types.frame;
@@ -12,8 +13,6 @@ const Table = types.compressed_block.Table;
1213

1314
pub const block = @import("decode/block.zig");
1415

15-
pub const RingBuffer = @import("RingBuffer.zig");
16-
1716
const readers = @import("readers.zig");
1817

1918
const readInt = std.mem.readIntLittle;

lib/std/std.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pub const PackedIntSliceEndian = @import("packed_int_array.zig").PackedIntSliceE
3131
pub const PriorityQueue = @import("priority_queue.zig").PriorityQueue;
3232
pub const PriorityDequeue = @import("priority_dequeue.zig").PriorityDequeue;
3333
pub const Progress = @import("Progress.zig");
34+
pub const RingBuffer = @import("RingBuffer.zig");
3435
pub const SegmentedList = @import("segmented_list.zig").SegmentedList;
3536
pub const SemanticVersion = @import("SemanticVersion.zig");
3637
pub const SinglyLinkedList = @import("linked_list.zig").SinglyLinkedList;

0 commit comments

Comments
 (0)