From 268a6948c0400905dfc335427395519689f067f5 Mon Sep 17 00:00:00 2001 From: hop311 Date: Mon, 22 Jan 2024 20:02:58 +0000 Subject: Added reserve_more, expect_dictionary_key[s|_map]_reserve_length[_and_default] --- src/openvic-simulation/dataloader/NodeTools.hpp | 57 ++++++++++++++++++------- 1 file changed, 42 insertions(+), 15 deletions(-) (limited to 'src/openvic-simulation/dataloader/NodeTools.hpp') diff --git a/src/openvic-simulation/dataloader/NodeTools.hpp b/src/openvic-simulation/dataloader/NodeTools.hpp index 0845e6c..9137d6a 100644 --- a/src/openvic-simulation/dataloader/NodeTools.hpp +++ b/src/openvic-simulation/dataloader/NodeTools.hpp @@ -32,6 +32,15 @@ namespace OpenVic { using name_list_t = std::vector; std::ostream& operator<<(std::ostream& stream, name_list_t const& name_list); + template + concept Reservable = requires(T& t, size_t size) { + { t.size() } -> std::same_as; + t.reserve(size); + }; + constexpr void reserve_more(Reservable auto& t, size_t size) { + t.reserve(t.size() + size); + } + namespace NodeTools { template @@ -232,25 +241,43 @@ namespace OpenVic { ); } - template - concept Reservable = requires(T& t) { - { t.size() } -> std::same_as; - t.reserve(size_t {}); - }; - template - LengthCallback auto reserve_length_callback(T& t) { - return [&t](size_t size) -> size_t { - t.reserve(size); + LengthCallback auto reserve_length_callback(Reservable auto& reservable) { + return [&reservable](size_t size) -> size_t { + reserve_more(reservable, size); return size; }; } - template - NodeCallback auto expect_list_reserve_length(T& t, NodeCallback auto callback) { - return expect_list_and_length(reserve_length_callback(t), callback); + NodeCallback auto expect_list_reserve_length(Reservable auto& reservable, NodeCallback auto callback) { + return expect_list_and_length(reserve_length_callback(reservable), callback); } - template - NodeCallback auto expect_dictionary_reserve_length(T& t, KeyValueCallback auto callback) { - return expect_list_reserve_length(t, expect_assign(callback)); + NodeCallback auto expect_dictionary_reserve_length(Reservable auto& reservable, KeyValueCallback auto callback) { + return expect_dictionary_and_length(reserve_length_callback(reservable), callback); + } + template + NodeCallback auto expect_dictionary_key_map_reserve_length_and_default( + Reservable auto& reservable, key_map_t key_map, KeyValueCallback auto default_callback, Args... args + ) { + return expect_dictionary_key_map_and_length_and_default( + std::move(key_map), reserve_length_callback(reservable), default_callback, args... + ); + } + template + NodeCallback auto expect_dictionary_key_map_reserve_length( + Reservable auto& reservable, key_map_t key_map, Args... args + ) { + return expect_dictionary_key_map_and_length(std::move(key_map), reserve_length_callback(reservable), args...); + } + template + NodeCallback auto expect_dictionary_keys_reserve_length_and_default( + Reservable auto& reservable, KeyValueCallback auto default_callback, Args... args + ) { + return expect_dictionary_keys_and_length_and_default( + reserve_length_callback(reservable), default_callback, args... + ); + } + template + NodeCallback auto expect_dictionary_keys_reserve_length(Reservable auto& reservable, Args... args) { + return expect_dictionary_keys_and_length(reserve_length_callback(reservable), args...); } node_callback_t name_list_callback(callback_t callback); -- cgit v1.2.3-56-ga3b1