diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2022-11-01 21:21:21 +0100 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2022-11-01 21:21:21 +0100 |
commit | af9e9e64a8dea21868cf3474cda6b8a85ac05442 (patch) | |
tree | 062d96f1f2f79bdd4b2cd4a65d1ec318cd4b6a55 /src | |
parent | 424e48a6779406db64ea6ef32b23cae64eadec5a (diff) |
iterator stuff
Diffstat (limited to 'src')
-rw-r--r-- | src/chunk.cpp | 15 | ||||
-rw-r--r-- | src/chunk.hpp | 26 | ||||
-rw-r--r-- | src/tile-iterator.cpp | 15 | ||||
-rw-r--r-- | src/tile-iterator.hpp | 28 |
4 files changed, 69 insertions, 15 deletions
diff --git a/src/chunk.cpp b/src/chunk.cpp index 59e05d73..b78b8f1a 100644 --- a/src/chunk.cpp +++ b/src/chunk.cpp @@ -19,4 +19,19 @@ bool chunk::empty(bool force) const noexcept return true; } +tile_ref chunk::operator[](std::size_t idx) noexcept { return { *this, std::uint8_t(idx) }; } +tile_proto chunk::operator[](std::size_t idx) const noexcept { return tile_proto(tile_ref { *const_cast<chunk*>(this), std::uint8_t(idx) }); } +tile_ref chunk::operator[](local_coords xy) noexcept { return operator[](xy.to_index()); } +tile_proto chunk::operator[](local_coords xy) const noexcept { return operator[](xy.to_index()); } + +auto chunk::begin() noexcept -> iterator { return iterator { *this, 0 }; } +auto chunk::end() noexcept -> iterator { return iterator { *this, TILE_COUNT }; } +auto chunk::cbegin() const noexcept -> const_iterator { return const_iterator { *this, 0 }; } +auto chunk::cend() const noexcept -> const_iterator { return const_iterator { *this, TILE_COUNT }; } +auto chunk::begin() const noexcept -> const_iterator { return cbegin(); } +auto chunk::end() const noexcept -> const_iterator { return cend(); } + +chunk::chunk(chunk&&) noexcept = default; +chunk& chunk::operator=(chunk&&) noexcept = default; + } // namespace floormat diff --git a/src/chunk.hpp b/src/chunk.hpp index 564f049e..fa3f6979 100644 --- a/src/chunk.hpp +++ b/src/chunk.hpp @@ -12,26 +12,30 @@ struct chunk final friend struct tile_ref; friend struct pass_mode_ref; - tile_ref operator[](std::size_t idx) noexcept { return { *this, std::uint8_t(idx) }; } - tile_proto operator[](std::size_t idx) const noexcept { return tile_proto(tile_ref { *const_cast<chunk*>(this), std::uint8_t(idx) }); } - tile_ref operator[](local_coords xy) noexcept { return operator[](xy.to_index()); } - tile_proto operator[](local_coords xy) const noexcept { return operator[](xy.to_index()); } + tile_ref operator[](std::size_t idx) noexcept; + tile_proto operator[](std::size_t idx) const noexcept; + tile_ref operator[](local_coords xy) noexcept; + tile_proto operator[](local_coords xy) const noexcept; using iterator = tile_iterator; + using const_iterator = tile_const_iterator; - iterator begin() noexcept { return iterator { *this, 0 }; } - iterator end() noexcept { return iterator { *this, TILE_COUNT }; } + iterator begin() noexcept; + iterator end() noexcept; + const_iterator cbegin() const noexcept; + const_iterator cend() const noexcept; + const_iterator begin() const noexcept; + const_iterator end() const noexcept; bool empty(bool force = false) const noexcept; chunk() noexcept = default; - - fm_DECLARE_DELETED_COPY_ASSIGNMENT(chunk); - fm_DECLARE_DEFAULT_MOVE_ASSIGNMENT_(chunk); + chunk(const chunk&) = delete; + chunk& operator=(const chunk&) = delete; + chunk(chunk&&) noexcept; + chunk& operator=(chunk&&) noexcept; private: - static constexpr std::size_t PASS_BITS = 2; - std::array<std::shared_ptr<tile_atlas>, TILE_COUNT> _ground_atlases, _wall_north_atlases, _wall_west_atlases; std::array<std::uint16_t, TILE_COUNT> _ground_variants = {}, _wall_north_variants = {}, _wall_west_variants = {}; std::bitset<TILE_COUNT*2> _passability = {}; diff --git a/src/tile-iterator.cpp b/src/tile-iterator.cpp index 6dfc5f6d..9214faa9 100644 --- a/src/tile-iterator.cpp +++ b/src/tile-iterator.cpp @@ -6,14 +6,21 @@ namespace floormat { tile_iterator::tile_iterator(chunk& c, std::size_t pos) noexcept : c{&c}, pos{pos} {} tile_iterator::tile_iterator(const tile_iterator&) noexcept = default; tile_iterator& tile_iterator::operator=(const tile_iterator&) noexcept = default; - tile_iterator& tile_iterator::operator++() noexcept { pos++; return *this; } tile_iterator tile_iterator::operator++(int) noexcept { auto it = *this; pos++; return it; } void tile_iterator::swap(tile_iterator& other) noexcept { std::swap(c, other.c); std::swap(pos, other.pos); } - bool operator==(const tile_iterator& a, const tile_iterator& b) noexcept { return a.c == b.c && a.pos == b.pos; } - tile_iterator_tuple tile_iterator::operator->() noexcept { return { tile_ref{*c, std::uint8_t(pos)}, pos, local_coords{pos} }; } -tile_iterator_tuple tile_iterator::operator*() noexcept { return { tile_ref{*c, std::uint8_t(pos)}, pos, local_coords{pos} }; } +tile_iterator_tuple tile_iterator::operator*() noexcept { return operator->(); } + +tile_const_iterator::tile_const_iterator(const chunk& c, std::size_t pos) noexcept : c{&c}, pos{pos} {} +tile_const_iterator::tile_const_iterator(const tile_const_iterator& x) noexcept = default; +tile_const_iterator& tile_const_iterator::operator=(const tile_const_iterator& x) noexcept { if (this != &x) { c = x.c; pos = x.pos; } return *this; } +tile_const_iterator& tile_const_iterator::operator++() noexcept { pos++; return *this; } +tile_const_iterator tile_const_iterator::operator++(int) noexcept { auto it = *this; pos++; return it; } +void tile_const_iterator::swap(tile_const_iterator& other) noexcept { std::swap(c, other.c); std::swap(pos, other.pos); } +bool operator==(const tile_const_iterator& a, const tile_const_iterator& b) noexcept { return a.c == b.c && a.pos == b.pos; } +tile_const_iterator_tuple tile_const_iterator::operator->() noexcept { return { tile_proto(tile_ref{*const_cast<chunk*>(c), std::uint8_t(pos)}), pos, local_coords{pos}, }; } +tile_const_iterator_tuple tile_const_iterator::operator*() noexcept { return operator->(); } } // namespace floormat diff --git a/src/tile-iterator.hpp b/src/tile-iterator.hpp index 5d999716..dcec01ab 100644 --- a/src/tile-iterator.hpp +++ b/src/tile-iterator.hpp @@ -15,6 +15,15 @@ struct tile_iterator_tuple final { // NOLINT(cppcoreguidelines-pro-type-member-i local_coords pt; }; +struct tile_const_iterator_tuple final { // NOLINT(cppcoreguidelines-pro-type-member-init) + const tile_const_iterator_tuple* operator->() const noexcept { return this; } + tile_const_iterator_tuple* operator->() noexcept { return this; } + + tile_proto x; + std::size_t k; + local_coords pt; +}; + class tile_iterator final { chunk* c; std::size_t pos; @@ -34,4 +43,23 @@ public: tile_iterator_tuple operator*() noexcept; }; +class tile_const_iterator final { + const chunk* c; + std::size_t pos; + + friend bool operator==(const tile_const_iterator&, const tile_const_iterator&) noexcept; + +public: + explicit tile_const_iterator(const chunk& c, std::size_t pos) noexcept; + tile_const_iterator(const tile_const_iterator&) noexcept; + tile_const_iterator& operator=(const tile_const_iterator&) noexcept; + + void swap(tile_const_iterator& other) noexcept; + + tile_const_iterator& operator++() noexcept; + tile_const_iterator operator++(int) noexcept; + tile_const_iterator_tuple operator->() noexcept; + tile_const_iterator_tuple operator*() noexcept; +}; + } // namespace floormat |