added CustomIndex capability only to Tensor and not yet to TensorBase.

using Sfinae and is_base_of to select correct template which converts to array<Index,NumIndices>


 user: Gabriel Nützi <gnuetzi@gmx.ch>
 branch 'default'
 added unsupported/Eigen/CXX11/src/Tensor/TensorMetaMacros.h
 added unsupported/test/cxx11_tensor_customIndex.cpp
 changed unsupported/Eigen/CXX11/Tensor
 changed unsupported/Eigen/CXX11/src/Tensor/Tensor.h
 changed unsupported/Eigen/CXX11/src/Tensor/TensorMeta.h
 changed unsupported/test/CMakeLists.txt
This commit is contained in:
Gabriel Nützi
2015-10-09 18:52:48 +02:00
parent b4c79ee1d3
commit 6edae2d30d
6 changed files with 205 additions and 3 deletions

View File

@@ -88,6 +88,11 @@ class Tensor : public TensorBase<Tensor<Scalar_, NumIndices_, Options_, IndexTyp
protected:
TensorStorage<Scalar, Dimensions, Options> m_storage;
template<typename CustomIndex>
struct isOfNormalIndex{
static const bool value = internal::is_base_of< array<Index, NumIndices>, CustomIndex >::value;
};
public:
// Metadata
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index rank() const { return NumIndices; }
@@ -111,14 +116,29 @@ class Tensor : public TensorBase<Tensor<Scalar_, NumIndices_, Options_, IndexTyp
EIGEN_STATIC_ASSERT(sizeof...(otherIndices) + 2 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
return coeff(array<Index, NumIndices>{{firstIndex, secondIndex, otherIndices...}});
}
#endif
/** Normal Index */
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar& coeff(const array<Index, NumIndices>& indices) const
{
eigen_internal_assert(checkIndexRange(indices));
return m_storage.data()[linearizedIndex(indices)];
}
/** Custom Index */
template<typename CustomIndex,
EIGEN_SFINAE_ENABLE_IF( !(isOfNormalIndex<CustomIndex>::value) )
>
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar& coeff(const CustomIndex & indices) const
{
return coeff(internal::customIndex2Array<Index,NumIndices>(indices));
}
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar& coeff(Index index) const
{
eigen_internal_assert(index >= 0 && index < size());
@@ -135,12 +155,23 @@ class Tensor : public TensorBase<Tensor<Scalar_, NumIndices_, Options_, IndexTyp
}
#endif
/** Normal Index */
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(const array<Index, NumIndices>& indices)
{
eigen_internal_assert(checkIndexRange(indices));
return m_storage.data()[linearizedIndex(indices)];
}
/** Custom Index */
template<typename CustomIndex,
EIGEN_SFINAE_ENABLE_IF( !(isOfNormalIndex<CustomIndex>::value) )
>
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(const CustomIndex & indices)
{
return coeffRef(internal::customIndex2Array<Index,NumIndices>(indices));
}
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(Index index)
{
eigen_internal_assert(index >= 0 && index < size());
@@ -178,9 +209,20 @@ class Tensor : public TensorBase<Tensor<Scalar_, NumIndices_, Options_, IndexTyp
}
#endif
/** Custom Index */
template<typename CustomIndex,
EIGEN_SFINAE_ENABLE_IF( !(isOfNormalIndex<CustomIndex>::value) )
>
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar& operator()(const CustomIndex & indices) const
{
//eigen_assert(checkIndexRange(indices)); /* already in coeff */
return coeff(internal::customIndex2Array<Index,NumIndices>(indices));
}
/** Normal Index */
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar& operator()(const array<Index, NumIndices>& indices) const
{
eigen_assert(checkIndexRange(indices));
//eigen_assert(checkIndexRange(indices)); /* already in coeff */
return coeff(indices);
}
@@ -228,12 +270,23 @@ class Tensor : public TensorBase<Tensor<Scalar_, NumIndices_, Options_, IndexTyp
}
#endif
/** Normal Index */
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& operator()(const array<Index, NumIndices>& indices)
{
eigen_assert(checkIndexRange(indices));
//eigen_assert(checkIndexRange(indices)); /* already in coeff */
return coeffRef(indices);
}
/** Custom Index */
template<typename CustomIndex,
EIGEN_SFINAE_ENABLE_IF( !(isOfNormalIndex<CustomIndex>::value) )
>
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& operator()(const CustomIndex & indices)
{
//eigen_assert(checkIndexRange(indices)); /* already in coeff */
return coeffRef(internal::customIndex2Array<Index,NumIndices>(indices));
}
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& operator()(Index index)
{
eigen_assert(index >= 0 && index < size());
@@ -295,12 +348,20 @@ class Tensor : public TensorBase<Tensor<Scalar_, NumIndices_, Options_, IndexTyp
}
#endif
/** Normal Dimension */
inline explicit Tensor(const array<Index, NumIndices>& dimensions)
: m_storage(internal::array_prod(dimensions), dimensions)
{
EIGEN_INITIALIZE_COEFFS_IF_THAT_OPTION_IS_ENABLED
}
/** Custom Dimension (delegating constructor c++11) */
template<typename CustomDimension,
EIGEN_SFINAE_ENABLE_IF( !(isOfNormalIndex<CustomDimension>::value) )
>
inline explicit Tensor(const CustomDimension & dimensions) : Tensor(internal::customIndex2Array<Index,NumIndices>(dimensions))
{}
template<typename OtherDerived>
EIGEN_DEVICE_FUNC
EIGEN_STRONG_INLINE Tensor(const TensorBase<OtherDerived, ReadOnlyAccessors>& other)
@@ -341,7 +402,7 @@ class Tensor : public TensorBase<Tensor<Scalar_, NumIndices_, Options_, IndexTyp
}
#ifdef EIGEN_HAS_VARIADIC_TEMPLATES
template<typename... IndexTypes> EIGEN_DEVICE_FUNC
template<typename... IndexTypes> EIGEN_DEVICE_FUNC
void resize(Index firstDimension, IndexTypes... otherDimensions)
{
// The number of dimensions used to resize a tensor must be equal to the rank of the tensor.
@@ -350,6 +411,7 @@ class Tensor : public TensorBase<Tensor<Scalar_, NumIndices_, Options_, IndexTyp
}
#endif
/** Normal Dimension */
EIGEN_DEVICE_FUNC void resize(const array<Index, NumIndices>& dimensions)
{
std::size_t i;
@@ -367,6 +429,8 @@ class Tensor : public TensorBase<Tensor<Scalar_, NumIndices_, Options_, IndexTyp
#endif
}
// Why this overload, DSizes is derived from array ??? //
EIGEN_DEVICE_FUNC void resize(const DSizes<Index, NumIndices>& dimensions) {
array<Index, NumIndices> dims;
for (std::size_t i = 0; i < NumIndices; ++i) {
@@ -375,6 +439,17 @@ class Tensor : public TensorBase<Tensor<Scalar_, NumIndices_, Options_, IndexTyp
resize(dims);
}
/** Custom Dimension */
template<typename CustomDimension,
EIGEN_SFINAE_ENABLE_IF( !(isOfNormalIndex<CustomDimension>::value) )
>
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void resize(const CustomDimension & dimensions)
{
//eigen_assert(checkIndexRange(indices)); /* already in coeff */
return coeffRef(internal::customIndex2Array<Index,NumIndices>(dimensions));
}
#ifndef EIGEN_EMULATE_CXX11_META_H
template <typename std::ptrdiff_t... Indices>
EIGEN_DEVICE_FUNC