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
//! DFS iterators over a byte `PersistentARTrie`.
//!
//! Split out of byte `dict_impl.rs` (Phase-5 decomposition). The public
//! `TermIterator<V>` / `TermValueIterator<V>` types yield a pre-collected
//! snapshot of (term[, value]) pairs.
//!
//! **L3.3c:** the owned tree is gone, so these iterators are driven solely by the
//! pre-collected `from_terms` snapshot (produced from the overlay enumeration by
//! `public_iter::iter` / `iter_with_values`). The owned stack-based DFS (the
//! `IterState` enum + the `::new(&TrieRoot)` constructors that walked the owned
//! `TrieRoot` / `ChildNode` representation) was deleted.
use crate::value::DictionaryValue;
/// Iterator over all terms in a PersistentARTrie.
///
/// Yields the pre-collected snapshot in lexicographic order.
///
/// # Example
///
/// ```text
/// use libdictenstein::persistent_artrie::PersistentARTrie;
///
/// let mut dict = PersistentARTrie::new();
/// dict.insert("apple");
/// dict.insert("banana");
///
/// for term in dict.iter() {
/// println!("{}", String::from_utf8_lossy(&term));
/// }
/// ```
pub struct TermIterator<V: DictionaryValue> {
/// Pre-collected snapshot used by disk-aware public iteration.
precollected: std::vec::IntoIter<Vec<u8>>,
/// Marker for value type
_marker: std::marker::PhantomData<V>,
}
impl<V: DictionaryValue> TermIterator<V> {
/// Create an iterator from a pre-collected snapshot.
pub(super) fn from_terms(terms: Vec<Vec<u8>>) -> Self {
Self {
precollected: terms.into_iter(),
_marker: std::marker::PhantomData,
}
}
}
impl<V: DictionaryValue> Iterator for TermIterator<V> {
type Item = Vec<u8>;
fn next(&mut self) -> Option<Self::Item> {
self.precollected.next()
}
}
/// Iterator over all terms with their values in a PersistentARTrie.
///
/// Yields the pre-collected (term, value) snapshot in lexicographic order.
pub struct TermValueIterator<V: DictionaryValue> {
/// Pre-collected snapshot used by disk-aware public iteration.
precollected: std::vec::IntoIter<(Vec<u8>, Option<V>)>,
/// Marker for value type
_marker: std::marker::PhantomData<V>,
}
impl<V: DictionaryValue> TermValueIterator<V> {
/// Create an iterator from a pre-collected snapshot.
pub(super) fn from_terms(terms: Vec<(Vec<u8>, Option<V>)>) -> Self {
Self {
precollected: terms.into_iter(),
_marker: std::marker::PhantomData,
}
}
}
impl<V: DictionaryValue> Iterator for TermValueIterator<V> {
type Item = (Vec<u8>, Option<V>);
fn next(&mut self) -> Option<Self::Item> {
self.precollected.next()
}
}