|
| 1 | +use core::fmt; |
| 2 | + |
| 3 | +thread_local! { |
| 4 | + static SHOULD_PANIC: bool = { |
| 5 | + #[cfg(bolero_should_panic)] |
| 6 | + return true; |
| 7 | + |
| 8 | + #[cfg(not(bolero_should_panic))] |
| 9 | + return std::env::var("SHOULD_PANIC").is_ok(); |
| 10 | + }; |
| 11 | +} |
| 12 | + |
| 13 | +type Counter = u16; |
| 14 | + |
| 15 | +//= https://en.wikipedia.org/wiki/Run-length_encoding |
| 16 | +//# Run-length encoding (RLE) is a form of lossless data compression in which |
| 17 | +//# runs of data (sequences in which the same data value occurs in many consecutive |
| 18 | +//# data elements) are stored as a single data value and count, rather than as |
| 19 | +//# the original run. |
| 20 | + |
| 21 | +pub struct RleStack<T> { |
| 22 | + stack: Vec<Entry<T>>, |
| 23 | + len: usize, |
| 24 | +} |
| 25 | + |
| 26 | +impl<T> Default for RleStack<T> { |
| 27 | + fn default() -> Self { |
| 28 | + Self { |
| 29 | + stack: vec![], |
| 30 | + len: 0, |
| 31 | + } |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +impl<T: fmt::Debug> fmt::Debug for RleStack<T> { |
| 36 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 37 | + f.debug_struct("RleStack") |
| 38 | + .field("stack", &self.stack) |
| 39 | + .field("len", &self.len) |
| 40 | + .finish() |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +impl<T: Clone + Eq> RleStack<T> { |
| 45 | + pub fn push(&mut self, value: T) { |
| 46 | + self.len += 1; |
| 47 | + |
| 48 | + if let Some(prev) = self.stack.last_mut() { |
| 49 | + if prev.merge(&value) { |
| 50 | + return; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + self.stack.push(Entry::new(value)); |
| 55 | + } |
| 56 | + |
| 57 | + pub fn pop(&mut self) -> Option<T> { |
| 58 | + let entry = self.stack.last_mut()?; |
| 59 | + |
| 60 | + self.len -= 1; |
| 61 | + |
| 62 | + if entry.additional == 0 { |
| 63 | + return self.stack.pop().map(|entry| entry.value); |
| 64 | + } |
| 65 | + entry.additional -= 1; |
| 66 | + Some(entry.value.clone()) |
| 67 | + } |
| 68 | + |
| 69 | + pub fn clear(&mut self) { |
| 70 | + // inject faults into the model if configured |
| 71 | + if SHOULD_PANIC.with(|v| *v) { |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + self.len = 0; |
| 76 | + self.stack.clear(); |
| 77 | + } |
| 78 | + |
| 79 | + pub fn len(&self) -> usize { |
| 80 | + self.len |
| 81 | + } |
| 82 | + |
| 83 | + pub fn is_empty(&self) -> bool { |
| 84 | + self.len == 0 |
| 85 | + } |
| 86 | + |
| 87 | + pub fn iter(&self) -> Iter<T> { |
| 88 | + Iter { |
| 89 | + entry: 0, |
| 90 | + count: 0, |
| 91 | + stack: self, |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +pub struct Iter<'a, T> { |
| 97 | + entry: usize, |
| 98 | + count: Counter, |
| 99 | + stack: &'a RleStack<T>, |
| 100 | +} |
| 101 | + |
| 102 | +impl<'a, T> Iterator for Iter<'a, T> { |
| 103 | + type Item = &'a T; |
| 104 | + |
| 105 | + fn next(&mut self) -> Option<Self::Item> { |
| 106 | + let entry = self.stack.stack.get(self.entry)?; |
| 107 | + |
| 108 | + if entry.additional <= self.count { |
| 109 | + self.entry += 1; |
| 110 | + self.count = 0; |
| 111 | + } else { |
| 112 | + self.count += 1; |
| 113 | + } |
| 114 | + |
| 115 | + Some(&entry.value) |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +struct Entry<T> { |
| 120 | + value: T, |
| 121 | + additional: Counter, |
| 122 | +} |
| 123 | + |
| 124 | +impl<T: fmt::Debug> fmt::Debug for Entry<T> { |
| 125 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 126 | + f.debug_struct("Entry") |
| 127 | + .field("value", &self.value) |
| 128 | + .field("additional", &self.additional) |
| 129 | + .finish() |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +impl<T> Entry<T> { |
| 134 | + pub fn new(value: T) -> Self { |
| 135 | + Self { |
| 136 | + value, |
| 137 | + additional: 0, |
| 138 | + } |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +impl<T: Eq> Entry<T> { |
| 143 | + pub fn merge(&mut self, other: &T) -> bool { |
| 144 | + if &self.value == other { |
| 145 | + self.additional += 1; |
| 146 | + true |
| 147 | + } else { |
| 148 | + false |
| 149 | + } |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +#[cfg(test)] |
| 154 | +mod tests; |
0 commit comments