make Unique_hash_map movable

This commit is contained in:
Laurent Rineau 2023-11-06 15:40:58 +01:00
parent 3148e8a6fe
commit cb933b1391
2 changed files with 42 additions and 0 deletions

View File

@ -19,6 +19,8 @@
#include <CGAL/memory.h> #include <CGAL/memory.h>
#include <iostream> #include <iostream>
#include <limits> #include <limits>
#include <type_traits>
#include <utility>
namespace CGAL { namespace CGAL {
@ -85,6 +87,10 @@ public:
chained_map(std::size_t n = default_size, const T& d = T()); chained_map(std::size_t n = default_size, const T& d = T());
chained_map(const chained_map<T, Allocator>& D); chained_map(const chained_map<T, Allocator>& D);
chained_map& operator=(const chained_map<T, Allocator>& D); chained_map& operator=(const chained_map<T, Allocator>& D);
chained_map(chained_map<T, Allocator>&& D)
noexcept(std::is_nothrow_move_constructible_v<Allocator> && std::is_nothrow_move_constructible_v<T>);
chained_map& operator=(chained_map<T, Allocator>&& D)
noexcept(std::is_nothrow_move_assignable_v<Allocator> && std::is_nothrow_move_assignable_v<T>);
void reserve(std::size_t n); void reserve(std::size_t n);
void clear(); void clear();
@ -246,6 +252,18 @@ chained_map<T, Allocator>::chained_map(const chained_map<T, Allocator>& D)
} }
} }
} }
template <typename T, typename Allocator>
chained_map<T, Allocator>::chained_map(chained_map<T, Allocator>&& D)
noexcept(std::is_nothrow_move_constructible_v<Allocator> && std::is_nothrow_move_constructible_v<T>)
: 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 <typename T, typename Allocator> template <typename T, typename Allocator>
chained_map<T, Allocator>& chained_map<T, Allocator>::operator=(const chained_map<T, Allocator>& D) chained_map<T, Allocator>& chained_map<T, Allocator>::operator=(const chained_map<T, Allocator>& D)
@ -263,6 +281,24 @@ chained_map<T, Allocator>& chained_map<T, Allocator>::operator=(const chained_ma
return *this; return *this;
} }
template <typename T, typename Allocator>
chained_map<T, Allocator>& chained_map<T, Allocator>::operator=(chained_map<T, Allocator>&& D)
noexcept(std::is_nothrow_move_assignable_v<Allocator> && std::is_nothrow_move_assignable_v<T>)
{
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 <typename T, typename Allocator> template <typename T, typename Allocator>
void chained_map<T, Allocator>::reserve(std::size_t n) void chained_map<T, Allocator>::reserve(std::size_t n)
{ {

View File

@ -1,6 +1,7 @@
#include <list> #include <list>
#include <CGAL/Unique_hash_map.h> #include <CGAL/Unique_hash_map.h>
#include <CGAL/test_macros.h> #include <CGAL/test_macros.h>
#include <type_traits>
using namespace std; using namespace std;
typedef list<int>::iterator Iterator; typedef list<int>::iterator Iterator;
@ -24,6 +25,10 @@ int main() {
H1[it1] = 2; H1[it1] = 2;
CGAL_TEST(H1[it1]==2); CGAL_TEST(H1[it1]==2);
CGAL_TEST(H2[it1]==-1); CGAL_TEST(H2[it1]==-1);
static_assert(std::is_nothrow_move_constructible_v<decltype(H1)>);
auto H1_moved = std::move(H1);
CGAL_TEST(H1_moved[it1]==2);
CGAL_TEST(H1[it1]==H1.default_value());
H1.clear(); H1.clear();
H2.clear(-2); H2.clear(-2);
H2[it1] = 2; H2[it1] = 2;
@ -67,6 +72,7 @@ int main() {
CGAL_TEST(get(H4_pmap, L.begin()) == 0); CGAL_TEST(get(H4_pmap, L.begin()) == 0);
typedef CGAL::Unique_hash_map<int, int, Integer_hash_function> Int_hmap; typedef CGAL::Unique_hash_map<int, int, Integer_hash_function> Int_hmap;
static_assert(std::is_nothrow_move_constructible_v<Int_hmap>);
typedef boost::associative_property_map<Int_hmap> Int_pmap; typedef boost::associative_property_map<Int_hmap> Int_pmap;
Int_hmap H5(-1); Int_hmap H5(-1);
Int_pmap H5_pmap(H5); Int_pmap H5_pmap(H5);