diff --git a/src/openvic-simulation/population/Pop.cpp b/src/openvic-simulation/population/Pop.cpp index d49673f7e..4a78abaf9 100644 --- a/src/openvic-simulation/population/Pop.cpp +++ b/src/openvic-simulation/population/Pop.cpp @@ -120,6 +120,28 @@ void Pop::setup_pop_test_values(TypedSpan reforms) item = fp::mul_div(item, new_total, old_total); } }; + static auto rescale_spans = []( + std::span span_a, + std::span span_b, + const fixed_point_t new_total + ) -> void { + fixed_point_t old_total = 0; + for (const fixed_point_t item : span_a) { + old_total += item; + } + for (const fixed_point_t item : span_b) { + old_total += item; + } + + if (old_total == 0) { return; } + + for (fixed_point_t& item : span_a) { + item = fp::mul_div(item, new_total, old_total); + } + for (fixed_point_t& item : span_b) { + item = fp::mul_div(item, new_total, old_total); + } + }; static auto test_weight_ordered = [](ordered_map& map, U const& key, int32_t min, int32_t max) -> void { if constexpr (std::is_convertible_v || std::is_convertible_v) { const int32_t value = rand() % (max + 1); @@ -139,17 +161,19 @@ void Pop::setup_pop_test_values(TypedSpan reforms) /* All entries equally weighted for testing. */ fill_span_with_test_weights(supporter_equivalents_by_ideology, 1, 5); rescale_span(supporter_equivalents_by_ideology, type_safe::get(size)); - - fill_span_with_test_weights(supporter_equivalents_by_party_policy, 3, 6); - rescale_span(supporter_equivalents_by_party_policy, type_safe::get(size)); - + + fill_span_with_test_weights(supporter_equivalents_by_party_policy, 3, 6); fill_span_with_test_weights(supporter_equivalents_by_reform, 3, 6); for (Reform const& reform : reforms) { if (reform.group.is_civilizing()) { supporter_equivalents_by_reform[reform.index] = 0; } } - rescale_span(supporter_equivalents_by_reform, type_safe::get(size)); + rescale_spans( + supporter_equivalents_by_party_policy, + supporter_equivalents_by_reform, + type_safe::get(size) + ); if (!vote_equivalents_by_party.empty()) { for (auto& [party, value] : vote_equivalents_by_party) {