Commit 348ca3e5 authored by Gauthier Quesnel's avatar Gauthier Quesnel
Browse files

WIP

parent b72a2dc5
Pipeline #41116 failed with stage
in 60 minutes and 1 second
......@@ -440,25 +440,6 @@ private:
streambuf buf;
std::istream is;
struct mapping
{
mapping(int index_, u64 value_)
: index(index_)
, value(value_)
{}
int index = 0;
u64 value = 0u;
bool operator<(const mapping& other) const noexcept
{
return index < other.index;
}
bool operator<(int i) const noexcept { return index < i; }
bool operator==(int i) const noexcept { return index == i; }
};
struct position
{
position() noexcept = default;
......@@ -471,12 +452,12 @@ private:
float x, y;
};
std::vector<model_id> map;
std::vector<position> positions; /* Stores positions of the models. */
std::vector<mapping> constant_mapping;
std::vector<mapping> binary_file_mapping;
std::vector<mapping> random_mapping;
std::vector<mapping> text_file_mapping;
table<int, u64> map;
table<int, u64> constant_mapping;
table<int, u64> binary_file_mapping;
table<int, u64> random_mapping;
table<int, u64> text_file_mapping;
vector<position> positions; /* Stores positions of the models. */
int source_number = 0;
int model_number = 0;
......@@ -488,7 +469,14 @@ public:
reader(std::istream& is_) noexcept
: buf(is_.rdbuf())
, is(&buf)
{}
{
map.data.init(64);
constant_mapping.data.init(64);
binary_file_mapping.data.init(64);
random_mapping.data.init(64);
text_file_mapping.data.init(64);
positions.init(64);
}
~reader() noexcept = default;
......@@ -496,13 +484,12 @@ public:
int connection_error = 0;
int line_error() const noexcept { return buf.m_line_number; }
int column_error() const noexcept { return buf.m_column; }
position get_position(const sz index) const noexcept
position get_position(const i32 index) const noexcept
{
return index < positions.size() ? positions[index]
: position{ 0.f, 0.f };
return index < positions.ssize() ? positions[index]
: position{ 0.f, 0.f };
}
status operator()(simulation& sim, external_source& srcs) noexcept
......@@ -530,7 +517,8 @@ public:
for (int i = 0; i != model_number; ++i, ++model_error) {
int id;
irt_return_if_bad(do_read_model(sim, &id));
f(map[id]);
if (auto* ptr = map.get(id); ptr)
f(enum_cast<model_id>(*ptr));
}
irt_return_if_bad(do_read_connections(sim));
......@@ -582,7 +570,7 @@ private:
return status::io_file_format_error;
elem.file_path = file_path;
binary_file_mapping.emplace_back(id, ordinal(elem_id));
binary_file_mapping.data.emplace_back(id, ordinal(elem_id));
return status::success;
}
......@@ -608,7 +596,7 @@ private:
return status::io_file_format_error;
elem.file_path = file_path;
text_file_mapping.emplace_back(id, ordinal(elem_id));
text_file_mapping.data.emplace_back(id, ordinal(elem_id));
return status::success;
}
......@@ -630,7 +618,7 @@ private:
auto cst_id = srcs.constant_sources.get_id(cst);
try {
constant_mapping.emplace_back(id, ordinal(cst_id));
constant_mapping.data.emplace_back(id, ordinal(cst_id));
} catch (const std::bad_alloc& /*e*/) {
return status::io_not_enough_memory;
}
......@@ -677,7 +665,7 @@ private:
auto elem_id = srcs.random_sources.get_id(elem);
try {
random_mapping.emplace_back(id, ordinal(elem_id));
random_mapping.data.emplace_back(id, ordinal(elem_id));
} catch (const std::bad_alloc& /*e*/) {
return status::io_not_enough_memory;
}
......@@ -827,11 +815,10 @@ private:
}
}
std::sort(std::begin(constant_mapping), std::end(constant_mapping));
std::sort(std::begin(binary_file_mapping),
std::end(binary_file_mapping));
std::sort(std::begin(text_file_mapping), std::end(text_file_mapping));
std::sort(std::begin(random_mapping), std::end(random_mapping));
constant_mapping.sort();
binary_file_mapping.sort();
text_file_mapping.sort();
random_mapping.sort();
return status::success;
}
......@@ -845,12 +832,12 @@ private:
irt_return_if_fail(model_number > 0,
status::io_file_format_model_number_error);
try {
map.resize(model_number, model_id{ 0 });
positions.resize(model_number);
} catch (const std::bad_alloc& /*e*/) {
return status::io_not_enough_memory;
}
irt_return_if_bad(map.data.init(model_number));
irt_return_if_bad(constant_mapping.data.init(model_number));
irt_return_if_bad(binary_file_mapping.data.init(model_number));
irt_return_if_bad(random_mapping.data.init(model_number));
irt_return_if_bad(text_file_mapping.data.init(model_number));
irt_return_if_bad(positions.init(model_number));
return status::success;
}
......@@ -904,10 +891,17 @@ private:
irt_return_if_fail(0 <= mdl_dst_id && mdl_dst_id < model_number,
status::io_file_format_model_error);
auto* mdl_src = sim.models.try_to_get(map[mdl_src_id]);
auto* mapped_mdl_src_id = map.get(mdl_src_id);
irt_return_if_fail(mapped_mdl_src_id,
status::io_file_format_model_unknown);
auto* mdl_src =
sim.models.try_to_get(enum_cast<model_id>(*mapped_mdl_src_id));
irt_return_if_fail(mdl_src, status::io_file_format_model_unknown);
auto* mdl_dst = sim.models.try_to_get(map[mdl_dst_id]);
auto* mapped_mdl_dst_id = map.get(mdl_dst_id);
auto* mdl_dst =
sim.models.try_to_get(enum_cast<model_id>(*mapped_mdl_dst_id));
irt_return_if_fail(mdl_dst, status::io_file_format_model_unknown);
output_port* out = nullptr;
......@@ -1012,7 +1006,7 @@ private:
return false;
}
status do_read_component(modeling& mod, component& compo)
status do_read_component(int id, modeling& mod, component& compo)
{
char temp[64];
......@@ -1023,13 +1017,28 @@ private:
if (!(is >> temp))
return status::io_file_format_error;
irt_return_if_bad(add_cpp_component_ref(temp, mod, compo));
irt_return_if_fail(mod.component_refs.can_alloc(),
status::io_file_format_error);
auto& compo_ref = mod.component_refs.alloc();
irt_return_if_bad(
add_cpp_component_ref(temp, mod, compo, compo_ref));
map.set(id, ordinal(mod.component_refs.get_id(compo_ref)));
} else if (std::strcmp(temp, "file") == 0) {
std::string file_path;
if (!(is >> file_path))
return status::io_file_format_error;
irt_return_if_bad(add_file_component_ref(temp, mod, compo));
irt_return_if_fail(mod.component_refs.can_alloc(),
status::io_file_format_error);
auto& compo_ref = mod.component_refs.alloc();
irt_return_if_bad(
add_file_component_ref(temp, mod, compo, compo_ref));
map.set(id, ordinal(mod.component_refs.get_id(compo_ref)));
} else {
return status::io_file_format_error;
}
......@@ -1055,8 +1064,7 @@ private:
});
irt_return_if_bad(ret);
map[id] = sim.models.get_id(mdl);
map.set(id, ordinal(sim.models.get_id(mdl)));
return status::success;
}
......@@ -1069,8 +1077,7 @@ private:
dynamics_type type;
if (std::strcmp(dynamics_name, "component") == 0) {
irt_return_if_bad(do_read_component(mod, compo));
// map[i] . previous component
irt_return_if_bad(do_read_component(id, mod, compo));
} else {
irt_return_if_fail(convert(dynamics_name, &type),
status::io_file_format_dynamics_unknown);
......@@ -1088,7 +1095,7 @@ private:
irt_return_if_bad(ret);
map[id] = mod.models.get_id(mdl);
map.set(id, ordinal(mod.models.get_id(mdl)));
}
return status::success;
......@@ -1303,48 +1310,42 @@ private:
{
switch (type) {
case external_source_type::binary_file: {
auto it = binary_find(
binary_file_mapping.begin(), binary_file_mapping.end(), index);
if (it == binary_file_mapping.end())
return false;
if (auto* ptr = binary_file_mapping.get(index); ptr) {
src.type = ordinal(type);
src.id = *ptr;
return true;
}
src.type = ordinal(type);
src.id = it->value;
return true;
};
return false;
}
case external_source_type::constant: {
auto it = binary_find(
constant_mapping.begin(), constant_mapping.end(), index);
if (it == constant_mapping.end())
return false;
if (auto* ptr = constant_mapping.get(index); ptr) {
src.type = ordinal(type);
src.id = *ptr;
return true;
}
src.type = ordinal(type);
src.id = it->value;
return true;
return false;
};
case external_source_type::text_file: {
auto it = binary_find(
text_file_mapping.begin(), text_file_mapping.end(), index);
if (it == text_file_mapping.end())
return false;
if (auto* ptr = text_file_mapping.get(index); ptr) {
src.type = ordinal(type);
src.id = *ptr;
return true;
}
src.type = ordinal(type);
src.id = it->value;
return true;
return false;
};
case external_source_type::random: {
auto it =
binary_find(random_mapping.begin(), random_mapping.end(), index);
if (it == random_mapping.end())
return false;
src.type = ordinal(type);
src.id = it->value;
return true;
if (auto* ptr = random_mapping.get(index); ptr) {
src.type = ordinal(type);
src.id = *ptr;
return true;
}
return false;
};
}
......
......@@ -32,12 +32,14 @@ struct component_ref;
struct modeling;
struct description;
status add_cpp_component_ref(const char* buffer,
modeling& mod,
component& parent) noexcept;
status add_file_component_ref(const char* buffer,
modeling& mod,
component& parent) noexcept;
status add_cpp_component_ref(const char* buffer,
modeling& mod,
component& parent,
component_ref& compo_ref) noexcept;
status add_file_component_ref(const char* buffer,
modeling& mod,
component& parent,
component_ref& compo_ref) noexcept;
status build_simulation(const modeling& mod, simulation& sim) noexcept;
......
......@@ -937,18 +937,15 @@ static component* find_cpp_component(modeling& mod,
irt_unreachable();
}
status add_file_component_ref(const char* file_path,
modeling& mod,
component& parent) noexcept
status add_file_component_ref(const char* file_path,
modeling& mod,
component& parent,
component_ref& compo_ref) noexcept
{
irt_return_if_fail(mod.component_refs.can_alloc(),
status::io_file_format_error);
irt_return_if_fail(parent.children.can_alloc(),
status::io_file_format_error);
auto* file_compo = find_file_component(mod, file_path);
auto& compo_ref = mod.component_refs.alloc();
compo_ref.id = mod.components.get_id(*file_compo);
parent.children.emplace_back(mod.component_refs.get_id(compo_ref));
......@@ -956,9 +953,10 @@ status add_file_component_ref(const char* file_path,
return status::success;
}
status add_cpp_component_ref(const char* buffer,
modeling& mod,
component& parent) noexcept
status add_cpp_component_ref(const char* buffer,
modeling& mod,
component& parent,
component_ref& compo_ref) noexcept
{
struct cpp_component_entry
{
......@@ -999,14 +997,10 @@ status add_cpp_component_ref(const char* buffer,
if (it == std::end(tab) || std::strcmp(it->name, buffer))
return status::io_file_format_error;
irt_return_if_fail(mod.component_refs.can_alloc(),
status::io_file_format_error);
irt_return_if_fail(parent.children.can_alloc(),
status::io_file_format_error);
auto* cpp_compo = find_cpp_component(mod, it->type);
auto& compo_ref = mod.component_refs.alloc();
compo_ref.id = mod.components.get_id(*cpp_compo);
parent.children.emplace_back(mod.component_refs.get_id(compo_ref));
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment