Skip to content

Commit c70d1ee

Browse files
Merge pull request #1181 from PowerGridModel/feature/fix-clang-tidy-2
Clang-tidy: fix clang-tidy-19 issues
2 parents 0b1088e + ae04ca0 commit c70d1ee

File tree

5 files changed

+60
-28
lines changed

5 files changed

+60
-28
lines changed

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,26 @@ namespace detail {
1212

1313
template <typename Tuple, class Functor, std::size_t... Indices>
1414
constexpr void run_functor_with_tuple_index_return_void(Functor&& functor, std::index_sequence<Indices...> /*unused*/) {
15-
(std::forward<Functor>(functor).template operator()<std::tuple_element_t<Indices, Tuple>>(), ...);
15+
if constexpr (sizeof...(Indices) == 1) {
16+
(std::forward<Functor>(functor).template operator()<std::tuple_element_t<Indices, Tuple>>(), ...);
17+
} else {
18+
(functor.template operator()<std::tuple_element_t<Indices, Tuple>>(), ...);
19+
capturing::into_the_void(std::forward<Functor>(functor));
20+
}
21+
}
22+
23+
template <typename Tuple, class Functor, std::size_t... Indices>
24+
constexpr auto run_functor_with_tuple_index_return_array(Functor&& functor,
25+
std::index_sequence<Indices...> /*unused*/) {
26+
if constexpr (sizeof...(Indices) == 1) {
27+
return std::array {
28+
std::forward<Functor>(functor).template operator()<std::tuple_element_t<Indices, Tuple>>()...
29+
};
30+
} else {
31+
auto result = std::array { functor.template operator()<std::tuple_element_t<Indices, Tuple>>()... };
32+
capturing::into_the_void(std::forward<Functor>(functor));
33+
return result;
34+
}
1635
}
1736

1837
} // namespace detail
@@ -25,4 +44,9 @@ template <typename Tuple, class Functor> constexpr void run_functor_with_tuple_r
2544
std::make_index_sequence<std::tuple_size_v<Tuple>>{});
2645
}
2746

47+
template <typename Tuple, class Functor> constexpr auto run_functor_with_tuple_return_array(Functor&& functor) {
48+
return detail::run_functor_with_tuple_index_return_array<Tuple>(
49+
std::forward<Functor>(functor), std::make_index_sequence<std::tuple_size_v<Tuple>>{});
50+
}
51+
2852
} // namespace power_grid_model::main_core::utils

power_grid_model_c/power_grid_model/include/power_grid_model/main_core/main_model_type.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ concept validate_component_types_c =
6767

6868
template <class T, class U> class MainModelType;
6969

70-
// TODO: discussion on checking dependent types can also be done here.
7170
template <class... ExtraRetrievableType, class... ComponentType>
7271
requires detail::validate_component_types_c<ComponentList<ComponentType...>>
7372
class MainModelType<ExtraRetrievableTypes<ExtraRetrievableType...>, ComponentList<ComponentType...>> {
@@ -111,11 +110,11 @@ class MainModelType<ExtraRetrievableTypes<ExtraRetrievableType...>, ComponentLis
111110
using ComponentFlags = std::array<bool, n_types>;
112111

113112
template <class Functor> static constexpr void run_functor_with_all_component_types_return_void(Functor&& functor) {
114-
(std::forward<Functor>(functor).template operator()<ComponentType>(), ...);
113+
return utils::run_functor_with_tuple_return_void<ComponentTypesTuple>(std::forward<Functor>(functor));
115114
}
116115
template <class Functor>
117116
static constexpr auto run_functor_with_all_component_types_return_array(Functor&& functor) {
118-
return std::array { std::forward<Functor>(functor).template operator()<ComponentType>()... };
117+
return utils::run_functor_with_tuple_return_array<ComponentTypesTuple>(std::forward<Functor>(functor));
119118
}
120119
};
121120

power_grid_model_c/power_grid_model/include/power_grid_model/math_solver/observability.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ ObservabilitySensorsResult scan_network_sensors(MeasuredValues<sym> const& measu
9494
// we only need one flow sensor, so the loop will break
9595
Idx const branch = element.idx;
9696
Idx const neighbour_bus = y_bus_structure.col_indices[ybus_index];
97-
BusNeighbourhoodInfo::neighbour const neighbour_info{neighbour_bus,
98-
ConnectivityStatus::has_no_measurement};
97+
BusNeighbourhoodInfo::neighbour const neighbour_info{.bus = neighbour_bus,
98+
.status = ConnectivityStatus::has_no_measurement};
9999
bus_neighbourhood_info[bus].direct_neighbours.push_back(neighbour_info);
100100
if (has_flow_sensor(branch) && is_branch_connected(branch)) {
101101
result.flow_sensors[ybus_index] = 1;
@@ -231,7 +231,7 @@ inline void complete_bidirectional_neighbourhood_info(std::vector<BusNeighbourho
231231
auto it = std::ranges::find_if(
232232
reverse_neighbour_list, [&bus](auto const& reverse_neighbour) { return reverse_neighbour.bus == bus; });
233233
if (it == reverse_neighbour_list.end()) {
234-
BusNeighbourhoodInfo::neighbour const reverse_neighbour_info{bus, neighbour.status};
234+
BusNeighbourhoodInfo::neighbour const reverse_neighbour_info{.bus = bus, .status = neighbour.status};
235235
reverse_neighbour_list.push_back(reverse_neighbour_info);
236236
}
237237
}

tests/cpp_unit_tests/test_observability.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,8 @@ TEST_CASE("Test Observability - complete_bidirectional_neighbourhood_info") {
568568
// Initialize test data
569569
neighbour_list[0].bus = 0;
570570
neighbour_list[0].status = has_no_measurement;
571-
neighbour_list[0].direct_neighbours = {{1, has_no_measurement}, {2, node_measured}};
571+
neighbour_list[0].direct_neighbours = {{.bus = 1, .status = has_no_measurement},
572+
{.bus = 2, .status = node_measured}};
572573

573574
neighbour_list[1].bus = 1;
574575
neighbour_list[1].status = node_measured;
@@ -615,17 +616,18 @@ TEST_CASE("Test Observability - complete_bidirectional_neighbourhood_info") {
615616
// Bus 0 connects to buses 1 and 3
616617
neighbour_list[0].bus = 0;
617618
neighbour_list[0].status = node_measured;
618-
neighbour_list[0].direct_neighbours = {{1, branch_native_measurement_unused}, {3, has_no_measurement}};
619+
neighbour_list[0].direct_neighbours = {{.bus = 1, .status = branch_native_measurement_unused},
620+
{.bus = 3, .status = has_no_measurement}};
619621

620622
// Bus 1 connects to bus 2 (but not back to 0 yet)
621623
neighbour_list[1].bus = 1;
622624
neighbour_list[1].status = has_no_measurement;
623-
neighbour_list[1].direct_neighbours = {{2, branch_discovered_with_from_node_sensor}};
625+
neighbour_list[1].direct_neighbours = {{.bus = 2, .status = branch_discovered_with_from_node_sensor}};
624626

625627
// Bus 2 has existing connection to bus 3
626628
neighbour_list[2].bus = 2;
627629
neighbour_list[2].status = branch_discovered_with_to_node_sensor;
628-
neighbour_list[2].direct_neighbours = {{3, branch_native_measurement_consumed}};
630+
neighbour_list[2].direct_neighbours = {{.bus = 3, .status = branch_native_measurement_consumed}};
629631

630632
// Bus 3 initially has no connections
631633
neighbour_list[3].bus = 3;
@@ -700,15 +702,17 @@ TEST_CASE("Test Observability - complete_bidirectional_neighbourhood_info") {
700702
// Initialize with some connections already bidirectional
701703
neighbour_list[0].bus = 0;
702704
neighbour_list[0].status = has_no_measurement;
703-
neighbour_list[0].direct_neighbours = {{1, has_no_measurement}, {2, node_measured}};
705+
neighbour_list[0].direct_neighbours = {{.bus = 1, .status = has_no_measurement},
706+
{.bus = 2, .status = node_measured}};
704707

705708
neighbour_list[1].bus = 1;
706709
neighbour_list[1].status = node_measured;
707-
neighbour_list[1].direct_neighbours = {{0, has_no_measurement}, {2, branch_native_measurement_unused}};
710+
neighbour_list[1].direct_neighbours = {{.bus = 0, .status = has_no_measurement},
711+
{.bus = 2, .status = branch_native_measurement_unused}};
708712

709713
neighbour_list[2].bus = 2;
710714
neighbour_list[2].status = node_measured;
711-
neighbour_list[2].direct_neighbours = {{1, branch_native_measurement_unused}};
715+
neighbour_list[2].direct_neighbours = {{.bus = 1, .status = branch_native_measurement_unused}};
712716

713717
// Test the function
714718
complete_bidirectional_neighbourhood_info(neighbour_list);

tests/cpp_unit_tests/test_y_bus.cpp

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -434,13 +434,13 @@ TEST_CASE("Test counting_sort_element") {
434434
SUBCASE("Test basic sorting") {
435435
// Create test data: elements at various matrix positions
436436
std::vector<YBusElementMap> vec = {
437-
{{2, 1}, {bft, 5}}, // pos (2,1)
438-
{{0, 0}, {bff, 0}}, // pos (0,0)
439-
{{1, 2}, {btf, 3}}, // pos (1,2)
440-
{{0, 1}, {bft, 1}}, // pos (0,1)
441-
{{2, 1}, {shunt, 6}}, // pos (2,1) - same position as first
442-
{{1, 0}, {btf, 2}}, // pos (1,0)
443-
{{2, 2}, {btt, 7}}, // pos (2,2)
437+
{.pos = {2, 1}, .element = {.element_type = bft, .idx = 5}}, // pos (2,1)
438+
{.pos = {0, 0}, .element = {.element_type = bff, .idx = 0}}, // pos (0,0)
439+
{.pos = {1, 2}, .element = {.element_type = btf, .idx = 3}}, // pos (1,2)
440+
{.pos = {0, 1}, .element = {.element_type = bft, .idx = 1}}, // pos (0,1)
441+
{.pos = {2, 1}, .element = {.element_type = shunt, .idx = 6}}, // pos (2,1) - same position as first
442+
{.pos = {1, 0}, .element = {.element_type = btf, .idx = 2}}, // pos (1,0)
443+
{.pos = {2, 2}, .element = {.element_type = btt, .idx = 7}}, // pos (2,2)
444444
};
445445

446446
// Expected sorted order: by row first, then by column
@@ -465,7 +465,7 @@ TEST_CASE("Test counting_sort_element") {
465465
}
466466

467467
SUBCASE("Test with single bus") {
468-
std::vector<YBusElementMap> vec = {{{0, 0}, {shunt, 10}}};
468+
std::vector<YBusElementMap> vec = {{.pos = {0, 0}, .element = {.element_type = shunt, .idx = 10}}};
469469

470470
counting_sort_element(vec, 1);
471471

@@ -483,9 +483,9 @@ TEST_CASE("Test counting_sort_element") {
483483

484484
SUBCASE("Test stability - elements with same position maintain relative order") {
485485
std::vector<YBusElementMap> vec = {
486-
{{1, 1}, {bff, 100}},
487-
{{1, 1}, {bft, 200}},
488-
{{1, 1}, {shunt, 300}},
486+
{.pos = {1, 1}, .element = {.element_type = bff, .idx = 100}},
487+
{.pos = {1, 1}, .element = {.element_type = bft, .idx = 200}},
488+
{.pos = {1, 1}, .element = {.element_type = shunt, .idx = 300}},
489489
};
490490

491491
counting_sort_element(vec, 2);
@@ -510,7 +510,7 @@ TEST_CASE("Test counting_sort_element") {
510510
Idx const row = i / n_bus;
511511
Idx const col = i % n_bus;
512512
if ((row + col) % 3 == 0) { // Sparse pattern
513-
vec.push_back({{row, col}, {bff, row * n_bus + col}});
513+
vec.push_back({.pos = {row, col}, .element = {.element_type = bff, .idx = row * n_bus + col}});
514514
}
515515
}
516516

@@ -535,8 +535,13 @@ TEST_CASE("Test counting_sort_element") {
535535

536536
SUBCASE("Test all YBusElementType values") {
537537
std::vector<YBusElementMap> vec = {
538-
{{1, 1}, {fill_in_tf, 6}}, {{0, 1}, {bft, 1}}, {{1, 0}, {btf, 2}}, {{0, 0}, {bff, 0}},
539-
{{1, 1}, {btt, 3}}, {{2, 2}, {shunt, 4}}, {{1, 2}, {fill_in_ft, 5}},
538+
{.pos = {1, 1}, .element = {.element_type = fill_in_tf, .idx = 6}},
539+
{.pos = {0, 1}, .element = {.element_type = bft, .idx = 1}},
540+
{.pos = {1, 0}, .element = {.element_type = btf, .idx = 2}},
541+
{.pos = {0, 0}, .element = {.element_type = bff, .idx = 0}},
542+
{.pos = {1, 1}, .element = {.element_type = btt, .idx = 3}},
543+
{.pos = {2, 2}, .element = {.element_type = shunt, .idx = 4}},
544+
{.pos = {1, 2}, .element = {.element_type = fill_in_ft, .idx = 5}},
540545
};
541546

542547
counting_sort_element(vec, 3);

0 commit comments

Comments
 (0)