-
Notifications
You must be signed in to change notification settings - Fork 2.3k
feat(trie): forward-only in-memory cursor #9079
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8a19b2e
feat(trie): forward only in-memory cursor
rkrasiuk 74a05a3
make new const
rkrasiuk b5b244c
typo
rkrasiuk f3eaa7c
use common advance_while
rkrasiuk bc1a410
integrate forward cursor into hashed post state cursors
rkrasiuk c333641
nits
rkrasiuk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| /// The implementation of forward-only in memory cursor over the entries. | ||
| /// The cursor operates under the assumption that the supplied collection is pre-sorted. | ||
| #[derive(Debug)] | ||
| pub struct ForwardInMemoryCursor<'a, K, V> { | ||
| /// The reference to the pre-sorted collection of entries. | ||
| entries: &'a Vec<(K, V)>, | ||
| /// The index where cursor is currently positioned. | ||
| index: usize, | ||
| } | ||
|
|
||
| impl<'a, K, V> ForwardInMemoryCursor<'a, K, V> { | ||
| /// Create new forward cursor positioned at the beginning of the collection. | ||
| /// The cursor expects all of the entries have been sorted in advance. | ||
| pub const fn new(entries: &'a Vec<(K, V)>) -> Self { | ||
| Self { entries, index: 0 } | ||
| } | ||
|
|
||
| /// Returns `true` if the cursor is empty, regardless of its position. | ||
| pub fn is_empty(&self) -> bool { | ||
| self.entries.is_empty() | ||
| } | ||
| } | ||
|
|
||
| impl<'a, K, V> ForwardInMemoryCursor<'a, K, V> | ||
| where | ||
| K: PartialOrd + Copy, | ||
| V: Copy, | ||
| { | ||
| /// Advances the cursor forward while `comparator` returns `true` or until the collection is | ||
| /// exhausted. Returns the first entry for which `comparator` returns `false` or `None`. | ||
| fn advance_while_false(&mut self, comparator: impl Fn(&K) -> bool) -> Option<(K, V)> { | ||
| let mut entry = self.entries.get(self.index); | ||
| while entry.map_or(false, |entry| comparator(&entry.0)) { | ||
| self.index += 1; | ||
| entry = self.entries.get(self.index); | ||
| } | ||
| entry.copied() | ||
| } | ||
|
|
||
| /// Returns the first entry from the current cursor position that's greater or equal to the | ||
| /// provided key. This method advances the cursor forward. | ||
| pub fn seek(&mut self, key: &K) -> Option<(K, V)> { | ||
| self.advance_while_false(|k| k < key) | ||
| } | ||
|
|
||
| /// Returns the first entry from the current cursor position that's greater than the provided | ||
| /// key. This method advances the cursor forward. | ||
| pub fn first_after(&mut self, key: &K) -> Option<(K, V)> { | ||
| self.advance_while_false(|k| k <= key) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.