aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/types
diff options
context:
space:
mode:
author hop311 <hop3114@gmail.com>2024-04-15 00:51:10 +0200
committer hop311 <hop3114@gmail.com>2024-04-15 00:51:10 +0200
commitf728fdd7319b88c299826d6a98aa11d3ec1ba6e4 (patch)
treec3ea25983923b191fe2dacf4c578086fde786726 /src/openvic-simulation/types
parent5799836bee29024ce8a2d0fc45e06664c0110751 (diff)
Rework province position loading and map dims
Diffstat (limited to 'src/openvic-simulation/types')
-rw-r--r--src/openvic-simulation/types/IdentifierRegistry.hpp34
-rw-r--r--src/openvic-simulation/types/Vector.hpp9
2 files changed, 33 insertions, 10 deletions
diff --git a/src/openvic-simulation/types/IdentifierRegistry.hpp b/src/openvic-simulation/types/IdentifierRegistry.hpp
index 393ea37..c1fef9d 100644
--- a/src/openvic-simulation/types/IdentifierRegistry.hpp
+++ b/src/openvic-simulation/types/IdentifierRegistry.hpp
@@ -421,18 +421,30 @@ namespace OpenVic {
return identifiers;
}
+ /* Parses a dictionary with item keys and decimal number values (in the form of fixed point values),
+ * with the resulting map move-returned via `callback`. The values can be transformed by providing
+ * a fixed point to fixed point function fixed_point_functor, which will be applied to ever parsed value. */
+ template<NodeTools::FunctorConvertible<fixed_point_t, fixed_point_t> FixedPointFunctor = std::identity>
constexpr NodeTools::NodeCallback auto expect_item_decimal_map(
- NodeTools::Callback<fixed_point_map_t<external_value_type const*>&&> auto callback
+ NodeTools::Callback<fixed_point_map_t<external_value_type const*>&&> auto callback,
+ FixedPointFunctor fixed_point_functor = {}
) const {
- return [this, callback](ast::NodeCPtr node) -> bool {
+ return [this, callback, fixed_point_functor](ast::NodeCPtr node) -> bool {
fixed_point_map_t<external_value_type const*> map;
- bool ret = expect_item_dictionary([&map](external_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);
- map.emplace(&key, std::move(val));
- return ret;
- })(node);
+
+ bool ret = expect_item_dictionary(
+ [&map, fixed_point_functor](external_value_type const& key, ast::NodeCPtr value) -> bool {
+ return NodeTools::expect_fixed_point(
+ [&map, fixed_point_functor, &key](fixed_point_t val) -> bool {
+ map.emplace(&key, fixed_point_functor(val));
+ return true;
+ }
+ )(value);
+ }
+ )(node);
+
ret &= callback(std::move(map));
+
return ret;
};
}
@@ -525,10 +537,12 @@ public: \
std::vector<std::string_view> get_##singular##_identifiers() const { \
return registry.get_item_identifiers(); \
} \
+ template<NodeTools::FunctorConvertible<fixed_point_t, fixed_point_t> FixedPointFunctor = std::identity> \
constexpr NodeTools::NodeCallback auto expect_##singular##_decimal_map( \
- NodeTools::Callback<fixed_point_map_t<decltype(registry)::external_value_type const*>&&> auto callback \
+ NodeTools::Callback<fixed_point_map_t<decltype(registry)::external_value_type const*>&&> auto callback, \
+ FixedPointFunctor fixed_point_functor = {} \
) const { \
- return registry.expect_item_decimal_map(callback); \
+ return registry.expect_item_decimal_map(callback, fixed_point_functor); \
} \
IDENTIFIER_REGISTRY_INTERNAL_SHARED(singular, plural, registry, index_offset, const) \
private:
diff --git a/src/openvic-simulation/types/Vector.hpp b/src/openvic-simulation/types/Vector.hpp
index 6897835..7ed952a 100644
--- a/src/openvic-simulation/types/Vector.hpp
+++ b/src/openvic-simulation/types/Vector.hpp
@@ -34,6 +34,15 @@ namespace OpenVic {
return x * x + y * y;
}
+ constexpr bool nonnegative() const {
+ return x >= 0 && y >= 0;
+ }
+ /* Checks that both coordinates are less than their corresponding values in the argument vector.
+ * This is intended for checking coordinates lie within certain bounds, it is not suitable for sorting vectors. */
+ constexpr bool less_than(vec2_t const& vec) const {
+ return x < vec.x && y < vec.y;
+ }
+
constexpr T* data() {
return reinterpret_cast<T*>(this);
}