Tessil / sparse-map

C++ implementation of a memory efficient hash map and hash set
MIT License
334 stars 36 forks source link

STL like extract #11

Closed bigerl closed 4 years ago

bigerl commented 4 years ago

Do you see any chance to implement extract() in the sparse-map efficiently? It is especially handy, when one wants to change the key of a entry where the value is expensive to copy.

Tessil commented 4 years ago

An extract method similar to the STL is not really possible as there is no notion of node in a open-addressing hash map.

It'd be possible to implement an erase function that returns the erased key-value pair, moving it out of the map to avoid a copy. It'd be then possible to change the key and reinsert the key-value. Unfortunately the erase method of the sparse-map is quite slow compared to the one of robin-map for example. I'm not sure that avoiding the copy will yield a big improvement except for values that are really expensive to copy.

bigerl commented 4 years ago

OK, thanks. That is good to know, especially that erase is expensive itself.

I tried to move the the value() of an iterator and inserted it as a new entry. That worked and I didn't get errors but I doubt it is really safe.

Tessil commented 4 years ago

Moving the value() is safe and does what you'd expect. It just leaves the value associated to the key in an undermined state (except if the move constructor/operator says otherwise). Removing the key from the map after that, or associating a new value to the key, is safe too. It's effectively a good way to avoid the copy of the value.