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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//! Ring buffer for time-series data
//!
//! A fixed-capacity buffer that automatically removes oldest values when full.
//! Useful for storing streaming data like CPU usage, latency, throughput.
//!
//! # Example
//! ```ignore
//! let mut buffer = SparklineBuffer::new(100);
//!
//! // Push values (oldest auto-removed when full)
//! buffer.push(45.0);
//! buffer.push(52.0);
//!
//! // Use with Sparkline
//! Sparkline::new(&buffer.as_vec()).show(ui);
//! ```
use std::collections::VecDeque;
/// Fixed-capacity ring buffer for time-series data
#[derive(Debug, Clone)]
pub struct SparklineBuffer {
data: VecDeque<f32>,
capacity: usize,
}
impl SparklineBuffer {
/// Create a new buffer with the specified capacity
pub fn new(capacity: usize) -> Self {
Self {
data: VecDeque::with_capacity(capacity),
capacity,
}
}
/// Create a buffer pre-filled with a default value
pub fn filled(capacity: usize, value: f32) -> Self {
let mut data = VecDeque::with_capacity(capacity);
data.resize(capacity, value);
Self { data, capacity }
}
/// Push a new value, removing the oldest if at capacity
pub fn push(&mut self, value: f32) {
if self.data.len() >= self.capacity {
self.data.pop_front();
}
self.data.push_back(value);
}
/// Push multiple values at once
pub fn extend(&mut self, values: impl IntoIterator<Item = f32>) {
for value in values {
self.push(value);
}
}
/// Get a contiguous Vec copy (for APIs that need &[f32])
pub fn as_vec(&self) -> Vec<f32> {
self.data.iter().copied().collect()
}
/// Get slices (may be split due to ring buffer)
pub fn as_slices(&self) -> (&[f32], &[f32]) {
self.data.as_slices()
}
/// Number of values currently stored
pub fn len(&self) -> usize {
self.data.len()
}
/// Check if buffer is empty
pub fn is_empty(&self) -> bool {
self.data.is_empty()
}
/// Maximum capacity
pub fn capacity(&self) -> usize {
self.capacity
}
/// Clear all values
pub fn clear(&mut self) {
self.data.clear();
}
/// Get the most recent value
pub fn last(&self) -> Option<f32> {
self.data.back().copied()
}
/// Get the oldest value
pub fn first(&self) -> Option<f32> {
self.data.front().copied()
}
/// Get min and max values (useful for auto-scaling)
pub fn min_max(&self) -> Option<(f32, f32)> {
if self.data.is_empty() {
return None;
}
let mut min = f32::MAX;
let mut max = f32::MIN;
for &v in &self.data {
min = min.min(v);
max = max.max(v);
}
Some((min, max))
}
/// Calculate average of all values
pub fn average(&self) -> Option<f32> {
if self.data.is_empty() {
return None;
}
let sum: f32 = self.data.iter().sum();
Some(sum / self.data.len() as f32)
}
/// Iterate over values (oldest to newest)
pub fn iter(&self) -> impl Iterator<Item = f32> + '_ {
self.data.iter().copied()
}
}
impl Default for SparklineBuffer {
fn default() -> Self {
Self::new(100)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_push_and_capacity() {
let mut buf = SparklineBuffer::new(3);
buf.push(1.0);
buf.push(2.0);
buf.push(3.0);
assert_eq!(buf.as_vec(), vec![1.0, 2.0, 3.0]);
// Should remove oldest
buf.push(4.0);
assert_eq!(buf.as_vec(), vec![2.0, 3.0, 4.0]);
}
#[test]
fn test_min_max() {
let mut buf = SparklineBuffer::new(5);
buf.extend([10.0, 5.0, 20.0, 15.0]);
assert_eq!(buf.min_max(), Some((5.0, 20.0)));
}
#[test]
fn test_average() {
let mut buf = SparklineBuffer::new(4);
buf.extend([10.0, 20.0, 30.0, 40.0]);
assert_eq!(buf.average(), Some(25.0));
}
}