From cb933b1391d23d09d79ed4a012fd08d8d61a3ce0 Mon Sep 17 00:00:00 2001 From: Laurent Rineau Date: Mon, 6 Nov 2023 15:40:58 +0100 Subject: [PATCH] make Unique_hash_map movable --- .../CGAL/Hash_map/internal/chained_map.h | 36 +++++++++++++++++++ .../test/Hash_map/Unique_hash_map_test.cpp | 6 ++++ 2 files changed, 42 insertions(+) diff --git a/Hash_map/include/CGAL/Hash_map/internal/chained_map.h b/Hash_map/include/CGAL/Hash_map/internal/chained_map.h index 729e2a58092..bea9b0993f0 100644 --- a/Hash_map/include/CGAL/Hash_map/internal/chained_map.h +++ b/Hash_map/include/CGAL/Hash_map/internal/chained_map.h @@ -19,6 +19,8 @@ #include #include #include +#include +#include namespace CGAL { @@ -85,6 +87,10 @@ public: chained_map(std::size_t n = default_size, const T& d = T()); chained_map(const chained_map& D); chained_map& operator=(const chained_map& D); + chained_map(chained_map&& D) + noexcept(std::is_nothrow_move_constructible_v && std::is_nothrow_move_constructible_v); + chained_map& operator=(chained_map&& D) + noexcept(std::is_nothrow_move_assignable_v && std::is_nothrow_move_assignable_v); void reserve(std::size_t n); void clear(); @@ -246,6 +252,18 @@ chained_map::chained_map(const chained_map& D) } } } +template +chained_map::chained_map(chained_map&& D) + noexcept(std::is_nothrow_move_constructible_v && std::is_nothrow_move_constructible_v) + : table(std::exchange(D.table, nullptr)) + , table_end(std::exchange(D.table_end, nullptr)) + , free(std::exchange(D.free, nullptr)) + , table_size(std::exchange(D.table_size, 0)) + , table_size_1(std::exchange(D.table_size_1, 0)) + , alloc(std::move(D.alloc)) + , reserved_size(std::exchange(D.reserved_size, 0)) + , def(std::move(D.def)) +{} template chained_map& chained_map::operator=(const chained_map& D) @@ -263,6 +281,24 @@ chained_map& chained_map::operator=(const chained_ma return *this; } +template +chained_map& chained_map::operator=(chained_map&& D) + noexcept(std::is_nothrow_move_assignable_v && std::is_nothrow_move_assignable_v) +{ + clear(); + + table = std::exchange(D.table, nullptr); + table_end = std::exchange(D.table_end, nullptr); + free = std::exchange(D.free, nullptr); + table_size = std::exchange(D.table_size, 0); + table_size_1 = std::exchange(D.table_size_1, 0); + alloc = std::move(D.alloc); + reserved_size = std::exchange(D.reserved_size, 0); + def = std::move(D.def); + + return *this; +} + template void chained_map::reserve(std::size_t n) { diff --git a/Hash_map/test/Hash_map/Unique_hash_map_test.cpp b/Hash_map/test/Hash_map/Unique_hash_map_test.cpp index 814fd7d94f3..bae896f360b 100644 --- a/Hash_map/test/Hash_map/Unique_hash_map_test.cpp +++ b/Hash_map/test/Hash_map/Unique_hash_map_test.cpp @@ -1,6 +1,7 @@ #include #include #include +#include using namespace std; typedef list::iterator Iterator; @@ -24,6 +25,10 @@ int main() { H1[it1] = 2; CGAL_TEST(H1[it1]==2); CGAL_TEST(H2[it1]==-1); + static_assert(std::is_nothrow_move_constructible_v); + auto H1_moved = std::move(H1); + CGAL_TEST(H1_moved[it1]==2); + CGAL_TEST(H1[it1]==H1.default_value()); H1.clear(); H2.clear(-2); H2[it1] = 2; @@ -67,6 +72,7 @@ int main() { CGAL_TEST(get(H4_pmap, L.begin()) == 0); typedef CGAL::Unique_hash_map Int_hmap; + static_assert(std::is_nothrow_move_constructible_v); typedef boost::associative_property_map Int_pmap; Int_hmap H5(-1); Int_pmap H5_pmap(H5);