Fixed an aliasing bug in the set_subm(), set_rowm(), and set_colm()

functions.  It was possible that you could get incorrect results
if you used these functions to copy one part of a matrix to another
part of the same matrix if the two areas overlapped.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%402305
This commit is contained in:
Davis King 2008-06-11 23:33:54 +00:00
parent 325c73087e
commit 7f3eb53a88
2 changed files with 56 additions and 11 deletions

View File

@ -1531,16 +1531,25 @@ namespace dlib
<< "\n\trect.height() (target matrix): " << rect.height()
);
long r_exp = 0;
for (long r = rect.top(); r <= rect.bottom(); ++r)
if (exp.destructively_aliases(m) == false)
{
long c_exp = 0;
for (long c = rect.left(); c <= rect.right(); ++c)
long r_exp = 0;
for (long r = rect.top(); r <= rect.bottom(); ++r)
{
m(r,c) = exp(r_exp,c_exp);
++c_exp;
long c_exp = 0;
for (long c = rect.left(); c <= rect.right(); ++c)
{
m(r,c) = exp(r_exp,c_exp);
++c_exp;
}
++r_exp;
}
++r_exp;
}
else
{
// make a temporary copy of the matrix we are going to assign to m to
// avoid aliasing issues during the copy
this->operator=(tmp(exp));
}
return *this;
@ -1638,9 +1647,18 @@ namespace dlib
<< "\n\tm.nr() (target matrix): " << m.nr()
);
for (long i = 0; i < m.nr(); ++i)
if (exp.destructively_aliases(m) == false)
{
m(i,col) = exp(i);
for (long i = 0; i < m.nr(); ++i)
{
m(i,col) = exp(i);
}
}
else
{
// make a temporary copy of the matrix we are going to assign to m to
// avoid aliasing issues during the copy
this->operator=(tmp(exp));
}
return *this;
@ -1708,9 +1726,18 @@ namespace dlib
<< "\n\tm.nc() (target matrix): " << m.nc()
);
for (long i = 0; i < m.nc(); ++i)
if (exp.destructively_aliases(m) == false)
{
m(row,i) = exp(i);
for (long i = 0; i < m.nc(); ++i)
{
m(row,i) = exp(i);
}
}
else
{
// make a temporary copy of the matrix we are going to assign to m to
// avoid aliasing issues during the copy
this->operator=(tmp(exp));
}
return *this;

View File

@ -1206,6 +1206,24 @@ namespace
DLIB_CASSERT(m1 == res, "m1: \n" << m1 << "\nres: \n" << res);
set_subm(m1,0,0,5,5) = m1*m1;
DLIB_CASSERT(m1 == res*res, "m1: \n" << m1 << "\nres*res: \n" << res*res);
m1 = res;
set_subm(m1,1,1,2,2) = subm(m1,0,0,2,2);
long res_vals2[] = {
9, 9, 9, 9, 9,
0, 9, 9, 0, 0,
0, 0, 1, 0, 2,
0, 0, 2, 2, 2,
0, 0, 2, 2, 0
};
res = res_vals2;
DLIB_CASSERT(m1 == res, "m1: \n" << m1 << "\nres: \n" << res);
}
{