indexmap-rs / indexmap

A hash table with consistent order and fast iteration; access items by key or sequence index
https://docs.rs/indexmap/
Other
1.67k stars 150 forks source link

Feature request: join two or more IndexMap #336

Closed M5135213521asd3 closed 1 month ago

M5135213521asd3 commented 1 month ago

Hi, could it be possible to implement join two IndexMap to one, or insert one IndexMap to another?

Thank you, Marian

cuviper commented 1 month ago

There's no method for it, as there are many variations of behavior that might be desired. The most generic way is to use Extend, like map.extend(other), or map.extend(other.drain(..)) to keep the other allocation.

This is equivalent to calling insert for each of them in order, which means that for keys that already existed in the map, their value is updated but it keeps the existing order.

If you want different update semantics, you can iterate the other map yourself and use the entry API for efficient lookups to update the target map.

It's also up to you to decide the desired order of the resulting map, and make that happen.

cuviper commented 1 month ago

The standard library also has BTreeMap::append, BTreeSet::append, and Vec::append, but not for HashMap and HashSet. I think it would be fine to add to IndexMap::append and IndexSet::append though, with the same insert-replacement semantics that I mentioned before.