diff --git a/app/gui/simulation-component.cpp b/app/gui/simulation-component.cpp index 8b590021..0433234d 100644 --- a/app/gui/simulation-component.cpp +++ b/app/gui/simulation-component.cpp @@ -17,6 +17,9 @@ static status simulation_init_observation(application& app) noexcept for (auto& grid_obs : app.pj.grid_observers) grid_obs.init(app.pj, app.mod, app.sim); + for (auto& graph_obs : app.pj.graph_observers) + graph_obs.init(app.pj, app.mod, app.sim); + for (auto& v_obs : app.pj.variable_observers) irt_check(v_obs.init(app.pj, app.sim)); diff --git a/app/gui/simulation-editor.cpp b/app/gui/simulation-editor.cpp index 585dcb70..2b4cfadd 100644 --- a/app/gui/simulation-editor.cpp +++ b/app/gui/simulation-editor.cpp @@ -2,10 +2,8 @@ // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) -#include "irritator/macros.hpp" #include #include -#include #define IMGUI_DEFINE_MATH_OPERATORS #include @@ -14,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -23,11 +22,6 @@ #include "editor.hpp" #include "internal.hpp" -#include -#include -#include -#include - namespace irt { simulation_editor::simulation_editor() noexcept @@ -518,7 +512,7 @@ static bool show_local_simulation_settings(application& app, tn.parameters_ids.set(uid, gp_id); current = gp_id; } else { - app.pj.parameters.erase(current); + app.pj.parameters.free(current); tn.parameters_ids.erase(uid); current = undefined(); enable = false; @@ -534,11 +528,10 @@ static bool show_local_simulation_settings(application& app, ImGui::TableNextColumn(); if (enable) { - if (app.pj.parameters.exists(current)) { - const auto idx = get_index(current); - show_parameter_editor( - app, mdl, app.pj.parameters.parameters(idx)); - } + app.pj.parameters.if_exists_do( + current, [&](auto /*id*/, auto& param) noexcept { + show_parameter_editor(app, mdl, param); + }); } ImGui::TableNextColumn(); diff --git a/lib/include/irritator/modeling-helpers.hpp b/lib/include/irritator/modeling-helpers.hpp index 52b372b1..c7b66864 100644 --- a/lib/include/irritator/modeling-helpers.hpp +++ b/lib/include/irritator/modeling-helpers.hpp @@ -329,10 +329,10 @@ void for_each_model(simulation& sim, tree_node& tn, Function&& f) noexcept } template -auto if_tree_node_is_grid_do(project& pj, +void if_tree_node_is_grid_do(project& pj, modeling& mod, tree_node_id tn_id, - Function&& f) noexcept -> status + Function&& f) noexcept { tree_node* grid_tn{}; component* compo{}; @@ -343,20 +343,18 @@ auto if_tree_node_is_grid_do(project& pj, if (compo->type == component_type::grid) { if (g_compo = mod.grid_components.try_to_get(compo->id.grid_id); g_compo) { - return f(*grid_tn, *compo, *g_compo); + f(*grid_tn, *compo, *g_compo); } } } } - - return success(); } template -auto if_tree_node_is_graph_do(project& pj, +void if_tree_node_is_graph_do(project& pj, modeling& mod, tree_node_id tn_id, - Function&& f) noexcept -> status + Function&& f) noexcept { tree_node* graph_tn{}; component* compo{}; @@ -368,13 +366,11 @@ auto if_tree_node_is_graph_do(project& pj, if (g_compo = mod.graph_components.try_to_get(compo->id.graph_id); g_compo) { - return f(*graph_tn, *compo, *g_compo); + f(*graph_tn, *compo, *g_compo); } } } } - - return success(); } } // namespace irt diff --git a/lib/include/irritator/modeling.hpp b/lib/include/irritator/modeling.hpp index 00c7baaf..34ef17e6 100644 --- a/lib/include/irritator/modeling.hpp +++ b/lib/include/irritator/modeling.hpp @@ -1081,7 +1081,7 @@ class graph_observer // Build or reuse existing observer for each pair `tn_id`, `mdl_id` and // reinitialize all buffers. - status init(project& pj, modeling& mod, simulation& sim) noexcept; + void init(project& pj, modeling& mod, simulation& sim) noexcept; // Clear the `observers` and `values` vectors. void clear() noexcept; @@ -1165,110 +1165,6 @@ class variable_observer } }; -//! Stores @c irt::project parameters per couple @c tree_node, @c child_id. -class global_parameters -{ -public: - static_limiter max_parameters = 256; - - global_parameters() noexcept; - - void clear() noexcept; - void erase(const global_parameter_id id) noexcept; - - bool can_alloc(std::integral auto nb) const noexcept - { - return m_ids.can_alloc(nb); - } - - bool exists(global_parameter_id id) const noexcept - { - return m_ids.exists(id); - } - - unsigned size() const noexcept { return m_ids.size(); } - int ssize() const noexcept { return m_ids.ssize(); } - - parameter& parameters(std::integral auto i) noexcept; - const parameter& parameters(std::integral auto i) const noexcept; - - template - global_parameter_id alloc(Function&& fn) noexcept - { - debug::ensure(m_ids.can_alloc(1)); - - const auto id = m_ids.alloc(); - const auto idx = get_index(id); - - fn(id, m_names[idx], m_tn_ids[idx], m_mdl_ids[idx], m_parameters[idx]); - - return id; - } - - template - void for_each(Function&& fn) noexcept - { - for (const auto id : m_ids) { - const auto idx = get_index(id); - - fn(id, - m_names[idx], - m_tn_ids[idx], - m_mdl_ids[idx], - m_parameters[idx]); - } - } - -private: - id_array m_ids; - vector m_names; - vector m_tn_ids; - vector m_mdl_ids; - vector m_parameters; -}; - -inline global_parameters::global_parameters() noexcept -{ - m_ids.reserve(max_parameters.value()); - - m_names.resize(max_parameters.value()); - m_tn_ids.resize(max_parameters.value()); - m_mdl_ids.resize(max_parameters.value()); - m_parameters.resize(max_parameters.value()); -} - -inline void global_parameters::clear() noexcept -{ - m_ids.clear(); - m_names.clear(); - m_tn_ids.clear(); - m_mdl_ids.clear(); - m_parameters.clear(); -} - -inline void global_parameters::erase(const global_parameter_id id) noexcept -{ - if (m_ids.exists(id)) - m_ids.free(id); -} - -inline parameter& global_parameters::parameters(std::integral auto i) noexcept -{ - debug::ensure(std::cmp_less_equal(0, i)); - debug::ensure(std::cmp_less(i, m_ids.size())); - - return m_parameters[i]; -} - -inline const parameter& global_parameters::parameters( - std::integral auto i) const noexcept -{ - debug::ensure(std::cmp_less_equal(0, i)); - debug::ensure(std::cmp_less(i, m_ids.size())); - - return m_parameters[i]; -} - struct log_entry { log_str buffer; log_level level; @@ -1618,7 +1514,13 @@ class project data_array grid_observers; data_array graph_observers; - global_parameters parameters; + id_data_array + parameters; private: component_id m_head = undefined(); diff --git a/lib/src/graph-observer.cpp b/lib/src/graph-observer.cpp index 32dd068b..3854b01f 100644 --- a/lib/src/graph-observer.cpp +++ b/lib/src/graph-observer.cpp @@ -8,18 +8,18 @@ namespace irt { -static status build_graph(graph_observer& graph_obs, - project& pj, - simulation& sim, - tree_node& graph_parent, - graph_component& graph_compo) noexcept +static void build_graph(graph_observer& graph_obs, + project& pj, + simulation& sim, + tree_node& graph_parent, + graph_component& graph_compo) noexcept { debug::ensure(pj.tree_nodes.try_to_get(graph_obs.tn_id) != nullptr); const auto* to = pj.tree_nodes.try_to_get(graph_obs.tn_id); if (!to) - return new_error(project::part::graph_observers, unknown_error{}); + return; const auto relative_path = pj.build_relative_path(graph_parent, *to, graph_obs.mdl_id); @@ -51,22 +51,18 @@ static status build_graph(graph_observer& graph_obs, child = child->tree.get_sibling(); } - - return success(); } -status graph_observer::init(project& pj, - modeling& mod, - simulation& sim) noexcept +void graph_observer::init(project& pj, modeling& mod, simulation& sim) noexcept { observers.clear(); values.clear(); - return if_tree_node_is_graph_do( + if_tree_node_is_graph_do( pj, mod, parent_id, - [&](auto& graph_parent_tn, auto& compo, auto& graph) -> status { + [&](auto& graph_parent_tn, auto& compo, auto& graph) noexcept { debug::ensure(compo.type == component_type::graph); const auto len = graph.children.ssize(); @@ -77,7 +73,7 @@ status graph_observer::init(project& pj, std::fill_n(observers.data(), len, undefined()); std::fill_n(values.data(), len, zero); - return build_graph(*this, pj, sim, graph_parent_tn, graph); + build_graph(*this, pj, sim, graph_parent_tn, graph); }); } diff --git a/lib/src/grid-observer.cpp b/lib/src/grid-observer.cpp index c72bfd44..dedccd39 100644 --- a/lib/src/grid-observer.cpp +++ b/lib/src/grid-observer.cpp @@ -76,32 +76,22 @@ void grid_observer::init(project& pj, modeling& mod, simulation& sim) noexcept observers.clear(); values.clear(); - tree_node* grid_tn{}; - component* compo{}; - grid_component* g_compo{}; + if_tree_node_is_grid_do( + pj, mod, parent_id, [&](auto& tn, auto& compo, auto& g_compo) noexcept { + debug::ensure(compo.type == component_type::grid); - if (grid_tn = pj.tree_nodes.try_to_get(tn_id); grid_tn) { - if (compo = mod.components.try_to_get(grid_tn->id); compo) { - if (compo->type == component_type::grid) { - if (g_compo = mod.grid_components.try_to_get(compo->id.grid_id); - g_compo) { + const auto len = g_compo.row * g_compo.column; + rows = g_compo.row; + cols = g_compo.column; - const auto len = g_compo->row * g_compo->column; - rows = g_compo->row; - cols = g_compo->column; + observers.resize(len); + values.resize(len); - observers.resize(len); - values.resize(len); + std::fill_n(observers.data(), len, undefined()); + std::fill_n(values.data(), len, zero); - std::fill_n( - observers.data(), len, undefined()); - std::fill_n(values.data(), len, zero); - - build_grid_observer(*this, pj, sim, *grid_tn, *g_compo); - } - } - } - } + build_grid_observer(*this, pj, sim, tn, g_compo); + }); } void grid_observer::clear() noexcept diff --git a/lib/src/json.cpp b/lib/src/json.cpp index 375ad750..bf2c5abe 100644 --- a/lib/src/json.cpp +++ b/lib/src/json.cpp @@ -475,8 +475,8 @@ struct reader { template bool for_members(const rapidjson::Value& val, - const std::string_view (&names)[N], - Function&& fn) noexcept + const std::string_view (&names)[N], + Function&& fn) noexcept { if (!val.IsObject()) report_json_error(error_id::value_not_object); @@ -1947,8 +1947,8 @@ struct reader { return nullptr; } - auto search_dir_in_reg(registred_path& reg, std::string_view name) noexcept - -> dir_path* + auto search_dir_in_reg(registred_path& reg, + std::string_view name) noexcept -> dir_path* { for (auto dir_id : reg.children) { if (auto* dir = mod().dir_paths.try_to_get(dir_id); dir) { @@ -2017,8 +2017,8 @@ struct reader { return nullptr; } - auto search_file(dir_path& dir, std::string_view name) noexcept - -> file_path* + auto search_file(dir_path& dir, + std::string_view name) noexcept -> file_path* { for (auto file_id : dir.children) if (auto* file = mod().file_paths.try_to_get(file_id); file) @@ -3713,8 +3713,7 @@ struct reader { }); } - bool read_global_parameter(const rapidjson::Value& val, - global_parameters& param) noexcept + bool read_global_parameter(const rapidjson::Value& val) noexcept { auto_stack s(this, stack_id::project_global_parameter); @@ -3764,11 +3763,11 @@ struct reader { if (success and name_opt.has_value() and tn_id_opt.has_value() and mdl_id_opt.has_value() and parameter_opt.has_value()) { - param.alloc([&](auto /*id*/, - auto& name, - auto& tn_id, - auto& mdl_id, - auto& p) noexcept { + pj().parameters.alloc([&](auto /*id*/, + auto& name, + auto& tn_id, + auto& mdl_id, + auto& p) noexcept { name = *name_opt; tn_id = *tn_id_opt; mdl_id = *mdl_id_opt; @@ -3792,7 +3791,7 @@ struct reader { for_each_array( val, [&](const auto /*i*/, const auto& value) noexcept -> bool { - return read_global_parameter(value, pj().parameters); + return read_global_parameter(value); }); } diff --git a/lib/src/project.cpp b/lib/src/project.cpp index 218461df..13ff08a2 100644 --- a/lib/src/project.cpp +++ b/lib/src/project.cpp @@ -990,6 +990,8 @@ static auto make_tree_from(simulation_copy& sc, status project::init(const modeling_initializer& init) noexcept { + parameters.reserve(256); + tree_nodes.reserve(init.tree_capacity); if (not tree_nodes.can_alloc()) return new_error(project::part::tree_nodes);