Commit be9c1a42 authored by Praetorius, Simon's avatar Praetorius, Simon
Browse files

add functionality to access vector and matrix components

parent 3fec45ce
#pragma once
#include <amdis/common/Concepts.hpp>
#include <amdis/common/Logical.hpp>
#include <amdis/common/TypeTraits.hpp>
namespace AMDiS
{
namespace Concepts
{
namespace Definition
{
struct HasVectorAccess
{
template <class V, class I>
auto requires_(V&& v, I&& i) -> decltype( v[i] );
};
struct HasMatrixAccess
{
template <class M, class I, class J>
auto requires_(M&& m, I&& i, J&& j) -> decltype( m[i][j] );
};
} // end namespace Definition
template <class V, class I>
using VectorAccessible_t = bool_t<models<Definition::HasVectorAccess(V, I)>>;
template <class M, class I, class J>
using MatrixAccessible_t = bool_t<models<Definition::HasMatrixAccess(M, I, J)>>;
} // end namespace Concepts
#ifdef DOXYGEN
/// \brief Uniform vector access using [.]
template <class Vector, class I>
decltype(auto) access(Vector&& vec, I const& i);
/// \brief Uniform matrix access using either [.][.] or (.,.)
template <class Matrix, class I, class J>
decltype(auto) access(Matrix&& mat, I const& i, J const& j);
#else
// access i'th component of a vector
template <class Vector, class I,
REQUIRES(Concepts::VectorAccessible_t<Vector,I>{})>
decltype(auto) access(Vector&& vec, I const& i) { return vec[i]; }
// fall-back implementation for scalars
template <class Vector, class I,
REQUIRES(not Concepts::VectorAccessible_t<Vector,I>{})>
decltype(auto) access(Vector&& vec, I const& /*i*/) { return FWD(vec); }
// access (i,j)'th component of a matrix using [.][.]
template <class Matrix, class I, class J,
REQUIRES(Concepts::MatrixAccessible_t<Matrix,I,J>{})>
decltype(auto) access(Matrix&& mat, I const& i, J const& j) { return mat[i][j]; }
// access (i,j)'th component of a matrix using (.,.)
template <class Matrix, class I, class J,
REQUIRES(not Concepts::MatrixAccessible_t<Matrix,I,J>{} && Concepts::Callable<Matrix,I,J>)>
decltype(auto) access(Matrix&& mat, I const& i, J const& j) { return mat(i,j); }
// fall-back implementation for scalars
template <class Matrix, class I, class J,
REQUIRES(not Concepts::MatrixAccessible_t<Matrix,I,J>{} && not Concepts::Callable<Matrix,I,J>)>
decltype(auto) access(Matrix&& mat, I const& /*i*/, J const& /*j*/) { return FWD(mat); }
#endif
} // end namespace AMDiS
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment