martinus / unordered_dense

A fast & densely stored hashmap and hashset based on robin-hood backward shift deletion
MIT License
898 stars 72 forks source link

About "no const Key" #52

Closed Philippe91 closed 1 year ago

Philippe91 commented 1 year ago

I am aware of this design: "no const Key in std::pair<Key, Value>". I did not care until today. For a map, this is fine; I mean, willing to change the key is not common. But for a set, the situation is more bug-prone.

For example, this does not compile and this is good:

std::unordered_set<int> set0({ 1, 2, 3 });
for (auto& x : set)
    x++;  // "can't change x"

While the following is compiling fine, hence there is a risk of introducing a subtle bug for a common for-loop scenario.

ankerl::unordered_dense::set<int> set({ 1, 2, 3 });
for (auto& x : set)
    x++;

Hence I am wondering: what is the reason for not being able to have a const key? No way to change that, at least for a set?

martinus commented 1 year ago

Good point, for unordered_dense::set iterator and const_iterator could be the same type. I'll see what I can do here.

I can't internally use const types though because the map/set has to move elements around. Whenever you delete one entry, the map/set will move an element into the empty place.

martinus commented 1 year ago

Fixed in #53