martinus / robin-hood-hashing

Fast & memory efficient hashtable based on robin hood hashing for C++11/14/17/20
https://gitter.im/martinus/robin-hood-hashing
MIT License
1.5k stars 142 forks source link

Member function `insert` with hint. #131

Closed slavenf closed 2 years ago

slavenf commented 2 years ago

Hello. Could you add overloaded member function insert with hint to be compatible with all standard container?

iterator insert(const_iterator hint, const value_type& value);
iterator insert(const_iterator hint, value_type&& value);

For example, these overloads are very usefule in generic code that is using std::insert_iterator or std::inserter. Once again, thank you for this great library.

dixlorenz commented 2 years ago

Voting this up. I just stumbled about this problem; it used to be in the library, but got removed at one point and when I updated to the current version my code wouldn't compile any more.

slavenf commented 2 years ago

@dixlorenz There is workaround before martinus write better function. Openrobin_hood.h. Somewhere near line 1869 is this:

std::pair<iterator, bool> insert(const value_type& keyval) {
    ROBIN_HOOD_TRACE(this)
    return emplace(keyval);
}

std::pair<iterator, bool> insert(value_type&& keyval) {
    return emplace(std::move(keyval));
}

You can add these two functions:

iterator insert(const_iterator /*hint*/, const value_type& keyval)
{
    return insert(keyval).first;
}

iterator insert(const_iterator /*hint*/, value_type&& keyval)
{
    return insert(std::move(keyval)).first;
}

That should work.

slavenf commented 2 years ago

If you need emplace_hint then you can also add this:

template <typename... Args>
iterator emplace_hint(const_iterator /*hint*/, Args&&... args)
{
    return emplace(std::forward<Args>(args)...).first;
}
martinus commented 2 years ago

Hi, thanks for being persitent :sweat_smile: I've created a new release with your fixes, thanks @slavenf!