From 4b1f025fece2e7e83a24e5350b797532720c93f3 Mon Sep 17 00:00:00 2001 From: Davis King Date: Sat, 14 Nov 2009 02:40:09 +0000 Subject: [PATCH] Slightly simplified and generalized the matrix expression code related to aliasing detection. --HG-- extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%403270 --- dlib/matrix/matrix.h | 72 ++++++------- dlib/matrix/matrix_abstract.h | 15 +-- dlib/matrix/matrix_expressions.h | 168 +++++++++++++++---------------- 3 files changed, 128 insertions(+), 127 deletions(-) diff --git a/dlib/matrix/matrix.h b/dlib/matrix/matrix.h index 45633b3f3..850fb0156 100644 --- a/dlib/matrix/matrix.h +++ b/dlib/matrix/matrix.h @@ -146,14 +146,14 @@ namespace dlib long nc ( ) const { return get_nc_helper::get(ref()); } - template + template bool aliases ( - const matrix& item + const matrix_exp& item ) const { return ref().aliases(item); } - template + template bool destructively_aliases ( - const matrix& item + const matrix_exp& item ) const { return ref().destructively_aliases(item); } inline const exp_type& ref ( @@ -359,14 +359,14 @@ namespace dlib long nc ( ) const { return rhs.nc(); } - template + template bool aliases ( - const matrix& item + const matrix_exp& item ) const { return lhs.aliases(item) || rhs.aliases(item); } - template + template bool destructively_aliases ( - const matrix& item + const matrix_exp& item ) const { return aliases(item); } LHS_ref_type lhs; @@ -508,14 +508,14 @@ namespace dlib inline const type operator() ( long i ) const { return matrix_exp::operator()(i); } - template + template bool aliases ( - const matrix& item + const matrix_exp& item ) const { return lhs.aliases(item) || rhs.aliases(item); } - template + template bool destructively_aliases ( - const matrix& item + const matrix_exp& item ) const { return lhs.destructively_aliases(item) || rhs.destructively_aliases(item); } long nr ( @@ -614,14 +614,14 @@ namespace dlib inline const type operator() ( long i ) const { return matrix_exp::operator()(i); } - template + template bool aliases ( - const matrix& item + const matrix_exp& item ) const { return lhs.aliases(item) || rhs.aliases(item); } - template + template bool destructively_aliases ( - const matrix& item + const matrix_exp& item ) const { return lhs.destructively_aliases(item) || rhs.destructively_aliases(item); } long nr ( @@ -701,14 +701,14 @@ namespace dlib inline const type operator() ( long i ) const { return matrix_exp::operator()(i); } - template + template bool aliases ( - const matrix& item + const matrix_exp& item ) const { return m.aliases(item); } - template + template bool destructively_aliases ( - const matrix& item + const matrix_exp& item ) const { return m.destructively_aliases(item); } long nr ( @@ -791,14 +791,14 @@ namespace dlib inline const type operator() ( long i ) const { return matrix_exp::operator()(i); } - template + template bool aliases ( - const matrix& item + const matrix_exp& item ) const { return m.aliases(item); } - template + template bool destructively_aliases ( - const matrix& item + const matrix_exp& item ) const { return m.destructively_aliases(item); } long nr ( @@ -1488,20 +1488,20 @@ namespace dlib data.swap(item.data); } - template + template bool aliases ( - const matrix& - ) const { return false; } - - template - bool destructively_aliases ( - const matrix& + const matrix_exp& item ) const { return false; } bool aliases ( - const matrix& item + const matrix_exp >& item ) const { return (this == &item); } + template + bool destructively_aliases ( + const matrix_exp& + ) const { return false; } + private: struct literal_assign_helper { @@ -1752,14 +1752,14 @@ namespace dlib const type operator() ( long i ) const { return ref_(i); } - template + template bool aliases ( - const matrix& item + const matrix_exp& item ) const { return ref_.aliases(item); } - template + template bool destructively_aliases ( - const matrix& item + const matrix_exp& item ) const { return ref_.destructively_aliases(item); } long nr ( diff --git a/dlib/matrix/matrix_abstract.h b/dlib/matrix/matrix_abstract.h index 16043137f..c7df87393 100644 --- a/dlib/matrix/matrix_abstract.h +++ b/dlib/matrix/matrix_abstract.h @@ -123,22 +123,23 @@ namespace dlib - returns nr()*nc() !*/ - template + template bool aliases ( - const matrix& item + const matrix_exp& item ) const; /*! ensures - - if (this matrix expression contains/aliases the given matrix or contains - any subexpressions that contain/alias the given matrix) then + - if (A change to the state of item could cause a change to the state of *this + matrix_exp object. ) then - returns true + - This happens when this matrix_exp contains item in some way. - else - returns false !*/ - template + template bool destructively_aliases ( - const matrix& item + const matrix_exp& item ) const; /*! ensures @@ -148,7 +149,7 @@ namespace dlib (i.e. if this expression has different dimensions than item then we have destructive aliasing) - - returns true if the following expression would evaluate incorrectly: + - returns true if the following assignment would evaluate incorrectly: for (long r = 0; r < nr(); ++r) for (long c = 0; c < nc(); ++c) item(r,c) = (*this)(r,c) diff --git a/dlib/matrix/matrix_expressions.h b/dlib/matrix/matrix_expressions.h index b9271e45c..4b6007da9 100644 --- a/dlib/matrix/matrix_expressions.h +++ b/dlib/matrix/matrix_expressions.h @@ -102,34 +102,34 @@ namespace dlib struct has_destructive_aliasing { - template + template static bool destructively_aliases ( const M& m, - const matrix& item + const matrix_exp& item ) { return m.aliases(item); } - template + template static bool destructively_aliases ( const M1& m1, const M2& m2, - const matrix& item + const matrix_exp& item ) { return m1.aliases(item) || m2.aliases(item) ; } - template + template static bool destructively_aliases ( const M1& m1, const M2& m2, const M3& m3, - const matrix& item + const matrix_exp& item ) { return m1.aliases(item) || m2.aliases(item) || m3.aliases(item); } - template + template static bool destructively_aliases ( const M1& m1, const M2& m2, const M3& m3, const M4& m4, - const matrix& item + const matrix_exp& item ) { return m1.aliases(item) || m2.aliases(item) || m3.aliases(item) || m4.aliases(item); } }; @@ -137,34 +137,34 @@ namespace dlib struct has_nondestructive_aliasing { - template + template static bool destructively_aliases ( const M& m, - const matrix& item + const matrix_exp& item ) { return m.destructively_aliases(item); } - template + template static bool destructively_aliases ( const M1& m1, const M2& m2, - const matrix& item + const matrix_exp& item ) { return m1.destructively_aliases(item) || m2.destructively_aliases(item) ; } - template + template static bool destructively_aliases ( const M1& m1, const M2& m2, const M3& m3, - const matrix& item + const matrix_exp& item ) { return m1.destructively_aliases(item) || m2.destructively_aliases(item) || m3.destructively_aliases(item) ; } - template + template static bool destructively_aliases ( const M1& m1, const M2& m2, const M3& m3, const M4& m4, - const matrix& item + const matrix& item ) { return m1.destructively_aliases(item) || m2.destructively_aliases(item) || m3.destructively_aliases(item) || @@ -261,14 +261,14 @@ namespace dlib const typename OP::type operator() ( long i ) const { return matrix_exp::operator()(i); } - template + template bool aliases ( - const matrix& item + const matrix_exp& item ) const { return m.aliases(item); } - template + template bool destructively_aliases ( - const matrix& item + const matrix_exp& item ) const { return OP::destructively_aliases(m,item); } long nr ( @@ -343,14 +343,14 @@ namespace dlib const typename OP::type operator() ( long i ) const { return matrix_exp::operator()(i); } - template + template bool aliases ( - const matrix& item + const matrix_exp& item ) const { return m.aliases(item); } - template + template bool destructively_aliases ( - const matrix& item + const matrix_exp& item ) const { return OP::destructively_aliases(m,item); } long nr ( @@ -428,14 +428,14 @@ namespace dlib const typename OP::type operator() ( long i ) const { return matrix_exp::operator()(i); } - template + template bool aliases ( - const matrix& item + const matrix_exp& item ) const { return m.aliases(item); } - template + template bool destructively_aliases ( - const matrix& item + const matrix_exp& item ) const { return OP::destructively_aliases(m,item); } long nr ( @@ -510,14 +510,14 @@ namespace dlib const typename OP::type operator() ( long i ) const { return matrix_exp::operator()(i); } - template + template bool aliases ( - const matrix& item + const matrix_exp& item ) const { return m1.aliases(item) || m2.aliases(item); } - template + template bool destructively_aliases ( - const matrix& item + const matrix_exp& item ) const { return OP::destructively_aliases(m1,m2,item); } long nr ( @@ -595,14 +595,14 @@ namespace dlib const typename OP::type operator() ( long i ) const { return matrix_exp::operator()(i); } - template + template bool aliases ( - const matrix& item + const matrix_exp& item ) const { return m1.aliases(item) || m2.aliases(item) || m3.aliases(item); } - template + template bool destructively_aliases ( - const matrix& item + const matrix_exp& item ) const { return OP::destructively_aliases(m1,m2,m3,item); } long nr ( @@ -684,14 +684,14 @@ namespace dlib const typename OP::type operator() ( long i ) const { return matrix_exp::operator()(i); } - template + template bool aliases ( - const matrix& item + const matrix_exp& item ) const { return m1.aliases(item) || m2.aliases(item) || m3.aliases(item) || m4.aliases(item); } - template + template bool destructively_aliases ( - const matrix& item + const matrix_exp& item ) const { return OP::destructively_aliases(m1,m2,m3,m4,item); } long nr ( @@ -763,14 +763,14 @@ namespace dlib const typename OP::type operator() ( long i ) const { return matrix_exp::operator()(i); } - template + template bool aliases ( - const matrix& + const matrix_exp& ) const { return false; } - template + template bool destructively_aliases ( - const matrix& + const matrix_exp& ) const { return false; } long nr ( @@ -837,14 +837,14 @@ namespace dlib const typename OP::type operator() ( long i ) const { return matrix_exp::operator()(i); } - template + template bool aliases ( - const matrix& + const matrix_exp& ) const { return false; } - template + template bool destructively_aliases ( - const matrix& + const matrix_exp& ) const { return false; } long nr ( @@ -899,14 +899,14 @@ namespace dlib const typename OP::type operator() ( long i ) const { return matrix_exp::operator()(i); } - template + template bool aliases ( - const matrix& item + const matrix_exp& item ) const { return false; } - template + template bool destructively_aliases ( - const matrix& item + const matrix_exp& item ) const { return false; } long nr ( @@ -981,14 +981,14 @@ namespace dlib const typename M::type operator() ( long i ) const { return matrix_exp::operator()(i); } - template + template bool aliases ( - const matrix& item + const matrix_exp& item ) const { return m.aliases(item) || rows.aliases(item) || cols.aliases(item); } - template + template bool destructively_aliases ( - const matrix& item + const matrix_exp& item ) const { return m.aliases(item) || rows.aliases(item) || cols.aliases(item); } long nr ( @@ -1059,14 +1059,14 @@ namespace dlib const typename M::value_type& operator() ( long i ) const { return m[i]; } - template + template bool aliases ( - const matrix& + const matrix_exp& ) const { return false; } - template + template bool destructively_aliases ( - const matrix& + const matrix_exp& ) const { return false; } long nr ( @@ -1134,14 +1134,14 @@ namespace dlib const typename M::type& operator() ( long i ) const { return m[i]; } - template + template bool aliases ( - const matrix& item + const matrix_exp& item ) const { return false; } - template + template bool destructively_aliases ( - const matrix& item + const matrix_exp& item ) const { return false; } long nr ( @@ -1209,14 +1209,14 @@ namespace dlib const typename M::type operator() ( long i ) const { return matrix_exp::operator()(i); } - template + template bool aliases ( - const matrix& + const matrix_exp& ) const { return false; } - template + template bool destructively_aliases ( - const matrix& + const matrix_exp& ) const { return false; } long nr ( @@ -1291,14 +1291,14 @@ namespace dlib const typename M::type operator() ( long i ) const { return matrix_exp::operator()(i); } - template + template bool aliases ( - const matrix& item + const matrix_exp& item ) const { return m.aliases(item); } - template + template bool destructively_aliases ( - const matrix& item + const matrix_exp& item ) const { return m.aliases(item); } long nr ( @@ -1396,14 +1396,14 @@ namespace dlib long c ) const { return start + c*inc; } - template + template bool aliases ( - const matrix& + const matrix_exp& ) const { return false; } - template + template bool destructively_aliases ( - const matrix& + const matrix_exp& ) const { return false; } long nr ( @@ -1474,14 +1474,14 @@ namespace dlib long c ) const { return std::pow(10,start + c*inc); } - template + template bool aliases ( - const matrix& + const matrix_exp& ) const { return false; } - template + template bool destructively_aliases ( - const matrix& + const matrix_exp& ) const { return false; } long nr ( @@ -1537,14 +1537,14 @@ namespace dlib long c ) const { return start + c*inc; } - template + template bool aliases ( - const matrix& item + const matrix_exp& item ) const { return false; } - template + template bool destructively_aliases ( - const matrix& item + const matrix_exp& item ) const { return false; } long nr (