Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use std::swap and add a default case for the switch statement #405

Merged
merged 1 commit into from
Feb 14, 2025
Merged
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
20 changes: 7 additions & 13 deletions utils/include/edm4hep/utils/cov_matrix_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <stdexcept>
#include <string>
#include <type_traits>
#include <utility>

namespace edm4hep {

Expand All @@ -28,13 +29,6 @@ namespace utils {
return static_cast<DimEnum>(index);
}

/// Need a constexpr swap for integers before c++20
constexpr void swap(int& i, int& j) {
int tmp = j;
j = i;
i = tmp;
}

/**
* Get the dimension of the covariance matrix from the size of the 1D array in
* which it is stored
Expand Down Expand Up @@ -63,12 +57,12 @@ namespace utils {
return 2;
case 1:
return 1;
default:
// We simply use throwing an exception to make compilation fail in constexpr
// cases.
throw std::invalid_argument(
"Not a valid size for a covariance matrix stored in lower triangular form (N = " + std::to_string(N) + ")");
}

// We simply use throwing an exception to make compilation fail in constexpr
// cases.
throw std::invalid_argument(
"Not a valid size for a covariance matrix stored in lower triangular form (N = " + std::to_string(N) + ")");
}

/**
Expand All @@ -81,7 +75,7 @@ namespace utils {
*/
constexpr int to_lower_tri(int i, int j) {
if (i < j) {
detail::swap(i, j);
std::swap(i, j);
}
return i * (i + 1) / 2 + j;
}
Expand Down
Loading