From a503a658b9092f1479abf4d961d37654d0abdc74 Mon Sep 17 00:00:00 2001 From: Martijn Govers Date: Tue, 16 Jul 2024 13:29:35 +0200 Subject: [PATCH] resolve comment, fix sonar Signed-off-by: Martijn Govers --- .../power_grid_model/auxiliary/dataset.hpp | 71 +++++++++---------- src/power_grid_model/data_types.py | 2 +- tests/cpp_unit_tests/test_dataset.cpp | 6 +- 3 files changed, 37 insertions(+), 42 deletions(-) diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/auxiliary/dataset.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/auxiliary/dataset.hpp index 561182f51..b0acc534d 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/auxiliary/dataset.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/auxiliary/dataset.hpp @@ -230,54 +230,49 @@ template class Dataset { throw DatasetError{"For a non-uniform buffer, indptr should be supplied!\n"}; } if constexpr (std::same_as) { - auto indptr_span = get_indptr_span(indptr); - if (auto decreasing_found = std::ranges::adjacent_find(indptr_span, std::greater<>()); - decreasing_found != indptr_span.end()) { - throw DatasetError{"For a non-uniform buffer, indptr must be increasing!\n"}; - } - if (indptr_span.front() < 0 || indptr_span.back() > total_elements) { - throw DatasetError{"For a non-uniform buffer, the elements of indptr cannot be less than 0 or " - "greater than total_elements!\n"}; + if (indptr[0] != 0 || indptr[batch_size()] != total_elements) { + throw DatasetError{ + "For a non-uniform buffer, indptr should begin with 0 and end with total_elements!\n"}; } + } else if (indptr) { + throw DatasetError{"For a uniform buffer, indptr should be nullptr!\n"}; } - } else if (indptr) { - throw DatasetError{"For a uniform buffer, indptr should be nullptr!\n"}; } - } - void add_component_info_impl(std::string_view component, Idx elements_per_scenario, Idx total_elements) { - if (find_component(component) >= 0) { - throw DatasetError{"Cannot have duplicated components!\n"}; + void add_component_info_impl(std::string_view component, Idx elements_per_scenario, Idx total_elements) { + if (find_component(component) >= 0) { + throw DatasetError{"Cannot have duplicated components!\n"}; + } + check_uniform_integrity(elements_per_scenario, total_elements); + dataset_info_.component_info.push_back( + {&dataset_info_.dataset->get_component(component), elements_per_scenario, total_elements}); + buffers_.push_back(Buffer{}); } - check_uniform_integrity(elements_per_scenario, total_elements); - dataset_info_.component_info.push_back( - {&dataset_info_.dataset->get_component(component), elements_per_scenario, total_elements}); - buffers_.push_back(Buffer{}); - } - template std::span get_buffer_span_impl(Idx scenario, Idx component_idx) const { - // return empty span if the component does not exist - if (component_idx < 0) { - return {}; - } - // return span based on uniform or non-uniform buffer - ComponentInfo const& info = dataset_info_.component_info[component_idx]; - Buffer const& buffer = buffers_[component_idx]; - auto const ptr = reinterpret_cast(buffer.data); - if (scenario < 0) { - return std::span{ptr, ptr + info.total_elements}; - } - if (info.elements_per_scenario < 0) { - return std::span{ptr + buffer.indptr[scenario], ptr + buffer.indptr[scenario + 1]}; + template std::span get_buffer_span_impl(Idx scenario, Idx component_idx) const { + // return empty span if the component does not exist + if (component_idx < 0) { + return {}; + } + // return span based on uniform or non-uniform buffer + ComponentInfo const& info = dataset_info_.component_info[component_idx]; + Buffer const& buffer = buffers_[component_idx]; + auto const ptr = reinterpret_cast(buffer.data); + if (scenario < 0) { + return std::span{ptr, ptr + info.total_elements}; + } + if (info.elements_per_scenario < 0) { + return std::span{ptr + buffer.indptr[scenario], ptr + buffer.indptr[scenario + 1]}; + } + return std::span{ptr + info.elements_per_scenario * scenario, + ptr + info.elements_per_scenario * (scenario + 1)}; } - return std::span{ptr + info.elements_per_scenario * scenario, - ptr + info.elements_per_scenario * (scenario + 1)}; - } -}; + }; } // namespace meta_data -template using Dataset = meta_data::Dataset; +template +using Dataset = meta_data::Dataset; using ConstDataset = Dataset; using MutableDataset = Dataset; using WritableDataset = Dataset; diff --git a/src/power_grid_model/data_types.py b/src/power_grid_model/data_types.py index 4ad7f43f4..98d351f1c 100644 --- a/src/power_grid_model/data_types.py +++ b/src/power_grid_model/data_types.py @@ -40,7 +40,7 @@ - The elements are the indices in the data that point to the first element of that scenario. - The last element is one after the data index of the last element of the last scenario. - - Usually, the last element will therefore be the size of the data. + - The first element and last element will therefore be 0 and the size of the data, respectively. - Examples: diff --git a/tests/cpp_unit_tests/test_dataset.cpp b/tests/cpp_unit_tests/test_dataset.cpp index 78c7db750..6e2f8834c 100644 --- a/tests/cpp_unit_tests/test_dataset.cpp +++ b/tests/cpp_unit_tests/test_dataset.cpp @@ -201,10 +201,10 @@ TEST_CASE_TEMPLATE("Test dataset (common)", DatasetType, ConstDataset, MutableDa CAPTURE(std::string_view{dataset_type.name}); for (auto const batch_size : {0, 1, 2}) { - CAPTURE(batch_size); - auto dataset = create_dataset(true, batch_size, dataset_type); - SUBCASE("No component added") { + CAPTURE(batch_size); + auto dataset = create_dataset(true, batch_size, dataset_type); + CHECK(dataset.n_components() == 0); CHECK_FALSE(dataset.contains_component(A::name)); CHECK(dataset.get_description().component_info.empty());