Skip to content

Commit 3337b98

Browse files
committed
CHG: Keep last edge instad of first one
1 parent c547df8 commit 3337b98

File tree

3 files changed

+12
-43
lines changed

3 files changed

+12
-43
lines changed

cpp/memilio/mobility/graph.h

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ class GraphBuilder
508508
*
509509
* Sorts the edges and optionally removes duplicate edges (same start and end node indices).
510510
* Wihout dupplicate removal, multiple edges between the same nodes are allowed and the order of insertion is stable.
511-
* @param make_unique If true, duplicate edges are removed. The first added edge is kept!
511+
* @param make_unique If true, duplicate edges are removed. The last added edge is kept!
512512
* @return Graph<NodePropertyT, EdgePropertyT> The constructed graph.
513513
*/
514514
Graph<NodeProperty, EdgeProperty> build(bool make_unique = false)
@@ -540,42 +540,30 @@ class GraphBuilder
540540
* @brief Remove duplicate edges from a sorted edge vector.
541541
*
542542
* Copies all the unique edges to a new vector and replaces the original edge vector with it. Unique means that
543-
* the start and end node indices are unique. Other edge properties are not checked and may get lost. Only the first
543+
* the start and end node indices are unique. Other edge properties are not checked and may get lost. Only the last
544544
* edge in the vector is kept.
545545
*/
546546
void remove_duplicate_edges()
547547
{
548548
std::vector<Edge<EdgePropertyT>> unique_edges;
549549
unique_edges.reserve(m_edges.size());
550-
std::ranges::unique_copy(m_edges, std::back_inserter(unique_edges), [](auto&& e1, auto&& e2) {
550+
auto curr_elem = m_edges.begin();
551+
auto next_elem = std::adjacent_find(curr_elem, m_edges.end(), [](auto&& e1, auto&& e2) {
551552
return e1.start_node_idx == e2.start_node_idx && e1.end_node_idx == e2.end_node_idx;
552553
});
553-
m_edges = std::move(unique_edges);
554-
}
555-
556-
/**
557-
* @brief Remove duplicate edges from a sorted edge vector.
558-
*
559-
* Copies all the unique edges to a new vector and replaces the original edge vector with it. Unique means that
560-
* the start and end node indices are unique. Other edge properties are not checked and may get lost. Only the first
561-
* edge in the vector is kept.
562-
*/
563-
void remove_double_edges()
564-
{
565-
std::vector<Edge<EdgePropertyT>> unique_edges;
566-
unique_edges.reserve(m_edges.size());
567-
auto curr_elem = m_edges.begin();
568-
auto next_elem = std::adjacent_find(curr_elem, m_edges.end(), is_equal);
569554
while (next_elem != m_edges.end()) {
570555
std::copy(curr_elem, next_elem, std::back_inserter(unique_edges));
571556
curr_elem = next_elem;
572-
while (is_equal(*curr_elem, *next_elem) && next_elem != m_edges.end()) {
557+
while ((*curr_elem).start_node_idx == (*next_elem).start_node_idx &&
558+
(*curr_elem).end_node_idx == (*next_elem).end_node_idx && next_elem != m_edges.end()) {
573559
next_elem = next(next_elem);
574560
}
575561
curr_elem = prev(next_elem);
576562
std::copy(curr_elem, next(curr_elem), std::back_inserter(unique_edges));
577563
curr_elem = next(curr_elem);
578-
next_elem = std::adjacent_find(curr_elem, m_edges.end(), is_equal);
564+
next_elem = std::adjacent_find(curr_elem, m_edges.end(), [](auto&& e1, auto&& e2) {
565+
return e1.start_node_idx == e2.start_node_idx && e1.end_node_idx == e2.end_node_idx;
566+
});
579567
}
580568
std::copy(curr_elem, next_elem, std::back_inserter(unique_edges));
581569
m_edges = std::move(unique_edges);

cpp/tests/test_graph.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -375,9 +375,8 @@ TEST(TestGraphBuilder, Build_unique)
375375

376376
EXPECT_EQ(g.nodes().size(), 3);
377377
EXPECT_EQ(g.edges().size(), 3);
378-
for (const auto& e : g.edges()) {
379-
EXPECT_EQ(e.property, 100);
380-
}
378+
// The last added edge is kept:
379+
EXPECT_EQ(g.edges()[1].property, 200);
381380
}
382381

383382
namespace

docs/source/cpp/graph_metapop.rst

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ The following steps detail how to configure and execute a graph simulation:
195195
196196
Usually, there should be no duplicate edges in your input. If this is not certain, the ``GraphBuilder`` can also remove duplicates.
197197
Here, duplicate means that the start and end node are the same. The parameters in the edge will not be compared.
198-
When duplicates are found, only the **first** inserted edge is kept, all subsequent edges are discarded:
198+
When duplicates are found, only the **last** inserted edge is kept, all previous edges are discarded:
199199

200200
.. code-block:: cpp
201201
@@ -208,24 +208,6 @@ The following steps detail how to configure and execute a graph simulation:
208208
auto graph = builder.build(true);
209209
// graph contains the edges (0, 1, 100) and (1, 0, 100)
210210
211-
To sum this up:
212-
213-
.. list-table::
214-
:header-rows: 1
215-
216-
* -
217-
- ``graph.add_edge``
218-
- ``GraphBuilder.add_edge``
219-
* - Sorting
220-
- Always
221-
- Only when graph is built
222-
* - Duplicate deletion
223-
- Always
224-
- Only when activated on insertion
225-
* - Keeps
226-
- last duplicate
227-
- first duplicate
228-
229211
230212
5. **Initialize and Advance the Mobility Simulation:**
231213

0 commit comments

Comments
 (0)