| 1 | use crate::graph::{Disambiguate, Graph, NodeId, Range}; |
| 2 | |
| 3 | #[derive(Clone)] |
| 4 | pub struct Fork { |
| 5 | /// LUT matching byte -> node id |
| 6 | lut: Box<[Option<NodeId>; 256]>, |
| 7 | /// State to go to if no arms are matching |
| 8 | pub miss: Option<NodeId>, |
| 9 | } |
| 10 | |
| 11 | impl Fork { |
| 12 | pub fn new() -> Self { |
| 13 | Fork { |
| 14 | lut: Box::new([None; 256]), |
| 15 | miss: None, |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | pub fn miss<M>(mut self, miss: M) -> Self |
| 20 | where |
| 21 | M: Into<Option<NodeId>>, |
| 22 | { |
| 23 | self.miss = miss.into(); |
| 24 | self |
| 25 | } |
| 26 | |
| 27 | pub fn add_branch<R, T>(&mut self, range: R, then: NodeId, graph: &mut Graph<T>) |
| 28 | where |
| 29 | R: Into<Range>, |
| 30 | T: Disambiguate, |
| 31 | { |
| 32 | for byte in range.into() { |
| 33 | match &mut self.lut[byte as usize] { |
| 34 | Some(other) if *other != then => { |
| 35 | *other = graph.merge(*other, then); |
| 36 | } |
| 37 | opt => *opt = Some(then), |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | // TODO: Add result with a printable error |
| 43 | pub fn merge<T>(&mut self, other: Fork, graph: &mut Graph<T>) |
| 44 | where |
| 45 | T: Disambiguate, |
| 46 | { |
| 47 | self.miss = match (self.miss, other.miss) { |
| 48 | (None, None) => None, |
| 49 | (Some(id), None) | (None, Some(id)) => Some(id), |
| 50 | (Some(a), Some(b)) => Some(graph.merge(a, b)), |
| 51 | }; |
| 52 | |
| 53 | for (left, right) in self.lut.iter_mut().zip(other.lut.iter()) { |
| 54 | *left = match (*left, *right) { |
| 55 | (None, None) => continue, |
| 56 | (Some(id), None) | (None, Some(id)) => Some(id), |
| 57 | (Some(a), Some(b)) => Some(graph.merge(a, b)), |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | pub fn branches(&self) -> ForkIter<'_> { |
| 63 | ForkIter { |
| 64 | offset: 0, |
| 65 | lut: &self.lut, |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /// Checks if all bytes in the `range` have a branch on this |
| 70 | /// fork, and those branches are resolve to the same `NodeId`. |
| 71 | pub fn contains<R>(&self, range: R) -> Option<NodeId> |
| 72 | where |
| 73 | R: Into<Range>, |
| 74 | { |
| 75 | let mut range = range.into(); |
| 76 | let byte = range.next()?; |
| 77 | let first = self.lut[byte as usize]?; |
| 78 | |
| 79 | for byte in range { |
| 80 | if first != self.lut[byte as usize]? { |
| 81 | return None; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | Some(first) |
| 86 | } |
| 87 | |
| 88 | pub fn branch<R>(mut self, range: R, then: NodeId) -> Self |
| 89 | where |
| 90 | R: Into<Range>, |
| 91 | { |
| 92 | for byte in range.into() { |
| 93 | match &mut self.lut[byte as usize] { |
| 94 | Some(other) if *other != then => { |
| 95 | panic!("Overlapping branches"); |
| 96 | } |
| 97 | opt => *opt = Some(then), |
| 98 | } |
| 99 | } |
| 100 | self |
| 101 | } |
| 102 | |
| 103 | pub fn shake<T>(&self, graph: &Graph<T>, filter: &mut [bool]) { |
| 104 | if let Some(id) = self.miss { |
| 105 | if !filter[id.get()] { |
| 106 | filter[id.get()] = true; |
| 107 | graph[id].shake(graph, filter); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | for (_, id) in self.branches() { |
| 112 | if !filter[id.get()] { |
| 113 | filter[id.get()] = true; |
| 114 | graph[id].shake(graph, filter); |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | pub struct ForkIter<'a> { |
| 121 | offset: usize, |
| 122 | lut: &'a [Option<NodeId>; 256], |
| 123 | } |
| 124 | |
| 125 | impl Iterator for ForkIter<'_> { |
| 126 | type Item = (Range, NodeId); |
| 127 | |
| 128 | fn next(&mut self) -> Option<Self::Item> { |
| 129 | // Consume empty slots |
| 130 | self.offset += self.lut[self.offset..] |
| 131 | .iter() |
| 132 | .take_while(|next| next.is_none()) |
| 133 | .count(); |
| 134 | |
| 135 | let then = self.lut.get(self.offset).copied().flatten()?; |
| 136 | let start = self.offset; |
| 137 | |
| 138 | // Consume all slots with same NodeId target |
| 139 | self.offset += self.lut[self.offset..] |
| 140 | .iter() |
| 141 | .take_while(|next| **next == Some(then)) |
| 142 | .count(); |
| 143 | |
| 144 | Some(( |
| 145 | Range { |
| 146 | start: start as u8, |
| 147 | end: (self.offset - 1) as u8, |
| 148 | }, |
| 149 | then, |
| 150 | )) |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | #[cfg(test)] |
| 155 | mod tests { |
| 156 | use super::*; |
| 157 | use crate::graph::Node; |
| 158 | use pretty_assertions::assert_eq; |
| 159 | |
| 160 | #[test] |
| 161 | fn fork_iter() { |
| 162 | let mut buf = [None; 256]; |
| 163 | |
| 164 | for byte in b'4'..=b'7' { |
| 165 | buf[byte as usize] = Some(NodeId::new(1)); |
| 166 | } |
| 167 | for byte in b'a'..=b'd' { |
| 168 | buf[byte as usize] = Some(NodeId::new(2)); |
| 169 | } |
| 170 | |
| 171 | let iter = ForkIter { |
| 172 | offset: 0, |
| 173 | lut: &buf, |
| 174 | }; |
| 175 | |
| 176 | assert_eq!( |
| 177 | &[ |
| 178 | ( |
| 179 | Range { |
| 180 | start: b'4', |
| 181 | end: b'7' |
| 182 | }, |
| 183 | NodeId::new(1) |
| 184 | ), |
| 185 | ( |
| 186 | Range { |
| 187 | start: b'a', |
| 188 | end: b'd' |
| 189 | }, |
| 190 | NodeId::new(2) |
| 191 | ), |
| 192 | ], |
| 193 | &*iter.collect::<Vec<_>>(), |
| 194 | ); |
| 195 | } |
| 196 | |
| 197 | #[test] |
| 198 | fn merge_no_conflict() { |
| 199 | let mut graph = Graph::new(); |
| 200 | |
| 201 | let leaf1 = graph.push(Node::Leaf("FOO")); |
| 202 | let leaf2 = graph.push(Node::Leaf("BAR")); |
| 203 | |
| 204 | let mut fork = Fork::new().branch(b'1', leaf1); |
| 205 | |
| 206 | fork.merge(Fork::new().branch(b'2', leaf2), &mut graph); |
| 207 | |
| 208 | assert_eq!(fork, Fork::new().branch(b'1', leaf1).branch(b'2', leaf2)); |
| 209 | } |
| 210 | |
| 211 | #[test] |
| 212 | fn merge_miss_right() { |
| 213 | let mut graph = Graph::new(); |
| 214 | |
| 215 | let leaf1 = graph.push(Node::Leaf("FOO")); |
| 216 | let leaf2 = graph.push(Node::Leaf("BAR")); |
| 217 | |
| 218 | let mut fork = Fork::new().branch(b'1', leaf1); |
| 219 | |
| 220 | fork.merge(Fork::new().miss(leaf2), &mut graph); |
| 221 | |
| 222 | assert_eq!(fork, Fork::new().branch(b'1', leaf1).miss(leaf2)); |
| 223 | } |
| 224 | |
| 225 | #[test] |
| 226 | fn merge_miss_left() { |
| 227 | let mut graph = Graph::new(); |
| 228 | |
| 229 | let leaf1 = graph.push(Node::Leaf("FOO")); |
| 230 | let leaf2 = graph.push(Node::Leaf("BAR")); |
| 231 | |
| 232 | let mut fork = Fork::new().miss(leaf1); |
| 233 | |
| 234 | fork.merge(Fork::new().branch(b'2', leaf2), &mut graph); |
| 235 | |
| 236 | assert_eq!(fork, Fork::new().branch(b'2', leaf2).miss(leaf1)); |
| 237 | } |
| 238 | |
| 239 | #[test] |
| 240 | fn contains_byte() { |
| 241 | let fork = Fork::new().branch('a'..='z', NodeId::new(42)); |
| 242 | |
| 243 | assert_eq!(fork.contains(b't'), Some(NodeId::new(42))); |
| 244 | } |
| 245 | |
| 246 | #[test] |
| 247 | fn contains_range() { |
| 248 | let fork = Fork::new() |
| 249 | .branch('a'..='m', NodeId::new(42)) |
| 250 | .branch('n'..='z', NodeId::new(42)); |
| 251 | |
| 252 | assert_eq!(fork.contains('i'..='r'), Some(NodeId::new(42))); |
| 253 | assert_eq!(fork.contains('a'..='z'), Some(NodeId::new(42))); |
| 254 | } |
| 255 | |
| 256 | #[test] |
| 257 | fn contains_different_ranges() { |
| 258 | let fork = Fork::new() |
| 259 | .branch('a'..='m', NodeId::new(42)) |
| 260 | .branch('n'..='z', NodeId::new(47)); |
| 261 | |
| 262 | assert_eq!(fork.contains('i'..='r'), None); |
| 263 | assert_eq!(fork.contains('a'..='z'), None); |
| 264 | assert_eq!(fork.contains('d'..='f'), Some(NodeId::new(42))); |
| 265 | assert_eq!(fork.contains('n'..='p'), Some(NodeId::new(47))); |
| 266 | } |
| 267 | } |