Finish implementing operator+= for positive offsets.

This commit is contained in:
King_DuckZ 2015-08-14 22:13:14 +02:00
parent dbdc814049
commit e3345c5cca
3 changed files with 11 additions and 4 deletions

View file

@ -1,5 +1,8 @@
namespace dk {
namespace implem {
///----------------------------------------------------------------------
///Equivalent to (parA + parB) % parDiv
///----------------------------------------------------------------------
inline CoordinateScalarType sum_mod (CoordinateScalarType parA, CoordinateScalarType parB, CoordinateScalarType parDiv) {
//const auto sum = m_pos[index] + parValue;
//const auto sz = m_size[index] + 1;
@ -12,6 +15,9 @@ namespace dk {
return std::max(parA, parB) - rounded_sum + std::min(parA, parB);
}
///----------------------------------------------------------------------
///Equivalent to (parA + parB) / parDiv
///----------------------------------------------------------------------
inline CoordinateScalarType sum_div (CoordinateScalarType parA, CoordinateScalarType parB, CoordinateScalarType parDiv) {
const auto x = ((parA % parDiv) + (parB % parDiv)) / parDiv;
return parA / parDiv + parB / parDiv + x;
@ -88,12 +94,13 @@ namespace dk {
//parValue += m_pos[index];
//m_pos[index] = parValue % (m_size[index] + 1);
//parValue /= (m_size[index] + 1);
//++index;
//overflow-aware implementation
const auto new_pos = implem::sum_mod(m_pos[index], parValue, m_size[index] + 1);
parValue = implem::sum_div(m_pos[index], parValue, m_size[index] + 1);
m_pos[index] = new_pos;
++index;
}
return *this;
}

View file

@ -81,9 +81,9 @@ TEST(advance, tilecoords) {
}
{
const coords4 max_coords(max_scalar, 800, max_scalar, max_scalar);
const coords4 max_coords(max_scalar - 1, 800, max_scalar - 1, max_scalar - 1);
tcoords4 test(coords4(max_scalar, 800, 1200, 710), max_coords);
tcoords4 test(coords4(max_scalar - 1, 800, 1200, 710), max_coords);
test += max_scalar;
EXPECT_EQ(2147483646, test[0]);
EXPECT_EQ(0, test[1]);

2
tools/calc_advance.rb Normal file → Executable file
View file

@ -17,4 +17,4 @@ def calc(arr, m)
end
p calc([540, 800, 1200, 710], [M - 10, M - 11, M - 12, M - 13])
p calc([M, 800, 1200, 710], [M, 800, M, M])
p calc([M - 1, 800, 1200, 710], [M - 1, 800, M - 1, M - 1])