Skip to content

Commit

Permalink
feat: [] indexing operator for vec<> class
Browse files Browse the repository at this point in the history
  • Loading branch information
mrdcvlsc committed Aug 1, 2023
1 parent c0e5cb6 commit fad7420
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
3 changes: 3 additions & 0 deletions include/cyfre/vectors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ namespace cyfre {

constexpr vec(std::initializer_list<T> sequence);

constexpr T &operator[](size_t i);
constexpr const T &operator[](size_t i) const;

/// @returns true if the matrix is equal, false otherwise.
constexpr bool operator==(vec const &that) const;

Expand Down
20 changes: 19 additions & 1 deletion src/cyfre/vectors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,24 @@ namespace cyfre {
}
}

template <concepts::scalars T, typename Dim, axis_t Axis, order_t Order, typename Blas>
constexpr T &vec<T, Dim, Axis, Order, Blas>::operator[](size_t i) {
if constexpr (Axis == axis_t::x) {
return mat<T, typename VecOrient<Dim, Axis>::OrientDim, Order, Blas>::operator()(0, i);
} else {
return mat<T, typename VecOrient<Dim, Axis>::OrientDim, Order, Blas>::operator()(i, 0);
}
}

template <concepts::scalars T, typename Dim, axis_t Axis, order_t Order, typename Blas>
constexpr const T &vec<T, Dim, Axis, Order, Blas>::operator[](size_t i) const {
if constexpr (Axis == axis_t::x) {
return mat<T, typename VecOrient<Dim, Axis>::OrientDim, Order, Blas>::operator()(0, i);
} else {
return mat<T, typename VecOrient<Dim, Axis>::OrientDim, Order, Blas>::operator()(i, 0);
}
}

/// @returns true if the matrix is equal, false otherwise.
template <concepts::scalars T, typename Dim, axis_t Axis, order_t Order, typename Blas>
constexpr bool vec<T, Dim, Axis, Order, Blas>::operator==(vec const &that) const {
Expand All @@ -56,7 +74,7 @@ namespace cyfre {
constexpr bool vec<T, Dim, Axis, Order, Blas>::operator==(Matrix const &that) const {
return mat<T, typename VecOrient<Dim, Axis>::OrientDim, Order, Blas>::operator==(that);
}

template <concepts::scalars T, typename Dim, axis_t Axis, order_t Order, typename Blas>
constexpr size_t vec<T, Dim, Axis, Order, Blas>::size() const {
return this->rows() * this->cols();
Expand Down

0 comments on commit fad7420

Please sign in to comment.