mirror of
https://gitlab.com/libeigen/eigen.git
synced 2025-04-24 02:29:33 +08:00
add Map static methods taking Strides, add test checking for compilation errors
This commit is contained in:
parent
2e2614b0fd
commit
4489c56c9e
@ -67,6 +67,7 @@ class PlainObjectBase : public internal::dense_xpr_base<Derived>::type
|
||||
using Base::IsVectorAtCompileTime;
|
||||
using Base::Flags;
|
||||
|
||||
template<typename PlainObjectType, int MapOptions, typename StrideType> friend class Eigen::Map;
|
||||
friend class Eigen::Map<Derived, Unaligned>;
|
||||
typedef Eigen::Map<Derived, Unaligned> MapType;
|
||||
friend class Eigen::Map<const Derived, Unaligned>;
|
||||
@ -75,6 +76,11 @@ class PlainObjectBase : public internal::dense_xpr_base<Derived>::type
|
||||
typedef Eigen::Map<Derived, Aligned> AlignedMapType;
|
||||
friend class Eigen::Map<const Derived, Aligned>;
|
||||
typedef const Eigen::Map<const Derived, Aligned> ConstAlignedMapType;
|
||||
template<typename StrideType> struct StridedMapType { typedef Eigen::Map<Derived, Unaligned, StrideType> type; };
|
||||
template<typename StrideType> struct StridedConstMapType { typedef Eigen::Map<const Derived, Unaligned, StrideType> type; };
|
||||
template<typename StrideType> struct StridedAlignedMapType { typedef Eigen::Map<Derived, Aligned, StrideType> type; };
|
||||
template<typename StrideType> struct StridedConstAlignedMapType { typedef Eigen::Map<const Derived, Aligned, StrideType> type; };
|
||||
|
||||
|
||||
protected:
|
||||
DenseStorage<Scalar, Base::MaxSizeAtCompileTime, Base::RowsAtCompileTime, Base::ColsAtCompileTime, Options> m_storage;
|
||||
@ -423,6 +429,44 @@ class PlainObjectBase : public internal::dense_xpr_base<Derived>::type
|
||||
{ return ConstAlignedMapType(data, rows, cols); }
|
||||
inline static AlignedMapType MapAligned(Scalar* data, Index rows, Index cols)
|
||||
{ return AlignedMapType(data, rows, cols); }
|
||||
|
||||
template<int Outer, int Inner>
|
||||
inline static typename StridedConstMapType<Stride<Outer, Inner> >::type Map(const Scalar* data, const Stride<Outer, Inner>& stride)
|
||||
{ return typename StridedConstMapType<Stride<Outer, Inner> >::type(data, stride); }
|
||||
template<int Outer, int Inner>
|
||||
inline static typename StridedMapType<Stride<Outer, Inner> >::type Map(Scalar* data, const Stride<Outer, Inner>& stride)
|
||||
{ return typename StridedMapType<Stride<Outer, Inner> >::type(data, stride); }
|
||||
template<int Outer, int Inner>
|
||||
inline static typename StridedConstMapType<Stride<Outer, Inner> >::type Map(const Scalar* data, Index size, const Stride<Outer, Inner>& stride)
|
||||
{ return typename StridedConstMapType<Stride<Outer, Inner> >::type(data, size, stride); }
|
||||
template<int Outer, int Inner>
|
||||
inline static typename StridedMapType<Stride<Outer, Inner> >::type Map(Scalar* data, Index size, const Stride<Outer, Inner>& stride)
|
||||
{ return typename StridedMapType<Stride<Outer, Inner> >::type(data, size, stride); }
|
||||
template<int Outer, int Inner>
|
||||
inline static typename StridedConstMapType<Stride<Outer, Inner> >::type Map(const Scalar* data, Index rows, Index cols, const Stride<Outer, Inner>& stride)
|
||||
{ return typename StridedConstMapType<Stride<Outer, Inner> >::type(data, rows, cols, stride); }
|
||||
template<int Outer, int Inner>
|
||||
inline static typename StridedMapType<Stride<Outer, Inner> >::type Map(Scalar* data, Index rows, Index cols, const Stride<Outer, Inner>& stride)
|
||||
{ return typename StridedMapType<Stride<Outer, Inner> >::type(data, rows, cols, stride); }
|
||||
|
||||
template<int Outer, int Inner>
|
||||
inline static typename StridedConstAlignedMapType<Stride<Outer, Inner> >::type MapAligned(const Scalar* data, const Stride<Outer, Inner>& stride)
|
||||
{ return typename StridedConstAlignedMapType<Stride<Outer, Inner> >::type(data, stride); }
|
||||
template<int Outer, int Inner>
|
||||
inline static typename StridedAlignedMapType<Stride<Outer, Inner> >::type MapAligned(Scalar* data, const Stride<Outer, Inner>& stride)
|
||||
{ return typename StridedAlignedMapType<Stride<Outer, Inner> >::type(data, stride); }
|
||||
template<int Outer, int Inner>
|
||||
inline static typename StridedConstAlignedMapType<Stride<Outer, Inner> >::type MapAligned(const Scalar* data, Index size, const Stride<Outer, Inner>& stride)
|
||||
{ return typename StridedConstAlignedMapType<Stride<Outer, Inner> >::type(data, size, stride); }
|
||||
template<int Outer, int Inner>
|
||||
inline static typename StridedAlignedMapType<Stride<Outer, Inner> >::type MapAligned(Scalar* data, Index size, const Stride<Outer, Inner>& stride)
|
||||
{ return typename StridedAlignedMapType<Stride<Outer, Inner> >::type(data, size, stride); }
|
||||
template<int Outer, int Inner>
|
||||
inline static typename StridedConstAlignedMapType<Stride<Outer, Inner> >::type MapAligned(const Scalar* data, Index rows, Index cols, const Stride<Outer, Inner>& stride)
|
||||
{ return typename StridedConstAlignedMapType<Stride<Outer, Inner> >::type(data, rows, cols, stride); }
|
||||
template<int Outer, int Inner>
|
||||
inline static typename StridedAlignedMapType<Stride<Outer, Inner> >::type MapAligned(Scalar* data, Index rows, Index cols, const Stride<Outer, Inner>& stride)
|
||||
{ return typename StridedAlignedMapType<Stride<Outer, Inner> >::type(data, rows, cols, stride); }
|
||||
//@}
|
||||
|
||||
using Base::setConstant;
|
||||
|
@ -22,11 +22,11 @@ and gives tips to help porting your application from Eigen2 to Eigen3.
|
||||
|
||||
\section CompatibilitySupport Eigen2 compatibility support
|
||||
|
||||
In order to ease the switch from Eigen2 to Eigen3, Eigen3 features a Eigen2 support modes \ref TopicResizing "this page"..
|
||||
In order to ease the switch from Eigen2 to Eigen3, Eigen3 features \ref Eigen2Support "Eigen2 support modes".
|
||||
|
||||
The quick way to enable this is to define the EIGEN2_SUPPORT preprocessor token \b before including any Eigen header (typically it should be set in your project options).
|
||||
The quick way to enable this is to define the \c EIGEN2_SUPPORT preprocessor token \b before including any Eigen header (typically it should be set in your project options).
|
||||
|
||||
Moreover, we also provided a staged migration path which may be useful to migrate large projects
|
||||
Moreover, we also provided a \b staged \b migration \b path which may be useful to migrate larger projects from Eigen2 to Eigen3. This explained on the \ref Eigen2Support "Eigen2 support modes" page.
|
||||
|
||||
\section Using The USING_PART_OF_NAMESPACE_EIGEN macro
|
||||
|
||||
|
@ -61,6 +61,7 @@ ei_add_test(commainitializer)
|
||||
ei_add_test(smallvectors)
|
||||
ei_add_test(map)
|
||||
ei_add_test(mapstride)
|
||||
ei_add_test(mapstaticmethods)
|
||||
ei_add_test(array)
|
||||
ei_add_test(array_for_matrix)
|
||||
ei_add_test(array_replicate)
|
||||
|
188
test/mapstaticmethods.cpp
Normal file
188
test/mapstaticmethods.cpp
Normal file
@ -0,0 +1,188 @@
|
||||
// This file is part of Eigen, a lightweight C++ template library
|
||||
// for linear algebra.
|
||||
//
|
||||
// Copyright (C) 2011 Benoit Jacob <jacob.benoit.1@gmail.com>
|
||||
//
|
||||
// Eigen is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation; either
|
||||
// version 3 of the License, or (at your option) any later version.
|
||||
//
|
||||
// Alternatively, you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License as
|
||||
// published by the Free Software Foundation; either version 2 of
|
||||
// the License, or (at your option) any later version.
|
||||
//
|
||||
// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License and a copy of the GNU General Public License along with
|
||||
// Eigen. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
#include "main.h"
|
||||
|
||||
float *ptr;
|
||||
const float *const_ptr;
|
||||
|
||||
template<typename PlainObjectType,
|
||||
bool IsDynamicSize = PlainObjectType::SizeAtCompileTime == Dynamic,
|
||||
bool IsVector = PlainObjectType::IsVectorAtCompileTime
|
||||
>
|
||||
struct mapstaticmethods_impl {};
|
||||
|
||||
template<typename PlainObjectType, bool IsVector>
|
||||
struct mapstaticmethods_impl<PlainObjectType, false, IsVector>
|
||||
{
|
||||
static void run(const PlainObjectType& m)
|
||||
{
|
||||
mapstaticmethods_impl<PlainObjectType, true, IsVector>::run(m);
|
||||
|
||||
int i = internal::random<int>(2,5), j = internal::random<int>(2,5);
|
||||
|
||||
PlainObjectType::Map(ptr).setZero();
|
||||
PlainObjectType::MapAligned(ptr).setZero();
|
||||
PlainObjectType::Map(const_ptr).sum();
|
||||
PlainObjectType::MapAligned(const_ptr).sum();
|
||||
|
||||
PlainObjectType::Map(ptr, InnerStride<>(i)).setZero();
|
||||
PlainObjectType::MapAligned(ptr, InnerStride<>(i)).setZero();
|
||||
PlainObjectType::Map(const_ptr, InnerStride<>(i)).sum();
|
||||
PlainObjectType::MapAligned(const_ptr, InnerStride<>(i)).sum();
|
||||
|
||||
PlainObjectType::Map(ptr, InnerStride<2>()).setZero();
|
||||
PlainObjectType::MapAligned(ptr, InnerStride<3>()).setZero();
|
||||
PlainObjectType::Map(const_ptr, InnerStride<4>()).sum();
|
||||
PlainObjectType::MapAligned(const_ptr, InnerStride<5>()).sum();
|
||||
|
||||
PlainObjectType::Map(ptr, OuterStride<>(i)).setZero();
|
||||
PlainObjectType::MapAligned(ptr, OuterStride<>(i)).setZero();
|
||||
PlainObjectType::Map(const_ptr, OuterStride<>(i)).sum();
|
||||
PlainObjectType::MapAligned(const_ptr, OuterStride<>(i)).sum();
|
||||
|
||||
PlainObjectType::Map(ptr, OuterStride<2>()).setZero();
|
||||
PlainObjectType::MapAligned(ptr, OuterStride<3>()).setZero();
|
||||
PlainObjectType::Map(const_ptr, OuterStride<4>()).sum();
|
||||
PlainObjectType::MapAligned(const_ptr, OuterStride<5>()).sum();
|
||||
|
||||
PlainObjectType::Map(ptr, Stride<Dynamic, Dynamic>(i,j)).setZero();
|
||||
PlainObjectType::MapAligned(ptr, Stride<2,Dynamic>(2,i)).setZero();
|
||||
PlainObjectType::Map(const_ptr, Stride<Dynamic,3>(i,3)).sum();
|
||||
PlainObjectType::MapAligned(const_ptr, Stride<Dynamic, Dynamic>(i,j)).sum();
|
||||
|
||||
PlainObjectType::Map(ptr, Stride<2,3>()).setZero();
|
||||
PlainObjectType::MapAligned(ptr, Stride<3,4>()).setZero();
|
||||
PlainObjectType::Map(const_ptr, Stride<2,4>()).sum();
|
||||
PlainObjectType::MapAligned(const_ptr, Stride<5,3>()).sum();
|
||||
}
|
||||
};
|
||||
|
||||
template<typename PlainObjectType>
|
||||
struct mapstaticmethods_impl<PlainObjectType, true, false>
|
||||
{
|
||||
static void run(const PlainObjectType& m)
|
||||
{
|
||||
int rows = m.rows(), cols = m.cols();
|
||||
|
||||
int i = internal::random<int>(2,5), j = internal::random<int>(2,5);
|
||||
|
||||
PlainObjectType::Map(ptr, rows, cols).setZero();
|
||||
PlainObjectType::MapAligned(ptr, rows, cols).setZero();
|
||||
PlainObjectType::Map(const_ptr, rows, cols).sum();
|
||||
PlainObjectType::MapAligned(const_ptr, rows, cols).sum();
|
||||
|
||||
PlainObjectType::Map(ptr, rows, cols, InnerStride<>(i)).setZero();
|
||||
PlainObjectType::MapAligned(ptr, rows, cols, InnerStride<>(i)).setZero();
|
||||
PlainObjectType::Map(const_ptr, rows, cols, InnerStride<>(i)).sum();
|
||||
PlainObjectType::MapAligned(const_ptr, rows, cols, InnerStride<>(i)).sum();
|
||||
|
||||
PlainObjectType::Map(ptr, rows, cols, InnerStride<2>()).setZero();
|
||||
PlainObjectType::MapAligned(ptr, rows, cols, InnerStride<3>()).setZero();
|
||||
PlainObjectType::Map(const_ptr, rows, cols, InnerStride<4>()).sum();
|
||||
PlainObjectType::MapAligned(const_ptr, rows, cols, InnerStride<5>()).sum();
|
||||
|
||||
PlainObjectType::Map(ptr, rows, cols, OuterStride<>(i)).setZero();
|
||||
PlainObjectType::MapAligned(ptr, rows, cols, OuterStride<>(i)).setZero();
|
||||
PlainObjectType::Map(const_ptr, rows, cols, OuterStride<>(i)).sum();
|
||||
PlainObjectType::MapAligned(const_ptr, rows, cols, OuterStride<>(i)).sum();
|
||||
|
||||
PlainObjectType::Map(ptr, rows, cols, OuterStride<2>()).setZero();
|
||||
PlainObjectType::MapAligned(ptr, rows, cols, OuterStride<3>()).setZero();
|
||||
PlainObjectType::Map(const_ptr, rows, cols, OuterStride<4>()).sum();
|
||||
PlainObjectType::MapAligned(const_ptr, rows, cols, OuterStride<5>()).sum();
|
||||
|
||||
PlainObjectType::Map(ptr, rows, cols, Stride<Dynamic, Dynamic>(i,j)).setZero();
|
||||
PlainObjectType::MapAligned(ptr, rows, cols, Stride<2,Dynamic>(2,i)).setZero();
|
||||
PlainObjectType::Map(const_ptr, rows, cols, Stride<Dynamic,3>(i,3)).sum();
|
||||
PlainObjectType::MapAligned(const_ptr, rows, cols, Stride<Dynamic, Dynamic>(i,j)).sum();
|
||||
|
||||
PlainObjectType::Map(ptr, rows, cols, Stride<2,3>()).setZero();
|
||||
PlainObjectType::MapAligned(ptr, rows, cols, Stride<3,4>()).setZero();
|
||||
PlainObjectType::Map(const_ptr, rows, cols, Stride<2,4>()).sum();
|
||||
PlainObjectType::MapAligned(const_ptr, rows, cols, Stride<5,3>()).sum();
|
||||
}
|
||||
};
|
||||
|
||||
template<typename PlainObjectType>
|
||||
struct mapstaticmethods_impl<PlainObjectType, true, true>
|
||||
{
|
||||
static void run(const PlainObjectType& v)
|
||||
{
|
||||
int size = v.size();
|
||||
|
||||
int i = internal::random<int>(2,5);
|
||||
|
||||
PlainObjectType::Map(ptr, size).setZero();
|
||||
PlainObjectType::MapAligned(ptr, size).setZero();
|
||||
PlainObjectType::Map(const_ptr, size).sum();
|
||||
PlainObjectType::MapAligned(const_ptr, size).sum();
|
||||
|
||||
PlainObjectType::Map(ptr, size, InnerStride<>(i)).setZero();
|
||||
PlainObjectType::MapAligned(ptr, size, InnerStride<>(i)).setZero();
|
||||
PlainObjectType::Map(const_ptr, size, InnerStride<>(i)).sum();
|
||||
PlainObjectType::MapAligned(const_ptr, size, InnerStride<>(i)).sum();
|
||||
|
||||
PlainObjectType::Map(ptr, size, InnerStride<2>()).setZero();
|
||||
PlainObjectType::MapAligned(ptr, size, InnerStride<3>()).setZero();
|
||||
PlainObjectType::Map(const_ptr, size, InnerStride<4>()).sum();
|
||||
PlainObjectType::MapAligned(const_ptr, size, InnerStride<5>()).sum();
|
||||
}
|
||||
};
|
||||
|
||||
template<typename PlainObjectType>
|
||||
void mapstaticmethods(const PlainObjectType& m)
|
||||
{
|
||||
mapstaticmethods_impl<PlainObjectType>::run(m);
|
||||
VERIFY(true); // just to avoid 'unused function' warning
|
||||
}
|
||||
|
||||
void test_mapstaticmethods()
|
||||
{
|
||||
ptr = internal::aligned_new<float>(1000);
|
||||
for(int i = 0; i < 1000; i++) ptr[i] = float(i);
|
||||
|
||||
const_ptr = ptr;
|
||||
|
||||
CALL_SUBTEST_1(( mapstaticmethods(Matrix<float, 1, 1>()) ));
|
||||
CALL_SUBTEST_1(( mapstaticmethods(Vector2f()) ));
|
||||
CALL_SUBTEST_2(( mapstaticmethods(Vector3f()) ));
|
||||
CALL_SUBTEST_2(( mapstaticmethods(Matrix2f()) ));
|
||||
CALL_SUBTEST_3(( mapstaticmethods(Matrix4f()) ));
|
||||
CALL_SUBTEST_3(( mapstaticmethods(Array4f()) ));
|
||||
CALL_SUBTEST_4(( mapstaticmethods(Array3f()) ));
|
||||
CALL_SUBTEST_4(( mapstaticmethods(Array33f()) ));
|
||||
CALL_SUBTEST_5(( mapstaticmethods(Array44f()) ));
|
||||
CALL_SUBTEST_5(( mapstaticmethods(VectorXf(1)) ));
|
||||
CALL_SUBTEST_5(( mapstaticmethods(VectorXf(8)) ));
|
||||
CALL_SUBTEST_6(( mapstaticmethods(MatrixXf(1,1)) ));
|
||||
CALL_SUBTEST_6(( mapstaticmethods(MatrixXf(5,7)) ));
|
||||
CALL_SUBTEST_7(( mapstaticmethods(ArrayXf(1)) ));
|
||||
CALL_SUBTEST_7(( mapstaticmethods(ArrayXf(5)) ));
|
||||
CALL_SUBTEST_8(( mapstaticmethods(ArrayXXf(1,1)) ));
|
||||
CALL_SUBTEST_8(( mapstaticmethods(ArrayXXf(8,6)) ));
|
||||
|
||||
internal::aligned_delete(ptr, 1000);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user