1*bf2c3715SXin Li // This file is part of Eigen, a lightweight C++ template library
2*bf2c3715SXin Li // for linear algebra.
3*bf2c3715SXin Li //
4*bf2c3715SXin Li // Copyright (C) 2018-2019 Gael Guennebaud <[email protected]>
5*bf2c3715SXin Li //
6*bf2c3715SXin Li // This Source Code Form is subject to the terms of the Mozilla
7*bf2c3715SXin Li // Public License v. 2.0. If a copy of the MPL was not distributed
8*bf2c3715SXin Li // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9*bf2c3715SXin Li
10*bf2c3715SXin Li #include "main.h"
11*bf2c3715SXin Li #include <iterator>
12*bf2c3715SXin Li #include <numeric>
13*bf2c3715SXin Li
14*bf2c3715SXin Li template< class Iterator >
15*bf2c3715SXin Li std::reverse_iterator<Iterator>
make_reverse_iterator(Iterator i)16*bf2c3715SXin Li make_reverse_iterator( Iterator i )
17*bf2c3715SXin Li {
18*bf2c3715SXin Li return std::reverse_iterator<Iterator>(i);
19*bf2c3715SXin Li }
20*bf2c3715SXin Li
21*bf2c3715SXin Li #if !EIGEN_HAS_CXX11
22*bf2c3715SXin Li template<class ForwardIt>
is_sorted_until(ForwardIt firstIt,ForwardIt lastIt)23*bf2c3715SXin Li ForwardIt is_sorted_until(ForwardIt firstIt, ForwardIt lastIt)
24*bf2c3715SXin Li {
25*bf2c3715SXin Li if (firstIt != lastIt) {
26*bf2c3715SXin Li ForwardIt next = firstIt;
27*bf2c3715SXin Li while (++next != lastIt) {
28*bf2c3715SXin Li if (*next < *firstIt)
29*bf2c3715SXin Li return next;
30*bf2c3715SXin Li firstIt = next;
31*bf2c3715SXin Li }
32*bf2c3715SXin Li }
33*bf2c3715SXin Li return lastIt;
34*bf2c3715SXin Li }
35*bf2c3715SXin Li template<class ForwardIt>
is_sorted(ForwardIt firstIt,ForwardIt lastIt)36*bf2c3715SXin Li bool is_sorted(ForwardIt firstIt, ForwardIt lastIt)
37*bf2c3715SXin Li {
38*bf2c3715SXin Li return ::is_sorted_until(firstIt, lastIt) == lastIt;
39*bf2c3715SXin Li }
40*bf2c3715SXin Li #else
41*bf2c3715SXin Li using std::is_sorted;
42*bf2c3715SXin Li #endif
43*bf2c3715SXin Li
44*bf2c3715SXin Li template<typename XprType>
is_pointer_based_stl_iterator(const internal::pointer_based_stl_iterator<XprType> &)45*bf2c3715SXin Li bool is_pointer_based_stl_iterator(const internal::pointer_based_stl_iterator<XprType> &) { return true; }
46*bf2c3715SXin Li
47*bf2c3715SXin Li template<typename XprType>
is_generic_randaccess_stl_iterator(const internal::generic_randaccess_stl_iterator<XprType> &)48*bf2c3715SXin Li bool is_generic_randaccess_stl_iterator(const internal::generic_randaccess_stl_iterator<XprType> &) { return true; }
49*bf2c3715SXin Li
50*bf2c3715SXin Li template<typename Iter>
is_default_constructible_and_assignable(const Iter & it)51*bf2c3715SXin Li bool is_default_constructible_and_assignable(const Iter& it)
52*bf2c3715SXin Li {
53*bf2c3715SXin Li #if EIGEN_HAS_CXX11
54*bf2c3715SXin Li VERIFY(std::is_default_constructible<Iter>::value);
55*bf2c3715SXin Li VERIFY(std::is_nothrow_default_constructible<Iter>::value);
56*bf2c3715SXin Li #endif
57*bf2c3715SXin Li Iter it2;
58*bf2c3715SXin Li it2 = it;
59*bf2c3715SXin Li return (it==it2);
60*bf2c3715SXin Li }
61*bf2c3715SXin Li
62*bf2c3715SXin Li template<typename Xpr>
check_begin_end_for_loop(Xpr xpr)63*bf2c3715SXin Li void check_begin_end_for_loop(Xpr xpr)
64*bf2c3715SXin Li {
65*bf2c3715SXin Li const Xpr& cxpr(xpr);
66*bf2c3715SXin Li Index i = 0;
67*bf2c3715SXin Li
68*bf2c3715SXin Li i = 0;
69*bf2c3715SXin Li for(typename Xpr::iterator it = xpr.begin(); it!=xpr.end(); ++it) { VERIFY_IS_EQUAL(*it,xpr[i++]); }
70*bf2c3715SXin Li
71*bf2c3715SXin Li i = 0;
72*bf2c3715SXin Li for(typename Xpr::const_iterator it = xpr.cbegin(); it!=xpr.cend(); ++it) { VERIFY_IS_EQUAL(*it,xpr[i++]); }
73*bf2c3715SXin Li
74*bf2c3715SXin Li i = 0;
75*bf2c3715SXin Li for(typename Xpr::const_iterator it = cxpr.begin(); it!=cxpr.end(); ++it) { VERIFY_IS_EQUAL(*it,xpr[i++]); }
76*bf2c3715SXin Li
77*bf2c3715SXin Li i = 0;
78*bf2c3715SXin Li for(typename Xpr::const_iterator it = xpr.begin(); it!=xpr.end(); ++it) { VERIFY_IS_EQUAL(*it,xpr[i++]); }
79*bf2c3715SXin Li
80*bf2c3715SXin Li {
81*bf2c3715SXin Li // simple API check
82*bf2c3715SXin Li typename Xpr::const_iterator cit = xpr.begin();
83*bf2c3715SXin Li cit = xpr.cbegin();
84*bf2c3715SXin Li
85*bf2c3715SXin Li #if EIGEN_HAS_CXX11
86*bf2c3715SXin Li auto tmp1 = xpr.begin();
87*bf2c3715SXin Li VERIFY(tmp1==xpr.begin());
88*bf2c3715SXin Li auto tmp2 = xpr.cbegin();
89*bf2c3715SXin Li VERIFY(tmp2==xpr.cbegin());
90*bf2c3715SXin Li #endif
91*bf2c3715SXin Li }
92*bf2c3715SXin Li
93*bf2c3715SXin Li VERIFY( xpr.end() -xpr.begin() == xpr.size() );
94*bf2c3715SXin Li VERIFY( xpr.cend()-xpr.begin() == xpr.size() );
95*bf2c3715SXin Li VERIFY( xpr.end() -xpr.cbegin() == xpr.size() );
96*bf2c3715SXin Li VERIFY( xpr.cend()-xpr.cbegin() == xpr.size() );
97*bf2c3715SXin Li
98*bf2c3715SXin Li if(xpr.size()>0) {
99*bf2c3715SXin Li VERIFY(xpr.begin() != xpr.end());
100*bf2c3715SXin Li VERIFY(xpr.begin() < xpr.end());
101*bf2c3715SXin Li VERIFY(xpr.begin() <= xpr.end());
102*bf2c3715SXin Li VERIFY(!(xpr.begin() == xpr.end()));
103*bf2c3715SXin Li VERIFY(!(xpr.begin() > xpr.end()));
104*bf2c3715SXin Li VERIFY(!(xpr.begin() >= xpr.end()));
105*bf2c3715SXin Li
106*bf2c3715SXin Li VERIFY(xpr.cbegin() != xpr.end());
107*bf2c3715SXin Li VERIFY(xpr.cbegin() < xpr.end());
108*bf2c3715SXin Li VERIFY(xpr.cbegin() <= xpr.end());
109*bf2c3715SXin Li VERIFY(!(xpr.cbegin() == xpr.end()));
110*bf2c3715SXin Li VERIFY(!(xpr.cbegin() > xpr.end()));
111*bf2c3715SXin Li VERIFY(!(xpr.cbegin() >= xpr.end()));
112*bf2c3715SXin Li
113*bf2c3715SXin Li VERIFY(xpr.begin() != xpr.cend());
114*bf2c3715SXin Li VERIFY(xpr.begin() < xpr.cend());
115*bf2c3715SXin Li VERIFY(xpr.begin() <= xpr.cend());
116*bf2c3715SXin Li VERIFY(!(xpr.begin() == xpr.cend()));
117*bf2c3715SXin Li VERIFY(!(xpr.begin() > xpr.cend()));
118*bf2c3715SXin Li VERIFY(!(xpr.begin() >= xpr.cend()));
119*bf2c3715SXin Li }
120*bf2c3715SXin Li }
121*bf2c3715SXin Li
122*bf2c3715SXin Li template<typename Scalar, int Rows, int Cols>
test_stl_iterators(int rows=Rows,int cols=Cols)123*bf2c3715SXin Li void test_stl_iterators(int rows=Rows, int cols=Cols)
124*bf2c3715SXin Li {
125*bf2c3715SXin Li typedef Matrix<Scalar,Rows,1> VectorType;
126*bf2c3715SXin Li #if EIGEN_HAS_CXX11
127*bf2c3715SXin Li typedef Matrix<Scalar,1,Cols> RowVectorType;
128*bf2c3715SXin Li #endif
129*bf2c3715SXin Li typedef Matrix<Scalar,Rows,Cols,ColMajor> ColMatrixType;
130*bf2c3715SXin Li typedef Matrix<Scalar,Rows,Cols,RowMajor> RowMatrixType;
131*bf2c3715SXin Li VectorType v = VectorType::Random(rows);
132*bf2c3715SXin Li const VectorType& cv(v);
133*bf2c3715SXin Li ColMatrixType A = ColMatrixType::Random(rows,cols);
134*bf2c3715SXin Li const ColMatrixType& cA(A);
135*bf2c3715SXin Li RowMatrixType B = RowMatrixType::Random(rows,cols);
136*bf2c3715SXin Li
137*bf2c3715SXin Li Index i, j;
138*bf2c3715SXin Li
139*bf2c3715SXin Li // Verify that iterators are default constructible (See bug #1900)
140*bf2c3715SXin Li {
141*bf2c3715SXin Li VERIFY( is_default_constructible_and_assignable(v.begin()));
142*bf2c3715SXin Li VERIFY( is_default_constructible_and_assignable(v.end()));
143*bf2c3715SXin Li VERIFY( is_default_constructible_and_assignable(cv.begin()));
144*bf2c3715SXin Li VERIFY( is_default_constructible_and_assignable(cv.end()));
145*bf2c3715SXin Li
146*bf2c3715SXin Li VERIFY( is_default_constructible_and_assignable(A.row(0).begin()));
147*bf2c3715SXin Li VERIFY( is_default_constructible_and_assignable(A.row(0).end()));
148*bf2c3715SXin Li VERIFY( is_default_constructible_and_assignable(cA.row(0).begin()));
149*bf2c3715SXin Li VERIFY( is_default_constructible_and_assignable(cA.row(0).end()));
150*bf2c3715SXin Li
151*bf2c3715SXin Li VERIFY( is_default_constructible_and_assignable(B.row(0).begin()));
152*bf2c3715SXin Li VERIFY( is_default_constructible_and_assignable(B.row(0).end()));
153*bf2c3715SXin Li }
154*bf2c3715SXin Li
155*bf2c3715SXin Li // Check we got a fast pointer-based iterator when expected
156*bf2c3715SXin Li {
157*bf2c3715SXin Li VERIFY( is_pointer_based_stl_iterator(v.begin()) );
158*bf2c3715SXin Li VERIFY( is_pointer_based_stl_iterator(v.end()) );
159*bf2c3715SXin Li VERIFY( is_pointer_based_stl_iterator(cv.begin()) );
160*bf2c3715SXin Li VERIFY( is_pointer_based_stl_iterator(cv.end()) );
161*bf2c3715SXin Li
162*bf2c3715SXin Li j = internal::random<Index>(0,A.cols()-1);
163*bf2c3715SXin Li VERIFY( is_pointer_based_stl_iterator(A.col(j).begin()) );
164*bf2c3715SXin Li VERIFY( is_pointer_based_stl_iterator(A.col(j).end()) );
165*bf2c3715SXin Li VERIFY( is_pointer_based_stl_iterator(cA.col(j).begin()) );
166*bf2c3715SXin Li VERIFY( is_pointer_based_stl_iterator(cA.col(j).end()) );
167*bf2c3715SXin Li
168*bf2c3715SXin Li i = internal::random<Index>(0,A.rows()-1);
169*bf2c3715SXin Li VERIFY( is_pointer_based_stl_iterator(A.row(i).begin()) );
170*bf2c3715SXin Li VERIFY( is_pointer_based_stl_iterator(A.row(i).end()) );
171*bf2c3715SXin Li VERIFY( is_pointer_based_stl_iterator(cA.row(i).begin()) );
172*bf2c3715SXin Li VERIFY( is_pointer_based_stl_iterator(cA.row(i).end()) );
173*bf2c3715SXin Li
174*bf2c3715SXin Li VERIFY( is_pointer_based_stl_iterator(A.reshaped().begin()) );
175*bf2c3715SXin Li VERIFY( is_pointer_based_stl_iterator(A.reshaped().end()) );
176*bf2c3715SXin Li VERIFY( is_pointer_based_stl_iterator(cA.reshaped().begin()) );
177*bf2c3715SXin Li VERIFY( is_pointer_based_stl_iterator(cA.reshaped().end()) );
178*bf2c3715SXin Li
179*bf2c3715SXin Li VERIFY( is_pointer_based_stl_iterator(B.template reshaped<AutoOrder>().begin()) );
180*bf2c3715SXin Li VERIFY( is_pointer_based_stl_iterator(B.template reshaped<AutoOrder>().end()) );
181*bf2c3715SXin Li
182*bf2c3715SXin Li VERIFY( is_generic_randaccess_stl_iterator(A.template reshaped<RowMajor>().begin()) );
183*bf2c3715SXin Li VERIFY( is_generic_randaccess_stl_iterator(A.template reshaped<RowMajor>().end()) );
184*bf2c3715SXin Li }
185*bf2c3715SXin Li
186*bf2c3715SXin Li {
187*bf2c3715SXin Li check_begin_end_for_loop(v);
188*bf2c3715SXin Li check_begin_end_for_loop(A.col(internal::random<Index>(0,A.cols()-1)));
189*bf2c3715SXin Li check_begin_end_for_loop(A.row(internal::random<Index>(0,A.rows()-1)));
190*bf2c3715SXin Li check_begin_end_for_loop(v+v);
191*bf2c3715SXin Li }
192*bf2c3715SXin Li
193*bf2c3715SXin Li #if EIGEN_HAS_CXX11
194*bf2c3715SXin Li // check swappable
195*bf2c3715SXin Li {
196*bf2c3715SXin Li using std::swap;
197*bf2c3715SXin Li // pointer-based
198*bf2c3715SXin Li {
199*bf2c3715SXin Li VectorType v_copy = v;
200*bf2c3715SXin Li auto a = v.begin();
201*bf2c3715SXin Li auto b = v.end()-1;
202*bf2c3715SXin Li swap(a,b);
203*bf2c3715SXin Li VERIFY_IS_EQUAL(v,v_copy);
204*bf2c3715SXin Li VERIFY_IS_EQUAL(*b,*v.begin());
205*bf2c3715SXin Li VERIFY_IS_EQUAL(*b,v(0));
206*bf2c3715SXin Li VERIFY_IS_EQUAL(*a,v.end()[-1]);
207*bf2c3715SXin Li VERIFY_IS_EQUAL(*a,v(last));
208*bf2c3715SXin Li }
209*bf2c3715SXin Li
210*bf2c3715SXin Li // generic
211*bf2c3715SXin Li {
212*bf2c3715SXin Li RowMatrixType B_copy = B;
213*bf2c3715SXin Li auto Br = B.reshaped();
214*bf2c3715SXin Li auto a = Br.begin();
215*bf2c3715SXin Li auto b = Br.end()-1;
216*bf2c3715SXin Li swap(a,b);
217*bf2c3715SXin Li VERIFY_IS_EQUAL(B,B_copy);
218*bf2c3715SXin Li VERIFY_IS_EQUAL(*b,*Br.begin());
219*bf2c3715SXin Li VERIFY_IS_EQUAL(*b,Br(0));
220*bf2c3715SXin Li VERIFY_IS_EQUAL(*a,Br.end()[-1]);
221*bf2c3715SXin Li VERIFY_IS_EQUAL(*a,Br(last));
222*bf2c3715SXin Li }
223*bf2c3715SXin Li }
224*bf2c3715SXin Li
225*bf2c3715SXin Li // check non-const iterator with for-range loops
226*bf2c3715SXin Li {
227*bf2c3715SXin Li i = 0;
228*bf2c3715SXin Li for(auto x : v) { VERIFY_IS_EQUAL(x,v[i++]); }
229*bf2c3715SXin Li
230*bf2c3715SXin Li j = internal::random<Index>(0,A.cols()-1);
231*bf2c3715SXin Li i = 0;
232*bf2c3715SXin Li for(auto x : A.col(j)) { VERIFY_IS_EQUAL(x,A(i++,j)); }
233*bf2c3715SXin Li
234*bf2c3715SXin Li i = 0;
235*bf2c3715SXin Li for(auto x : (v+A.col(j))) { VERIFY_IS_APPROX(x,v(i)+A(i,j)); ++i; }
236*bf2c3715SXin Li
237*bf2c3715SXin Li j = 0;
238*bf2c3715SXin Li i = internal::random<Index>(0,A.rows()-1);
239*bf2c3715SXin Li for(auto x : A.row(i)) { VERIFY_IS_EQUAL(x,A(i,j++)); }
240*bf2c3715SXin Li
241*bf2c3715SXin Li i = 0;
242*bf2c3715SXin Li for(auto x : A.reshaped()) { VERIFY_IS_EQUAL(x,A(i++)); }
243*bf2c3715SXin Li }
244*bf2c3715SXin Li
245*bf2c3715SXin Li // same for const_iterator
246*bf2c3715SXin Li {
247*bf2c3715SXin Li i = 0;
248*bf2c3715SXin Li for(auto x : cv) { VERIFY_IS_EQUAL(x,v[i++]); }
249*bf2c3715SXin Li
250*bf2c3715SXin Li i = 0;
251*bf2c3715SXin Li for(auto x : cA.reshaped()) { VERIFY_IS_EQUAL(x,A(i++)); }
252*bf2c3715SXin Li
253*bf2c3715SXin Li j = 0;
254*bf2c3715SXin Li i = internal::random<Index>(0,A.rows()-1);
255*bf2c3715SXin Li for(auto x : cA.row(i)) { VERIFY_IS_EQUAL(x,A(i,j++)); }
256*bf2c3715SXin Li }
257*bf2c3715SXin Li
258*bf2c3715SXin Li // check reshaped() on row-major
259*bf2c3715SXin Li {
260*bf2c3715SXin Li i = 0;
261*bf2c3715SXin Li Matrix<Scalar,Dynamic,Dynamic,ColMajor> Bc = B;
262*bf2c3715SXin Li for(auto x : B.reshaped()) { VERIFY_IS_EQUAL(x,Bc(i++)); }
263*bf2c3715SXin Li }
264*bf2c3715SXin Li
265*bf2c3715SXin Li // check write access
266*bf2c3715SXin Li {
267*bf2c3715SXin Li VectorType w(v.size());
268*bf2c3715SXin Li i = 0;
269*bf2c3715SXin Li for(auto& x : w) { x = v(i++); }
270*bf2c3715SXin Li VERIFY_IS_EQUAL(v,w);
271*bf2c3715SXin Li }
272*bf2c3715SXin Li
273*bf2c3715SXin Li // check for dangling pointers
274*bf2c3715SXin Li {
275*bf2c3715SXin Li // no dangling because pointer-based
276*bf2c3715SXin Li {
277*bf2c3715SXin Li j = internal::random<Index>(0,A.cols()-1);
278*bf2c3715SXin Li auto it = A.col(j).begin();
279*bf2c3715SXin Li for(i=0;i<rows;++i) {
280*bf2c3715SXin Li VERIFY_IS_EQUAL(it[i],A(i,j));
281*bf2c3715SXin Li }
282*bf2c3715SXin Li }
283*bf2c3715SXin Li
284*bf2c3715SXin Li // no dangling because pointer-based
285*bf2c3715SXin Li {
286*bf2c3715SXin Li i = internal::random<Index>(0,A.rows()-1);
287*bf2c3715SXin Li auto it = A.row(i).begin();
288*bf2c3715SXin Li for(j=0;j<cols;++j) { VERIFY_IS_EQUAL(it[j],A(i,j)); }
289*bf2c3715SXin Li }
290*bf2c3715SXin Li
291*bf2c3715SXin Li {
292*bf2c3715SXin Li j = internal::random<Index>(0,A.cols()-1);
293*bf2c3715SXin Li // this would produce a dangling pointer:
294*bf2c3715SXin Li // auto it = (A+2*A).col(j).begin();
295*bf2c3715SXin Li // we need to name the temporary expression:
296*bf2c3715SXin Li auto tmp = (A+2*A).col(j);
297*bf2c3715SXin Li auto it = tmp.begin();
298*bf2c3715SXin Li for(i=0;i<rows;++i) {
299*bf2c3715SXin Li VERIFY_IS_APPROX(it[i],3*A(i,j));
300*bf2c3715SXin Li }
301*bf2c3715SXin Li }
302*bf2c3715SXin Li }
303*bf2c3715SXin Li
304*bf2c3715SXin Li {
305*bf2c3715SXin Li // check basic for loop on vector-wise iterators
306*bf2c3715SXin Li j=0;
307*bf2c3715SXin Li for (auto it = A.colwise().cbegin(); it != A.colwise().cend(); ++it, ++j) {
308*bf2c3715SXin Li VERIFY_IS_APPROX( it->coeff(0), A(0,j) );
309*bf2c3715SXin Li VERIFY_IS_APPROX( (*it).coeff(0), A(0,j) );
310*bf2c3715SXin Li }
311*bf2c3715SXin Li j=0;
312*bf2c3715SXin Li for (auto it = A.colwise().begin(); it != A.colwise().end(); ++it, ++j) {
313*bf2c3715SXin Li (*it).coeffRef(0) = (*it).coeff(0); // compilation check
314*bf2c3715SXin Li it->coeffRef(0) = it->coeff(0); // compilation check
315*bf2c3715SXin Li VERIFY_IS_APPROX( it->coeff(0), A(0,j) );
316*bf2c3715SXin Li VERIFY_IS_APPROX( (*it).coeff(0), A(0,j) );
317*bf2c3715SXin Li }
318*bf2c3715SXin Li
319*bf2c3715SXin Li // check valuetype gives us a copy
320*bf2c3715SXin Li j=0;
321*bf2c3715SXin Li for (auto it = A.colwise().cbegin(); it != A.colwise().cend(); ++it, ++j) {
322*bf2c3715SXin Li typename decltype(it)::value_type tmp = *it;
323*bf2c3715SXin Li VERIFY_IS_NOT_EQUAL( tmp.data() , it->data() );
324*bf2c3715SXin Li VERIFY_IS_APPROX( tmp, A.col(j) );
325*bf2c3715SXin Li }
326*bf2c3715SXin Li }
327*bf2c3715SXin Li
328*bf2c3715SXin Li #endif
329*bf2c3715SXin Li
330*bf2c3715SXin Li if(rows>=3) {
331*bf2c3715SXin Li VERIFY_IS_EQUAL((v.begin()+rows/2)[1], v(rows/2+1));
332*bf2c3715SXin Li
333*bf2c3715SXin Li VERIFY_IS_EQUAL((A.rowwise().begin()+rows/2)[1], A.row(rows/2+1));
334*bf2c3715SXin Li }
335*bf2c3715SXin Li
336*bf2c3715SXin Li if(cols>=3) {
337*bf2c3715SXin Li VERIFY_IS_EQUAL((A.colwise().begin()+cols/2)[1], A.col(cols/2+1));
338*bf2c3715SXin Li }
339*bf2c3715SXin Li
340*bf2c3715SXin Li // check std::sort
341*bf2c3715SXin Li {
342*bf2c3715SXin Li // first check that is_sorted returns false when required
343*bf2c3715SXin Li if(rows>=2)
344*bf2c3715SXin Li {
345*bf2c3715SXin Li v(1) = v(0)-Scalar(1);
346*bf2c3715SXin Li #if EIGEN_HAS_CXX11
347*bf2c3715SXin Li VERIFY(!is_sorted(std::begin(v),std::end(v)));
348*bf2c3715SXin Li #else
349*bf2c3715SXin Li VERIFY(!is_sorted(v.cbegin(),v.cend()));
350*bf2c3715SXin Li #endif
351*bf2c3715SXin Li }
352*bf2c3715SXin Li
353*bf2c3715SXin Li // on a vector
354*bf2c3715SXin Li {
355*bf2c3715SXin Li std::sort(v.begin(),v.end());
356*bf2c3715SXin Li VERIFY(is_sorted(v.begin(),v.end()));
357*bf2c3715SXin Li VERIFY(!::is_sorted(make_reverse_iterator(v.end()),make_reverse_iterator(v.begin())));
358*bf2c3715SXin Li }
359*bf2c3715SXin Li
360*bf2c3715SXin Li // on a column of a column-major matrix -> pointer-based iterator and default increment
361*bf2c3715SXin Li {
362*bf2c3715SXin Li j = internal::random<Index>(0,A.cols()-1);
363*bf2c3715SXin Li // std::sort(begin(A.col(j)),end(A.col(j))); // does not compile because this returns const iterators
364*bf2c3715SXin Li typename ColMatrixType::ColXpr Acol = A.col(j);
365*bf2c3715SXin Li std::sort(Acol.begin(),Acol.end());
366*bf2c3715SXin Li VERIFY(is_sorted(Acol.cbegin(),Acol.cend()));
367*bf2c3715SXin Li A.setRandom();
368*bf2c3715SXin Li
369*bf2c3715SXin Li std::sort(A.col(j).begin(),A.col(j).end());
370*bf2c3715SXin Li VERIFY(is_sorted(A.col(j).cbegin(),A.col(j).cend()));
371*bf2c3715SXin Li A.setRandom();
372*bf2c3715SXin Li }
373*bf2c3715SXin Li
374*bf2c3715SXin Li // on a row of a rowmajor matrix -> pointer-based iterator and runtime increment
375*bf2c3715SXin Li {
376*bf2c3715SXin Li i = internal::random<Index>(0,A.rows()-1);
377*bf2c3715SXin Li typename ColMatrixType::RowXpr Arow = A.row(i);
378*bf2c3715SXin Li VERIFY_IS_EQUAL( std::distance(Arow.begin(),Arow.end()), cols);
379*bf2c3715SXin Li std::sort(Arow.begin(),Arow.end());
380*bf2c3715SXin Li VERIFY(is_sorted(Arow.cbegin(),Arow.cend()));
381*bf2c3715SXin Li A.setRandom();
382*bf2c3715SXin Li
383*bf2c3715SXin Li std::sort(A.row(i).begin(),A.row(i).end());
384*bf2c3715SXin Li VERIFY(is_sorted(A.row(i).cbegin(),A.row(i).cend()));
385*bf2c3715SXin Li A.setRandom();
386*bf2c3715SXin Li }
387*bf2c3715SXin Li
388*bf2c3715SXin Li // with a generic iterator
389*bf2c3715SXin Li {
390*bf2c3715SXin Li Reshaped<RowMatrixType,RowMatrixType::SizeAtCompileTime,1> B1 = B.reshaped();
391*bf2c3715SXin Li std::sort(B1.begin(),B1.end());
392*bf2c3715SXin Li VERIFY(is_sorted(B1.cbegin(),B1.cend()));
393*bf2c3715SXin Li B.setRandom();
394*bf2c3715SXin Li
395*bf2c3715SXin Li // assertion because nested expressions are different
396*bf2c3715SXin Li // std::sort(B.reshaped().begin(),B.reshaped().end());
397*bf2c3715SXin Li // VERIFY(is_sorted(B.reshaped().cbegin(),B.reshaped().cend()));
398*bf2c3715SXin Li // B.setRandom();
399*bf2c3715SXin Li }
400*bf2c3715SXin Li }
401*bf2c3715SXin Li
402*bf2c3715SXin Li // check with partial_sum
403*bf2c3715SXin Li {
404*bf2c3715SXin Li j = internal::random<Index>(0,A.cols()-1);
405*bf2c3715SXin Li typename ColMatrixType::ColXpr Acol = A.col(j);
406*bf2c3715SXin Li std::partial_sum(Acol.begin(), Acol.end(), v.begin());
407*bf2c3715SXin Li VERIFY_IS_APPROX(v(seq(1,last)), v(seq(0,last-1))+Acol(seq(1,last)));
408*bf2c3715SXin Li
409*bf2c3715SXin Li // inplace
410*bf2c3715SXin Li std::partial_sum(Acol.begin(), Acol.end(), Acol.begin());
411*bf2c3715SXin Li VERIFY_IS_APPROX(v, Acol);
412*bf2c3715SXin Li }
413*bf2c3715SXin Li
414*bf2c3715SXin Li // stress random access as required by std::nth_element
415*bf2c3715SXin Li if(rows>=3)
416*bf2c3715SXin Li {
417*bf2c3715SXin Li v.setRandom();
418*bf2c3715SXin Li VectorType v1 = v;
419*bf2c3715SXin Li std::sort(v1.begin(),v1.end());
420*bf2c3715SXin Li std::nth_element(v.begin(), v.begin()+rows/2, v.end());
421*bf2c3715SXin Li VERIFY_IS_APPROX(v1(rows/2), v(rows/2));
422*bf2c3715SXin Li
423*bf2c3715SXin Li v.setRandom();
424*bf2c3715SXin Li v1 = v;
425*bf2c3715SXin Li std::sort(v1.begin()+rows/2,v1.end());
426*bf2c3715SXin Li std::nth_element(v.begin()+rows/2, v.begin()+rows/4, v.end());
427*bf2c3715SXin Li VERIFY_IS_APPROX(v1(rows/4), v(rows/4));
428*bf2c3715SXin Li }
429*bf2c3715SXin Li
430*bf2c3715SXin Li #if EIGEN_HAS_CXX11
431*bf2c3715SXin Li // check rows/cols iterators with range-for loops
432*bf2c3715SXin Li {
433*bf2c3715SXin Li j = 0;
434*bf2c3715SXin Li for(auto c : A.colwise()) { VERIFY_IS_APPROX(c.sum(), A.col(j).sum()); ++j; }
435*bf2c3715SXin Li j = 0;
436*bf2c3715SXin Li for(auto c : B.colwise()) { VERIFY_IS_APPROX(c.sum(), B.col(j).sum()); ++j; }
437*bf2c3715SXin Li
438*bf2c3715SXin Li j = 0;
439*bf2c3715SXin Li for(auto c : B.colwise()) {
440*bf2c3715SXin Li i = 0;
441*bf2c3715SXin Li for(auto& x : c) {
442*bf2c3715SXin Li VERIFY_IS_EQUAL(x, B(i,j));
443*bf2c3715SXin Li x = A(i,j);
444*bf2c3715SXin Li ++i;
445*bf2c3715SXin Li }
446*bf2c3715SXin Li ++j;
447*bf2c3715SXin Li }
448*bf2c3715SXin Li VERIFY_IS_APPROX(A,B);
449*bf2c3715SXin Li B.setRandom();
450*bf2c3715SXin Li
451*bf2c3715SXin Li i = 0;
452*bf2c3715SXin Li for(auto r : A.rowwise()) { VERIFY_IS_APPROX(r.sum(), A.row(i).sum()); ++i; }
453*bf2c3715SXin Li i = 0;
454*bf2c3715SXin Li for(auto r : B.rowwise()) { VERIFY_IS_APPROX(r.sum(), B.row(i).sum()); ++i; }
455*bf2c3715SXin Li }
456*bf2c3715SXin Li
457*bf2c3715SXin Li
458*bf2c3715SXin Li // check rows/cols iterators with STL algorithms
459*bf2c3715SXin Li {
460*bf2c3715SXin Li RowVectorType row = RowVectorType::Random(cols);
461*bf2c3715SXin Li A.rowwise() = row;
462*bf2c3715SXin Li VERIFY( std::all_of(A.rowwise().begin(), A.rowwise().end(), [&row](typename ColMatrixType::RowXpr x) { return internal::isApprox(x.squaredNorm(),row.squaredNorm()); }) );
463*bf2c3715SXin Li VERIFY( std::all_of(A.rowwise().rbegin(), A.rowwise().rend(), [&row](typename ColMatrixType::RowXpr x) { return internal::isApprox(x.squaredNorm(),row.squaredNorm()); }) );
464*bf2c3715SXin Li
465*bf2c3715SXin Li VectorType col = VectorType::Random(rows);
466*bf2c3715SXin Li A.colwise() = col;
467*bf2c3715SXin Li VERIFY( std::all_of(A.colwise().begin(), A.colwise().end(), [&col](typename ColMatrixType::ColXpr x) { return internal::isApprox(x.squaredNorm(),col.squaredNorm()); }) );
468*bf2c3715SXin Li VERIFY( std::all_of(A.colwise().rbegin(), A.colwise().rend(), [&col](typename ColMatrixType::ColXpr x) { return internal::isApprox(x.squaredNorm(),col.squaredNorm()); }) );
469*bf2c3715SXin Li VERIFY( std::all_of(A.colwise().cbegin(), A.colwise().cend(), [&col](typename ColMatrixType::ConstColXpr x) { return internal::isApprox(x.squaredNorm(),col.squaredNorm()); }) );
470*bf2c3715SXin Li VERIFY( std::all_of(A.colwise().crbegin(), A.colwise().crend(), [&col](typename ColMatrixType::ConstColXpr x) { return internal::isApprox(x.squaredNorm(),col.squaredNorm()); }) );
471*bf2c3715SXin Li
472*bf2c3715SXin Li i = internal::random<Index>(0,A.rows()-1);
473*bf2c3715SXin Li A.setRandom();
474*bf2c3715SXin Li A.row(i).setZero();
475*bf2c3715SXin Li VERIFY_IS_EQUAL( std::find_if(A.rowwise().begin(), A.rowwise().end(), [](typename ColMatrixType::RowXpr x) { return x.squaredNorm() == Scalar(0); })-A.rowwise().begin(), i );
476*bf2c3715SXin Li VERIFY_IS_EQUAL( std::find_if(A.rowwise().rbegin(), A.rowwise().rend(), [](typename ColMatrixType::RowXpr x) { return x.squaredNorm() == Scalar(0); })-A.rowwise().rbegin(), (A.rows()-1) - i );
477*bf2c3715SXin Li
478*bf2c3715SXin Li j = internal::random<Index>(0,A.cols()-1);
479*bf2c3715SXin Li A.setRandom();
480*bf2c3715SXin Li A.col(j).setZero();
481*bf2c3715SXin Li VERIFY_IS_EQUAL( std::find_if(A.colwise().begin(), A.colwise().end(), [](typename ColMatrixType::ColXpr x) { return x.squaredNorm() == Scalar(0); })-A.colwise().begin(), j );
482*bf2c3715SXin Li VERIFY_IS_EQUAL( std::find_if(A.colwise().rbegin(), A.colwise().rend(), [](typename ColMatrixType::ColXpr x) { return x.squaredNorm() == Scalar(0); })-A.colwise().rbegin(), (A.cols()-1) - j );
483*bf2c3715SXin Li }
484*bf2c3715SXin Li
485*bf2c3715SXin Li {
486*bf2c3715SXin Li using VecOp = VectorwiseOp<ArrayXXi, 0>;
487*bf2c3715SXin Li STATIC_CHECK(( internal::is_same<VecOp::const_iterator, decltype(std::declval<const VecOp&>().cbegin())>::value ));
488*bf2c3715SXin Li STATIC_CHECK(( internal::is_same<VecOp::const_iterator, decltype(std::declval<const VecOp&>().cend ())>::value ));
489*bf2c3715SXin Li #if EIGEN_COMP_CXXVER>=14
490*bf2c3715SXin Li STATIC_CHECK(( internal::is_same<VecOp::const_iterator, decltype(std::cbegin(std::declval<const VecOp&>()))>::value ));
491*bf2c3715SXin Li STATIC_CHECK(( internal::is_same<VecOp::const_iterator, decltype(std::cend (std::declval<const VecOp&>()))>::value ));
492*bf2c3715SXin Li #endif
493*bf2c3715SXin Li }
494*bf2c3715SXin Li
495*bf2c3715SXin Li #endif
496*bf2c3715SXin Li }
497*bf2c3715SXin Li
498*bf2c3715SXin Li
499*bf2c3715SXin Li #if EIGEN_HAS_CXX11
500*bf2c3715SXin Li // When the compiler sees expression IsContainerTest<C>(0), if C is an
501*bf2c3715SXin Li // STL-style container class, the first overload of IsContainerTest
502*bf2c3715SXin Li // will be viable (since both C::iterator* and C::const_iterator* are
503*bf2c3715SXin Li // valid types and NULL can be implicitly converted to them). It will
504*bf2c3715SXin Li // be picked over the second overload as 'int' is a perfect match for
505*bf2c3715SXin Li // the type of argument 0. If C::iterator or C::const_iterator is not
506*bf2c3715SXin Li // a valid type, the first overload is not viable, and the second
507*bf2c3715SXin Li // overload will be picked.
508*bf2c3715SXin Li template <class C,
509*bf2c3715SXin Li class Iterator = decltype(::std::declval<const C&>().begin()),
510*bf2c3715SXin Li class = decltype(::std::declval<const C&>().end()),
511*bf2c3715SXin Li class = decltype(++::std::declval<Iterator&>()),
512*bf2c3715SXin Li class = decltype(*::std::declval<Iterator>()),
513*bf2c3715SXin Li class = typename C::const_iterator>
IsContainerType(int)514*bf2c3715SXin Li bool IsContainerType(int /* dummy */) { return true; }
515*bf2c3715SXin Li
516*bf2c3715SXin Li template <class C>
IsContainerType(long)517*bf2c3715SXin Li bool IsContainerType(long /* dummy */) { return false; }
518*bf2c3715SXin Li
519*bf2c3715SXin Li template <typename Scalar, int Rows, int Cols>
test_stl_container_detection(int rows=Rows,int cols=Cols)520*bf2c3715SXin Li void test_stl_container_detection(int rows=Rows, int cols=Cols)
521*bf2c3715SXin Li {
522*bf2c3715SXin Li typedef Matrix<Scalar,Rows,1> VectorType;
523*bf2c3715SXin Li typedef Matrix<Scalar,Rows,Cols,ColMajor> ColMatrixType;
524*bf2c3715SXin Li typedef Matrix<Scalar,Rows,Cols,RowMajor> RowMatrixType;
525*bf2c3715SXin Li
526*bf2c3715SXin Li ColMatrixType A = ColMatrixType::Random(rows, cols);
527*bf2c3715SXin Li RowMatrixType B = RowMatrixType::Random(rows, cols);
528*bf2c3715SXin Li
529*bf2c3715SXin Li Index i = 1;
530*bf2c3715SXin Li
531*bf2c3715SXin Li using ColMatrixColType = decltype(A.col(i));
532*bf2c3715SXin Li using ColMatrixRowType = decltype(A.row(i));
533*bf2c3715SXin Li using RowMatrixColType = decltype(B.col(i));
534*bf2c3715SXin Li using RowMatrixRowType = decltype(B.row(i));
535*bf2c3715SXin Li
536*bf2c3715SXin Li // Vector and matrix col/row are valid Stl-style container.
537*bf2c3715SXin Li VERIFY_IS_EQUAL(IsContainerType<VectorType>(0), true);
538*bf2c3715SXin Li VERIFY_IS_EQUAL(IsContainerType<ColMatrixColType>(0), true);
539*bf2c3715SXin Li VERIFY_IS_EQUAL(IsContainerType<ColMatrixRowType>(0), true);
540*bf2c3715SXin Li VERIFY_IS_EQUAL(IsContainerType<RowMatrixColType>(0), true);
541*bf2c3715SXin Li VERIFY_IS_EQUAL(IsContainerType<RowMatrixRowType>(0), true);
542*bf2c3715SXin Li
543*bf2c3715SXin Li // But the matrix itself is not a valid Stl-style container.
544*bf2c3715SXin Li VERIFY_IS_EQUAL(IsContainerType<ColMatrixType>(0), rows == 1 || cols == 1);
545*bf2c3715SXin Li VERIFY_IS_EQUAL(IsContainerType<RowMatrixType>(0), rows == 1 || cols == 1);
546*bf2c3715SXin Li }
547*bf2c3715SXin Li #endif
548*bf2c3715SXin Li
EIGEN_DECLARE_TEST(stl_iterators)549*bf2c3715SXin Li EIGEN_DECLARE_TEST(stl_iterators)
550*bf2c3715SXin Li {
551*bf2c3715SXin Li for(int i = 0; i < g_repeat; i++) {
552*bf2c3715SXin Li CALL_SUBTEST_1(( test_stl_iterators<double,2,3>() ));
553*bf2c3715SXin Li CALL_SUBTEST_1(( test_stl_iterators<float,7,5>() ));
554*bf2c3715SXin Li CALL_SUBTEST_1(( test_stl_iterators<int,Dynamic,Dynamic>(internal::random<int>(5,10), internal::random<int>(5,10)) ));
555*bf2c3715SXin Li CALL_SUBTEST_1(( test_stl_iterators<int,Dynamic,Dynamic>(internal::random<int>(10,200), internal::random<int>(10,200)) ));
556*bf2c3715SXin Li }
557*bf2c3715SXin Li
558*bf2c3715SXin Li #if EIGEN_HAS_CXX11
559*bf2c3715SXin Li CALL_SUBTEST_1(( test_stl_container_detection<float,1,1>() ));
560*bf2c3715SXin Li CALL_SUBTEST_1(( test_stl_container_detection<float,5,5>() ));
561*bf2c3715SXin Li #endif
562*bf2c3715SXin Li }
563