-
Notifications
You must be signed in to change notification settings - Fork 0
/
bad_ring_buffer.rs
121 lines (98 loc) · 3.02 KB
/
bad_ring_buffer.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//! This proved to be okish for small buffers, but a bad idea for big ones - the
//! whole massive buffer would get loaded onto the stack and crash the program.
#[allow(unused_imports)]
use micromath::F32Ext;
pub struct RingBuffer<const N: usize> {
buffer: [f32; N],
write_index: usize,
}
impl<const N: usize> RingBuffer<N> {
pub fn new() -> Self {
let buffer = {
let mut data: [core::mem::MaybeUninit<f32>; N] =
unsafe { core::mem::MaybeUninit::uninit().assume_init() };
for elem in &mut data[..] {
unsafe {
core::ptr::write(elem.as_mut_ptr(), 0.0);
}
}
unsafe { core::mem::transmute_copy(&data) }
};
Self {
buffer,
write_index: 0,
}
}
pub fn write(&mut self, value: f32) {
self.write_index %= N;
self.buffer[self.write_index] = value;
self.write_index += 1;
}
pub fn peek(&self, relative_index: i32) -> f32 {
let index =
(self.write_index as i32 + relative_index - 1).wrapping_rem_euclid(N as i32) as usize;
self.buffer[index]
}
pub fn peek_interpolated(&self, relative_index: f32) -> f32 {
let index_a = relative_index.floor() as i32;
let a = self.peek(index_a);
let index_b = relative_index.ceil() as i32;
let b = self.peek(index_b);
let diff = b - a;
let root = if relative_index < 0.0 { b } else { a };
root + diff * relative_index.fract()
}
}
impl<const N: usize> Default for RingBuffer<N> {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn initialize_buffer() {
let _buffer = RingBuffer::<2>::new();
}
#[test]
fn write_to_buffer() {
let mut buffer = RingBuffer::<2>::new();
buffer.write(1.0);
}
#[test]
fn read_from_buffer() {
let mut buffer = RingBuffer::<3>::new();
buffer.write(1.0);
buffer.write(2.0);
buffer.write(3.0);
assert_eq!(buffer.peek(0), 3.0);
assert_eq!(buffer.peek(-1), 2.0);
assert_eq!(buffer.peek(-2), 1.0);
}
#[test]
fn read_interpolated_from_buffer_with_positive_index() {
let mut buffer = RingBuffer::<3>::new();
buffer.write(1.0);
buffer.write(10.0);
buffer.write(0.0);
assert_relative_eq!(buffer.peek_interpolated(0.6), 0.6);
}
#[test]
fn read_interpolated_from_buffer_with_negative_index() {
let mut buffer = RingBuffer::<3>::new();
buffer.write(10.0);
buffer.write(1.0);
buffer.write(0.0);
assert_relative_eq!(buffer.peek_interpolated(-0.6), 0.6);
}
#[test]
fn cross_buffer_end_while_reading() {
let mut buffer = RingBuffer::<101>::new();
for x in 0..=100 {
buffer.write(x as f32);
}
assert_eq!(buffer.peek(0) as usize, 100);
assert_eq!(buffer.peek(-1) as usize, 100 - 1);
}
}