Improved the alias detection capability of kernel_matrix() expressions. Now statements

of the form: sample = kernel_matrix(kern, *, sample) can be used since the aliasing of
sample will be handled.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%403862
This commit is contained in:
Davis King 2010-10-31 14:59:51 +00:00
parent 2a544906e5
commit 64dc4c2378
2 changed files with 25 additions and 1 deletions

View File

@ -112,7 +112,7 @@ namespace dlib
}
template <typename K, typename vect_type1, typename vect_type2>
struct op_kern_mat : does_not_alias
struct op_kern_mat
{
op_kern_mat(
const K& kern_,
@ -149,6 +149,18 @@ namespace dlib
long nr () const { return impl::size<K>(vect1); }
long nc () const { return impl::size<K>(vect2); }
template <typename U> bool aliases ( const matrix_exp<U>& item ) const { return alias_helper(item.ref()); }
template <typename U> bool destructively_aliases ( const matrix_exp<U>& item ) const { return alias_helper(item.ref()); }
template <typename U> bool alias_helper ( const U& ) const { return false; }
typedef typename K::sample_type samp_type;
// Say we destructively alias if one of the vect* objects is actually item.
bool alias_helper (const samp_type& item ) const { return are_same(item, vect1) || are_same(item, vect2); }
template <typename U> bool are_same (const samp_type& a, const U& b) const { return false; }
bool are_same (const samp_type& a, const samp_type& b) const { return (&a == &b); }
};
// ----------------------------------------------------------------------------------------

View File

@ -46,6 +46,7 @@ namespace
std::vector<sample_type> vect2;
const sample_type samp = randm(4,1);
sample_type samp2, samp3;
vect1.push_back(randm(4,1));
vect1.push_back(randm(4,1));
@ -122,6 +123,17 @@ namespace
DLIB_TEST(equal(K, kernel_matrix(kern, samp, vector_to_matrix(vect1))));
samp2 = samp;
samp3 = samp;
// test the alias detection
samp2 = kernel_matrix(kern, vect1, samp2);
DLIB_TEST(equal(samp2, kernel_matrix(kern, vect1, samp)));
samp3 = trans(kernel_matrix(kern, samp3, vect2));
DLIB_TEST(equal(samp3, trans(kernel_matrix(kern, samp, vect2))));
}
};