reth_trie/
forward_cursor.rs

1/// The implementation of forward-only in memory cursor over the entries.
2///
3/// The cursor operates under the assumption that the supplied collection is pre-sorted.
4#[derive(Debug)]
5pub struct ForwardInMemoryCursor<'a, K, V> {
6    /// The reference to the pre-sorted collection of entries.
7    entries: std::slice::Iter<'a, (K, V)>,
8    is_empty: bool,
9}
10
11impl<'a, K, V> ForwardInMemoryCursor<'a, K, V> {
12    /// Create new forward cursor positioned at the beginning of the collection.
13    ///
14    /// The cursor expects all of the entries have been sorted in advance.
15    #[inline]
16    pub fn new(entries: &'a [(K, V)]) -> Self {
17        Self { entries: entries.iter(), is_empty: entries.is_empty() }
18    }
19
20    /// Returns `true` if the cursor is empty, regardless of its position.
21    #[inline]
22    pub fn is_empty(&self) -> bool {
23        self.is_empty
24    }
25
26    #[inline]
27    fn peek(&self) -> Option<&(K, V)> {
28        self.entries.clone().next()
29    }
30
31    #[inline]
32    fn next(&mut self) -> Option<&(K, V)> {
33        self.entries.next()
34    }
35}
36
37impl<K, V> ForwardInMemoryCursor<'_, K, V>
38where
39    K: PartialOrd + Clone,
40    V: Clone,
41{
42    /// Returns the first entry from the current cursor position that's greater or equal to the
43    /// provided key. This method advances the cursor forward.
44    pub fn seek(&mut self, key: &K) -> Option<(K, V)> {
45        self.advance_while(|k| k < key)
46    }
47
48    /// Returns the first entry from the current cursor position that's greater than the provided
49    /// key. This method advances the cursor forward.
50    pub fn first_after(&mut self, key: &K) -> Option<(K, V)> {
51        self.advance_while(|k| k <= key)
52    }
53
54    /// Advances the cursor forward while `predicate` returns `true` or until the collection is
55    /// exhausted.
56    ///
57    /// Returns the first entry for which `predicate` returns `false` or `None`. The cursor will
58    /// point to the returned entry.
59    fn advance_while(&mut self, predicate: impl Fn(&K) -> bool) -> Option<(K, V)> {
60        let mut entry;
61        loop {
62            entry = self.peek();
63            if entry.is_some_and(|(k, _)| predicate(k)) {
64                self.next();
65            } else {
66                break;
67            }
68        }
69        entry.cloned()
70    }
71}
72
73#[cfg(test)]
74mod tests {
75    use super::*;
76
77    #[test]
78    fn test_cursor() {
79        let mut cursor = ForwardInMemoryCursor::new(&[(1, ()), (2, ()), (3, ()), (4, ()), (5, ())]);
80
81        assert_eq!(cursor.seek(&0), Some((1, ())));
82        assert_eq!(cursor.peek(), Some(&(1, ())));
83
84        assert_eq!(cursor.seek(&3), Some((3, ())));
85        assert_eq!(cursor.peek(), Some(&(3, ())));
86
87        assert_eq!(cursor.seek(&3), Some((3, ())));
88        assert_eq!(cursor.peek(), Some(&(3, ())));
89
90        assert_eq!(cursor.seek(&4), Some((4, ())));
91        assert_eq!(cursor.peek(), Some(&(4, ())));
92
93        assert_eq!(cursor.seek(&6), None);
94        assert_eq!(cursor.peek(), None);
95    }
96}