cache.rs (5029B)
1 /// Unit tests. 2 #[cfg(test)] 3 mod tests; 4 use core::ops::Index; 5 /// A cache of `N` `T`s. When the cache is filled up, 6 /// a new entry overwrites the oldest entry. `Cache<T, N>` 7 /// cannot be expanded or shrunk. 8 #[derive(Debug)] 9 pub struct Cache<T, const N: usize> { 10 /// The cache of `T`s. 11 vals: [T; N], 12 /// The number of `T`s in our cache. Once 13 /// `N` number of `T`s have been cached, this will 14 /// always be `N`. 15 len: usize, 16 /// The index position of the next insert. 17 next_head: usize, 18 } 19 impl<T, const N: usize> Cache<T, N> { 20 /// Returns the number of items cached. 21 /// This will always return `N` once at least 22 /// `N` items have been cached. 23 #[inline] 24 pub const fn len(&self) -> usize { 25 self.len 26 } 27 /// Returns true iff no items have been cached. 28 #[inline] 29 pub const fn is_empty(&self) -> bool { 30 self.len == 0 31 } 32 } 33 /// Implements the functions `get`, `get_unsafe`, and `push` as well as `Index<usize, Output = T>` 34 /// for each of the passed usize literals. 35 /// Only powers of two are allowed to be passed due to how the above functions are implemented. 36 /// If any other value is passed, the implementation of those functions will be wrong. 37 macro_rules! cache { 38 ( $( $x:literal),* ) => { 39 $( 40 impl<T> Cache<T, $x> { 41 /// Returns `Some(&T)` iff there is an item located 42 /// at index `idx`. Indexing begins at the newest 43 /// entry (i.e., `self.get(0)` will return the most recent entry 44 /// iff at least one item has been cached). 45 #[inline] 46 pub fn get(&self, idx: usize) -> Option<&T> { 47 (idx < self.len).then(|| self.vals.index(self.next_head.wrapping_sub(1).wrapping_sub(idx) & ($x - 1))) 48 } 49 /// Returns a `&T` at index position `idx % N`. In the event 50 /// `(idx % N) >= self.len()`, then `&T` references the 51 /// default value of `T` that was inserted at creation but *not* 52 /// actually inserted via `push`. 53 /// 54 /// # Correctness 55 /// 56 /// `idx < self.len()`; otherwise a value that was not actually inserted will be returned. 57 #[inline] 58 pub fn get_unchecked(&self, idx: usize) -> &T { 59 self.vals.index(self.next_head.wrapping_sub(1).wrapping_sub(idx) & ($x - 1)) 60 } 61 /// Adds `val` to the cache. In the event `N` values have already been cached, 62 /// the oldest entry is overwritten. 63 #[expect(clippy::arithmetic_side_effects, reason = "must, and overflow is not possible")] 64 #[expect(clippy::indexing_slicing, reason = "wont panic since next_head is always correct")] 65 #[inline] 66 pub fn push(&mut self, val: T) { 67 if self.len < $x { 68 self.len += 1; 69 } 70 // next_head is always less than or equal to N since we only implement 71 // this function when N is a power of 2. 72 self.vals[self.next_head] = val; 73 self.next_head = (self.next_head + 1) & ($x - 1); 74 } 75 } 76 impl<T> Index<usize> for Cache<T, $x> { 77 type Output = T; 78 #[inline] 79 fn index(&self, index: usize) -> &Self::Output { 80 self.get(index).unwrap() 81 } 82 } 83 )* 84 }; 85 } 86 // MUST only pass powers of two! Anything else will lead to wrong code. 87 cache!( 88 0x1, 89 0x2, 90 0x4, 91 0x8, 92 0x10, 93 0x20, 94 0x40, 95 0x80, 96 0x100, 97 0x200, 98 0x400, 99 0x800, 100 0x1000, 101 0x2000, 102 0x4000, 103 0x8000, 104 0x0001_0000, 105 0x0002_0000, 106 0x0004_0000, 107 0x0008_0000, 108 0x0010_0000, 109 0x0020_0000, 110 0x0040_0000, 111 0x0080_0000, 112 0x0100_0000, 113 0x0200_0000, 114 0x0400_0000, 115 0x0800_0000, 116 0x1000_0000, 117 0x2000_0000, 118 0x4000_0000, 119 0x8000_0000, 120 0x0001_0000_0000, 121 0x0002_0000_0000, 122 0x0004_0000_0000, 123 0x0008_0000_0000, 124 0x0010_0000_0000, 125 0x0020_0000_0000, 126 0x0040_0000_0000, 127 0x0080_0000_0000, 128 0x0100_0000_0000, 129 0x0200_0000_0000, 130 0x0400_0000_0000, 131 0x0800_0000_0000, 132 0x1000_0000_0000, 133 0x2000_0000_0000, 134 0x4000_0000_0000, 135 0x8000_0000_0000, 136 0x0001_0000_0000_0000, 137 0x0002_0000_0000_0000, 138 0x0004_0000_0000_0000, 139 0x0008_0000_0000_0000, 140 0x0010_0000_0000_0000, 141 0x0020_0000_0000_0000, 142 0x0040_0000_0000_0000, 143 0x0080_0000_0000_0000, 144 0x0100_0000_0000_0000, 145 0x0200_0000_0000_0000, 146 0x0400_0000_0000_0000, 147 0x0800_0000_0000_0000, 148 0x1000_0000_0000_0000, 149 0x2000_0000_0000_0000, 150 0x4000_0000_0000_0000, 151 0x8000_0000_0000_0000 152 ); 153 impl<T, const N: usize> Cache<T, N> 154 where 155 [T; N]: Default, 156 { 157 /// Returns an instance of itself pre-loaded with 158 /// the `N` instances of the default value of `T`. 159 /// Note these default instances are treated as though 160 /// they don't exist (i.e., `self.is_empty()` will return true). 161 #[inline] 162 #[must_use] 163 pub fn new() -> Self { 164 Self::default() 165 } 166 } 167 impl<T, const N: usize> Default for Cache<T, N> 168 where 169 [T; N]: Default, 170 { 171 #[inline] 172 fn default() -> Self { 173 Self { 174 vals: Default::default(), 175 len: 0, 176 next_head: 0, 177 } 178 } 179 }