Rename span/range to seqN/seq

This commit is contained in:
Gael Guennebaud 2017-01-09 17:35:21 +01:00
parent 75aef5b37f
commit 68064e14fa
2 changed files with 88 additions and 69 deletions

View File

@ -87,22 +87,33 @@ inline fix_t<N> fix() { return fix_t<N>(); }
#endif
//--------------------------------------------------------------------------------
// range(first,last,incr) and span(first,size,incr)
// seq(first,last,incr) and seqN(first,size,incr)
//--------------------------------------------------------------------------------
template<typename FirstType=Index,typename LastType=Index,typename IncrType=fix_t<1> >
struct Range_t {
Range_t(FirstType f, LastType l) : m_first(f), m_last(l) {}
Range_t(FirstType f, LastType l, IncrType s) : m_first(f), m_last(l), m_incr(s) {}
template<typename FirstType=Index,typename LastType=Index,typename IncrType=fix_t<1> >
class ArithemeticSequenceProxyWithBounds
{
public:
ArithemeticSequenceProxyWithBounds(FirstType f, LastType l) : m_first(f), m_last(l) {}
ArithemeticSequenceProxyWithBounds(FirstType f, LastType l, IncrType s) : m_first(f), m_last(l), m_incr(s) {}
enum {
SizeAtCompileTime = -1,
IncrAtCompileTime = get_compile_time<IncrType,DynamicIndex>::value
};
Index size() const { return (m_last-m_first+m_incr)/m_incr; }
Index operator[](Index i) const { return m_first + i * m_incr; }
const FirstType& firstObject() const { return m_first; }
const LastType& lastObject() const { return m_last; }
const IncrType& incrObject() const { return m_incr; }
protected:
FirstType m_first;
LastType m_last;
IncrType m_incr;
enum { SizeAtCompileTime = -1 };
Index size() const { return (m_last-m_first+m_incr)/m_incr; }
Index operator[] (Index k) const { return m_first + k*m_incr; }
};
template<typename T> struct cleanup_slice_type { typedef Index type; };
@ -114,45 +125,55 @@ template<int N> struct cleanup_slice_type<fix_t<N> > { typedef fix_t<N> type; };
template<int N> struct cleanup_slice_type<fix_t<N> (*)() > { typedef fix_t<N> type; };
template<typename FirstType,typename LastType>
Range_t<typename cleanup_slice_type<FirstType>::type,typename cleanup_slice_type<LastType>::type >
range(FirstType f, LastType l) {
return Range_t<typename cleanup_slice_type<FirstType>::type,typename cleanup_slice_type<LastType>::type>(f,l);
ArithemeticSequenceProxyWithBounds<typename cleanup_slice_type<FirstType>::type,typename cleanup_slice_type<LastType>::type >
seq(FirstType f, LastType l) {
return ArithemeticSequenceProxyWithBounds<typename cleanup_slice_type<FirstType>::type,typename cleanup_slice_type<LastType>::type>(f,l);
}
template<typename FirstType,typename LastType,typename IncrType>
Range_t<typename cleanup_slice_type<FirstType>::type,typename cleanup_slice_type<LastType>::type,typename cleanup_slice_type<IncrType>::type >
range(FirstType f, LastType l, IncrType s) {
return Range_t<typename cleanup_slice_type<FirstType>::type,typename cleanup_slice_type<LastType>::type,typename cleanup_slice_type<IncrType>::type>(f,l,typename cleanup_slice_type<IncrType>::type(s));
ArithemeticSequenceProxyWithBounds<typename cleanup_slice_type<FirstType>::type,typename cleanup_slice_type<LastType>::type,typename cleanup_slice_type<IncrType>::type >
seq(FirstType f, LastType l, IncrType s) {
return ArithemeticSequenceProxyWithBounds<typename cleanup_slice_type<FirstType>::type,typename cleanup_slice_type<LastType>::type,typename cleanup_slice_type<IncrType>::type>(f,l,typename cleanup_slice_type<IncrType>::type(s));
}
template<typename FirstType=Index,typename SizeType=Index,typename IncrType=fix_t<1> >
struct Span_t {
Span_t(FirstType first, SizeType size) : m_first(first), m_size(size) {}
Span_t(FirstType first, SizeType size, IncrType incr) : m_first(first), m_size(size), m_incr(incr) {}
class ArithemeticSequenceProxyWithSize
{
public:
ArithemeticSequenceProxyWithSize(FirstType first, SizeType size) : m_first(first), m_size(size) {}
ArithemeticSequenceProxyWithSize(FirstType first, SizeType size, IncrType incr) : m_first(first), m_size(size), m_incr(incr) {}
enum {
SizeAtCompileTime = get_compile_time<SizeType>::value,
IncrAtCompileTime = get_compile_time<IncrType,DynamicIndex>::value
};
Index size() const { return m_size; }
Index operator[](Index i) const { return m_first + i * m_incr; }
const FirstType& firstObject() const { return m_first; }
const SizeType& sizeObject() const { return m_size; }
const IncrType& incrObject() const { return m_incr; }
protected:
FirstType m_first;
SizeType m_size;
IncrType m_incr;
enum { SizeAtCompileTime = get_compile_time<SizeType>::value };
Index size() const { return m_size; }
Index operator[] (Index k) const { return m_first + k*m_incr; }
};
template<typename FirstType,typename SizeType,typename IncrType>
Span_t<typename cleanup_slice_type<FirstType>::type,typename cleanup_slice_type<SizeType>::type,typename cleanup_slice_type<IncrType>::type >
span(FirstType first, SizeType size, IncrType incr) {
return Span_t<typename cleanup_slice_type<FirstType>::type,typename cleanup_slice_type<SizeType>::type,typename cleanup_slice_type<IncrType>::type>(first,size,incr);
ArithemeticSequenceProxyWithSize<typename cleanup_slice_type<FirstType>::type,typename cleanup_slice_type<SizeType>::type,typename cleanup_slice_type<IncrType>::type >
seqN(FirstType first, SizeType size, IncrType incr) {
return ArithemeticSequenceProxyWithSize<typename cleanup_slice_type<FirstType>::type,typename cleanup_slice_type<SizeType>::type,typename cleanup_slice_type<IncrType>::type>(first,size,incr);
}
template<typename FirstType,typename SizeType>
Span_t<typename cleanup_slice_type<FirstType>::type,typename cleanup_slice_type<SizeType>::type >
span(FirstType first, SizeType size) {
return Span_t<typename cleanup_slice_type<FirstType>::type,typename cleanup_slice_type<SizeType>::type>(first,size);
ArithemeticSequenceProxyWithSize<typename cleanup_slice_type<FirstType>::type,typename cleanup_slice_type<SizeType>::type >
seqN(FirstType first, SizeType size) {
return ArithemeticSequenceProxyWithSize<typename cleanup_slice_type<FirstType>::type,typename cleanup_slice_type<SizeType>::type>(first,size);
}
@ -188,12 +209,12 @@ template<typename T, typename EnableIf = void> struct get_compile_time_incr {
};
template<typename FirstType,typename LastType,typename IncrType>
struct get_compile_time_incr<Range_t<FirstType,LastType,IncrType> > {
struct get_compile_time_incr<ArithemeticSequenceProxyWithBounds<FirstType,LastType,IncrType> > {
enum { value = get_compile_time<IncrType,DynamicIndex>::value };
};
template<typename FirstType,typename SizeType,typename IncrType>
struct get_compile_time_incr<Span_t<FirstType,SizeType,IncrType> > {
struct get_compile_time_incr<ArithemeticSequenceProxyWithSize<FirstType,SizeType,IncrType> > {
enum { value = get_compile_time<IncrType,DynamicIndex>::value };
};
@ -239,24 +260,24 @@ Index symbolic2value(shifted_end x, Index size) { return size+x.offset; }
// Convert a symbolic range into a usable one (i.e., remove last/end "keywords")
template<typename FirstType,typename LastType,typename IncrType>
struct MakeIndexing<Range_t<FirstType,LastType,IncrType> > {
typedef Range_t<Index,Index,IncrType> type;
struct MakeIndexing<ArithemeticSequenceProxyWithBounds<FirstType,LastType,IncrType> > {
typedef ArithemeticSequenceProxyWithBounds<Index,Index,IncrType> type;
};
template<typename FirstType,typename LastType,typename IncrType>
Range_t<Index,Index,IncrType> make_indexing(const Range_t<FirstType,LastType,IncrType>& ids, Index size) {
return Range_t<Index,Index,IncrType>(symbolic2value(ids.m_first,size),symbolic2value(ids.m_last,size),ids.m_incr);
ArithemeticSequenceProxyWithBounds<Index,Index,IncrType> make_indexing(const ArithemeticSequenceProxyWithBounds<FirstType,LastType,IncrType>& ids, Index size) {
return ArithemeticSequenceProxyWithBounds<Index,Index,IncrType>(symbolic2value(ids.firstObject(),size),symbolic2value(ids.lastObject(),size),ids.incrObject());
}
// Convert a symbolic span into a usable one (i.e., remove last/end "keywords")
template<typename FirstType,typename SizeType,typename IncrType>
struct MakeIndexing<Span_t<FirstType,SizeType,IncrType> > {
typedef Span_t<Index,SizeType,IncrType> type;
struct MakeIndexing<ArithemeticSequenceProxyWithSize<FirstType,SizeType,IncrType> > {
typedef ArithemeticSequenceProxyWithSize<Index,SizeType,IncrType> type;
};
template<typename FirstType,typename SizeType,typename IncrType>
Span_t<Index,SizeType,IncrType> make_indexing(const Span_t<FirstType,SizeType,IncrType>& ids, Index size) {
return Span_t<Index,SizeType,IncrType>(symbolic2value(ids.m_first,size),ids.m_size,ids.m_incr);
ArithemeticSequenceProxyWithSize<Index,SizeType,IncrType> make_indexing(const ArithemeticSequenceProxyWithSize<FirstType,SizeType,IncrType>& ids, Index size) {
return ArithemeticSequenceProxyWithSize<Index,SizeType,IncrType>(symbolic2value(ids.firstObject(),size),ids.sizeObject(),ids.incrObject());
}
// Convert a symbolic 'all' into a usable range

View File

@ -52,11 +52,11 @@ void check_indexed_view()
std::valarray<int> vali(4); Map<ArrayXi>(&vali[0],4) = eii;
std::vector<int> veci(4); Map<ArrayXi>(veci.data(),4) = eii;
VERIFY( MATCH( A(3, range(9,3,-1)),
VERIFY( MATCH( A(3, seq(9,3,-1)),
"309 308 307 306 305 304 303")
);
VERIFY( MATCH( A(span(2,5), range(9,3,-1)),
VERIFY( MATCH( A(seqN(2,5), seq(9,3,-1)),
"209 208 207 206 205 204 203\n"
"309 308 307 306 305 304 303\n"
"409 408 407 406 405 404 403\n"
@ -64,7 +64,7 @@ void check_indexed_view()
"609 608 607 606 605 604 603")
);
VERIFY( MATCH( A(span(2,5), 5),
VERIFY( MATCH( A(seqN(2,5), 5),
"205\n"
"305\n"
"405\n"
@ -72,7 +72,7 @@ void check_indexed_view()
"605")
);
VERIFY( MATCH( A(span(last,5,-1), range(2,last)),
VERIFY( MATCH( A(seqN(last,5,-1), seq(2,last)),
"902 903 904 905 906 907 908 909\n"
"802 803 804 805 806 807 808 809\n"
"702 703 704 705 706 707 708 709\n"
@ -95,7 +95,7 @@ void check_indexed_view()
);
// takes the row numer 3, and repeat it 5 times
VERIFY( MATCH( A(span(3,5,0), all),
VERIFY( MATCH( A(seqN(3,5,0), all),
"300 301 302 303 304 305 306 307 308 309\n"
"300 301 302 303 304 305 306 307 308 309\n"
"300 301 302 303 304 305 306 307 308 309\n"
@ -105,28 +105,28 @@ void check_indexed_view()
Array44i B;
B.setRandom();
VERIFY( (A(span(2,5), 5)).ColsAtCompileTime == 1);
VERIFY( (A(span(2,5), 5)).RowsAtCompileTime == Dynamic);
VERIFY_IS_EQUAL( (A(span(2,5), 5)).InnerStrideAtCompileTime , A.InnerStrideAtCompileTime);
VERIFY_IS_EQUAL( (A(span(2,5), 5)).OuterStrideAtCompileTime , A.col(5).OuterStrideAtCompileTime);
VERIFY( (A(seqN(2,5), 5)).ColsAtCompileTime == 1);
VERIFY( (A(seqN(2,5), 5)).RowsAtCompileTime == Dynamic);
VERIFY_IS_EQUAL( (A(seqN(2,5), 5)).InnerStrideAtCompileTime , A.InnerStrideAtCompileTime);
VERIFY_IS_EQUAL( (A(seqN(2,5), 5)).OuterStrideAtCompileTime , A.col(5).OuterStrideAtCompileTime);
VERIFY_IS_EQUAL( (A(5,span(2,5))).InnerStrideAtCompileTime , A.row(5).InnerStrideAtCompileTime);
VERIFY_IS_EQUAL( (A(5,span(2,5))).OuterStrideAtCompileTime , A.row(5).OuterStrideAtCompileTime);
VERIFY_IS_EQUAL( (B(1,span(1,2))).InnerStrideAtCompileTime , B.row(1).InnerStrideAtCompileTime);
VERIFY_IS_EQUAL( (B(1,span(1,2))).OuterStrideAtCompileTime , B.row(1).OuterStrideAtCompileTime);
VERIFY_IS_EQUAL( (A(5,seqN(2,5))).InnerStrideAtCompileTime , A.row(5).InnerStrideAtCompileTime);
VERIFY_IS_EQUAL( (A(5,seqN(2,5))).OuterStrideAtCompileTime , A.row(5).OuterStrideAtCompileTime);
VERIFY_IS_EQUAL( (B(1,seqN(1,2))).InnerStrideAtCompileTime , B.row(1).InnerStrideAtCompileTime);
VERIFY_IS_EQUAL( (B(1,seqN(1,2))).OuterStrideAtCompileTime , B.row(1).OuterStrideAtCompileTime);
VERIFY_IS_EQUAL( (A(span(2,5), range(1,3))).InnerStrideAtCompileTime , A.InnerStrideAtCompileTime);
VERIFY_IS_EQUAL( (A(span(2,5), range(1,3))).OuterStrideAtCompileTime , A.OuterStrideAtCompileTime);
VERIFY_IS_EQUAL( (B(span(1,2), range(1,3))).InnerStrideAtCompileTime , B.InnerStrideAtCompileTime);
VERIFY_IS_EQUAL( (B(span(1,2), range(1,3))).OuterStrideAtCompileTime , B.OuterStrideAtCompileTime);
VERIFY_IS_EQUAL( (A(span(2,5,2), range(1,3,2))).InnerStrideAtCompileTime , Dynamic);
VERIFY_IS_EQUAL( (A(span(2,5,2), range(1,3,2))).OuterStrideAtCompileTime , Dynamic);
VERIFY_IS_EQUAL( (A(span(2,5,fix<2>), range(1,3,fix<3>))).InnerStrideAtCompileTime , 2);
VERIFY_IS_EQUAL( (A(span(2,5,fix<2>), range(1,3,fix<3>))).OuterStrideAtCompileTime , Dynamic);
VERIFY_IS_EQUAL( (B(span(1,2,fix<2>), range(1,3,fix<3>))).InnerStrideAtCompileTime , 2);
VERIFY_IS_EQUAL( (B(span(1,2,fix<2>), range(1,3,fix<3>))).OuterStrideAtCompileTime , 3*4);
VERIFY_IS_EQUAL( (A(seqN(2,5), seq(1,3))).InnerStrideAtCompileTime , A.InnerStrideAtCompileTime);
VERIFY_IS_EQUAL( (A(seqN(2,5), seq(1,3))).OuterStrideAtCompileTime , A.OuterStrideAtCompileTime);
VERIFY_IS_EQUAL( (B(seqN(1,2), seq(1,3))).InnerStrideAtCompileTime , B.InnerStrideAtCompileTime);
VERIFY_IS_EQUAL( (B(seqN(1,2), seq(1,3))).OuterStrideAtCompileTime , B.OuterStrideAtCompileTime);
VERIFY_IS_EQUAL( (A(seqN(2,5,2), seq(1,3,2))).InnerStrideAtCompileTime , Dynamic);
VERIFY_IS_EQUAL( (A(seqN(2,5,2), seq(1,3,2))).OuterStrideAtCompileTime , Dynamic);
VERIFY_IS_EQUAL( (A(seqN(2,5,fix<2>), seq(1,3,fix<3>))).InnerStrideAtCompileTime , 2);
VERIFY_IS_EQUAL( (A(seqN(2,5,fix<2>), seq(1,3,fix<3>))).OuterStrideAtCompileTime , Dynamic);
VERIFY_IS_EQUAL( (B(seqN(1,2,fix<2>), seq(1,3,fix<3>))).InnerStrideAtCompileTime , 2);
VERIFY_IS_EQUAL( (B(seqN(1,2,fix<2>), seq(1,3,fix<3>))).OuterStrideAtCompileTime , 3*4);
VERIFY( (A(span(2,fix<5>), 5)).RowsAtCompileTime == 5);
VERIFY( (A(seqN(2,fix<5>), 5)).RowsAtCompileTime == 5);
VERIFY( (A(4, all)).ColsAtCompileTime == Dynamic);
VERIFY( (A(4, all)).RowsAtCompileTime == 1);
VERIFY( (B(1, all)).ColsAtCompileTime == 4);
@ -139,12 +139,10 @@ void check_indexed_view()
VERIFY_IS_EQUAL( (A(eii, eii)).InnerStrideAtCompileTime, 0);
VERIFY_IS_EQUAL( (A(eii, eii)).OuterStrideAtCompileTime, 0);
#if EIGEN_HAS_CXX11
VERIFY( (A(all, std::array<int,4>{{1,3,2,4}})).ColsAtCompileTime == 4);
VERIFY_IS_APPROX( (A(std::array<int,3>{{1,3,5}}, std::array<int,4>{{9,6,3,0}})), A(span(1,3,2), span(9,4,-3)) );
VERIFY_IS_APPROX( (A(std::array<int,3>{{1,3,5}}, std::array<int,4>{{9,6,3,0}})), A(seqN(1,3,2), seqN(9,4,-3)) );
#if (!EIGEN_COMP_CLANG) || (EIGEN_COMP_CLANG>=308 && !defined(__apple_build_version__))
VERIFY_IS_APPROX( A({3, 1, 6, 5}, all), A(std::array<int,4>{{3, 1, 6, 5}}, all) );