Skip to content

Commit

Permalink
sort m_persons vector before moving persons between nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
jubicker committed Jan 14, 2025
1 parent 34ebe3b commit f21f37f
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion cpp/models/graph_abm/graph_abm_mobility.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
#include "memilio/mobility/graph_simulation.h"
#include "memilio/mobility/graph.h"
#include "memilio/utils/compiler_diagnostics.h"
#include <algorithm>
#include <cstddef>
#include <iostream>
#include <utility>
#include <vector>

Expand Down Expand Up @@ -117,6 +119,8 @@ class ABMMobilityEdge
auto& model_from = node_from.get_simulation().get_model();
auto& model_to = node_to.get_simulation().get_model();
auto& persons_to_change = model_from.get_person_buffer();
//sort vector such that we start removing the persons from the bottom
std::sort(persons_to_change.begin(), persons_to_change.end());
//iterate over all persons that change from node_from
for (int i = int(persons_to_change.size()) - 1; i >= 0; --i) {
auto& person = model_from.get_persons()[persons_to_change[i]];
Expand All @@ -130,7 +134,8 @@ class ABMMobilityEdge
model_to.add_person(std::move(person));
//remove person from model_from
model_from.remove_person(persons_to_change[i]);
// correct indices in persons buffer from node_from
//correct indices in persons buffer from node_from
//here it is required that the vector is sorted
for (size_t j = i + 1; j < persons_to_change.size(); ++j) {
persons_to_change[j]--;
}
Expand Down

0 comments on commit f21f37f

Please sign in to comment.