diff options
author | Hop311 <Hop3114@gmail.com> | 2023-11-17 10:19:53 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-17 10:19:53 +0100 |
commit | e76336cd92639f4ec71088fc4c80aea4c25528cd (patch) | |
tree | eeed419a7d97ecb58adf63a17eb9184db3e5ed7a /src/openvic-simulation/types | |
parent | a00b558a53edb40c9e6789790036f0b618e80ec1 (diff) | |
parent | 886b8b8f396438fc2b7da7d2508f2064d14150a8 (diff) |
Merge pull request #75 from OpenVicProject/accumulated-changes
Accumulated changes
Diffstat (limited to 'src/openvic-simulation/types')
-rw-r--r-- | src/openvic-simulation/types/Colour.hpp | 2 | ||||
-rw-r--r-- | src/openvic-simulation/types/Date.cpp | 4 | ||||
-rw-r--r-- | src/openvic-simulation/types/Date.hpp | 2 | ||||
-rw-r--r-- | src/openvic-simulation/types/IdentifierRegistry.hpp | 26 | ||||
-rw-r--r-- | src/openvic-simulation/types/fixed_point/FixedPointMap.hpp | 48 |
5 files changed, 62 insertions, 20 deletions
diff --git a/src/openvic-simulation/types/Colour.hpp b/src/openvic-simulation/types/Colour.hpp index e516d5b..7c97b12 100644 --- a/src/openvic-simulation/types/Colour.hpp +++ b/src/openvic-simulation/types/Colour.hpp @@ -15,7 +15,7 @@ namespace OpenVic { * When colour_t is used in a purely graphical context, NULL_COLOUR * should be allowed. */ - static constexpr colour_t NULL_COLOUR = 0, FULL_COLOUR = 0xFF; + static constexpr colour_t NULL_COLOUR = 0, COLOUR_COMPONENT = 0xFF; static constexpr colour_t MAX_COLOUR_RGB = 0xFFFFFF, MAX_COLOUR_ARGB = 0xFFFFFFFF; constexpr colour_t float_to_colour_byte(float f, float min = 0.0f, float max = 1.0f) { diff --git a/src/openvic-simulation/types/Date.cpp b/src/openvic-simulation/types/Date.cpp index c5eac41..e695e45 100644 --- a/src/openvic-simulation/types/Date.cpp +++ b/src/openvic-simulation/types/Date.cpp @@ -204,6 +204,10 @@ Date Date::operator++(int) { return old; } +bool Date::in_range(Date start, Date end) const { + return start <= *this && *this <= end; +} + std::string Date::to_string() const { std::stringstream ss; ss << *this; diff --git a/src/openvic-simulation/types/Date.hpp b/src/openvic-simulation/types/Date.hpp index b6e693c..5aed49b 100644 --- a/src/openvic-simulation/types/Date.hpp +++ b/src/openvic-simulation/types/Date.hpp @@ -91,6 +91,8 @@ namespace OpenVic { Date& operator++(); Date operator++(int); + bool in_range(Date start, Date end) const; + std::string to_string() const; explicit operator std::string() const; // Parsed from string of the form YYYY.MM.DD diff --git a/src/openvic-simulation/types/IdentifierRegistry.hpp b/src/openvic-simulation/types/IdentifierRegistry.hpp index 662815e..6c0dd3b 100644 --- a/src/openvic-simulation/types/IdentifierRegistry.hpp +++ b/src/openvic-simulation/types/IdentifierRegistry.hpp @@ -6,6 +6,7 @@ #include <vector> #include "openvic-simulation/dataloader/NodeTools.hpp" +#include "openvic-simulation/types/fixed_point/FixedPointMap.hpp" #include "openvic-simulation/utility/Getters.hpp" #include "openvic-simulation/utility/Logger.hpp" @@ -77,22 +78,6 @@ namespace OpenVic { HasIdentifierAndColour& operator=(HasIdentifierAndColour&&) = delete; }; - template<typename T> - using decimal_map_t = std::map<T, fixed_point_t>; - - template<typename T> - constexpr typename decimal_map_t<T>::value_type get_largest_item(decimal_map_t<T> const& map) { - constexpr auto pred = [](typename decimal_map_t<T>::value_type a, typename decimal_map_t<T>::value_type b) -> bool { - return a.second < b.second; - }; - const typename decimal_map_t<T>::const_iterator result = std::max_element(map.begin(), map.end(), pred); - if (result != map.end()) { - return *result; - } else { - return { nullptr, -1 }; - } - } - /* Callbacks for trying to add duplicate keys via UniqueKeyRegistry::add_item */ static bool duplicate_fail_callback(std::string_view registry_name, std::string_view duplicate_identifier) { Logger::error( @@ -274,10 +259,10 @@ namespace OpenVic { } NodeTools::node_callback_t expect_item_decimal_map( - NodeTools::callback_t<decimal_map_t<value_type const*>&&> callback + NodeTools::callback_t<fixed_point_map_t<value_type const*>&&> callback ) const { return [this, callback](ast::NodeCPtr node) -> bool { - decimal_map_t<value_type const*> map; + fixed_point_map_t<value_type const*> map; bool ret = expect_item_dictionary([&map](value_type const& key, ast::NodeCPtr value) -> bool { fixed_point_t val; const bool ret = NodeTools::expect_fixed_point(NodeTools::assign_variable_callback(val))(value); @@ -348,6 +333,9 @@ namespace OpenVic { size_t get_##singular##_count() const { \ return plural.size(); \ } \ + bool plural##_empty() const { \ + return plural.empty(); \ + } \ std::vector<decltype(plural)::storage_type> const& get_##plural() const { \ return plural.get_items(); \ } \ @@ -370,7 +358,7 @@ namespace OpenVic { return plural.expect_item_dictionary(callback); \ } \ NodeTools::node_callback_t expect_##singular##_decimal_map( \ - NodeTools::callback_t<decimal_map_t<decltype(plural)::value_type const*>&&> callback \ + NodeTools::callback_t<fixed_point_map_t<decltype(plural)::value_type const*>&&> callback \ ) const { \ return plural.expect_item_decimal_map(callback); \ } diff --git a/src/openvic-simulation/types/fixed_point/FixedPointMap.hpp b/src/openvic-simulation/types/fixed_point/FixedPointMap.hpp new file mode 100644 index 0000000..a7d298b --- /dev/null +++ b/src/openvic-simulation/types/fixed_point/FixedPointMap.hpp @@ -0,0 +1,48 @@ +#pragma once + +#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" + +namespace OpenVic { + + template<typename T> + using fixed_point_map_t = std::map<T, fixed_point_t>; + + template<typename T> + using fixed_point_map_value_t = typename fixed_point_map_t<T>::value_type; + + template<typename T> + using fixed_point_map_const_iterator_t = typename fixed_point_map_t<T>::const_iterator; + + template<typename T> + constexpr fixed_point_t get_total(fixed_point_map_t<T> const& map) { + fixed_point_t total = 0; + for (auto const& [key, value] : map) { + total += value; + } + return total; + } + + template<typename T> + constexpr fixed_point_map_const_iterator_t<T> get_largest_item(fixed_point_map_t<T> const& map) { + constexpr auto pred = [](fixed_point_map_value_t<T> a, fixed_point_map_value_t<T> b) -> bool { + return a.second < b.second; + }; + return std::max_element(map.begin(), map.end(), pred); + } + + template<typename T> + constexpr std::pair<fixed_point_map_const_iterator_t<T>, fixed_point_map_const_iterator_t<T>> get_largest_two_items( + fixed_point_map_t<T> const& map + ) { + fixed_point_map_const_iterator_t<T> largest = map.end(), second_largest = map.end(); + for (fixed_point_map_const_iterator_t<T> it = map.begin(); it != map.end(); ++it) { + if (largest == map.end() || it->second > largest->second) { + second_largest = largest; + largest = it; + } else if (second_largest == map.end() || it->second > second_largest->second) { + second_largest = it; + } + } + return std::make_pair(std::move(largest), std::move(second_largest)); + } +} |