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

Modifying key without changing order #18

Closed
beviu opened this issue Aug 3, 2023 · 4 comments
Closed

Modifying key without changing order #18

beviu opened this issue Aug 3, 2023 · 4 comments

Comments

@beviu
Copy link

beviu commented Aug 3, 2023

Hello, thank you for this very nice crate!

Is there a way to modify an entry's key without changing its position? I use this crate to implement a cache and sometimes the key associated with an object changes, so I would like to modify it without having to reinsert the entry as it would put it on the back.

In case there is no such API, would you accept a PR that adds it? If so, how do you think that API should look like?

I thought about adding a replace_key method on LinkedHashMap itself, or maybe adding a reinsert method on RawOccupiedEntryMut but the latter looks more complicated because the RawOccupiedEntryMut does not have a reference to the hashbrown HashMap and I think it cannot be added because the hashbrown raw entry borrows it. I don't really know how these methods should handle an entry with the new key already existing. In my case, I would like to cancel the operation.

@beviu
Copy link
Author

beviu commented Aug 3, 2023

I think it would make more sense for hashbrown to have an easy way to reinsert an entry after modifying its key first before adding it here. I have created a PR on hashbrown (rust-lang/hashbrown#450) to add a reinsert method on their RawOccupiedEntryMut that does this. Let's see what they think about it.

@qtfkwk
Copy link

qtfkwk commented Jul 24, 2024

I think I just ran into this... Ethiraric/yaml-rust2#35

Summary: I'd recommend replacing use of the entry / and_modify / or_insert_with pattern (which moves entries to the end) to the contains_key / replace / insert pattern. More info at the above link.

@olebedev
Copy link
Contributor

After the cursor API PR #25 was merged it's possible now to insert elements into any location within the underlying doulby-linked list.

I hope that helps resolving addressing issue.

@beviu
Copy link
Author

beviu commented Jul 26, 2024

I'd recommend replacing use of the entry / and_modify / or_insert_with pattern (which moves entries to the end) to the contains_key / replace / insert pattern.

I think that replace doesn't fit my use case because I wanted to change the key and preserve the value whereas replace does the opposite: it changes the value and preserves the key.

After the cursor API PR #25 was merged it's possible now to insert elements into any location within the underlying doulby-linked list.

I hope that helps resolving addressing issue.

Yes, I think this PR allows doing this. To update a key you can now use CursorMut::insert_before with the new key and then LinkedHashMap::remove (or CursorMut::remove_current if somebody adds it) to remove the original entry.

Thanks for the responses!

@beviu beviu closed this as completed Jul 26, 2024
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