Skip to main content

ds_ring/
oracle.rs

1//! Naive reference implementations: the forever-oracle.
2//!
3//! Plain `Vec` with shift-on-pop — spec forms, not optimized (the shift is
4//! deliberately quadratic on eviction). Differential tests in
5//! tests/props.rs prove the real rings match these exactly; the oracle is
6//! never deleted (ds-kit AGENTS.md). Do not optimize this module.
7
8use alloc::vec::Vec;
9
10/// Oracle for [`crate::SampleRing`]: oldest value at index `0`, eviction
11/// shifts the whole `Vec`.
12#[derive(Debug, Clone, Default)]
13pub struct OracleSampleRing<T, const N: usize> {
14    /// Live values, oldest first; `len() <= N`.
15    buf: Vec<T>,
16}
17
18impl<T, const N: usize> OracleSampleRing<T, N> {
19    /// New empty oracle ring.
20    #[must_use]
21    pub fn new() -> Self {
22        Self { buf: Vec::new() }
23    }
24
25    /// Push one value; mirrors [`crate::SampleRing::push`] exactly:
26    /// `None` when accepted without eviction, `Some(old)` with the evicted
27    /// oldest when full, and the pushed value straight back when `N == 0`.
28    pub fn push(&mut self, value: T) -> Option<T> {
29        if N == 0 {
30            return Some(value);
31        }
32        if self.buf.len() == N {
33            let oldest = self.buf.remove(0);
34            self.buf.push(value);
35            Some(oldest)
36        } else {
37            self.buf.push(value);
38            None
39        }
40    }
41
42    /// Oldest-first iterator over the live values.
43    pub fn iter(&self) -> core::slice::Iter<'_, T> {
44        self.buf.iter()
45    }
46
47    /// Number of live values (`0..=N`).
48    #[must_use]
49    pub fn len(&self) -> usize {
50        self.buf.len()
51    }
52
53    /// True when the oracle ring holds no values.
54    #[must_use]
55    pub fn is_empty(&self) -> bool {
56        self.buf.is_empty()
57    }
58
59    /// Newest value, or `None` when empty.
60    #[must_use]
61    pub fn last(&self) -> Option<&T> {
62        self.buf.last()
63    }
64}
65
66impl<'a, T, const N: usize> IntoIterator for &'a OracleSampleRing<T, N> {
67    type Item = &'a T;
68    type IntoIter = core::slice::Iter<'a, T>;
69
70    fn into_iter(self) -> Self::IntoIter {
71        self.buf.iter()
72    }
73}
74
75/// Oracle for [`crate::SpscRing`]: plain `Vec` bounded by `capacity`,
76/// shift-on-pop FIFO.
77#[derive(Debug, Clone)]
78pub struct OracleSpsc<T> {
79    /// Fixed bound (never mutated after construction).
80    capacity: usize,
81    /// Live values, oldest first.
82    buf: Vec<T>,
83}
84
85impl<T> OracleSpsc<T> {
86    /// New empty oracle queue with `capacity` slots; `None` for capacity
87    /// `0`, mirroring [`crate::SpscRing::new`].
88    #[must_use]
89    pub fn new(capacity: usize) -> Option<Self> {
90        if capacity == 0 {
91            None
92        } else {
93            Some(Self {
94                capacity,
95                buf: Vec::new(),
96            })
97        }
98    }
99
100    /// Push one value; `Err(value)` when full, mirroring
101    /// [`crate::SpscRing::push`].
102    ///
103    /// # Errors
104    ///
105    /// Returns `Err(value)` unchanged when the queue is full; the value is
106    /// not stored.
107    pub fn push(&mut self, value: T) -> Result<(), T> {
108        if self.buf.len() == self.capacity {
109            Err(value)
110        } else {
111            self.buf.push(value);
112            Ok(())
113        }
114    }
115
116    /// Pop the oldest value, or `None` when empty, mirroring
117    /// [`crate::SpscRing::pop`].
118    pub fn pop(&mut self) -> Option<T> {
119        if self.buf.is_empty() {
120            None
121        } else {
122            Some(self.buf.remove(0))
123        }
124    }
125
126    /// Number of live values.
127    #[must_use]
128    pub fn len(&self) -> usize {
129        self.buf.len()
130    }
131
132    /// True when the oracle queue holds no values.
133    #[must_use]
134    pub fn is_empty(&self) -> bool {
135        self.buf.is_empty()
136    }
137}