Allow swapping parallel hash maps/sets with different mutex types

This commit is contained in:
greg7mdp
2022-09-11 10:07:32 -04:00
parent ca9f02fda3
commit 0d912f754f
+20 -10
View File
@@ -3473,14 +3473,19 @@ public:
return it == end() ? node_type() : extract(const_iterator{it});
}
void swap(parallel_hash_set& that) noexcept(
IsNoThrowSwappable<EmbeddedSet>() &&
(!AllocTraits::propagate_on_container_swap::value ||
IsNoThrowSwappable<allocator_type>())) {
template<class Mtx2_>
void swap(parallel_hash_set<N, RefSet, Mtx2_, Policy, Hash, Eq, Alloc>& that)
noexcept(IsNoThrowSwappable<EmbeddedSet>() &&
(!AllocTraits::propagate_on_container_swap::value ||
IsNoThrowSwappable<allocator_type>()))
{
using std::swap;
using Lockable2 = phmap::LockableImpl<Mtx2_>;
for (size_t i=0; i<num_tables; ++i)
{
typename Lockable::UniqueLocks l(sets_[i], that.sets_[i]);
typename Lockable::UniqueLock l(sets_[i]);
typename Lockable2::UniqueLock l2(that.sets_[i]);
swap(sets_[i].set_, that.sets_[i].set_);
}
}
@@ -3620,8 +3625,11 @@ public:
return !(a == b);
}
template<class Mtx2_>
friend void swap(parallel_hash_set& a,
parallel_hash_set& b) noexcept(noexcept(a.swap(b))) {
parallel_hash_set<N, RefSet, Mtx2_, Policy, Hash, Eq, Alloc>& b)
noexcept(noexcept(a.swap(b)))
{
a.swap(b);
}
@@ -3700,14 +3708,16 @@ private:
// TODO(alkis): Optimize this assuming *this and that don't overlap.
// --------------------------------------------------------------------
parallel_hash_set& move_assign(parallel_hash_set&& that, std::true_type) {
parallel_hash_set tmp(std::move(that));
template<class Mtx2_>
parallel_hash_set& move_assign(parallel_hash_set<N, RefSet, Mtx2_, Policy, Hash, Eq, Alloc>&& that, std::true_type) {
parallel_hash_set<N, RefSet, Mtx2_, Policy, Hash, Eq, Alloc> tmp(std::move(that));
swap(tmp);
return *this;
}
parallel_hash_set& move_assign(parallel_hash_set&& that, std::false_type) {
parallel_hash_set tmp(std::move(that), alloc_ref());
template<class Mtx2_>
parallel_hash_set& move_assign(parallel_hash_set<N, RefSet, Mtx2_, Policy, Hash, Eq, Alloc>&& that, std::false_type) {
parallel_hash_set<N, RefSet, Mtx2_, Policy, Hash, Eq, Alloc> tmp(std::move(that), alloc_ref());
swap(tmp);
return *this;
}