Add shift_remove family to IndexMap/IndexSet - #685
Open
HarnageaGabriel wants to merge 2 commits into
Open
Conversation
swap_remove is O(1) but perturbs the position of the last element, which isn't acceptable when callers need remaining entries to keep their relative insertion order after a removal. This mirrors the upstream indexmap crate's shift_remove/shift_remove_entry API (O(n), shifts entries down via Vec::remove and fixes up the hash table's index pointers) adapted to heapless's fixed-capacity, no_std CoreMap/Pos representation. Fixes rust-embedded#678
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
swap_removeis O(1) but perturbs map/set order by moving the last element into the removed slot. This adds order-preserving removal, mirroring theindexmapcrate'sshift_removeAPI:IndexMap::shift_remove/shift_remove_entry/shift_remove_indexOccupiedEntry::shift_remove/shift_remove_entryIndexSet::shift_removeImplementation:
CoreMap::shift_remove_foundremoves the entry withVec::remove(order-preserving shift) instead ofswap_remove_unchecked, then walks theindicesrobin-hood table decrementing everyPoswhose index pointed past the removed slot, then runs the existingbackward_shift_after_removalto restore the probe-distance invariant. O(n) instead ofswap_remove's O(1), same as upstreamindexmap.remove/swap_remove/remove_entry/swap_remove_entryare untouched.Fixes #678
Test plan
cargo buildcargo test --lib(251 passed, including 5 newshift_removetests covering order preservation onIndexMap,OccupiedEntry,shift_remove_index, andIndexSet)cargo test --doc index_map(31 passed, including new doctests)cargo clippy --libclean (repo deniesclippy::undocumented_unsafe_blocks)cargo fmt --checkclean