Commit 7f3eb53a authored by Davis King's avatar Davis King

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
parent 325c7308
...@@ -1531,6 +1531,8 @@ namespace dlib ...@@ -1531,6 +1531,8 @@ namespace dlib
<< "\n\trect.height() (target matrix): " << rect.height() << "\n\trect.height() (target matrix): " << rect.height()
); );
if (exp.destructively_aliases(m) == false)
{
long r_exp = 0; long r_exp = 0;
for (long r = rect.top(); r <= rect.bottom(); ++r) for (long r = rect.top(); r <= rect.bottom(); ++r)
{ {
...@@ -1542,6 +1544,13 @@ namespace dlib ...@@ -1542,6 +1544,13 @@ namespace dlib
} }
++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; return *this;
} }
...@@ -1638,10 +1647,19 @@ namespace dlib ...@@ -1638,10 +1647,19 @@ namespace dlib
<< "\n\tm.nr() (target matrix): " << m.nr() << "\n\tm.nr() (target matrix): " << m.nr()
); );
if (exp.destructively_aliases(m) == false)
{
for (long i = 0; i < m.nr(); ++i) for (long i = 0; i < m.nr(); ++i)
{ {
m(i,col) = exp(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; return *this;
} }
...@@ -1708,10 +1726,19 @@ namespace dlib ...@@ -1708,10 +1726,19 @@ namespace dlib
<< "\n\tm.nc() (target matrix): " << m.nc() << "\n\tm.nc() (target matrix): " << m.nc()
); );
if (exp.destructively_aliases(m) == false)
{
for (long i = 0; i < m.nc(); ++i) for (long i = 0; i < m.nc(); ++i)
{ {
m(row,i) = exp(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; return *this;
} }
......
...@@ -1206,6 +1206,24 @@ namespace ...@@ -1206,6 +1206,24 @@ namespace
DLIB_CASSERT(m1 == res, "m1: \n" << m1 << "\nres: \n" << res); 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);
} }
{ {
......
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