Chriscbr / thinset

A data structure for sparse sets of unsigned integers.
Apache License 2.0
2 stars 1 forks source link

Implement `Index` and `IndexMut` for `SparseMap` #11

Closed thass0 closed 8 months ago

thass0 commented 8 months ago

Index and IndexMut allows using the map[x] syntax to retrieve and mutate values in the map. Both traits are implemented as wrappers around get and get_mut, respectively.

The SparseMap::get now returns an Option<&V> to accommodate the type needs of Index.

get_mut was added to retrieve mutable references for IndexMut. Handing out mutable access to the values inside the map is safe because the algorithm for maintaining valid map entries doesn't depend on the value stored in any of the pairs in dense. In addition, Rust guarantees that the underlying structure of the map is not changed while a mutable reference is alive, so the internal swapping is not a concern either :)

Closes #9