trie_rs/internal_data_structure/naive_trie/naive_trie_b_f_iter.rs
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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
use super::NaiveTrie;
use std::collections::VecDeque;
#[derive(Debug)]
/// Iterates over NaiveTrie in Breadth-First manner.
pub struct NaiveTrieBFIter<Label, Value> {
unvisited: VecDeque<NaiveTrie<Label, Value>>,
}
impl<Label, Value> NaiveTrieBFIter<Label, Value> {
pub fn new(iter_start: NaiveTrie<Label, Value>) -> Self {
let mut unvisited = VecDeque::new();
unvisited.push_back(iter_start);
Self { unvisited }
}
}
impl<Label: Ord, Value> Iterator for NaiveTrieBFIter<Label, Value> {
type Item = NaiveTrie<Label, Value>;
/// Returns:
///
/// - None: All nodes are visited.
/// - Some(NaiveTrie::Root): Root node.
/// - Some(NaiveTrie::IntermOrLeaf): Intermediate or leaf node.
/// - Some(NaiveTrie::PhantomSibling): Marker to represent "all siblings are iterated".
fn next(&mut self) -> Option<Self::Item> {
self.unvisited.pop_front().map(|mut trie| {
match trie {
NaiveTrie::Root(_) | NaiveTrie::IntermOrLeaf(_) => {
for child in trie.drain_children() {
self.unvisited.push_back(child);
}
self.unvisited.push_back(NaiveTrie::PhantomSibling);
}
NaiveTrie::PhantomSibling => {}
};
trie
})
}
}
#[cfg(test)]
mod bf_iter_tests {
type NaiveTrie<T> = super::NaiveTrie<T, ()>;
const TRUE: Option<()> = Some(());
const FALSE: Option<()> = None;
macro_rules! parameterized_tests {
($($name:ident: $value:expr,)*) => {
$(
#[test]
fn $name() {
let (words, expected_nodes) = $value;
let mut trie = NaiveTrie::make_root();
for word in words {
trie.push(word.bytes().into_iter(), ());
}
let nodes: Vec<NaiveTrie<u8>> = trie.into_iter().collect();
assert_eq!(nodes.len(), expected_nodes.len());
for i in 0..nodes.len() {
let node = &nodes[i];
let expected_node = &expected_nodes[i];
assert!(std::mem::discriminant(node) == std::mem::discriminant(expected_node));
if let NaiveTrie::IntermOrLeaf(n) = node {
assert_eq!(n.label, *expected_node.label());
assert_eq!(n.value.is_some(), expected_node.value().is_some());
}
}
}
)*
}
}
parameterized_tests! {
t1: (
Vec::<&str>::new(),
vec![
NaiveTrie::make_root(),
// parent = root
NaiveTrie::PhantomSibling,
]
),
t2: (
vec!["a"],
vec![
NaiveTrie::make_root(),
// parent = root
NaiveTrie::make_interm_or_leaf(b'a', TRUE),
NaiveTrie::PhantomSibling,
// parent = a
NaiveTrie::PhantomSibling,
]
),
t3: (
vec!["a", "a"],
vec![
NaiveTrie::make_root(),
// parent = root
NaiveTrie::make_interm_or_leaf(b'a', TRUE),
NaiveTrie::PhantomSibling,
// parent = a
NaiveTrie::PhantomSibling,
]
),
t4: (
// root
// |-----------------------+-----------------------+
// | | |
// a (term) b Ph
// |---------+ |-----------------+
// | | | |
// n (term) Ph a Ph
// | |--------+
// | | |
// Ph d (term) Ph
// |
// |
// Ph
vec!["a", "bad", "an"],
vec![
NaiveTrie::make_root(),
// parent = root
NaiveTrie::make_interm_or_leaf(b'a', TRUE),
NaiveTrie::make_interm_or_leaf(b'b', FALSE),
NaiveTrie::PhantomSibling,
// parent = [a]
NaiveTrie::make_interm_or_leaf(b'n', TRUE),
NaiveTrie::PhantomSibling,
// parent = b
NaiveTrie::make_interm_or_leaf(b'a', FALSE),
NaiveTrie::PhantomSibling,
// parent = n
NaiveTrie::PhantomSibling,
// parent = b[a]d
NaiveTrie::make_interm_or_leaf(b'd', TRUE),
NaiveTrie::PhantomSibling,
// parent = d
NaiveTrie::PhantomSibling,
]
),
t5: (
// 'り' => 227, 130, 138
// 'ん' => 227, 130, 147
// 'ご' => 227, 129, 148
vec!["a", "an", "りんご", "りんりん"],
vec![
NaiveTrie::make_root(),
// parent = root
NaiveTrie::make_interm_or_leaf(b'a', TRUE),
NaiveTrie::make_interm_or_leaf(227, FALSE),
NaiveTrie::PhantomSibling,
// parent = a
NaiveTrie::make_interm_or_leaf(b'n', TRUE),
NaiveTrie::PhantomSibling,
// parent = [227] 130 138 (り)
NaiveTrie::make_interm_or_leaf(130, FALSE),
NaiveTrie::PhantomSibling,
// parent = n
NaiveTrie::PhantomSibling,
// parent = 227 [130] 138 (り)
NaiveTrie::make_interm_or_leaf(138, FALSE),
NaiveTrie::PhantomSibling,
// parent = 227 130 [138] (り)
NaiveTrie::make_interm_or_leaf(227, FALSE),
NaiveTrie::PhantomSibling,
// parent = [227] 130 147 (ん)
NaiveTrie::make_interm_or_leaf(130, FALSE),
NaiveTrie::PhantomSibling,
// parent = 227 [130] 147 (ん)
NaiveTrie::make_interm_or_leaf(147, FALSE),
NaiveTrie::PhantomSibling,
// parent = 227 130 [147] (ん)
NaiveTrie::make_interm_or_leaf(227, FALSE),
NaiveTrie::PhantomSibling,
// parent = [227] _ _ (ご or り)
NaiveTrie::make_interm_or_leaf(129, FALSE),
NaiveTrie::make_interm_or_leaf(130, FALSE),
NaiveTrie::PhantomSibling,
// parent = 227 [129] 148 (ご)
NaiveTrie::make_interm_or_leaf(148, TRUE),
NaiveTrie::PhantomSibling,
// parent = 227 [130] 138 (り)
NaiveTrie::make_interm_or_leaf(138, FALSE),
NaiveTrie::PhantomSibling,
// parent = 227 129 [148] (ご)
NaiveTrie::PhantomSibling,
// parent = 227 130 [138] (り)
NaiveTrie::make_interm_or_leaf(227, FALSE),
NaiveTrie::PhantomSibling,
// parent = [227] 130 147 (ん)
NaiveTrie::make_interm_or_leaf(130, FALSE),
NaiveTrie::PhantomSibling,
// parent = 227 [130] 147 (ん)
NaiveTrie::make_interm_or_leaf(147, TRUE),
NaiveTrie::PhantomSibling,
// parent = 227 130 [147] (ん)
NaiveTrie::PhantomSibling,
]
),
}
}