mirror of https://github.com/OpenRCT2/OpenRCT2.git
Simplify iterator class
This commit is contained in:
parent
9c2f545225
commit
8eb7356320
|
@ -262,24 +262,19 @@ int32_t check_for_sprite_list_cycles(bool fix);
|
||||||
int32_t check_for_spatial_index_cycles(bool fix);
|
int32_t check_for_spatial_index_cycles(bool fix);
|
||||||
int32_t fix_disjoint_sprites();
|
int32_t fix_disjoint_sprites();
|
||||||
|
|
||||||
template<typename T = SpriteBase> class EntityTileList
|
template<typename T, uint16_t SpriteBase::*NextList> class EntityIterator
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
uint16_t FirstEntity = SPRITE_INDEX_NULL;
|
|
||||||
|
|
||||||
class EntityTileIterator
|
|
||||||
{
|
|
||||||
private:
|
|
||||||
T* Entity = nullptr;
|
T* Entity = nullptr;
|
||||||
uint16_t NextEntityId = SPRITE_INDEX_NULL;
|
uint16_t NextEntityId = SPRITE_INDEX_NULL;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
EntityTileIterator(const uint16_t _EntityId)
|
EntityIterator(const uint16_t _EntityId)
|
||||||
: NextEntityId(_EntityId)
|
: NextEntityId(_EntityId)
|
||||||
{
|
{
|
||||||
++(*this);
|
++(*this);
|
||||||
}
|
}
|
||||||
EntityTileIterator& operator++()
|
EntityIterator& operator++()
|
||||||
{
|
{
|
||||||
Entity = nullptr;
|
Entity = nullptr;
|
||||||
|
|
||||||
|
@ -291,23 +286,23 @@ private:
|
||||||
NextEntityId = SPRITE_INDEX_NULL;
|
NextEntityId = SPRITE_INDEX_NULL;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
NextEntityId = baseEntity->next_in_quadrant;
|
NextEntityId = baseEntity->*NextList;
|
||||||
Entity = baseEntity->template As<T>();
|
Entity = baseEntity->template As<T>();
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
EntityTileIterator operator++(int)
|
EntityIterator operator++(int)
|
||||||
{
|
{
|
||||||
EntityTileIterator retval = *this;
|
EntityIterator retval = *this;
|
||||||
++(*this);
|
++(*this);
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
bool operator==(EntityTileIterator other) const
|
bool operator==(EntityIterator other) const
|
||||||
{
|
{
|
||||||
return Entity == other.Entity;
|
return Entity == other.Entity;
|
||||||
}
|
}
|
||||||
bool operator!=(EntityTileIterator other) const
|
bool operator!=(EntityIterator other) const
|
||||||
{
|
{
|
||||||
return !(*this == other);
|
return !(*this == other);
|
||||||
}
|
}
|
||||||
|
@ -321,7 +316,13 @@ private:
|
||||||
using pointer = const T*;
|
using pointer = const T*;
|
||||||
using reference = const T&;
|
using reference = const T&;
|
||||||
using iterator_category = std::forward_iterator_tag;
|
using iterator_category = std::forward_iterator_tag;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename T = SpriteBase> class EntityTileList
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
uint16_t FirstEntity = SPRITE_INDEX_NULL;
|
||||||
|
using EntityTileIterator = EntityIterator<T, &SpriteBase::next_in_quadrant>;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
EntityTileList(const CoordsXY& loc)
|
EntityTileList(const CoordsXY& loc)
|
||||||
|
@ -343,62 +344,7 @@ template<typename T = SpriteBase> class EntityList
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
uint16_t FirstEntity = SPRITE_INDEX_NULL;
|
uint16_t FirstEntity = SPRITE_INDEX_NULL;
|
||||||
|
using EntityListIterator = EntityIterator<T, &SpriteBase::next>;
|
||||||
class EntityListIterator
|
|
||||||
{
|
|
||||||
private:
|
|
||||||
T* Entity = nullptr;
|
|
||||||
uint16_t NextEntityId = SPRITE_INDEX_NULL;
|
|
||||||
|
|
||||||
public:
|
|
||||||
EntityListIterator(const uint16_t _EntityId)
|
|
||||||
: NextEntityId(_EntityId)
|
|
||||||
{
|
|
||||||
++(*this);
|
|
||||||
}
|
|
||||||
EntityListIterator& operator++()
|
|
||||||
{
|
|
||||||
Entity = nullptr;
|
|
||||||
|
|
||||||
while (NextEntityId != SPRITE_INDEX_NULL && Entity == nullptr)
|
|
||||||
{
|
|
||||||
auto baseEntity = GetEntity(NextEntityId);
|
|
||||||
if (!baseEntity)
|
|
||||||
{
|
|
||||||
NextEntityId = SPRITE_INDEX_NULL;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
NextEntityId = baseEntity->next;
|
|
||||||
Entity = baseEntity->template As<T>();
|
|
||||||
}
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
EntityListIterator operator++(int)
|
|
||||||
{
|
|
||||||
EntityListIterator retval = *this;
|
|
||||||
++(*this);
|
|
||||||
return retval;
|
|
||||||
}
|
|
||||||
bool operator==(EntityListIterator other) const
|
|
||||||
{
|
|
||||||
return Entity == other.Entity;
|
|
||||||
}
|
|
||||||
bool operator!=(EntityListIterator other) const
|
|
||||||
{
|
|
||||||
return !(*this == other);
|
|
||||||
}
|
|
||||||
T* operator*()
|
|
||||||
{
|
|
||||||
return Entity;
|
|
||||||
}
|
|
||||||
// iterator traits
|
|
||||||
using difference_type = std::ptrdiff_t;
|
|
||||||
using value_type = T;
|
|
||||||
using pointer = const T*;
|
|
||||||
using reference = const T&;
|
|
||||||
using iterator_category = std::forward_iterator_tag;
|
|
||||||
};
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
EntityList(SPRITE_LIST type)
|
EntityList(SPRITE_LIST type)
|
||||||
|
|
Loading…
Reference in New Issue