Codechange: introduce iterator to iterate over the Tiles of a Map

Note: this version is using TileIndex, but that changes later
This commit is contained in:
Rubidium
2023-01-21 17:07:06 +01:00
committed by rubidium42
parent 20a1b24b45
commit 0fb0469e47
2 changed files with 81 additions and 49 deletions

View File

@@ -36,6 +36,34 @@ extern TileExtended *_me;
*/
struct Map {
private:
/**
* Iterator to iterate all Tiles
*/
struct Iterator {
typedef TileIndex value_type;
typedef TileIndex *pointer;
typedef TileIndex &reference;
typedef size_t difference_type;
typedef std::forward_iterator_tag iterator_category;
explicit Iterator(TileIndex index) : index(index) {}
bool operator==(const Iterator &other) const { return this->index == other.index; }
bool operator!=(const Iterator &other) const { return !(*this == other); }
TileIndex operator*() const { return this->index; }
Iterator & operator++() { this->index++; return *this; }
private:
TileIndex index;
};
/*
* Iterable ensemble of all Tiles
*/
struct IterateWrapper {
Iterator begin() { return Iterator(0); }
Iterator end() { return Iterator(Map::Size()); }
bool empty() { return false; }
};
static uint log_x; ///< 2^_map_log_x == _map_size_x
static uint log_y; ///< 2^_map_log_y == _map_size_y
static uint size_x; ///< Size of the map along the X
@@ -158,6 +186,12 @@ public:
{
return _m != nullptr;
}
/**
* Returns an iterable ensemble of all Tiles
* @return an iterable ensemble of all Tiles
*/
static IterateWrapper Iterate() { return IterateWrapper(); }
};
/**