Remove superflous sentinel in DynamicGraph, fixes #4738

This commit is contained in:
Patrick Niklaus 2017-12-15 23:23:38 +00:00 committed by Patrick Niklaus
parent b99d3a0a69
commit ccfbce5300
3 changed files with 10 additions and 5 deletions

View File

@ -108,7 +108,7 @@ struct ContractedEdgeContainer
edges.insert(edges.end(), new_edges.begin(), new_end); edges.insert(edges.end(), new_edges.begin(), new_end);
auto edges_size = edges.size(); auto edges_size = edges.size();
auto new_edges_size = std::distance(new_edges.begin(), new_end); auto new_edges_size = std::distance(new_edges.begin(), new_end);
BOOST_ASSERT(edges_size >= new_edges_size); BOOST_ASSERT(static_cast<int>(edges_size) >= new_edges_size);
flags.resize(edges_size); flags.resize(edges_size);
std::fill(flags.begin() + edges_size - new_edges_size, flags.end(), flag); std::fill(flags.begin() + edges_size - new_edges_size, flags.end(), flag);

View File

@ -115,7 +115,7 @@ template <typename EdgeDataT> class DynamicGraph
number_of_nodes = nodes; number_of_nodes = nodes;
number_of_edges = static_cast<EdgeIterator>(graph.size()); number_of_edges = static_cast<EdgeIterator>(graph.size());
node_array.resize(number_of_nodes + 1); node_array.resize(number_of_nodes);
EdgeIterator edge = 0; EdgeIterator edge = 0;
EdgeIterator position = 0; EdgeIterator position = 0;
for (const auto node : irange(0u, number_of_nodes)) for (const auto node : irange(0u, number_of_nodes))
@ -129,7 +129,6 @@ template <typename EdgeDataT> class DynamicGraph
node_array[node].edges = edge - last_edge; node_array[node].edges = edge - last_edge;
position += node_array[node].edges; position += node_array[node].edges;
} }
node_array.back().first_edge = position;
edge_list.reserve(static_cast<std::size_t>(edge_list.size() * 1.1)); edge_list.reserve(static_cast<std::size_t>(edge_list.size() * 1.1));
edge_list.resize(position); edge_list.resize(position);
edge = 0; edge = 0;
@ -144,6 +143,8 @@ template <typename EdgeDataT> class DynamicGraph
++edge; ++edge;
} }
} }
BOOST_ASSERT(node_array.size() == number_of_nodes);
} }
// Copy&move for the same data // Copy&move for the same data
@ -191,6 +192,8 @@ template <typename EdgeDataT> class DynamicGraph
// Removes all edges to and from nodes for which filter(node_id) returns false // Removes all edges to and from nodes for which filter(node_id) returns false
template <typename Pred> auto Filter(Pred filter) const & template <typename Pred> auto Filter(Pred filter) const &
{ {
BOOST_ASSERT(node_array.size() == number_of_nodes);
DynamicGraph other; DynamicGraph other;
other.number_of_nodes = number_of_nodes; other.number_of_nodes = number_of_nodes;
@ -202,6 +205,8 @@ template <typename EdgeDataT> class DynamicGraph
std::transform( std::transform(
node_array.begin(), node_array.end(), other.node_array.begin(), [&](const Node &node) { node_array.begin(), node_array.end(), other.node_array.begin(), [&](const Node &node) {
const EdgeIterator first_edge = other.edge_list.size(); const EdgeIterator first_edge = other.edge_list.size();
BOOST_ASSERT(node_id < number_of_nodes);
if (filter(node_id++)) if (filter(node_id++))
{ {
std::copy_if(edge_list.begin() + node.first_edge, std::copy_if(edge_list.begin() + node.first_edge,
@ -416,7 +421,7 @@ template <typename EdgeDataT> class DynamicGraph
void Renumber(const std::vector<NodeID> &old_to_new_node) void Renumber(const std::vector<NodeID> &old_to_new_node)
{ {
// permutate everything but the sentinel // permutate everything but the sentinel
util::inplacePermutation(node_array.begin(), std::prev(node_array.end()), old_to_new_node); util::inplacePermutation(node_array.begin(), node_array.end(), old_to_new_node);
// Build up edge permutation // Build up edge permutation
auto new_edge_index = 0; auto new_edge_index = 0;

View File

@ -384,7 +384,7 @@ void RenumberData(std::vector<RemainingNodeData> &remaining_nodes,
// we need to make a copy here because we are going to modify it // we need to make a copy here because we are going to modify it
auto to_orig = new_to_old_node_id; auto to_orig = new_to_old_node_id;
auto new_node_id = 0; auto new_node_id = 0u;
// All remaining nodes get the low IDs // All remaining nodes get the low IDs
for (auto &remaining : remaining_nodes) for (auto &remaining : remaining_nodes)