Skip to content

Commit 1e9cf07

Browse files
authored
Merge pull request #637 from OpenVicProject/fix/with-stable-sort
Fix occurrences of sort to be stable_sort for determinism
2 parents 186346c + d3b5a11 commit 1e9cf07

File tree

4 files changed

+13
-9
lines changed

4 files changed

+13
-9
lines changed

src/openvic-simulation/country/CountryInstance.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "CountryInstanceDeps.hpp"
33
#include "CountryInstanceManager.hpp"
44

5+
#include <algorithm>
56
#include <cstddef>
67
#include <cstdint>
78

@@ -1284,11 +1285,11 @@ void CountryInstance::_update_production() {
12841285

12851286
industrial_power.set(industrial_power_running_total);
12861287

1287-
std::sort(
1288+
std::stable_sort(
12881289
industrial_power_from_states.begin(), industrial_power_from_states.end(),
12891290
[](auto const& a, auto const& b) -> bool { return a.second > b.second; }
12901291
);
1291-
std::sort(
1292+
std::stable_sort(
12921293
industrial_power_from_investments.begin(), industrial_power_from_investments.end(),
12931294
[](auto const& a, auto const& b) -> bool { return a.second > b.second; }
12941295
);

src/openvic-simulation/country/CountryInstanceManager.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "CountryInstanceManager.hpp"
2+
#include <algorithm>
23

34
#include "openvic-simulation/country/CountryDefinition.hpp"
45
#include "openvic-simulation/defines/CountryDefines.hpp"
@@ -59,27 +60,27 @@ void CountryInstanceManager::update_rankings(const Date today) {
5960
industrial_power_ranking = total_ranking;
6061
military_power_ranking = total_ranking;
6162

62-
std::sort(
63+
std::stable_sort(
6364
total_ranking.begin(), total_ranking.end(),
6465
[](CountryInstance* a, CountryInstance* b) -> bool {
6566
const bool a_civilised = a->is_civilised();
6667
const bool b_civilised = b->is_civilised();
6768
return a_civilised != b_civilised ? a_civilised : a->total_score.get_untracked() > b->total_score.get_untracked();
6869
}
6970
);
70-
std::sort(
71+
std::stable_sort(
7172
prestige_ranking.begin(), prestige_ranking.end(),
7273
[](CountryInstance const* a, CountryInstance const* b) -> bool {
7374
return a->get_prestige_untracked() > b->get_prestige_untracked();
7475
}
7576
);
76-
std::sort(
77+
std::stable_sort(
7778
industrial_power_ranking.begin(), industrial_power_ranking.end(),
7879
[](CountryInstance const* a, CountryInstance const* b) -> bool {
7980
return a->get_industrial_power_untracked() > b->get_industrial_power_untracked();
8081
}
8182
);
82-
std::sort(
83+
std::stable_sort(
8384
military_power_ranking.begin(), military_power_ranking.end(),
8485
[](CountryInstance* a, CountryInstance* b) -> bool {
8586
return a->military_power.get_untracked() > b->military_power.get_untracked();
@@ -158,7 +159,7 @@ void CountryInstanceManager::update_rankings(const Date today) {
158159

159160
// Sort the great powers list by total rank, as pre-existing great powers may have changed rank order and new great
160161
// powers will have been added to the end of the list regardless of rank.
161-
std::sort(great_powers.begin(), great_powers.end(), [](CountryInstance const* a, CountryInstance const* b) -> bool {
162+
std::stable_sort(great_powers.begin(), great_powers.end(), [](CountryInstance const* a, CountryInstance const* b) -> bool {
162163
return a->get_total_rank() < b->get_total_rank();
163164
});
164165

src/openvic-simulation/history/HistoryMap.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include <algorithm>
34
#include <system_error>
45

56
#include "openvic-simulation/dataloader/NodeTools.hpp"
@@ -115,7 +116,7 @@ namespace OpenVic {
115116
for (typename decltype(entries)::value_type const& entry : entries) {
116117
keys.push_back(entry.first);
117118
}
118-
std::sort(keys.begin(), keys.end());
119+
std::stable_sort(keys.begin(), keys.end());
119120
ordered_map<Date, memory::unique_ptr<entry_type>> new_entries;
120121
for (Date const& key : keys) {
121122
new_entries.emplace(key, std::move(entries[key]));

src/openvic-simulation/pop/PopValuesFromProvince.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "PopValuesFromProvince.hpp"
2+
#include <algorithm>
23

34
#include "openvic-simulation/country/CountryInstance.hpp"
45
#include "openvic-simulation/defines/PopsDefines.hpp"
@@ -90,7 +91,7 @@ void PopValuesFromProvince::update_pop_values_from_province(ProvinceInstance& pr
9091
}
9192

9293
if (!ranked_artisanal_production_types.empty()) {
93-
std::sort(ranked_artisanal_production_types.begin(), ranked_artisanal_production_types.end(),
94+
std::stable_sort(ranked_artisanal_production_types.begin(), ranked_artisanal_production_types.end(),
9495
[](const auto& a, const auto& b) {
9596
return a.second > b.second;
9697
}

0 commit comments

Comments
 (0)