Skip to content

Commit 3cb8456

Browse files
committed
CHG: Simplify function
1 parent 785275e commit 3cb8456

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

cpp/memilio/mobility/graph_builder.h

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class GraphBuilder
8888
* @brief Build the graph from the added nodes and edges.
8989
*
9090
* Sorts the edges and optionally removes duplicate edges (same start and end node indices).
91-
* Wihout dupplicate removal, multiple edges between the same nodes are allowed and the order of insertion is stable.
91+
* Without duplicate removal, multiple edges between the same nodes are allowed and the order of insertion is stable.
9292
* @param make_unique If true, duplicate edges are removed. The last added edge is kept!
9393
* @return Graph<NodePropertyT, EdgePropertyT> The constructed graph.
9494
*/
@@ -130,25 +130,19 @@ class GraphBuilder
130130
unique_edges.reserve(m_edges.size());
131131
bool duplicate_edges = false;
132132
auto curr_elem = m_edges.begin();
133-
auto next_elem = std::adjacent_find(curr_elem, m_edges.end(), [](auto&& e1, auto&& e2) {
134-
return e1.start_node_idx == e2.start_node_idx && e1.end_node_idx == e2.end_node_idx;
135-
});
136-
while (next_elem != m_edges.end()) {
137-
std::copy(curr_elem, next_elem, std::back_inserter(unique_edges));
138-
curr_elem = next_elem;
139-
while (next_elem != m_edges.end() && (*curr_elem).start_node_idx == (*next_elem).start_node_idx &&
140-
(*curr_elem).end_node_idx == (*next_elem).end_node_idx) {
141-
next_elem = next(next_elem);
133+
134+
while (curr_elem != m_edges.end()) {
135+
auto next_elem = std::next(curr_elem);
136+
if (next_elem != m_edges.end() && curr_elem->start_node_idx == next_elem->start_node_idx &&
137+
curr_elem->end_node_idx == next_elem->end_node_idx) {
138+
duplicate_edges = true;
142139
}
143-
curr_elem = prev(next_elem);
144-
std::copy(curr_elem, next(curr_elem), std::back_inserter(unique_edges));
145-
duplicate_edges = true;
146-
curr_elem = next(curr_elem);
147-
next_elem = std::adjacent_find(curr_elem, m_edges.end(), [](auto&& e1, auto&& e2) {
148-
return e1.start_node_idx == e2.start_node_idx && e1.end_node_idx == e2.end_node_idx;
149-
});
140+
else if (next_elem != m_edges.end()) {
141+
std::copy(curr_elem, next_elem, std::back_inserter(unique_edges));
142+
}
143+
curr_elem = next_elem;
150144
}
151-
std::copy(curr_elem, next_elem, std::back_inserter(unique_edges));
145+
std::copy(std::prev(m_edges.end()), m_edges.end(), std::back_inserter(unique_edges));
152146
m_edges = std::move(unique_edges);
153147
if (duplicate_edges) {
154148
mio::log_warning("Removed duplicate edge(s)");

0 commit comments

Comments
 (0)