Skip to content

Commit 0d912f7

Browse files
committed
Allow swapping parallel hash maps/sets with different mutex types
1 parent ca9f02f commit 0d912f7

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

parallel_hashmap/phmap.h

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3473,14 +3473,19 @@ class parallel_hash_set
34733473
return it == end() ? node_type() : extract(const_iterator{it});
34743474
}
34753475

3476-
void swap(parallel_hash_set& that) noexcept(
3477-
IsNoThrowSwappable<EmbeddedSet>() &&
3478-
(!AllocTraits::propagate_on_container_swap::value ||
3479-
IsNoThrowSwappable<allocator_type>())) {
3476+
template<class Mtx2_>
3477+
void swap(parallel_hash_set<N, RefSet, Mtx2_, Policy, Hash, Eq, Alloc>& that)
3478+
noexcept(IsNoThrowSwappable<EmbeddedSet>() &&
3479+
(!AllocTraits::propagate_on_container_swap::value ||
3480+
IsNoThrowSwappable<allocator_type>()))
3481+
{
34803482
using std::swap;
3483+
using Lockable2 = phmap::LockableImpl<Mtx2_>;
3484+
34813485
for (size_t i=0; i<num_tables; ++i)
34823486
{
3483-
typename Lockable::UniqueLocks l(sets_[i], that.sets_[i]);
3487+
typename Lockable::UniqueLock l(sets_[i]);
3488+
typename Lockable2::UniqueLock l2(that.sets_[i]);
34843489
swap(sets_[i].set_, that.sets_[i].set_);
34853490
}
34863491
}
@@ -3620,8 +3625,11 @@ class parallel_hash_set
36203625
return !(a == b);
36213626
}
36223627

3628+
template<class Mtx2_>
36233629
friend void swap(parallel_hash_set& a,
3624-
parallel_hash_set& b) noexcept(noexcept(a.swap(b))) {
3630+
parallel_hash_set<N, RefSet, Mtx2_, Policy, Hash, Eq, Alloc>& b)
3631+
noexcept(noexcept(a.swap(b)))
3632+
{
36253633
a.swap(b);
36263634
}
36273635

@@ -3700,14 +3708,16 @@ class parallel_hash_set
37003708

37013709
// TODO(alkis): Optimize this assuming *this and that don't overlap.
37023710
// --------------------------------------------------------------------
3703-
parallel_hash_set& move_assign(parallel_hash_set&& that, std::true_type) {
3704-
parallel_hash_set tmp(std::move(that));
3711+
template<class Mtx2_>
3712+
parallel_hash_set& move_assign(parallel_hash_set<N, RefSet, Mtx2_, Policy, Hash, Eq, Alloc>&& that, std::true_type) {
3713+
parallel_hash_set<N, RefSet, Mtx2_, Policy, Hash, Eq, Alloc> tmp(std::move(that));
37053714
swap(tmp);
37063715
return *this;
37073716
}
37083717

3709-
parallel_hash_set& move_assign(parallel_hash_set&& that, std::false_type) {
3710-
parallel_hash_set tmp(std::move(that), alloc_ref());
3718+
template<class Mtx2_>
3719+
parallel_hash_set& move_assign(parallel_hash_set<N, RefSet, Mtx2_, Policy, Hash, Eq, Alloc>&& that, std::false_type) {
3720+
parallel_hash_set<N, RefSet, Mtx2_, Policy, Hash, Eq, Alloc> tmp(std::move(that), alloc_ref());
37113721
swap(tmp);
37123722
return *this;
37133723
}

0 commit comments

Comments
 (0)