Commit 8557e674 authored by Davis King's avatar Davis King

Added the const_ret_type typedef to the matrix_exp. It is now required that all

matrix expressions define this type.  This enables the expressions to return elements
by constant reference when appropriate rather than always returning by value.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%403432
parent fc8807a7
......@@ -64,6 +64,7 @@ namespace dlib
struct matrix_traits
{
typedef typename EXP::type type;
typedef typename EXP::const_ret_type const_ret_type;
typedef typename EXP::mem_manager_type mem_manager_type;
typedef typename EXP::layout_type layout_type;
const static long NR = EXP::NR;
......@@ -84,6 +85,7 @@ namespace dlib
public:
typedef typename matrix_traits<EXP>::type type;
typedef typename matrix_traits<EXP>::const_ret_type const_ret_type;
typedef typename matrix_traits<EXP>::mem_manager_type mem_manager_type;
typedef typename matrix_traits<EXP>::layout_type layout_type;
const static long NR = matrix_traits<EXP>::NR;
......@@ -93,7 +95,7 @@ namespace dlib
typedef matrix<type,NR,NC,mem_manager_type,layout_type> matrix_type;
typedef EXP exp_type;
inline const type operator() (
inline const const_ret_type operator() (
long r,
long c
) const
......@@ -110,7 +112,7 @@ namespace dlib
return ref()(r,c);
}
const type operator() (
const const_ret_type operator() (
long i
) const
{
......@@ -261,6 +263,7 @@ namespace dlib
struct matrix_traits<matrix_multiply_exp<LHS,RHS> >
{
typedef typename LHS::type type;
typedef typename LHS::type const_ret_type;
typedef typename LHS::mem_manager_type mem_manager_type;
typedef typename LHS::layout_type layout_type;
const static long NR = LHS::NR;
......@@ -303,6 +306,7 @@ namespace dlib
public:
typedef typename matrix_traits<matrix_multiply_exp>::type type;
typedef typename matrix_traits<matrix_multiply_exp>::const_ret_type const_ret_type;
typedef typename matrix_traits<matrix_multiply_exp>::mem_manager_type mem_manager_type;
const static long NR = matrix_traits<matrix_multiply_exp>::NR;
const static long NC = matrix_traits<matrix_multiply_exp>::NC;
......@@ -450,6 +454,7 @@ namespace dlib
struct matrix_traits<matrix_add_exp<LHS,RHS> >
{
typedef typename LHS::type type;
typedef typename LHS::type const_ret_type;
typedef typename LHS::mem_manager_type mem_manager_type;
typedef typename LHS::layout_type layout_type;
const static long NR = (RHS::NR > LHS::NR) ? RHS::NR : LHS::NR;
......@@ -469,6 +474,7 @@ namespace dlib
!*/
public:
typedef typename matrix_traits<matrix_add_exp>::type type;
typedef typename matrix_traits<matrix_add_exp>::const_ret_type const_ret_type;
typedef typename matrix_traits<matrix_add_exp>::mem_manager_type mem_manager_type;
const static long NR = matrix_traits<matrix_add_exp>::NR;
const static long NC = matrix_traits<matrix_add_exp>::NC;
......@@ -555,6 +561,7 @@ namespace dlib
struct matrix_traits<matrix_subtract_exp<LHS,RHS> >
{
typedef typename LHS::type type;
typedef typename LHS::type const_ret_type;
typedef typename LHS::mem_manager_type mem_manager_type;
typedef typename LHS::layout_type layout_type;
const static long NR = (RHS::NR > LHS::NR) ? RHS::NR : LHS::NR;
......@@ -574,6 +581,7 @@ namespace dlib
!*/
public:
typedef typename matrix_traits<matrix_subtract_exp>::type type;
typedef typename matrix_traits<matrix_subtract_exp>::const_ret_type const_ret_type;
typedef typename matrix_traits<matrix_subtract_exp>::mem_manager_type mem_manager_type;
const static long NR = matrix_traits<matrix_subtract_exp>::NR;
const static long NC = matrix_traits<matrix_subtract_exp>::NC;
......@@ -661,6 +669,7 @@ namespace dlib
struct matrix_traits<matrix_div_scal_exp<M> >
{
typedef typename M::type type;
typedef typename M::type const_ret_type;
typedef typename M::mem_manager_type mem_manager_type;
typedef typename M::layout_type layout_type;
const static long NR = M::NR;
......@@ -679,6 +688,7 @@ namespace dlib
!*/
public:
typedef typename matrix_traits<matrix_div_scal_exp>::type type;
typedef typename matrix_traits<matrix_div_scal_exp>::const_ret_type const_ret_type;
typedef typename matrix_traits<matrix_div_scal_exp>::mem_manager_type mem_manager_type;
const static long NR = matrix_traits<matrix_div_scal_exp>::NR;
const static long NC = matrix_traits<matrix_div_scal_exp>::NC;
......@@ -745,6 +755,7 @@ namespace dlib
struct matrix_traits<matrix_mul_scal_exp<M,use_reference> >
{
typedef typename M::type type;
typedef typename M::type const_ret_type;
typedef typename M::mem_manager_type mem_manager_type;
typedef typename M::layout_type layout_type;
const static long NR = M::NR;
......@@ -769,6 +780,7 @@ namespace dlib
!*/
public:
typedef typename matrix_traits<matrix_mul_scal_exp>::type type;
typedef typename matrix_traits<matrix_mul_scal_exp>::const_ret_type const_ret_type;
typedef typename matrix_traits<matrix_mul_scal_exp>::mem_manager_type mem_manager_type;
const static long NR = matrix_traits<matrix_mul_scal_exp>::NR;
const static long NC = matrix_traits<matrix_mul_scal_exp>::NC;
......@@ -973,6 +985,7 @@ namespace dlib
struct matrix_traits<matrix<T,num_rows, num_cols, mem_manager, layout> >
{
typedef T type;
typedef const T& const_ret_type;
typedef mem_manager mem_manager_type;
typedef layout layout_type;
const static long NR = num_rows;
......@@ -995,6 +1008,7 @@ namespace dlib
public:
typedef typename matrix_traits<matrix>::type type;
typedef typename matrix_traits<matrix>::const_ret_type const_ret_type;
typedef typename matrix_traits<matrix>::mem_manager_type mem_manager_type;
typedef typename matrix_traits<matrix>::layout_type layout_type;
const static long NR = matrix_traits<matrix>::NR;
......@@ -1723,6 +1737,7 @@ namespace dlib
struct matrix_traits<const_temp_matrix<EXP> >
{
typedef typename EXP::type type;
typedef typename EXP::const_ret_type const_ret_type;
typedef typename EXP::mem_manager_type mem_manager_type;
typedef typename EXP::layout_type layout_type;
const static long NR = EXP::NR;
......@@ -1735,6 +1750,7 @@ namespace dlib
{
public:
typedef typename matrix_traits<const_temp_matrix>::type type;
typedef typename matrix_traits<const_temp_matrix>::const_ret_type const_ret_type;
typedef typename matrix_traits<const_temp_matrix>::mem_manager_type mem_manager_type;
typedef typename matrix_traits<const_temp_matrix>::layout_type layout_type;
const static long NR = matrix_traits<const_temp_matrix>::NR;
......@@ -1752,12 +1768,12 @@ namespace dlib
ref_(item)
{}
const type operator() (
const const_ret_type operator() (
long r,
long c
) const { return ref_(r,c); }
const type operator() ( long i ) const
const const_ret_type operator() ( long i ) const
{ return ref_(i); }
template <typename U>
......
......@@ -49,10 +49,17 @@ namespace dlib
are going to be accessing the same element over and over it might
be faster to assign the matrix_exp to a temporary matrix and then
use that temporary.
const_ret_type typedef (defined below)
The purpose of the const_ret_type typedef is to allow matrix expressions
to return their elements by reference when appropriate. So const_ret_type
should either be of the same type as "type" or be of type "const type&".
!*/
public:
typedef typename EXP::type type;
typedef typename EXP::const_ret_type const_ret_type;
typedef typename EXP::mem_manager_type mem_manager_type;
typedef typename EXP::layout_type layout_type;
const static long cost = EXP::cost;
......@@ -61,7 +68,7 @@ namespace dlib
typedef matrix<type,NR,NC, mem_manager_type,layout_type> matrix_type;
typedef EXP exp_type;
const type operator() (
const const_ret_type operator() (
long r,
long c
) const;
......@@ -75,7 +82,7 @@ namespace dlib
the matrix represented by this matrix expression)
!*/
const type operator() (
const const_ret_type operator() (
long i
) const;
/*!
......
......@@ -31,9 +31,10 @@ namespace dlib
const static long NR = EXP::NC;
const static long NC = EXP::NR;
typedef typename EXP::type type;
typedef typename EXP::type const_ret_type;
typedef typename EXP::mem_manager_type mem_manager_type;
template <typename M>
static type apply ( const M& m, long r, long c)
static const const_ret_type apply ( const M& m, long r, long c)
{ return std::conj(m(c,r)); }
template <typename M>
......
This diff is collapsed.
......@@ -23,8 +23,9 @@ namespace dlib
{ \
const static long cost = EXP::cost+(extra_cost); \
typedef typename EXP::type type; \
typedef typename EXP::type const_ret_type; \
template <typename M> \
static type apply ( const M& m, long r, long c) \
static const const_ret_type apply ( const M& m, long r, long c) \
{ return static_cast<type>(std::name(m(r,c))); } \
};}; \
template < typename EXP > \
......@@ -65,8 +66,9 @@ DLIB_MATRIX_SIMPLE_STD_FUNCTION(atan,7)
{
const static long cost = EXP::cost+7;
typedef typename EXP::type type;
typedef typename EXP::type const_ret_type;
template <typename M>
static type apply ( const M& m, long r, long c)
static const const_ret_type apply ( const M& m, long r, long c)
{
return static_cast<type>(1/(1 + std::exp(-m(r,c))));
}
......@@ -92,8 +94,9 @@ DLIB_MATRIX_SIMPLE_STD_FUNCTION(atan,7)
{
const static long cost = EXP::cost+7;
typedef typename EXP::type type;
typedef typename EXP::type const_ret_type;
template <typename M, typename T>
static type apply ( const M& m, const T& eps, long r, long c)
static const const_ret_type apply ( const M& m, const T& eps, long r, long c)
{
const type temp = m(r,c);
if (temp >= eps || temp <= -eps)
......@@ -139,8 +142,9 @@ DLIB_MATRIX_SIMPLE_STD_FUNCTION(atan,7)
{
const static long cost = EXP::cost+7;
typedef typename EXP::type type;
typedef typename EXP::type const_ret_type;
template <typename M>
static type apply ( const M& m, long r, long c)
static const const_ret_type apply ( const M& m, long r, long c)
{
const type temp = m(r,c);
return temp*temp*temp;
......@@ -167,8 +171,9 @@ DLIB_MATRIX_SIMPLE_STD_FUNCTION(atan,7)
{
const static long cost = EXP::cost+6;
typedef typename EXP::type type;
typedef typename EXP::type const_ret_type;
template <typename M>
static type apply ( const M& m, long r, long c)
static const const_ret_type apply ( const M& m, long r, long c)
{
const type temp = m(r,c);
return temp*temp;
......@@ -195,8 +200,9 @@ DLIB_MATRIX_SIMPLE_STD_FUNCTION(atan,7)
{
const static long cost = EXP::cost+7;
typedef typename EXP::type type;
typedef typename EXP::type const_ret_type;
template <typename M, typename S>
static type apply ( const M& m, const S& s, long r, long c)
static const const_ret_type apply ( const M& m, const S& s, long r, long c)
{ return static_cast<type>(std::pow(m(r,c),s)); }
};
};
......@@ -228,8 +234,9 @@ DLIB_MATRIX_SIMPLE_STD_FUNCTION(atan,7)
{
const static long cost = EXP::cost+7;
typedef typename EXP::type type;
typedef typename EXP::type const_ret_type;
template <typename M, typename S>
static type apply ( const M& m, const S& s, long r, long c)
static const const_ret_type apply ( const M& m, const S& s, long r, long c)
{ return static_cast<type>(std::pow(s,m(r,c))); }
};
};
......@@ -261,8 +268,9 @@ DLIB_MATRIX_SIMPLE_STD_FUNCTION(atan,7)
{
const static long cost = EXP::cost+6;
typedef typename EXP::type type;
typedef typename EXP::type const_ret_type;
template <typename M>
static type apply ( const M& m, long r, long c)
static const const_ret_type apply ( const M& m, long r, long c)
{
const type temp = m(r,c);
if (temp != static_cast<type>(0))
......@@ -301,8 +309,9 @@ DLIB_MATRIX_SIMPLE_STD_FUNCTION(atan,7)
{
const static long cost = EXP::cost+6;
typedef typename EXP::type type;
typedef typename EXP::type const_ret_type;
template <typename M>
static type apply ( const M& m, long r, long c)
static const const_ret_type apply ( const M& m, long r, long c)
{
const type temp = m(r,c);
if (temp != static_cast<type>(0))
......@@ -338,8 +347,9 @@ DLIB_MATRIX_SIMPLE_STD_FUNCTION(atan,7)
{
const static long cost = EXP::cost+5;
typedef typename EXP::type type;
typedef typename EXP::type const_ret_type;
template <typename M>
static type apply ( const M& m, const type& s, long r, long c)
static const const_ret_type apply ( const M& m, const type& s, long r, long c)
{
return m(r,c)*s;
}
......@@ -377,8 +387,9 @@ DLIB_MATRIX_SIMPLE_STD_FUNCTION(atan,7)
{
const static long cost = EXP::cost+7;
typedef typename EXP::type type;
typedef typename EXP::type const_ret_type;
template <typename M>
static type apply ( const M& m, long r, long c)
static const const_ret_type apply ( const M& m, long r, long c)
{
return static_cast<type>(std::floor(m(r,c)+0.5));
}
......@@ -390,8 +401,9 @@ DLIB_MATRIX_SIMPLE_STD_FUNCTION(atan,7)
{
const static long cost = EXP::cost;
typedef typename EXP::type type;
typedef typename EXP::const_ret_type const_ret_type;
template <typename M>
static type apply ( const M& m, long r, long c)
static const const_ret_type apply ( const M& m, long r, long c)
{
return m(r,c);
}
......@@ -420,8 +432,9 @@ DLIB_MATRIX_SIMPLE_STD_FUNCTION(atan,7)
{
const static long cost = EXP::cost+7;
typedef typename EXP::type type;
typedef typename EXP::type const_ret_type;
template <typename M>
static type apply ( const M& m, long r, long c)
static const const_ret_type apply ( const M& m, long r, long c)
{
return static_cast<type>(std::abs(m(r,c)));
}
......@@ -432,8 +445,9 @@ DLIB_MATRIX_SIMPLE_STD_FUNCTION(atan,7)
{
const static long cost = EXP::cost;
typedef T type;
typedef T const_ret_type;
template <typename M>
static type apply ( const M& m, long r, long c)
static const const_ret_type apply ( const M& m, long r, long c)
{
return static_cast<type>(std::abs(m(r,c)));
}
......@@ -460,8 +474,9 @@ DLIB_MATRIX_SIMPLE_STD_FUNCTION(atan,7)
{
const static long cost = EXP::cost+1;
typedef std::complex<typename EXP::type> type;
typedef std::complex<typename EXP::type> const_ret_type;
template <typename M>
static type apply ( const M& m, long r, long c)
static const const_ret_type apply ( const M& m, long r, long c)
{
return type(m(r,c));
}
......@@ -487,9 +502,10 @@ DLIB_MATRIX_SIMPLE_STD_FUNCTION(atan,7)
{
const static long cost = EXP1::cost+EXP2::cost+1;
typedef std::complex<typename EXP1::type> type;
typedef std::complex<typename EXP1::type> const_ret_type;
template <typename M1, typename M2>
static type apply ( const M1& m1, const M2& m2 , long r, long c)
static const const_ret_type apply ( const M1& m1, const M2& m2 , long r, long c)
{ return type(m1(r,c),m2(r,c)); }
};
};
......@@ -529,8 +545,9 @@ DLIB_MATRIX_SIMPLE_STD_FUNCTION(atan,7)
{
const static long cost = EXP::cost+6;
typedef typename EXP::type::value_type type;
typedef typename EXP::type::value_type const_ret_type;
template <typename M>
static type apply ( const M& m, long r, long c)
static const const_ret_type apply ( const M& m, long r, long c)
{ return std::norm(m(r,c)); }
};
};
......@@ -555,8 +572,9 @@ DLIB_MATRIX_SIMPLE_STD_FUNCTION(atan,7)
{
const static long cost = EXP::cost;
typedef typename EXP::type::value_type type;
typedef typename EXP::type::value_type const_ret_type;
template <typename M>
static type apply ( const M& m, long r, long c)
static const const_ret_type apply ( const M& m, long r, long c)
{ return std::real(m(r,c)); }
};
};
......@@ -581,8 +599,9 @@ DLIB_MATRIX_SIMPLE_STD_FUNCTION(atan,7)
{
const static long cost = EXP::cost;
typedef typename EXP::type::value_type type;
typedef typename EXP::type::value_type const_ret_type;
template <typename M>
static type apply ( const M& m, long r, long c)
static const const_ret_type apply ( const M& m, long r, long c)
{ return std::imag(m(r,c)); }
};
};
......
......@@ -158,9 +158,10 @@ namespace dlib
const static long NR = 1;
const static long NC = EXP::NC;
typedef typename EXP::type type;
typedef typename EXP::const_ret_type const_ret_type;
typedef typename EXP::mem_manager_type mem_manager_type;
template <typename M>
static type apply ( const M& m, long row, long, long c)
static const const_ret_type apply ( const M& m, long row, long, long c)
{ return m(row,c); }
template <typename M>
......@@ -201,9 +202,10 @@ namespace dlib
const static long NR = 1;
const static long NC = 0;
typedef typename EXP::type type;
typedef typename EXP::const_ret_type const_ret_type;
typedef typename EXP::mem_manager_type mem_manager_type;
template <typename M>
static type apply ( const M& m, long row, long , long , long c)
static const const_ret_type apply ( const M& m, long row, long , long , long c)
{ return m(row,c); }
template <typename M>
......@@ -245,12 +247,13 @@ namespace dlib
{
const static long cost = EXP1::cost+EXP2::cost;
typedef typename EXP1::type type;
typedef typename EXP1::const_ret_type const_ret_type;
typedef typename EXP1::mem_manager_type mem_manager_type;
const static long NR = EXP2::NC*EXP2::NR;
const static long NC = EXP1::NC;
template <typename M1, typename M2>
static type apply ( const M1& m1, const M2& rows , long r, long c)
static const const_ret_type apply ( const M1& m1, const M2& rows , long r, long c)
{ return m1(rows(r),c); }
template <typename M1, typename M2>
......@@ -298,9 +301,10 @@ namespace dlib
const static long NR = EXP::NR;
const static long NC = 1;
typedef typename EXP::type type;
typedef typename EXP::const_ret_type const_ret_type;
typedef typename EXP::mem_manager_type mem_manager_type;
template <typename M>
static type apply ( const M& m, long col, long r, long)
static const const_ret_type apply ( const M& m, long col, long r, long)
{ return m(r,col); }
template <typename M>
......@@ -341,9 +345,10 @@ namespace dlib
const static long NR = 0;
const static long NC = 1;
typedef typename EXP::type type;
typedef typename EXP::const_ret_type const_ret_type;
typedef typename EXP::mem_manager_type mem_manager_type;
template <typename M>
static type apply ( const M& m, long col, long , long r, long )
static const const_ret_type apply ( const M& m, long col, long , long r, long )
{ return m(r,col); }
template <typename M>
......@@ -384,13 +389,14 @@ namespace dlib
struct op : has_destructive_aliasing
{
typedef typename EXP1::type type;
typedef typename EXP1::const_ret_type const_ret_type;
typedef typename EXP1::mem_manager_type mem_manager_type;
const static long NR = EXP1::NR;
const static long NC = EXP2::NC*EXP2::NR;
const static long cost = EXP1::cost+EXP2::cost;
template <typename M1, typename M2>
static type apply ( const M1& m1, const M2& cols , long r, long c)
static const const_ret_type apply ( const M1& m1, const M2& cols , long r, long c)
{ return m1(r,cols(c)); }
template <typename M1, typename M2>
......
This diff is collapsed.
......@@ -22,6 +22,7 @@ namespace dlib
struct matrix_traits<kernel_matrix_exp<kernel_type,alloc> >
{
typedef typename kernel_type::scalar_type type;
typedef typename kernel_type::scalar_type const_ret_type;
typedef typename kernel_type::mem_manager_type mem_manager_type;
typedef row_major_layout layout_type;
const static long NR = 0;
......@@ -38,6 +39,7 @@ namespace dlib
typedef typename kernel_type::sample_type sample_type;
public:
typedef typename matrix_traits<kernel_matrix_exp>::type type;
typedef typename matrix_traits<kernel_matrix_exp>::const_ret_type const_ret_type;
typedef typename matrix_traits<kernel_matrix_exp>::mem_manager_type mem_manager_type;
typedef typename matrix_traits<kernel_matrix_exp>::layout_type layout_type;
const static long NR = matrix_traits<kernel_matrix_exp>::NR;
......@@ -114,8 +116,9 @@ namespace dlib
const static long cost = EXP::cost+100;
typedef typename kernel_type::scalar_type type;
typedef typename kernel_type::scalar_type const_ret_type;
template <typename M>
static type apply ( const M& m, const kernel_type& kern, const long r, long c)
static const const_ret_type apply ( const M& m, const kernel_type& kern, const long r, long c)
{
return kern(m(r),m(c));
}
......@@ -161,6 +164,7 @@ namespace dlib
struct matrix_traits<kernel_matrix_exp1<kernel_type,lhs_type> >
{
typedef typename kernel_type::scalar_type type;
typedef typename kernel_type::scalar_type const_ret_type;
typedef typename kernel_type::mem_manager_type mem_manager_type;
typedef row_major_layout layout_type;
const static long NR = 0;
......@@ -177,6 +181,7 @@ namespace dlib
typedef typename kernel_type::sample_type sample_type;
public:
typedef typename matrix_traits<kernel_matrix_exp1>::type type;
typedef typename matrix_traits<kernel_matrix_exp1>::type const_ret_type;
typedef typename matrix_traits<kernel_matrix_exp1>::mem_manager_type mem_manager_type;
typedef typename matrix_traits<kernel_matrix_exp1>::layout_type layout_type;
const static long NR = matrix_traits<kernel_matrix_exp1>::NR;
......@@ -276,6 +281,7 @@ namespace dlib
struct matrix_traits<kernel_matrix_exp2<kernel_type,lhs_type> >
{
typedef typename kernel_type::scalar_type type;
typedef typename kernel_type::scalar_type const_ret_type;
typedef typename kernel_type::mem_manager_type mem_manager_type;
typedef row_major_layout layout_type;
const static long NR = 1;
......@@ -292,6 +298,7 @@ namespace dlib
typedef typename kernel_type::sample_type sample_type;
public:
typedef typename matrix_traits<kernel_matrix_exp2>::type type;
typedef typename matrix_traits<kernel_matrix_exp2>::const_ret_type const_ret_type;
typedef typename matrix_traits<kernel_matrix_exp2>::mem_manager_type mem_manager_type;
typedef typename matrix_traits<kernel_matrix_exp2>::layout_type layout_type;
const static long NR = matrix_traits<kernel_matrix_exp2>::NR;
......@@ -391,6 +398,7 @@ namespace dlib
struct matrix_traits<kernel_matrix_exp3<kernel_type,lhs_type,rhs_type> >
{
typedef typename kernel_type::scalar_type type;
typedef typename kernel_type::scalar_type const_ret_type;
typedef typename kernel_type::mem_manager_type mem_manager_type;
typedef row_major_layout layout_type;
const static long NR = 0;
......@@ -408,6 +416,7 @@ namespace dlib
typedef typename kernel_type::sample_type sample_type;
public:
typedef typename matrix_traits<kernel_matrix_exp3>::type type;
typedef typename matrix_traits<kernel_matrix_exp3>::const_ret_type const_ret_type;
typedef typename matrix_traits<kernel_matrix_exp3>::mem_manager_type mem_manager_type;
typedef typename matrix_traits<kernel_matrix_exp3>::layout_type layout_type;
const static long NR = matrix_traits<kernel_matrix_exp3>::NR;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment