// This file is part of Eigen, a lightweight C++ template library // for linear algebra. // // Copyright (C) 2018-2019 Gael Guennebaud <gael.guennebaud@inria.fr> // // This Source Code Form is subject to the terms of the Mozilla // Public License v. 2.0. If a copy of the MPL was not distributed // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
#if EIGEN_HAS_CXX11 // check swappable
{ using std::swap; // pointer-based
{
VectorType v_copy = v; auto a = v.begin(); auto b = v.end()-1;
swap(a,b);
VERIFY_IS_EQUAL(v,v_copy);
VERIFY_IS_EQUAL(*b,*v.begin());
VERIFY_IS_EQUAL(*b,v(0));
VERIFY_IS_EQUAL(*a,v.end()[-1]);
VERIFY_IS_EQUAL(*a,v(last));
}
// generic
{
RowMatrixType B_copy = B; auto Br = B.reshaped(); auto a = Br.begin(); auto b = Br.end()-1;
swap(a,b);
VERIFY_IS_EQUAL(B,B_copy);
VERIFY_IS_EQUAL(*b,*Br.begin());
VERIFY_IS_EQUAL(*b,Br(0));
VERIFY_IS_EQUAL(*a,Br.end()[-1]);
VERIFY_IS_EQUAL(*a,Br(last));
}
}
// check non-const iterator with for-range loops
{
i = 0; for(auto x : v) { VERIFY_IS_EQUAL(x,v[i++]); }
j = internal::random<Index>(0,A.cols()-1);
i = 0; for(auto x : A.col(j)) { VERIFY_IS_EQUAL(x,A(i++,j)); }
i = 0; for(auto x : (v+A.col(j))) { VERIFY_IS_APPROX(x,v(i)+A(i,j)); ++i; }
j = 0;
i = internal::random<Index>(0,A.rows()-1); for(auto x : A.row(i)) { VERIFY_IS_EQUAL(x,A(i,j++)); }
i = 0; for(auto x : A.reshaped()) { VERIFY_IS_EQUAL(x,A(i++)); }
}
// same for const_iterator
{
i = 0; for(auto x : cv) { VERIFY_IS_EQUAL(x,v[i++]); }
i = 0; for(auto x : cA.reshaped()) { VERIFY_IS_EQUAL(x,A(i++)); }
j = 0;
i = internal::random<Index>(0,A.rows()-1); for(auto x : cA.row(i)) { VERIFY_IS_EQUAL(x,A(i,j++)); }
}
// check reshaped() on row-major
{
i = 0;
Matrix<Scalar,Dynamic,Dynamic,ColMajor> Bc = B; for(auto x : B.reshaped()) { VERIFY_IS_EQUAL(x,Bc(i++)); }
}
// check write access
{
VectorType w(v.size());
i = 0; for(auto& x : w) { x = v(i++); }
VERIFY_IS_EQUAL(v,w);
}
// check for dangling pointers
{ // no dangling because pointer-based
{
j = internal::random<Index>(0,A.cols()-1); auto it = A.col(j).begin(); for(i=0;i<rows;++i) {
VERIFY_IS_EQUAL(it[i],A(i,j));
}
}
// no dangling because pointer-based
{
i = internal::random<Index>(0,A.rows()-1); auto it = A.row(i).begin(); for(j=0;j<cols;++j) { VERIFY_IS_EQUAL(it[j],A(i,j)); }
}
{
j = internal::random<Index>(0,A.cols()-1); // this would produce a dangling pointer: // auto it = (A+2*A).col(j).begin(); // we need to name the temporary expression: auto tmp = (A+2*A).col(j); auto it = tmp.begin(); for(i=0;i<rows;++i) {
VERIFY_IS_APPROX(it[i],3*A(i,j));
}
}
}
{ // check basic for loop on vector-wise iterators
j=0; for (auto it = A.colwise().cbegin(); it != A.colwise().cend(); ++it, ++j) {
VERIFY_IS_APPROX( it->coeff(0), A(0,j) );
VERIFY_IS_APPROX( (*it).coeff(0), A(0,j) );
}
j=0; for (auto it = A.colwise().begin(); it != A.colwise().end(); ++it, ++j) {
(*it).coeffRef(0) = (*it).coeff(0); // compilation check
it->coeffRef(0) = it->coeff(0); // compilation check
VERIFY_IS_APPROX( it->coeff(0), A(0,j) );
VERIFY_IS_APPROX( (*it).coeff(0), A(0,j) );
}
// check valuetype gives us a copy
j=0; for (auto it = A.colwise().cbegin(); it != A.colwise().cend(); ++it, ++j) { typename decltype(it)::value_type tmp = *it;
VERIFY_IS_NOT_EQUAL( tmp.data() , it->data() );
VERIFY_IS_APPROX( tmp, A.col(j) );
}
}
// check std::sort
{ // first check that is_sorted returns false when required if(rows>=2)
{
v(1) = v(0)-Scalar(1); #if EIGEN_HAS_CXX11
VERIFY(!is_sorted(std::begin(v),std::end(v))); #else
VERIFY(!is_sorted(v.cbegin(),v.cend())); #endif
}
// on a vector
{
std::sort(v.begin(),v.end());
VERIFY(is_sorted(v.begin(),v.end()));
VERIFY(!::is_sorted(make_reverse_iterator(v.end()),make_reverse_iterator(v.begin())));
}
// on a column of a column-major matrix -> pointer-based iterator and default increment
{
j = internal::random<Index>(0,A.cols()-1); // std::sort(begin(A.col(j)),end(A.col(j))); // does not compile because this returns const iterators typename ColMatrixType::ColXpr Acol = A.col(j);
std::sort(Acol.begin(),Acol.end());
VERIFY(is_sorted(Acol.cbegin(),Acol.cend()));
A.setRandom();
// on a row of a rowmajor matrix -> pointer-based iterator and runtime increment
{
i = internal::random<Index>(0,A.rows()-1); typename ColMatrixType::RowXpr Arow = A.row(i);
VERIFY_IS_EQUAL( std::distance(Arow.begin(),Arow.end()), cols);
std::sort(Arow.begin(),Arow.end());
VERIFY(is_sorted(Arow.cbegin(),Arow.cend()));
A.setRandom();
// with a generic iterator
{
Reshaped<RowMatrixType,RowMatrixType::SizeAtCompileTime,1> B1 = B.reshaped();
std::sort(B1.begin(),B1.end());
VERIFY(is_sorted(B1.cbegin(),B1.cend()));
B.setRandom();
// assertion because nested expressions are different // std::sort(B.reshaped().begin(),B.reshaped().end()); // VERIFY(is_sorted(B.reshaped().cbegin(),B.reshaped().cend())); // B.setRandom();
}
}
#if EIGEN_HAS_CXX11 // When the compiler sees expression IsContainerTest<C>(0), if C is an // STL-style container class, the first overload of IsContainerTest // will be viable (since both C::iterator* and C::const_iterator* are // valid types and NULL can be implicitly converted to them). It will // be picked over the second overload as 'int' is a perfect match for // the type of argument 0. If C::iterator or C::const_iterator is not // a valid type, the first overload is not viable, and the second // overload will be picked. template <class C, class Iterator = decltype(::std::declval<const C&>().begin()), class = decltype(::std::declval<const C&>().end()), class = decltype(++::std::declval<Iterator&>()), class = decltype(*::std::declval<Iterator>()), class = typename C::const_iterator> bool IsContainerType(int/* dummy */) { return true; }
template <typename Scalar, int Rows, int Cols> void test_stl_container_detection(int rows=Rows, int cols=Cols)
{ typedef Matrix<Scalar,Rows,1> VectorType; typedef Matrix<Scalar,Rows,Cols,ColMajor> ColMatrixType; typedef Matrix<Scalar,Rows,Cols,RowMajor> RowMatrixType;
ColMatrixType A = ColMatrixType::Random(rows, cols);
RowMatrixType B = RowMatrixType::Random(rows, cols);
Index i = 1;
using ColMatrixColType = decltype(A.col(i)); using ColMatrixRowType = decltype(A.row(i)); using RowMatrixColType = decltype(B.col(i)); using RowMatrixRowType = decltype(B.row(i));
// Vector and matrix col/row are valid Stl-style container.
VERIFY_IS_EQUAL(IsContainerType<VectorType>(0), true);
VERIFY_IS_EQUAL(IsContainerType<ColMatrixColType>(0), true);
VERIFY_IS_EQUAL(IsContainerType<ColMatrixRowType>(0), true);
VERIFY_IS_EQUAL(IsContainerType<RowMatrixColType>(0), true);
VERIFY_IS_EQUAL(IsContainerType<RowMatrixRowType>(0), true);
// But the matrix itself is not a valid Stl-style container.
VERIFY_IS_EQUAL(IsContainerType<ColMatrixType>(0), rows == 1 || cols == 1);
VERIFY_IS_EQUAL(IsContainerType<RowMatrixType>(0), rows == 1 || cols == 1);
} #endif
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.