Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Checks: '
-*,
boost-*,
-boost-use-ranges,
bugprone-*,
-bugprone-easily-swappable-parameters,
-bugprone-exception-escape,
Expand Down Expand Up @@ -55,6 +56,8 @@ readability-*,
-readability-function-cognitive-complexity,
-readability-identifier-length,
-readability-magic-numbers,
-readability-math-missing-parentheses,
-readability-redundant-member-init,
-bugprone-unchecked-optional-access,
'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -535,8 +535,9 @@ template <dataset_type_tag dataset_type_> class Dataset {
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});
dataset_info_.component_info.push_back({.component = &dataset_info_.dataset->get_component(component),
.elements_per_scenario = elements_per_scenario,
.total_elements = total_elements});
buffers_.push_back(Buffer{});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ template <class T> struct DefaultErrorVisitor : DefaultNullVisitor {
bool throw_error() { throw SerializationError{(static_cast<T&>(*this)).get_err_msg()}; }

std::string get_err_msg() { return std::string{T::static_err_msg}; }

private:
DefaultErrorVisitor() = default;
friend T; // CRTP compliance
};

struct visit_map_t;
Expand Down Expand Up @@ -238,6 +242,8 @@ struct MapArrayVisitor : DefaultErrorVisitor<MapArrayVisitor<map_array>> {
assert(size == 0);
return true;
}

MapArrayVisitor() = default;
};

struct StringVisitor : DefaultErrorVisitor<StringVisitor> {
Expand All @@ -248,6 +254,8 @@ struct StringVisitor : DefaultErrorVisitor<StringVisitor> {
str = {v, size};
return true;
}

StringVisitor() = default;
};

struct BoolVisitor : DefaultErrorVisitor<BoolVisitor> {
Expand All @@ -258,6 +266,8 @@ struct BoolVisitor : DefaultErrorVisitor<BoolVisitor> {
value = v;
return true;
}

BoolVisitor() = default;
};

template <class T> struct ValueVisitor;
Expand All @@ -282,6 +292,8 @@ template <std::integral T> struct ValueVisitor<T> : DefaultErrorVisitor<ValueVis
value = static_cast<T>(v);
return true;
}

ValueVisitor(T& v) : DefaultErrorVisitor<ValueVisitor<T>>{}, value{v} {}
};

template <> struct ValueVisitor<double> : DefaultErrorVisitor<ValueVisitor<double>> {
Expand All @@ -306,6 +318,8 @@ template <> struct ValueVisitor<double> : DefaultErrorVisitor<ValueVisitor<doubl
value = v;
return true;
}

ValueVisitor(double& v) : DefaultErrorVisitor<ValueVisitor<double>>{}, value{v} {}
};

template <> struct ValueVisitor<RealValue<asymmetric_t>> : DefaultErrorVisitor<ValueVisitor<RealValue<asymmetric_t>>> {
Expand Down Expand Up @@ -357,6 +371,8 @@ template <> struct ValueVisitor<RealValue<asymmetric_t>> : DefaultErrorVisitor<V
value[idx] = v;
return true;
}

ValueVisitor(RealValue<asymmetric_t>& v) : DefaultErrorVisitor<ValueVisitor<RealValue<asymmetric_t>>>{}, value{v} {}
};

} // namespace detail
Expand Down Expand Up @@ -659,7 +675,8 @@ class Deserializer {
size_t const scenario_offset = offset_;
// skip all the real content but check if it has map
bool const has_map = parse_skip_check_map();
count_per_scenario.push_back({component_key_, component_size, scenario_offset, has_map});
count_per_scenario.push_back(
{.component = component_key_, .size = component_size, .offset = scenario_offset, .has_map = has_map});
}
component_key_ = {};
return count_per_scenario;
Expand Down Expand Up @@ -708,8 +725,8 @@ class Deserializer {
elements_per_scenario * batch_size; // multiply
handler.add_component_info(component_key_, elements_per_scenario, total_elements);
// check if all scenarios only contain array data
bool const only_values_in_data = std::none_of(component_byte_meta.cbegin(), component_byte_meta.cend(),
[](auto const& x) { return x.has_map; });
bool const only_values_in_data =
std::ranges::none_of(component_byte_meta, [](auto const& x) { return x.has_map; });
msg_data_offsets_.push_back(std::move(component_byte_meta));
// enable attribute indications if possible
if (only_values_in_data) {
Expand Down Expand Up @@ -943,7 +960,7 @@ class Deserializer {

ctype_func_selector(attribute.ctype, [&buffer_view, &component, &attribute, this]<class T> {
ValueVisitor<T> visitor{
{}, attribute.get_attribute<T>(component.advance_ptr(buffer_view.buffer->data, buffer_view.idx))};
attribute.get_attribute<T>(component.advance_ptr(buffer_view.buffer->data, buffer_view.idx))};
msgpack::parse(data_, size_, offset_, visitor);
});
}
Expand All @@ -954,7 +971,7 @@ class Deserializer {
assert(buffer.meta_attribute != nullptr);

ctype_func_selector(buffer.meta_attribute->ctype, [&buffer, &idx, this]<class T> {
ValueVisitor<T> visitor{{}, *(reinterpret_cast<T*>(buffer.data) + idx)};
ValueVisitor<T> visitor{*(reinterpret_cast<T*>(buffer.data) + idx)};
msgpack::parse(data_, size_, offset_, visitor);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,19 @@ struct MapArray {
bool begin{true};
};

struct JsonConverter : msgpack::null_visitor {
template <typename T> struct NullVisitor : msgpack::null_visitor {
private:
NullVisitor() = default;
friend T; // CRTP compliance
};

struct JsonConverter : NullVisitor<JsonConverter> {
static constexpr char sep_char = ' ';

Idx indent{};
Idx max_indent_level{};
std::stringstream ss{}; // NOLINT(readability-redundant-member-init)
std::stack<MapArray> map_array{}; // NOLINT(readability-redundant-member-init)
std::stringstream ss{};
std::stack<MapArray> map_array{};

void print_indent() {
if (indent < 0) {
Expand Down Expand Up @@ -194,6 +200,9 @@ struct JsonConverter : msgpack::null_visitor {
ss << '}';
return true;
}

JsonConverter(Idx indent_, Idx max_indent_level_)
: NullVisitor<JsonConverter>{}, indent{indent_}, max_indent_level{max_indent_level_} {}
};

} // namespace json_converter
Expand Down Expand Up @@ -380,7 +389,7 @@ class Serializer {
std::string const& get_json(bool use_compact_list, Idx indent) {
if (json_buffer_.empty() || (use_compact_list_ != use_compact_list) || (json_indent_ != indent)) {
Idx const max_indent_level = dataset_handler_.is_batch() ? 4 : 3;
json_converter::JsonConverter visitor{{}, indent, max_indent_level};
json_converter::JsonConverter visitor{indent, max_indent_level};
auto const msgpack_data = get_msgpack(use_compact_list);
msgpack::parse(msgpack_data.data(), msgpack_data.size(), visitor);
json_buffer_ = visitor.ss.str();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct Idx2D {
};

struct Idx2DHash {
std::size_t operator()(const Idx2D& idx) const {
std::size_t operator()(Idx2D const& idx) const {
size_t const h1 = std::hash<Idx>{}(idx.group);
size_t const h2 = std::hash<Idx>{}(idx.pos);
return h1 ^ (h2 << 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "enum.hpp"

#include <exception>
#include <format>
#include <sstream>
#include <string>

Expand All @@ -23,7 +24,7 @@ inline auto to_string(std::integral auto x) { return std::to_string(x); }

class PowerGridError : public std::exception {
public:
void append_msg(std::string_view msg) { msg_ += msg; }
void append_msg(std::string_view msg) { msg_ = std::format("{}{}", msg_, msg); }
char const* what() const noexcept final { return msg_.c_str(); }

private:
Expand All @@ -44,17 +45,16 @@ class InvalidArguments : public PowerGridError {

template <class... Options>
requires(std::same_as<std::remove_cvref_t<Options>, TypeValuePair> && ...)
InvalidArguments(std::string const& method, Options&&... options)
InvalidArguments(std::string const& method, Options const&... options)
: InvalidArguments{method, "the following combination of options"} {
(append_msg(" " + std::forward<Options>(options).name + ": " + std::forward<Options>(options).value + "\n"),
...);
(append_msg(" " + options.name + ": " + options.value + "\n"), ...);
}
};

class MissingCaseForEnumError : public InvalidArguments {
public:
template <typename T>
MissingCaseForEnumError(std::string const& method, const T& value)
MissingCaseForEnumError(std::string const& method, T const& value)
: InvalidArguments{method, std::string{typeid(T).name()} + " #" + detail::to_string(static_cast<IntS>(value))} {
}
};
Expand Down Expand Up @@ -292,7 +292,7 @@ class UnreachableHit : public PowerGridError {
class TapSearchStrategyIncompatibleError : public InvalidArguments {
public:
template <typename T1, typename T2>
TapSearchStrategyIncompatibleError(std::string const& method, const T1& value1, const T2& value2)
TapSearchStrategyIncompatibleError(std::string const& method, T1 const& value1, T2 const& value2)
: InvalidArguments{
method, std::string{typeid(T1).name()} + " #" + detail::to_string(static_cast<IntS>(value1)) + " and " +
std::string{typeid(T2).name()} + " #" + detail::to_string(static_cast<IntS>(value2))} {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class SparseGroupedIdxVector {
: indptr_{sparse_group_elements.empty() ? IdxVector{0} : std::move(sparse_group_elements)} {
assert(size() >= 0);
assert(element_size() >= 0);
assert(std::is_sorted(std::begin(indptr_), std::end(indptr_)));
assert(std::ranges::is_sorted(indptr_));
}
SparseGroupedIdxVector(from_sparse_t /* tag */, IdxVector sparse_group_elements)
: SparseGroupedIdxVector{std::move(sparse_group_elements)} {}
Expand All @@ -154,46 +154,44 @@ class DenseGroupedIdxVector {
explicit constexpr GroupIterator(IdxVector const& dense_vector, Idx group)
: dense_vector_{&dense_vector},
group_{group},
group_range_{std::equal_range(std::cbegin(*dense_vector_), std::cend(*dense_vector_), group)} {}
group_range_{std::ranges::equal_range(*dense_vector_, group)} {}

private:
using group_iterator = IdxVector::const_iterator;

IdxVector const* dense_vector_{};
Idx group_{};
std::pair<group_iterator, group_iterator> group_range_;
std::ranges::subrange<group_iterator> group_range_;

friend class boost::iterator_core_access;

auto dereference() const -> iterator {
assert(dense_vector_ != nullptr);
return boost::counting_range(
narrow_cast<Idx>(std::distance(std::cbegin(*dense_vector_), group_range_.first)),
narrow_cast<Idx>(std::distance(std::cbegin(*dense_vector_), group_range_.second)));
narrow_cast<Idx>(std::distance(std::cbegin(*dense_vector_), group_range_.begin())),
narrow_cast<Idx>(std::distance(std::cbegin(*dense_vector_), group_range_.end())));
}
constexpr auto equal(GroupIterator const& other) const { return group_ == other.group_; }
constexpr auto distance_to(GroupIterator const& other) const { return other.group_ - group_; }

constexpr void increment() {
++group_;
group_range_ = std::make_pair(group_range_.second,
std::find_if(group_range_.second, std::cend(*dense_vector_),
[group = group_](Idx value) { return value > group; }));
group_range_ = {group_range_.end(), std::find_if(group_range_.end(), std::cend(*dense_vector_),
[group = group_](Idx value) { return value > group; })};
}
constexpr void decrement() {
--group_;
group_range_ =
std::make_pair(std::find_if(std::make_reverse_iterator(group_range_.first), std::crend(*dense_vector_),
[group = group_](Idx value) { return value < group; })
.base(),
group_range_.first);
group_range_ = {std::find_if(std::make_reverse_iterator(group_range_.begin()), std::crend(*dense_vector_),
[group = group_](Idx value) { return value < group; })
.base(),
group_range_.begin()};
}
constexpr void advance(Idx n) {
auto const start = n > 0 ? group_range_.second : std::cbegin(*dense_vector_);
auto const stop = n < 0 ? group_range_.first : std::cend(*dense_vector_);
auto const start = n > 0 ? group_range_.end() : std::cbegin(*dense_vector_);
auto const stop = n < 0 ? group_range_.begin() : std::cend(*dense_vector_);

group_ += n;
group_range_ = std::equal_range(start, stop, group_);
group_range_ = std::ranges::equal_range(std::ranges::subrange{start, stop}, group_);
}
};

Expand All @@ -215,7 +213,7 @@ class DenseGroupedIdxVector {
: num_groups_{num_groups}, dense_vector_{std::move(dense_vector)} {
assert(size() >= 0);
assert(element_size() >= 0);
assert(std::is_sorted(std::begin(dense_vector_), std::end(dense_vector_)));
assert(std::ranges::is_sorted(dense_vector_));
assert(num_groups_ >= (dense_vector_.empty() ? 0 : dense_vector_.back()));
}
DenseGroupedIdxVector(from_sparse_t /* tag */, IdxVector const& sparse_group_elements)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ template <column_vector DerivedA> inline auto mean_val(Eigen::ArrayBase<DerivedA
inline DoubleComplex mean_val(DoubleComplex const& z) { return z; }
inline double mean_val(double z) { return z; }

template <symmetry_tag sym, class T> inline auto process_mean_val(const T& m) {
template <symmetry_tag sym, class T> inline auto process_mean_val(T const& m) {
if constexpr (is_symmetric_v<sym>) {
return mean_val(m);
} else {
Expand Down Expand Up @@ -348,9 +348,7 @@ inline auto all_zero(RealValue<asymmetric_t> const& value) { return (value == Re
//
// The function assumes that the current value is normalized and new value should be normalized with scalar
template <symmetry_tag sym, class Proxy>
inline void
update_real_value(RealValue<sym> const& new_value, Proxy&& current_value,
double scalar) { // NOLINT(cppcoreguidelines-missing-std-forward) // perfect forward into void
inline void update_real_value(RealValue<sym> const& new_value, Proxy&& current_value, double scalar) {
if constexpr (is_symmetric_v<sym>) {
if (!is_nan(new_value)) {
std::forward<Proxy>(current_value) = scalar * new_value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ class Timer {
Timer(CalculationInfo& info, int code, std::string name)
: info_(&info), code_(code), name_(std::move(name)), start_(Clock::now()) {}

Timer(const Timer&) = delete;
Timer(Timer const&) = delete;
Timer(Timer&&) = default;
Timer& operator=(const Timer&) = delete;
Timer& operator=(Timer const&) = delete;

Timer& operator=(Timer&& timer) noexcept {
// Stop the current timer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ class Base {
virtual ~Base() = default;
constexpr ID id() const noexcept { return id_; }
constexpr BaseOutput base_output(bool is_energized) const {
return BaseOutput{id_, static_cast<IntS>(is_energized)};
return BaseOutput{.id = id_, .energized = static_cast<IntS>(is_energized)};
}
virtual bool energized(bool is_connected_to_source) const = 0;

Base(Base&&) = default;
Base& operator=(Base&&) = default;

protected:
Base(const Base&) = default;
Base& operator=(const Base&) = default;
Base(Base const&) = default;
Base& operator=(Base const&) = default;

private:
ID id_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ class Branch : public Base {
assert(update_data.id == this->id() || is_nan(update_data.id));
bool const changed = set_status(update_data.from_status, update_data.to_status);
// change branch connection will change both topo and param
return {changed, changed};
return {.topo = changed, .param = changed};
}

auto inverse(std::convertible_to<BranchUpdate> auto update_data) const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ class Branch3 : public Base {
assert(update_data.id == this->id() || is_nan(update_data.id));
bool const changed = set_status(update_data.status_1, update_data.status_2, update_data.status_3);
// change in branch3 connection will change both topo and param
return {changed, changed};
return {.topo = changed, .param = changed};
}

auto inverse(std::convertible_to<Branch3Update> auto update_data) const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct UpdateChange {
bool param{};

friend constexpr UpdateChange operator||(UpdateChange const& x, UpdateChange const& y) {
return UpdateChange{x.topo || y.topo, x.param || y.param};
return UpdateChange{.topo = x.topo || y.topo, .param = x.param || y.param};
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ template <symmetry_tag current_sensor_symmetry_> class CurrentSensor : public Ge
update_real_value<current_sensor_symmetry>(update_data.i_measured, i_measured_, base_current_inv_);
update_real_value<current_sensor_symmetry>(update_data.i_angle_measured, i_angle_measured_, 1.0);

return {false, false};
return {.topo = false, .param = false};
}

CurrentSensorUpdate<current_sensor_symmetry>
Expand Down
Loading
Loading