Skip to main content

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: &'a [(K, V)],
8    /// Current index in the collection.
9    idx: usize,
10}
11
12impl<'a, K, V> ForwardInMemoryCursor<'a, K, V> {
13    /// Create new forward cursor positioned at the beginning of the collection.
14    ///
15    /// The cursor expects all of the entries to have been sorted in advance.
16    #[inline]
17    pub const fn new(entries: &'a [(K, V)]) -> Self {
18        Self { entries, idx: 0 }
19    }
20
21    /// Returns `true` if the cursor is empty, regardless of its position.
22    #[inline]
23    pub const fn is_empty(&self) -> bool {
24        self.entries.is_empty()
25    }
26
27    /// Returns `true` if any entry satisfies the predicate.
28    #[inline]
29    pub fn has_any<F>(&self, predicate: F) -> bool
30    where
31        F: Fn(&(K, V)) -> bool,
32    {
33        self.entries.iter().any(predicate)
34    }
35
36    /// Returns the current entry pointed to be the cursor, or `None` if no entries are left.
37    #[inline]
38    pub fn current(&self) -> Option<&(K, V)> {
39        self.entries.get(self.idx)
40    }
41
42    /// Resets the cursor to the beginning of the collection.
43    #[inline]
44    pub const fn reset(&mut self) {
45        self.idx = 0;
46    }
47}
48
49/// Threshold for remaining entries above which binary search is used instead of linear scan.
50/// For small slices, linear scan has better cache locality and lower overhead.
51const BINARY_SEARCH_THRESHOLD: usize = 128;
52
53impl<K: Ord, V> ForwardInMemoryCursor<'_, K, V> {
54    /// Returns the first entry from the current cursor position that's greater or equal to the
55    /// provided key. This method advances the cursor forward.
56    pub fn seek(&mut self, key: &K) -> Option<&(K, V)> {
57        if self.current().is_some_and(|(k, _)| k >= key) {
58            return self.current()
59        }
60
61        self.advance_while(|k| k < key)
62    }
63
64    /// Returns the first entry from the current cursor position that's greater than the provided
65    /// key. This method advances the cursor forward.
66    pub fn first_after(&mut self, key: &K) -> Option<&(K, V)> {
67        if self.current().is_some_and(|(k, _)| k > key) {
68            return self.current()
69        }
70
71        self.advance_while(|k| k <= key)
72    }
73
74    /// Advances the cursor forward while `predicate` returns `true` or until the collection is
75    /// exhausted.
76    ///
77    /// Uses binary search for large remaining slices (>= 128 entries), linear scan for small ones.
78    ///
79    /// Returns the first entry for which `predicate` returns `false` or `None`. The cursor will
80    /// point to the returned entry.
81    fn advance_while(&mut self, predicate: impl Fn(&K) -> bool) -> Option<&(K, V)> {
82        let remaining = self.entries.len().saturating_sub(self.idx);
83        if remaining >= BINARY_SEARCH_THRESHOLD {
84            let slice = &self.entries[self.idx..];
85            let pos = slice.partition_point(|(k, _)| predicate(k));
86            self.idx += pos;
87        } else {
88            while self.idx < self.entries.len() && predicate(&self.entries[self.idx].0) {
89                self.idx += 1;
90            }
91        }
92        self.current()
93    }
94}
95
96#[cfg(test)]
97mod tests {
98    use super::*;
99
100    #[test]
101    fn test_cursor_small() {
102        let mut cursor = ForwardInMemoryCursor::new(&[(1, ()), (2, ()), (3, ()), (4, ()), (5, ())]);
103        assert_eq!(cursor.current(), Some(&(1, ())));
104
105        assert_eq!(cursor.seek(&0), Some(&(1, ())));
106        assert_eq!(cursor.current(), Some(&(1, ())));
107
108        assert_eq!(cursor.seek(&3), Some(&(3, ())));
109        assert_eq!(cursor.current(), Some(&(3, ())));
110
111        assert_eq!(cursor.seek(&3), Some(&(3, ())));
112        assert_eq!(cursor.current(), Some(&(3, ())));
113
114        assert_eq!(cursor.seek(&4), Some(&(4, ())));
115        assert_eq!(cursor.current(), Some(&(4, ())));
116
117        assert_eq!(cursor.seek(&6), None);
118        assert_eq!(cursor.current(), None);
119    }
120
121    #[test]
122    fn test_cursor_large_binary_search() {
123        // Create a large enough collection to trigger binary search
124        let entries: Vec<(i32, ())> = (0..200).map(|i| (i * 2, ())).collect();
125        let mut cursor = ForwardInMemoryCursor::new(&entries);
126
127        // Seek to beginning
128        assert_eq!(cursor.seek(&0), Some(&(0, ())));
129        assert_eq!(cursor.idx, 0);
130
131        // Seek to middle (should use binary search)
132        assert_eq!(cursor.seek(&100), Some(&(100, ())));
133        assert_eq!(cursor.idx, 50);
134
135        // Seek to non-existent key (should find next greater)
136        assert_eq!(cursor.seek(&101), Some(&(102, ())));
137        assert_eq!(cursor.idx, 51);
138
139        // Seek to end
140        assert_eq!(cursor.seek(&398), Some(&(398, ())));
141        assert_eq!(cursor.idx, 199);
142
143        // Seek past end
144        assert_eq!(cursor.seek(&1000), None);
145    }
146
147    #[test]
148    fn test_first_after_large() {
149        let entries: Vec<(i32, ())> = (0..200).map(|i| (i * 2, ())).collect();
150        let mut cursor = ForwardInMemoryCursor::new(&entries);
151
152        // first_after should find strictly greater
153        assert_eq!(cursor.first_after(&0), Some(&(2, ())));
154        assert_eq!(cursor.idx, 1);
155
156        // Reset and test from beginning
157        cursor.reset();
158        assert_eq!(cursor.first_after(&99), Some(&(100, ())));
159
160        // first_after on exact match
161        cursor.reset();
162        assert_eq!(cursor.first_after(&100), Some(&(102, ())));
163    }
164
165    #[test]
166    fn test_cursor_consistency() {
167        // Verify binary search and linear scan produce same results
168        let entries: Vec<(i32, ())> = (0..200).map(|i| (i * 3, ())).collect();
169
170        for search_key in [0, 1, 3, 50, 150, 299, 300, 597, 598, 599, 1000] {
171            // Test with fresh cursor (binary search path)
172            let mut cursor1 = ForwardInMemoryCursor::new(&entries);
173            let result1 = cursor1.seek(&search_key);
174
175            // Manually advance to trigger linear path by getting close first
176            let mut cursor2 = ForwardInMemoryCursor::new(&entries);
177            if search_key > 100 {
178                cursor2.seek(&(search_key - 50));
179            }
180            let result2 = cursor2.seek(&search_key);
181
182            assert_eq!(
183                result1, result2,
184                "Mismatch for key {search_key}: binary={result1:?}, linear={result2:?}"
185            );
186        }
187    }
188}