Skip to content
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

Add reverse map iteration #596

Merged
merged 2 commits into from
Mar 9, 2024
Merged

Conversation

devsnek
Copy link
Contributor

@devsnek devsnek commented Mar 5, 2024

pretty self explanatory. i found need of this api in a library i'm writing.

@filmor
Copy link
Member

filmor commented Mar 5, 2024

I know that it might be a bit tedious to implement as the model doesn't map 1-to-1, but it would be more ergonomic to extend MapIterator to be a DoubleEndedIterator as you could then use .rev() on it (as one would expect).

DoubleEndedIterator sadly doesn't have "cursor" semantics, so this would need to be implemented as a pair of Option<SimpleMapIterator>, one going backwards, one going forwards, with an additional check of whether they point to the same element. So roughly

struct SimpleMapIterator {
    is_forward: bool,
    last_key: Option<Term>,
    iter: ...
}

struct MapIterator {
    done: bool,
    fwd: SimpleMapIterator,
    bck: SimpleMapIterator,
}

impl<'a> DoubleEndedIterator for MapIterator {
    // ...
    fn next(mut self) -> Option<...> {
        if self.done { return None }
        if let Some((k, v)) = self.fwd.next() {
            if let Some((b_k, _)) = self.bwd.last_key {
                if k == b_k {
                   self.done = true;
                   return None;
                }
            }
            Some((k, v))
        } else {
            self.done = true;
            None
        }
    }

    fn next_back(mut self) -> ... {
       // same with roles of bwd and fwd reversed
    }
}

@filmor filmor requested a review from evnu March 5, 2024 18:30
@filmor filmor changed the title add reverse map iteration Add reverse map iteration Mar 9, 2024
@filmor filmor merged commit f944588 into rusterlium:master Mar 9, 2024
7 checks passed
@devsnek
Copy link
Contributor Author

devsnek commented Mar 10, 2024

@filmor thanks, do you know when the next release will be cut?

@devsnek devsnek deleted the x/reverse-iter branch March 11, 2024 17:27
@devsnek devsnek restored the x/reverse-iter branch March 12, 2024 22:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants