From 4b9331f0866aa0edd9abd6c2e6044b2aeb08ca09 Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Wed, 20 Oct 2021 09:41:27 +0200 Subject: [PATCH] sparse_set: iterator review --- src/entt/entity/sparse_set.hpp | 67 ++++++++++++++++++++-------------- 1 file changed, 39 insertions(+), 28 deletions(-) diff --git a/src/entt/entity/sparse_set.hpp b/src/entt/entity/sparse_set.hpp index c2cc0feda..630194fb0 100644 --- a/src/entt/entity/sparse_set.hpp +++ b/src/entt/entity/sparse_set.hpp @@ -72,38 +72,10 @@ struct sparse_set_iterator final { return (*this + -value); } - difference_type operator-(const sparse_set_iterator &other) const ENTT_NOEXCEPT { - return other.index - index; - } - [[nodiscard]] reference operator[](const difference_type value) const { return *operator+(value); } - [[nodiscard]] bool operator==(const sparse_set_iterator &other) const ENTT_NOEXCEPT { - return other.index == index; - } - - [[nodiscard]] bool operator!=(const sparse_set_iterator &other) const ENTT_NOEXCEPT { - return !(*this == other); - } - - [[nodiscard]] bool operator<(const sparse_set_iterator &other) const ENTT_NOEXCEPT { - return index > other.index; - } - - [[nodiscard]] bool operator>(const sparse_set_iterator &other) const ENTT_NOEXCEPT { - return index < other.index; - } - - [[nodiscard]] bool operator<=(const sparse_set_iterator &other) const ENTT_NOEXCEPT { - return !(*this > other); - } - - [[nodiscard]] bool operator>=(const sparse_set_iterator &other) const ENTT_NOEXCEPT { - return !(*this < other); - } - [[nodiscard]] pointer operator->() const { const auto pos = index - 1; return packed->data() + pos; @@ -113,11 +85,50 @@ struct sparse_set_iterator final { return *operator->(); } + [[nodiscard]] difference_type base() const ENTT_NOEXCEPT { + return index; + } + private: const Container *packed; difference_type index; }; +template +[[nodiscard]] auto operator-(const sparse_set_iterator &lhs, const sparse_set_iterator &rhs) ENTT_NOEXCEPT { + return rhs.base() - lhs.base(); +} + +template +[[nodiscard]] bool operator==(const sparse_set_iterator &lhs, const sparse_set_iterator &rhs) ENTT_NOEXCEPT { + return lhs.base() == rhs.base(); +} + +template +[[nodiscard]] bool operator!=(const sparse_set_iterator &lhs, const sparse_set_iterator &rhs) ENTT_NOEXCEPT { + return !(lhs == rhs); +} + +template +[[nodiscard]] bool operator<(const sparse_set_iterator &lhs, const sparse_set_iterator &rhs) ENTT_NOEXCEPT { + return lhs.base() > rhs.base(); +} + +template +[[nodiscard]] bool operator>(const sparse_set_iterator &lhs, const sparse_set_iterator &rhs) ENTT_NOEXCEPT { + return lhs.base() < rhs.base(); +} + +template +[[nodiscard]] bool operator<=(const sparse_set_iterator &lhs, const sparse_set_iterator &rhs) ENTT_NOEXCEPT { + return !(lhs > rhs); +} + +template +[[nodiscard]] bool operator>=(const sparse_set_iterator &lhs, const sparse_set_iterator &rhs) ENTT_NOEXCEPT { + return !(lhs < rhs); +} + } // namespace internal /**