Make DynamicGraph copyable

This commit is contained in:
Patrick Niklaus 2017-08-22 19:53:44 +00:00 committed by Patrick Niklaus
parent 53f87c08b5
commit 4b75cb8b0e
2 changed files with 37 additions and 3 deletions

View File

@ -6,6 +6,7 @@
#include <boost/iterator/iterator_facade.hpp>
#include <algorithm>
#include <limits>
#include <utility>
#include <vector>
@ -254,9 +255,25 @@ template <typename ElementT> class DeallocatingVector
bucket_list.emplace_back(new ElementT[ELEMENTS_PER_BLOCK]);
}
// copying is not safe since this would only do a shallow copy
DeallocatingVector(DeallocatingVector &other) = delete;
DeallocatingVector &operator=(DeallocatingVector &other) = delete;
// Performs a deep copy of the buckets
DeallocatingVector(const DeallocatingVector &other)
{
bucket_list.resize(other.bucket_list.size());
for (const auto index : util::irange<std::size_t>(0, bucket_list.size()))
{
bucket_list[index] = new ElementT[ELEMENTS_PER_BLOCK];
std::copy_n(other.bucket_list[index], ELEMENTS_PER_BLOCK, bucket_list[index]);
}
current_size = other.current_size;
}
// Note we capture other by value
DeallocatingVector &operator=(const DeallocatingVector &other)
{
auto copy_other = other;
swap(copy_other);
return *this;
}
// moving is fine
DeallocatingVector(DeallocatingVector &&other) { swap(other); }
DeallocatingVector &operator=(DeallocatingVector &&other)

View File

@ -120,6 +120,23 @@ template <typename EdgeDataT> class DynamicGraph
}
}
DynamicGraph(const DynamicGraph &other)
{
number_of_nodes = other.number_of_nodes;
// atomics can't be moved this is why we need an own constructor
number_of_edges = static_cast<std::uint32_t>(other.number_of_edges);
node_array = other.node_array;
edge_list = other.edge_list;
}
DynamicGraph&operator=(const DynamicGraph &other)
{
auto copy_other = other;
*this = std::move(other);
return *this;
}
DynamicGraph(DynamicGraph &&other)
{
number_of_nodes = other.number_of_nodes;