From 928c521599b2b18e8964aeb445c3ca5a55bacbd1 Mon Sep 17 00:00:00 2001 From: hop311 Date: Wed, 17 Jul 2024 00:01:03 +0100 Subject: Use IndexedMap + helper function support --- .../classes/GFXPieChartTexture.hpp | 45 +++++++++++++++++----- 1 file changed, 36 insertions(+), 9 deletions(-) (limited to 'extension/src/openvic-extension/classes/GFXPieChartTexture.hpp') diff --git a/extension/src/openvic-extension/classes/GFXPieChartTexture.hpp b/extension/src/openvic-extension/classes/GFXPieChartTexture.hpp index 8683b10..3135050 100644 --- a/extension/src/openvic-extension/classes/GFXPieChartTexture.hpp +++ b/extension/src/openvic-extension/classes/GFXPieChartTexture.hpp @@ -38,23 +38,50 @@ namespace OpenVic { /* Generate slice data from a distribution of HasIdentifierAndColour derived objects, sorted by their weight. * The resulting Array of Dictionaries can be used as an argument for set_slices_array. */ - template T> - static godot_pie_chart_data_t distribution_to_slices_array(fixed_point_map_t const& dist) { + template + static godot_pie_chart_data_t distribution_to_slices_array(Container const& dist) + requires( + ( + /* fixed_point_map_t, T derived from HasIdentifierAndColour */ + utility::is_specialization_of_v && + std::derived_from, HasIdentifierAndColour> && + std::is_same_v + ) || ( + /* IndexedMap, T derived from HasIdentifierAndColour */ + utility::is_specialization_of_v && + std::derived_from && + std::is_same_v + ) + ) { using namespace godot; - using entry_t = std::pair; + using entry_t = std::pair; std::vector sorted_dist; - sorted_dist.reserve(dist.size()); - for (entry_t const& entry : dist) { - ERR_CONTINUE_MSG( - entry.first == nullptr, vformat("Null distribution key with value %f", entry.second.to_float()) - ); - sorted_dist.push_back(entry); + + if constexpr (utility::is_specialization_of_v) { + sorted_dist.reserve(dist.size()); + for (entry_t const& entry : dist) { + ERR_CONTINUE_MSG( + entry.first == nullptr, vformat("Null distribution key with value %f", entry.second.to_float()) + ); + sorted_dist.push_back(entry); + } + } else { + for (size_t index = 0; index < dist.size(); ++index) { + fixed_point_t const& value = dist[index]; + if (value != 0) { + HasIdentifierAndColour const* key = &dist(index); + sorted_dist.emplace_back(key, value); + } + } } + std::sort(sorted_dist.begin(), sorted_dist.end(), [](entry_t const& lhs, entry_t const& rhs) -> bool { return lhs.first < rhs.first; }); + godot_pie_chart_data_t array; ERR_FAIL_COND_V(array.resize(sorted_dist.size()) != OK, {}); + for (size_t idx = 0; idx < array.size(); ++idx) { auto const& [key, val] = sorted_dist[idx]; Dictionary sub_dict; -- cgit v1.2.3-56-ga3b1