#ring-buffer #circular-buffer #buffer #ring #dsp #circular

slice_ring_buf

适用于与切片一起工作的环形缓冲区实现

12个版本

0.2.7 2023年3月24日
0.2.6 2020年8月25日
0.1.3 2020年8月23日

#257 in 数据结构

Download history 112/week @ 2024-03-14 2/week @ 2024-03-21 19/week @ 2024-03-28 10/week @ 2024-04-04 4/week @ 2024-04-11 20/week @ 2024-04-18 7/week @ 2024-04-25 3/week @ 2024-05-16 7/week @ 2024-05-23 9/week @ 2024-05-30 8/week @ 2024-06-06 36/week @ 2024-06-13 29/week @ 2024-06-20 3/week @ 2024-06-27

每月 77 次下载
用于 2 crates

MIT 许可证

150KB
2K SLoC

切片环形缓冲区

Test Documentation Crates.io License

一种优化用于与切片一起工作的环形缓冲区实现。注意这基本上与 VecDeque 做同样的事情,但增加了使用负值索引的能力,以及与堆栈分配的缓冲区一起工作的能力。

此包没有消费者/生产者逻辑,旨在用作原始数据结构或基本结构。

此包针对以块方式操作数据进行优化。如果您的算法一次索引一个元素,并且只使用大小为2的幂的缓冲区,请考虑使用bit_mask_ring_buf

安装

slice_ring_buf 添加到您的 Cargo.toml 文件中:

slice_ring_buf = 0.2

```toml

use slice_ring_buf::{SliceRB, SliceRbRef};

// Create a ring buffer with type u32. The data will be
// initialized with the default value (0 in this case).
let mut rb = SliceRB::<u32>::from_len(4);

// Memcpy data from a slice into the ring buffer at
// arbitrary `isize` indexes. Earlier data will not be
// copied if it will be overwritten by newer data,
// avoiding unecessary memcpy's. The correct placement
// of the newer data will still be preserved.
rb.write_latest(&[0, 2, 3, 4, 1], 0);
assert_eq!(rb[0], 1);
assert_eq!(rb[1], 2);
assert_eq!(rb[2], 3);
assert_eq!(rb[3], 4);

// Memcpy into slices at arbitrary `isize` indexes
// and length.
let mut read_buffer = [0u32; 7];
rb.read_into(&mut read_buffer, 2);
assert_eq!(read_buffer, [3, 4, 1, 2, 3, 4, 1]);

// Read/write by retrieving slices directly.
let (s1, s2) = rb.as_slices_len(1, 4);
assert_eq!(s1, &[2, 3, 4]);
assert_eq!(s2, &[1]);

// Read/write to buffer by indexing. Performance will be
// limited by the modulo (remainder) operation on an
// `isize` value.
rb[0] = 0;
rb[1] = 1;
rb[2] = 2;
rb[3] = 3;

// Wrap when reading/writing outside of bounds.
// Performance will be limited by the modulo (remainder)
// operation on an `isize` value.
assert_eq!(rb[-1], 3);
assert_eq!(rb[10], 2);

// Aligned/stack data may also be used.
let mut stack_data = [0u32, 1, 2, 3];
let mut rb_ref = SliceRbRef::new(&mut stack_data);
rb_ref[-4] = 5;
let (s1, s2) = rb_ref.as_slices_len(0, 3);
assert_eq!(s1, &[5, 1, 2]);
assert_eq!(s2, &[]);

[dependencies]