mirror of
https://gitlab.com/libeigen/eigen.git
synced 2025-06-04 18:54:00 +08:00
Use Index instead of ptrdiff_t or int, fix random-accessors.
This commit is contained in:
parent
b0c66adfb1
commit
37e29fc893
@ -13,7 +13,7 @@ template<typename XprType,typename Derived>
|
|||||||
class DenseStlIteratorBase
|
class DenseStlIteratorBase
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
typedef std::ptrdiff_t difference_type;
|
typedef Index difference_type;
|
||||||
typedef std::random_access_iterator_tag iterator_category;
|
typedef std::random_access_iterator_tag iterator_category;
|
||||||
|
|
||||||
DenseStlIteratorBase() : mp_xpr(0), m_index(0) {}
|
DenseStlIteratorBase() : mp_xpr(0), m_index(0) {}
|
||||||
@ -30,13 +30,13 @@ public:
|
|||||||
Derived operator++(int) { Derived prev(derived()); operator++(); return prev;}
|
Derived operator++(int) { Derived prev(derived()); operator++(); return prev;}
|
||||||
Derived operator--(int) { Derived prev(derived()); operator--(); return prev;}
|
Derived operator--(int) { Derived prev(derived()); operator--(); return prev;}
|
||||||
|
|
||||||
friend Derived operator+(const DenseStlIteratorBase& a, int b) { Derived ret(a.derived()); ret += b; return ret; }
|
friend Derived operator+(const DenseStlIteratorBase& a, Index b) { Derived ret(a.derived()); ret += b; return ret; }
|
||||||
friend Derived operator-(const DenseStlIteratorBase& a, int b) { Derived ret(a.derived()); ret -= b; return ret; }
|
friend Derived operator-(const DenseStlIteratorBase& a, Index b) { Derived ret(a.derived()); ret -= b; return ret; }
|
||||||
friend Derived operator+(int a, const DenseStlIteratorBase& b) { Derived ret(b.derived()); ret += a; return ret; }
|
friend Derived operator+(Index a, const DenseStlIteratorBase& b) { Derived ret(b.derived()); ret += a; return ret; }
|
||||||
friend Derived operator-(int a, const DenseStlIteratorBase& b) { Derived ret(b.derived()); ret -= a; return ret; }
|
friend Derived operator-(Index a, const DenseStlIteratorBase& b) { Derived ret(b.derived()); ret -= a; return ret; }
|
||||||
|
|
||||||
Derived& operator+=(int b) { m_index += b; return derived(); }
|
Derived& operator+=(Index b) { m_index += b; return derived(); }
|
||||||
Derived& operator-=(int b) { m_index -= b; return derived(); }
|
Derived& operator-=(Index b) { m_index -= b; return derived(); }
|
||||||
|
|
||||||
difference_type operator-(const DenseStlIteratorBase& other) const { eigen_assert(mp_xpr == other.mp_xpr);return m_index - other.m_index; }
|
difference_type operator-(const DenseStlIteratorBase& other) const { eigen_assert(mp_xpr == other.mp_xpr);return m_index - other.m_index; }
|
||||||
|
|
||||||
@ -85,8 +85,7 @@ public:
|
|||||||
DenseStlIterator(XprType& xpr, Index index) : Base(xpr,index) {}
|
DenseStlIterator(XprType& xpr, Index index) : Base(xpr,index) {}
|
||||||
|
|
||||||
reference operator*() const { return (*mp_xpr)(m_index); }
|
reference operator*() const { return (*mp_xpr)(m_index); }
|
||||||
reference operator[](int i) const { return (*mp_xpr)(i); }
|
reference operator[](Index i) const { return (*mp_xpr)(m_index+i); }
|
||||||
|
|
||||||
pointer operator->() const { return &((*mp_xpr)(m_index)); }
|
pointer operator->() const { return &((*mp_xpr)(m_index)); }
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -155,8 +154,7 @@ public:
|
|||||||
DenseColStlIterator(XprType& xpr, Index index) : Base(xpr,index) {}
|
DenseColStlIterator(XprType& xpr, Index index) : Base(xpr,index) {}
|
||||||
|
|
||||||
reference operator*() const { return (*mp_xpr).col(m_index); }
|
reference operator*() const { return (*mp_xpr).col(m_index); }
|
||||||
reference operator[](int i) const { return (*mp_xpr).col(i); }
|
reference operator[](Index i) const { return (*mp_xpr).col(m_index+i); }
|
||||||
|
|
||||||
pointer operator->() const { return &((*mp_xpr).col(m_index)); }
|
pointer operator->() const { return &((*mp_xpr).col(m_index)); }
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -180,8 +178,7 @@ public:
|
|||||||
DenseRowStlIterator(XprType& xpr, Index index) : Base(xpr,index) {}
|
DenseRowStlIterator(XprType& xpr, Index index) : Base(xpr,index) {}
|
||||||
|
|
||||||
reference operator*() const { return (*mp_xpr).row(m_index); }
|
reference operator*() const { return (*mp_xpr).row(m_index); }
|
||||||
reference operator[](int i) const { return (*mp_xpr).row(i); }
|
reference operator[](Index i) const { return (*mp_xpr).row(m_index+i); }
|
||||||
|
|
||||||
pointer operator->() const { return &((*mp_xpr).row(m_index)); }
|
pointer operator->() const { return &((*mp_xpr).row(m_index)); }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -59,6 +59,16 @@ void test_range_for_loop(int rows=Rows, int cols=Cols)
|
|||||||
VERIFY_IS_EQUAL(v,w);
|
VERIFY_IS_EQUAL(v,w);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
if(rows>=3) {
|
||||||
|
VERIFY_IS_EQUAL((v.begin()+rows/2)[1], v(rows/2+1));
|
||||||
|
|
||||||
|
VERIFY_IS_EQUAL((A.allRows().begin()+rows/2)[1], A.row(rows/2+1));
|
||||||
|
}
|
||||||
|
|
||||||
|
if(cols>=3) {
|
||||||
|
VERIFY_IS_EQUAL((A.allCols().begin()+cols/2)[1], A.col(cols/2+1));
|
||||||
|
}
|
||||||
|
|
||||||
if(rows>=2)
|
if(rows>=2)
|
||||||
{
|
{
|
||||||
v(1) = v(0)-Scalar(1);
|
v(1) = v(0)-Scalar(1);
|
||||||
@ -84,11 +94,27 @@ void test_range_for_loop(int rows=Rows, int cols=Cols)
|
|||||||
j = internal::random<Index>(0,A.cols()-1);
|
j = internal::random<Index>(0,A.cols()-1);
|
||||||
typename ColMatrixType::ColXpr Acol = A.col(j);
|
typename ColMatrixType::ColXpr Acol = A.col(j);
|
||||||
std::partial_sum(begin(Acol), end(Acol), begin(v));
|
std::partial_sum(begin(Acol), end(Acol), begin(v));
|
||||||
VERIFY_IS_APPROX(v(seq(1,last)), v(seq(0,last-1))+Acol(seq(1,last)));
|
VERIFY_IS_EQUAL(v(seq(1,last)), v(seq(0,last-1))+Acol(seq(1,last)));
|
||||||
|
|
||||||
// inplace
|
// inplace
|
||||||
std::partial_sum(begin(Acol), end(Acol), begin(Acol));
|
std::partial_sum(begin(Acol), end(Acol), begin(Acol));
|
||||||
VERIFY_IS_APPROX(v, Acol);
|
VERIFY_IS_EQUAL(v, Acol);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(rows>=3)
|
||||||
|
{
|
||||||
|
// stress random access
|
||||||
|
v.setRandom();
|
||||||
|
VectorType v1 = v;
|
||||||
|
std::sort(begin(v1),end(v1));
|
||||||
|
std::nth_element(v.begin(), v.begin()+rows/2, v.end());
|
||||||
|
VERIFY_IS_APPROX(v1(rows/2), v(rows/2));
|
||||||
|
|
||||||
|
v.setRandom();
|
||||||
|
v1 = v;
|
||||||
|
std::sort(begin(v1)+rows/2,end(v1));
|
||||||
|
std::nth_element(v.begin()+rows/2, v.begin()+rows/4, v.end());
|
||||||
|
VERIFY_IS_APPROX(v1(rows/4), v(rows/4));
|
||||||
}
|
}
|
||||||
|
|
||||||
#if EIGEN_HAS_CXX11
|
#if EIGEN_HAS_CXX11
|
||||||
|
Loading…
x
Reference in New Issue
Block a user