Skip to content

Commit

Permalink
Use std::swap and add a default case for the switch statement (#405)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcarcell authored Feb 14, 2025
1 parent 86644d4 commit 44cf2a3
Showing 1 changed file with 7 additions and 13 deletions.
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

0 comments on commit 44cf2a3

Please sign in to comment.