Skip to content

Commit 9f196f5

Browse files
committed
remove most variadics
Signed-off-by: Nitish Bharambe <[email protected]>
1 parent df398f1 commit 9f196f5

File tree

5 files changed

+100
-94
lines changed

5 files changed

+100
-94
lines changed

power_grid_model_c/power_grid_model/include/power_grid_model/job_adapter.hpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515

1616
namespace power_grid_model {
1717

18-
template <class MainModel, typename... ComponentType> class JobAdapter;
18+
template <class MainModel> class JobAdapter;
1919

20-
template <class MainModel, class... ComponentType>
21-
class JobAdapter<MainModel, ComponentList<ComponentType...>>
22-
: public JobInterface<JobAdapter<MainModel, ComponentList<ComponentType...>>> {
20+
template <class MainModel> class JobAdapter : public JobInterface<JobAdapter<MainModel>> {
2321
public:
22+
using MainModelType = typename MainModel::MainModelType;
23+
2424
JobAdapter(std::reference_wrapper<MainModel> model_reference,
2525
std::reference_wrapper<MainModelOptions const> options)
2626
: model_reference_{model_reference}, options_{options} {}
@@ -76,12 +76,12 @@ class JobAdapter<MainModel, ComponentList<ComponentType...>>
7676
std::reference_wrapper<MainModel> model_reference_;
7777
std::reference_wrapper<MainModelOptions const> options_;
7878

79-
main_core::utils::ComponentFlags<ComponentType...> components_to_update_{};
80-
main_core::update::independence::UpdateIndependence<ComponentType...> update_independence_{};
81-
main_core::utils::ComponentFlags<ComponentType...> independence_flags_{};
82-
std::shared_ptr<main_core::utils::SequenceIdx<ComponentType...>> all_scenarios_sequence_;
79+
typename MainModelType::ComponentFlags components_to_update_{};
80+
typename MainModelType::UpdateIndependence update_independence_{};
81+
typename MainModelType::ComponentFlags independence_flags_{};
82+
std::shared_ptr<typename MainModelType::SequenceIdx> all_scenarios_sequence_;
8383
// current_scenario_sequence_cache_ is calculated per scenario, so it is excluded from the constructors.
84-
main_core::utils::SequenceIdx<ComponentType...> current_scenario_sequence_cache_{};
84+
typename MainModelType::SequenceIdx current_scenario_sequence_cache_{};
8585

8686
std::mutex calculation_info_mutex_;
8787

@@ -112,17 +112,17 @@ class JobAdapter<MainModel, ComponentList<ComponentType...>>
112112
// cache component update order where possible.
113113
// the order for a cacheable (independent) component by definition is the same across all scenarios
114114
components_to_update_ = model_reference_.get().get_components_to_update(update_data);
115-
update_independence_ = main_core::update::independence::check_update_independence<ComponentType...>(
115+
update_independence_ = main_core::update::independence::check_update_independence<MainModelType>(
116116
model_reference_.get().state(), update_data);
117117
std::ranges::transform(update_independence_, independence_flags_.begin(),
118118
[](auto const& comp) { return comp.is_independent(); });
119-
all_scenarios_sequence_ = std::make_shared<main_core::utils::SequenceIdx<ComponentType...>>(
120-
main_core::update::get_all_sequence_idx_map<ComponentType...>(
119+
all_scenarios_sequence_ = std::make_shared<typename MainModelType::SequenceIdx>(
120+
main_core::update::get_all_sequence_idx_map<MainModelType>(
121121
model_reference_.get().state(), update_data, 0, components_to_update_, update_independence_, false));
122122
}
123123

124124
void setup_impl(ConstDataset const& update_data, Idx scenario_idx) {
125-
current_scenario_sequence_cache_ = main_core::update::get_all_sequence_idx_map<ComponentType...>(
125+
current_scenario_sequence_cache_ = main_core::update::get_all_sequence_idx_map<MainModelType>(
126126
model_reference_.get().state(), update_data, scenario_idx, components_to_update_, update_independence_,
127127
true);
128128
auto const current_scenario_sequence = get_current_scenario_sequence_view_();
@@ -144,8 +144,8 @@ class JobAdapter<MainModel, ComponentList<ComponentType...>>
144144
}
145145

146146
auto get_current_scenario_sequence_view_() const {
147-
return main_core::utils::run_functor_with_all_types_return_array<ComponentType...>([this]<typename CT>() {
148-
constexpr auto comp_idx = main_core::utils::index_of_component<CT, ComponentType...>;
147+
return MainModelType::run_functor_with_all_component_types_return_array([this]<typename CT>() {
148+
constexpr auto comp_idx = MainModelType::template index_of_component<CT>;
149149
if (std::get<comp_idx>(independence_flags_)) {
150150
return std::span<Idx2D const>{std::get<comp_idx>(*all_scenarios_sequence_)};
151151
}

power_grid_model_c/power_grid_model/include/power_grid_model/main_core/core_utils.hpp

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,34 @@
1313
namespace power_grid_model::main_core::utils {
1414

1515
constexpr Idx invalid_index{-1};
16+
17+
struct UpdateCompProperties {
18+
bool has_any_elements{false}; // whether the component has any elements in the update data
19+
bool ids_all_na{false}; // whether all ids are all NA
20+
bool ids_part_na{false}; // whether some ids are NA but some are not
21+
bool dense{false}; // whether the component is dense
22+
bool uniform{false}; // whether the component is uniform
23+
bool is_columnar{false}; // whether the component is columnar
24+
bool update_ids_match{false}; // whether the ids match
25+
Idx elements_ps_in_update{utils::invalid_index}; // count of elements for this component per scenario in update
26+
Idx elements_in_base{utils::invalid_index}; // count of elements for this component per scenario in input
27+
28+
constexpr bool no_id() const { return !has_any_elements || ids_all_na; }
29+
constexpr bool qualify_for_optional_id() const {
30+
return update_ids_match && ids_all_na && uniform && elements_ps_in_update == elements_in_base;
31+
}
32+
constexpr bool provided_ids_valid() const {
33+
return is_empty_component() || (update_ids_match && !(ids_all_na || ids_part_na));
34+
}
35+
constexpr bool is_empty_component() const { return !has_any_elements; }
36+
constexpr bool is_independent() const { return qualify_for_optional_id() || provided_ids_valid(); }
37+
constexpr Idx get_n_elements() const {
38+
assert(uniform || elements_ps_in_update == utils::invalid_index);
39+
40+
return qualify_for_optional_id() ? elements_ps_in_update : na_Idx;
41+
}
42+
};
43+
1644
/////////////////// To remove ///////////////////
1745

1846
template <class... ComponentTypes> constexpr size_t n_types = sizeof...(ComponentTypes);
@@ -106,7 +134,7 @@ struct MainModelType<ExtraRetrievableTypes<ExtraRetrievableType...>, ComponentLi
106134
// Update related types
107135
using OwnedUpdateDataset = std::tuple<std::vector<typename ComponentType::UpdateType>...>;
108136
using SequenceIdxView = std::array<std::span<Idx2D const>, n_types>;
109-
// using UpdateIndependence = std::array<UpdateCompProperties, n_types>;
137+
using UpdateIndependence = std::array<UpdateCompProperties, n_types>;
110138
using SequenceIdx = std::array<std::vector<Idx2D>, n_types>;
111139
using ComponentFlags = std::array<bool, n_types>;
112140

power_grid_model_c/power_grid_model/include/power_grid_model/main_core/update.hpp

Lines changed: 13 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -45,32 +45,8 @@ template <typename T> bool check_id_na(T const& obj) {
4545
} // namespace detail
4646

4747
namespace independence {
48-
struct UpdateCompProperties {
49-
bool has_any_elements{false}; // whether the component has any elements in the update data
50-
bool ids_all_na{false}; // whether all ids are all NA
51-
bool ids_part_na{false}; // whether some ids are NA but some are not
52-
bool dense{false}; // whether the component is dense
53-
bool uniform{false}; // whether the component is uniform
54-
bool is_columnar{false}; // whether the component is columnar
55-
bool update_ids_match{false}; // whether the ids match
56-
Idx elements_ps_in_update{utils::invalid_index}; // count of elements for this component per scenario in update
57-
Idx elements_in_base{utils::invalid_index}; // count of elements for this component per scenario in input
58-
59-
constexpr bool no_id() const { return !has_any_elements || ids_all_na; }
60-
constexpr bool qualify_for_optional_id() const {
61-
return update_ids_match && ids_all_na && uniform && elements_ps_in_update == elements_in_base;
62-
}
63-
constexpr bool provided_ids_valid() const {
64-
return is_empty_component() || (update_ids_match && !(ids_all_na || ids_part_na));
65-
}
66-
constexpr bool is_empty_component() const { return !has_any_elements; }
67-
constexpr bool is_independent() const { return qualify_for_optional_id() || provided_ids_valid(); }
68-
constexpr Idx get_n_elements() const {
69-
assert(uniform || elements_ps_in_update == utils::invalid_index);
7048

71-
return qualify_for_optional_id() ? elements_ps_in_update : na_Idx;
72-
}
73-
};
49+
using UpdateCompProperties = utils::UpdateCompProperties;
7450

7551
template <typename CompType> void process_buffer_span(auto const& all_spans, UpdateCompProperties& properties) {
7652
properties.ids_all_na = std::ranges::all_of(all_spans, [](auto const& vec) {
@@ -127,9 +103,6 @@ UpdateCompProperties check_component_independence(ConstDataset const& update_dat
127103
return properties;
128104
}
129105

130-
template <class... ComponentTypes>
131-
using UpdateIndependence = std::array<UpdateCompProperties, utils::n_types<ComponentTypes...>>;
132-
133106
inline void validate_update_data_independence(UpdateCompProperties const& comp, std::string const& comp_name) {
134107
if (comp.is_empty_component()) {
135108
return; // empty dataset is still supported
@@ -150,10 +123,10 @@ inline void validate_update_data_independence(UpdateCompProperties const& comp,
150123
}
151124
}
152125

153-
template <class... ComponentTypes, class ComponentContainer>
154-
UpdateIndependence<ComponentTypes...> check_update_independence(MainModelState<ComponentContainer> const& state,
155-
ConstDataset const& update_data) {
156-
return utils::run_functor_with_all_types_return_array<ComponentTypes...>(
126+
template <typename MainModelType>
127+
typename MainModelType::UpdateIndependence
128+
check_update_independence(typename MainModelType::MainModelState const& state, ConstDataset const& update_data) {
129+
return MainModelType::run_functor_with_all_component_types_return_array(
157130
[&state, &update_data]<typename CompType>() {
158131
auto const n_component = state.components.template size<CompType>();
159132
return check_component_independence<CompType>(update_data, n_component);
@@ -214,22 +187,22 @@ std::vector<Idx2D> get_component_sequence(MainModelState<ComponentContainer> con
214187
}
215188
} // namespace detail
216189

217-
template <class... ComponentTypes, class ComponentContainer>
218-
utils::SequenceIdx<ComponentTypes...>
219-
get_all_sequence_idx_map(MainModelState<ComponentContainer> const& state, ConstDataset const& update_data,
220-
Idx scenario_idx, utils::ComponentFlags<ComponentTypes...> const& components_to_store,
221-
independence::UpdateIndependence<ComponentTypes...> const& independence, bool cached) {
222-
return utils::run_functor_with_all_types_return_array<ComponentTypes...>(
190+
template <class MainModelType>
191+
typename MainModelType::SequenceIdx
192+
get_all_sequence_idx_map(typename MainModelType::MainModelState const& state, ConstDataset const& update_data,
193+
Idx scenario_idx, typename MainModelType::ComponentFlags const& components_to_store,
194+
typename MainModelType::UpdateIndependence const& independence, bool cached) {
195+
return MainModelType::run_functor_with_all_component_types_return_array(
223196
[&state, &update_data, scenario_idx, &components_to_store, &independence, cached]<typename CompType>() {
224197
auto const component_properties =
225-
std::get<utils::index_of_component<CompType, ComponentTypes...>>(independence);
198+
std::get<MainModelType::template index_of_component<CompType>>(independence);
226199
// The sequence for the independent components is cached (true). For the remaining components, the sequence
227200
// cannot be cached (false), so the independence flags are inverted to not return an empty sequence when
228201
// this is the case.
229202

230203
if (bool const component_independence = cached != component_properties.is_independent();
231204
!component_independence ||
232-
!std::get<utils::index_of_component<CompType, ComponentTypes...>>(components_to_store)) {
205+
!std::get<MainModelType::template index_of_component<CompType>>(components_to_store)) {
233206
return std::vector<Idx2D>{};
234207
}
235208
independence::validate_update_data_independence(component_properties, CompType::name);

power_grid_model_c/power_grid_model/include/power_grid_model/main_model.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class MainModel {
7878
*/
7979
BatchParameter calculate(Options const& options, MutableDataset const& result_data,
8080
ConstDataset const& update_data) {
81-
JobAdapter<Impl, AllComponents> adapter{std::ref(impl()), std::ref(options)};
81+
JobAdapter<Impl> adapter{std::ref(impl()), std::ref(options)};
8282
return JobDispatch::batch_calculation(adapter, result_data, update_data, options.threading);
8383
}
8484

0 commit comments

Comments
 (0)