Skip to content

Commit

Permalink
Merge pull request #206 from ecatmur/try-emplace-p
Browse files Browse the repository at this point in the history
Add try_emplace_p extension method
  • Loading branch information
greg7mdp authored Sep 12, 2023
2 parents 3362017 + 4468822 commit 81ca453
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions parallel_hashmap/phmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -4022,6 +4022,24 @@ class parallel_hash_map : public parallel_hash_set<N, RefSet, Mtx_, Policy, Hash
return std::get<2>(res);
}

// returns {pointer, bool} instead of {iterator, bool} per try_emplace.
// useful for node-based containers, since the pointer is not invalidated by concurrent insert etc.
template <class K = key_type, class... Args>
std::pair<typename parallel_hash_map::parallel_hash_set::pointer, bool> try_emplace_p(K&& k, Args&&... args) {
size_t hashval = this->hash(k);
typename Lockable::UniqueLock m;
auto res = this->find_or_prepare_insert_with_hash(hashval, k, m);
typename Base::Inner *inner = std::get<0>(res);
if (std::get<2>(res)) {
inner->set_.emplace_at(std::get<1>(res), std::piecewise_construct,
std::forward_as_tuple(std::forward<K>(k)),
std::forward_as_tuple(std::forward<Args>(args)...));
inner->set_.set_ctrl(std::get<1>(res), H2(hashval));
}
auto it = this->iterator_at(inner, inner->set_.iterator_at(std::get<1>(res)));
return {&*it, std::get<2>(res)};
}

// ----------- end of phmap extensions --------------------------

template <class K = key_type, class P = Policy, K* = nullptr>
Expand Down

0 comments on commit 81ca453

Please sign in to comment.