-
Notifications
You must be signed in to change notification settings - Fork 47
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
peek methods on iterators. #82
Comments
Thanks for the reminder on this! I would still like to add these, yes. I'm currently on vacation, so I'll respond in more detail after I get back. |
So, although I would like these to be added eventually, I don't consider it especially high priority. However, I'd be happy to accept a PR with an initial implementation that more-or-less does what your code listing does: first advance the iterator to get the item, and then immediately undo that advance. It wouldn't be the most efficient implementation, but it would be easy to verify for correctness and would at least provide a more ergonomic API. If you want to take a crack at a more efficient implementation, I think the (probably?) best approach would be to simply keep a cache of the previous and next items, which gets updated as the iterator advances. And then they're always just available immediately. |
So this can be implemented similar to Yeah, I can try implementing this |
In #18 (comment) there was a discussion about adding
.peek_next()
and.peek_prev()
methods to iterators, but they were never added. And I'd like them to be added, because I have to do this:And I would very much like to not do that. I don't know if compiler would optimize this away or if not, how bad is the overhead of doing this, but I assume this will be worse than proper implementation, especially if we hit that that worst case O(log N).
I can see in the code that implementing these methods would be not as trivial as just copy-pasting
.next_impl()
and.prev_impl()
but removing the part that advances iterator (otherwise I would probably submit a PR).I think these methods would be very useful, so it is probably worth implementing them
The text was updated successfully, but these errors were encountered: