Skip to content

Commit

Permalink
Factorize matrix::at const and non-const implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasChollet committed Oct 10, 2021
1 parent f224247 commit 7d584d1
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions include/cotila/matrix/matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,22 @@ template <typename T, std::size_t N, std::size_t M> struct matrix {
* ease the writing of generic algorithm
*/
constexpr T &at(std::size_t row, std::size_t column = 0) {
if (row >= N || column >= M)
throw "index out of range";
return arrays[row][column];
get_real_by_name_impl(*this, row, column);
}

/**
* @copydoc at
*/
constexpr const T &at(std::size_t row, std::size_t column = 0) const {
get_real_by_name_impl(*this, row, column);
}

template <typename Self>
static constexpr auto &get_real_by_name_impl(Self &self, std::size_t row, std::size_t column = 0) {
if (row >= N || column >= M)
throw "index out of range";
return arrays[row][column];
}
return self.arrays[row][column];
} ///< @private

/** @brief access specified element
* @param i index of the row
Expand Down

0 comments on commit 7d584d1

Please sign in to comment.