This change adds a method get_index_entry to IndexMap, which is the by-index counterpart to entry. The returned struct IndexedEntry provides the same methods as OccupiedEntry, except replace_key.
A concrete use case where I found this useful is having a map: IndexMap<_, Vec<_>> with the invariant that each Vec is non-empty. Imagine we want to pick an entry at a random index and pop a value from the Vec. Without this change we'd have to use get_mut_index and swap_remove_index, and ensure that we use the same index. With get_index_entry we can write:
let mut e = map.get_index_entry(index).unwrap();
let v = e.get_mut();
let x = v.pop().unwrap();
if v.is_empty() {
e.swap_remove();
}
This change adds a method
get_index_entry
toIndexMap
, which is the by-index counterpart toentry
. The returned structIndexedEntry
provides the same methods asOccupiedEntry
, exceptreplace_key
.A concrete use case where I found this useful is having a
map: IndexMap<_, Vec<_>>
with the invariant that eachVec
is non-empty. Imagine we want to pick an entry at a random index and pop a value from theVec
. Without this change we'd have to useget_mut_index
andswap_remove_index
, and ensure that we use the same index. Withget_index_entry
we can write: