DoorKeeper/include/implem/tileiterator.inl
King_DuckZ 436d738620 Refactoring in map reading code - part 1/2
Fixed a bug when iterating over a view that was smaller than the map size
Added a class to abstract map readers
2015-01-05 18:01:28 +01:00

110 lines
3 KiB
C++

namespace dk {
namespace implem {
template <size_t D>
inline size_t get_index_from_pos (const Vector<CoordinateScalarType, D>& parPos, const Vector<CoordinateScalarType, D>& parSize) {
size_t index = 0;
for (size_t d = 0; d < D; ++d) {
size_t pos = static_cast<size_t>(parPos[D - 1 - d]);
for (size_t p = 0; p < D - 1 - d; ++p) {
pos *= static_cast<size_t>(parSize[p]);
}
index += pos;
}
return index;
}
#if defined(NDEBUG)
template <>
inline size_t get_index_from_pos<2> (const Vector<CoordinateScalarType, 2>& parPos, const Vector<CoordinateScalarType, 2>& parSize) {
return parPos.y() * parSize.x() + parPos.x();
}
#endif
template <size_t D>
inline Vector<CoordinateScalarType, D> buildPastEndCoordinate (const Vector<CoordinateScalarType, D>& parFrom, const Vector<CoordinateScalarType, D>& parTo) {
Vector<CoordinateScalarType, D> retval;
for (size_t d = 0; d < D - 1; ++d) {
retval[d] = parFrom[d];
}
retval[D - 1] = parTo[D - 1];
return retval;
}
} //namespace implem
template <typename T, size_t D, typename T1>
TileIterator<T, D, T1>::TileIterator() :
m_pos(CoordinateScalarType()),
m_from(CoordinateScalarType()),
m_to(CoordinateScalarType()),
m_areafrom(CoordinateScalarType()),
m_areato(CoordinateScalarType()),
m_data(nullptr)
{
}
template <typename T, size_t D, typename T1>
TileIterator<T, D, T1>::TileIterator (qualif_vector_type* parData, const coords& parFrom, const coords& parTo) :
m_pos(parFrom),
m_from(parFrom),
m_to(parTo),
m_areafrom(parFrom),
m_areato(parTo),
m_data(parData)
{
DK_ASSERT(parData);
}
template <typename T, size_t D, typename T1>
TileIterator<T, D, T1>::TileIterator (qualif_vector_type* parData, const coords& parFrom, const coords& parTo, const coords& parAreaFrom, const coords& parAreaTo) :
m_pos(parFrom),
m_from(parFrom),
m_to(parTo),
m_areafrom(parAreaFrom),
m_areato(parAreaTo),
m_data(parData)
{
DK_ASSERT(parData);
}
template <typename T, size_t D, typename T1>
void TileIterator<T, D, T1>::increment() {
for (size_t d = 0; d < D - 1; ++d) {
++m_pos[d];
if (m_pos[d] >= m_to[d])
m_pos[d] = m_from[d];
else
return;
}
++m_pos[D - 1];
}
template <typename T, size_t D, typename T1>
void TileIterator<T, D, T1>::decrement() {
for (size_t d = 0; d < D; ++d) {
if (m_pos[d] > m_from[d]) {
--m_pos[d];
return;
}
else {
m_pos[d] = m_areato[d];
}
}
++m_pos[D - 1];
}
template <typename T, size_t D, typename T1>
void TileIterator<T, D, T1>::advance (size_t parAdvance) {
//TODO: implement
}
template <typename T, size_t D, typename T1>
ptrdiff_t TileIterator<T, D, T1>::distance_to (const TileIterator& parOther) {
return std::distance(this->get_current_index(), parOther.get_current_index());
}
template <typename T, size_t D, typename T1>
bool TileIterator<T, D, T1>::equal (const TileIterator& parOther) const {
return m_data == parOther.m_data and m_pos == parOther.m_pos;
}
} //namespace dk