Try mplementing operator+= (broken but almost there).
This commit is contained in:
parent
9ea9743e95
commit
dbdc814049
4 changed files with 136 additions and 6 deletions
|
@ -1,10 +1,30 @@
|
|||
namespace dk {
|
||||
namespace implem {
|
||||
inline CoordinateScalarType sum_mod (CoordinateScalarType parA, CoordinateScalarType parB, CoordinateScalarType parDiv) {
|
||||
//const auto sum = m_pos[index] + parValue;
|
||||
//const auto sz = m_size[index] + 1;
|
||||
//const auto remainder = sum - (sum / sz) * sz;
|
||||
|
||||
//m_pos[index] = remainder;
|
||||
//parValue /= sz;
|
||||
|
||||
const auto rounded_sum = sum_div(parA, parB, parDiv) * parDiv;
|
||||
return std::max(parA, parB) - rounded_sum + std::min(parA, parB);
|
||||
}
|
||||
|
||||
inline CoordinateScalarType sum_div (CoordinateScalarType parA, CoordinateScalarType parB, CoordinateScalarType parDiv) {
|
||||
const auto x = ((parA % parDiv) + (parB % parDiv)) / parDiv;
|
||||
return parA / parDiv + parB / parDiv + x;
|
||||
}
|
||||
} //namespace implem
|
||||
|
||||
template <uint32_t D>
|
||||
TileCoords<D>::TileCoords (const coords& parSize) :
|
||||
m_pos(CoordinateScalarType(0)),
|
||||
m_size(parSize)
|
||||
{
|
||||
DK_ASSERT(m_pos <= m_size);
|
||||
DK_ASSERT(m_size < coords(std::numeric_limits<CoordinateScalarType>::max()));
|
||||
}
|
||||
|
||||
template <uint32_t D>
|
||||
|
@ -13,6 +33,7 @@ namespace dk {
|
|||
m_size(parSize)
|
||||
{
|
||||
DK_ASSERT(m_pos <= m_size);
|
||||
DK_ASSERT(m_size < coords(std::numeric_limits<CoordinateScalarType>::max()));
|
||||
}
|
||||
|
||||
template <uint32_t D>
|
||||
|
@ -61,13 +82,20 @@ namespace dk {
|
|||
|
||||
template <uint32_t D>
|
||||
const TileCoords<D>& TileCoords<D>::operator+= (CoordinateScalarType parValue) {
|
||||
CoordinateScalarType acc(parValue);
|
||||
CoordinateScalarType div(1);
|
||||
for (std::size_t z = 0; z < D; ++z) {
|
||||
m_pos[z] = (m_pos[z] + acc / div) % m_size[z];
|
||||
div *= m_size[z];
|
||||
acc += m_pos[z];
|
||||
std::size_t index = 0;
|
||||
while (parValue) {
|
||||
//naïve implementation
|
||||
//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;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <uint32_t D>
|
||||
|
@ -79,4 +107,17 @@ namespace dk {
|
|||
bool TileCoords<D>::operator!= (const TileCoords& parOther) const {
|
||||
return not(*this == parOther);
|
||||
}
|
||||
|
||||
template <uint32_t D>
|
||||
const CoordinateScalarType& TileCoords<D>::operator[] (int parIndex) const {
|
||||
DK_ASSERT(parIndex >= 0);
|
||||
DK_ASSERT(static_cast<uint32_t>(parIndex) < D);
|
||||
return m_pos[parIndex];
|
||||
}
|
||||
template <uint32_t D>
|
||||
CoordinateScalarType& TileCoords<D>::operator[] (int parIndex) {
|
||||
DK_ASSERT(parIndex >= 0);
|
||||
DK_ASSERT(static_cast<uint32_t>(parIndex) < D);
|
||||
return m_pos[parIndex];
|
||||
}
|
||||
} //namespace dk
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue