Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Most efficient way to get next/prev items in an ordered map. #621

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

Closed
kentor opened this issue Sep 11, 2015 · 2 comments
Closed

Most efficient way to get next/prev items in an ordered map. #621

kentor opened this issue Sep 11, 2015 · 2 comments

Comments

@kentor
Copy link

kentor commented Sep 11, 2015

Given a key that is in an OrderedMap, is there an efficient way (O(1) or O(log n)) to get the next or previous item?

@vinnymac
Copy link
Contributor

It appears that with the current libraries API the best I can do is O(n).

It would be convenient to implement a TreeMap so we can have an OrderedMap that can be navigated via a NavigableMap. The lowerKey() and higherKey() methods return the next and previous keys. This simply keeps track of three nodes (next, current, and previous). Although I know SortedMap is also under consideration and that would be nice to have alongside this.

If you were to achieve a more performant lookup I imagine it would be similar to what users are doing for SortedMap right now. Which is to maintain an array of the keys. Then you can use the index of the current key in order to get the previous or next keys.

If anyone has any other tips or ways of achieving this I would love to hear about them!

@leebyron
Copy link
Collaborator

leebyron commented Mar 4, 2017

Unfortunately this is not possible at the moment

@leebyron leebyron closed this as completed Mar 4, 2017
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

No branches or pull requests

3 participants