Skip to content

Commit

Permalink
mod: replace global-observers struct with id-data-array
Browse files Browse the repository at this point in the history
  • Loading branch information
quesnel committed May 30, 2024
1 parent 3229076 commit 6faa0ad
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 179 deletions.
3 changes: 3 additions & 0 deletions app/gui/simulation-component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
19 changes: 6 additions & 13 deletions app/gui/simulation-editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <mutex>
#include <optional>
#include <utility>

#define IMGUI_DEFINE_MATH_OPERATORS
#include <imgui.h>
Expand All @@ -14,6 +12,7 @@
#include <irritator/core.hpp>
#include <irritator/helpers.hpp>
#include <irritator/io.hpp>
#include <irritator/macros.hpp>
#include <irritator/modeling-helpers.hpp>
#include <irritator/modeling.hpp>
#include <irritator/observation.hpp>
Expand All @@ -23,11 +22,6 @@
#include "editor.hpp"
#include "internal.hpp"

#include <chrono>
#include <filesystem>
#include <fstream>
#include <string>

namespace irt {

simulation_editor::simulation_editor() noexcept
Expand Down Expand Up @@ -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<global_parameter_id>();
enable = false;
Expand All @@ -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<irt::parameter>(
current, [&](auto /*id*/, auto& param) noexcept {
show_parameter_editor(app, mdl, param);
});
}

ImGui::TableNextColumn();
Expand Down
16 changes: 6 additions & 10 deletions lib/include/irritator/modeling-helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,10 +329,10 @@ void for_each_model(simulation& sim, tree_node& tn, Function&& f) noexcept
}

template<typename Function>
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{};
Expand All @@ -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<typename Function>
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{};
Expand All @@ -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
Expand Down
114 changes: 8 additions & 106 deletions lib/include/irritator/modeling.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<i32, 8, 65536> 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<typename Function>
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<typename Function>
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<global_parameter_id> m_ids;
vector<name_str> m_names;
vector<tree_node_id> m_tn_ids;
vector<model_id> m_mdl_ids;
vector<parameter> 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;
Expand Down Expand Up @@ -1618,7 +1514,13 @@ class project
data_array<grid_observer, grid_observer_id> grid_observers;
data_array<graph_observer, graph_observer_id> graph_observers;

global_parameters parameters;
id_data_array<global_parameter_id,
default_allocator,
name_str,
tree_node_id,
model_id,
parameter>
parameters;

private:
component_id m_head = undefined<component_id>();
Expand Down
24 changes: 10 additions & 14 deletions lib/src/graph-observer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand All @@ -77,7 +73,7 @@ status graph_observer::init(project& pj,
std::fill_n(observers.data(), len, undefined<observer_id>());
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);
});
}

Expand Down
34 changes: 12 additions & 22 deletions lib/src/grid-observer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<observer_id>());
std::fill_n(values.data(), len, zero);

std::fill_n(
observers.data(), len, undefined<observer_id>());
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
Expand Down
Loading

0 comments on commit 6faa0ad

Please sign in to comment.