Skip to content

Add move_index to change the position of an entry #176

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 3 commits into from
May 24, 2022

Conversation

cuviper
Copy link
Member

@cuviper cuviper commented Feb 27, 2021

This moves the position of a key-value pair from one index to another by
shifting all other pairs in-between, making this an O(n) operation.

This could be used as a building-block for other operations, like #173
which wants to insert at a particular index. You can insert_full to
insert it somewhere, then choose whether to move_index depending on
whether you want to also change pre-existing entries.

@cuviper
Copy link
Member Author

cuviper commented Feb 27, 2021

I'm not entirely committed to the name move_index. It's somewhat similar to swap_indices, but I chose singular index because the movement of the one from index is the main point, while all the other shifted indices are a side-effect. Semantically, this is equivalent to let value = vec.remove(from); vec.insert(to, value);, or a slice rotation as this implementation actually does in its entries. Maybe shift or rotate would be better in the name.

@cuviper cuviper force-pushed the move_index branch 2 times, most recently from c4c03e6 to 94cb62a Compare February 27, 2021 20:08
@cuviper
Copy link
Member Author

cuviper commented Feb 28, 2021

I just noticed that #83 had a similar method called reorder_entry_index.

@praveenperera
Copy link

I would love to get access to this function. Still thinking about merging it in?

@cuviper
Copy link
Member Author

cuviper commented May 12, 2022

Sure -- do you have any thoughts about the name?

@praveenperera
Copy link

praveenperera commented May 12, 2022

I think move_index works best.

Reads really nicely as move index from x to y

Or move_entry_index

This moves the position of a key-value pair from one index to another by
shifting all other pairs in-between, making this an O(n) operation.

This could be used as a building-block for other operations, like indexmap-rs#173
which wants to insert at a particular index. You can `insert_full` to
insert it _somewhere_, then choose whether to `move_index` depending on
whether you want to also change pre-existing entries.
@praveenperera
Copy link

Ignore my last comment if you saw it (now deleted). This works great, I tired it out on my fork. This is exactly what I needed to make IndexSet and IndexMap work with my data structure.

@cuviper cuviper merged commit 793f6d1 into indexmap-rs:master May 24, 2022
@cuviper cuviper deleted the move_index branch July 18, 2023 02:41
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