diff --git a/src/entt/container/dense_hash_map.hpp b/src/entt/container/dense_hash_map.hpp index 802d44b32..5b731b512 100644 --- a/src/entt/container/dense_hash_map.hpp +++ b/src/entt/container/dense_hash_map.hpp @@ -109,9 +109,14 @@ public: return *operator->(); } - [[nodiscard]] iterator_type base() const ENTT_NOEXCEPT { - return it; - } + template + friend auto operator-(const dense_hash_map_iterator &, const dense_hash_map_iterator &) ENTT_NOEXCEPT; + + template + friend bool operator==(const dense_hash_map_iterator &, const dense_hash_map_iterator &) ENTT_NOEXCEPT; + + template + friend bool operator<(const dense_hash_map_iterator &, const dense_hash_map_iterator &) ENTT_NOEXCEPT; private: iterator_type it; @@ -119,12 +124,12 @@ private: template [[nodiscard]] auto operator-(const dense_hash_map_iterator &lhs, const dense_hash_map_iterator &rhs) ENTT_NOEXCEPT { - return lhs.base() - rhs.base(); + return lhs.it - rhs.it; } template [[nodiscard]] bool operator==(const dense_hash_map_iterator &lhs, const dense_hash_map_iterator &rhs) ENTT_NOEXCEPT { - return lhs.base() == rhs.base(); + return lhs.it == rhs.it; } template @@ -134,12 +139,12 @@ template template [[nodiscard]] bool operator<(const dense_hash_map_iterator &lhs, const dense_hash_map_iterator &rhs) ENTT_NOEXCEPT { - return lhs.base() < rhs.base(); + return lhs.it < rhs.it; } template [[nodiscard]] bool operator>(const dense_hash_map_iterator &lhs, const dense_hash_map_iterator &rhs) ENTT_NOEXCEPT { - return lhs.base() > rhs.base(); + return rhs < lhs; } template @@ -170,15 +175,15 @@ public: dense_hash_map_local_iterator(iterator_type iter, const std::size_t pos) ENTT_NOEXCEPT : it{iter}, - curr{pos} {} + offset{pos} {} template>, typename = std::enable_if_t> dense_hash_map_local_iterator(const dense_hash_map_local_iterator> *> &other) : it{other.it}, - curr{other.curr} {} + offset{other.offset} {} dense_hash_map_local_iterator &operator++() ENTT_NOEXCEPT { - return curr = it[curr].next, *this; + return offset = it[offset].next, *this; } dense_hash_map_local_iterator operator++(int) ENTT_NOEXCEPT { @@ -187,28 +192,25 @@ public: } [[nodiscard]] pointer operator->() const { - return std::addressof(it[curr].element); + return std::addressof(it[offset].element); } [[nodiscard]] reference operator*() const { return *operator->(); } - [[nodiscard]] iterator_type base() const ENTT_NOEXCEPT { - return (it + curr); + [[nodiscard]] std::size_t index() const ENTT_NOEXCEPT { + return offset; } - template - friend bool operator==(const dense_hash_map_local_iterator &, const dense_hash_map_local_iterator &) ENTT_NOEXCEPT; - private: iterator_type it; - std::size_t curr; + std::size_t offset; }; template [[nodiscard]] bool operator==(const dense_hash_map_local_iterator &lhs, const dense_hash_map_local_iterator &rhs) ENTT_NOEXCEPT { - return lhs.curr == rhs.curr; + return lhs.index() == rhs.index(); } template @@ -257,7 +259,7 @@ class dense_hash_map final { [[nodiscard]] auto constrained_find(const Other &key, std::size_t bucket) { for(auto it = begin(bucket), last = end(bucket); it != last; ++it) { if(packed.second()(it->first, key)) { - return iterator{it.base()}; + return begin() + it.index(); } } @@ -266,9 +268,9 @@ class dense_hash_map final { template [[nodiscard]] auto constrained_find(const Other &key, std::size_t bucket) const { - for(auto it = begin(bucket), last = end(bucket); it != last; ++it) { + for(auto it = cbegin(bucket), last = cend(bucket); it != last; ++it) { if(packed.second()(it->first, key)) { - return const_iterator{it.base()}; + return cbegin() + it.index(); } } diff --git a/src/entt/container/dense_hash_set.hpp b/src/entt/container/dense_hash_set.hpp index c1e293c83..4b5379818 100644 --- a/src/entt/container/dense_hash_set.hpp +++ b/src/entt/container/dense_hash_set.hpp @@ -109,9 +109,14 @@ public: return *operator->(); } - [[nodiscard]] iterator_type base() const ENTT_NOEXCEPT { - return it; - } + template + friend auto operator-(const dense_hash_set_iterator &, const dense_hash_set_iterator &) ENTT_NOEXCEPT; + + template + friend bool operator==(const dense_hash_set_iterator &, const dense_hash_set_iterator &) ENTT_NOEXCEPT; + + template + friend bool operator<(const dense_hash_set_iterator &, const dense_hash_set_iterator &) ENTT_NOEXCEPT; private: iterator_type it; @@ -119,12 +124,12 @@ private: template [[nodiscard]] auto operator-(const dense_hash_set_iterator &lhs, const dense_hash_set_iterator &rhs) ENTT_NOEXCEPT { - return lhs.base() - rhs.base(); + return lhs.it - rhs.it; } template [[nodiscard]] bool operator==(const dense_hash_set_iterator &lhs, const dense_hash_set_iterator &rhs) ENTT_NOEXCEPT { - return lhs.base() == rhs.base(); + return lhs.it == rhs.it; } template @@ -134,12 +139,12 @@ template template [[nodiscard]] bool operator<(const dense_hash_set_iterator &lhs, const dense_hash_set_iterator &rhs) ENTT_NOEXCEPT { - return lhs.base() < rhs.base(); + return lhs.it < rhs.it; } template [[nodiscard]] bool operator>(const dense_hash_set_iterator &lhs, const dense_hash_set_iterator &rhs) ENTT_NOEXCEPT { - return lhs.base() > rhs.base(); + return rhs < lhs; } template @@ -170,15 +175,15 @@ public: dense_hash_set_local_iterator(iterator_type iter, const std::size_t pos) ENTT_NOEXCEPT : it{iter}, - curr{pos} {} + offset{pos} {} template>, typename = std::enable_if_t> dense_hash_set_local_iterator(const dense_hash_set_local_iterator> *> &other) : it{other.it}, - curr{other.curr} {} + offset{other.offset} {} dense_hash_set_local_iterator &operator++() ENTT_NOEXCEPT { - return curr = it[curr].next, *this; + return offset = it[offset].next, *this; } dense_hash_set_local_iterator operator++(int) ENTT_NOEXCEPT { @@ -187,28 +192,25 @@ public: } [[nodiscard]] pointer operator->() const { - return std::addressof(it[curr].element); + return std::addressof(it[offset].element); } [[nodiscard]] reference operator*() const { return *operator->(); } - [[nodiscard]] iterator_type base() const ENTT_NOEXCEPT { - return (it + curr); + [[nodiscard]] std::size_t index() const ENTT_NOEXCEPT { + return offset; } - template - friend bool operator==(const dense_hash_set_local_iterator &, const dense_hash_set_local_iterator &) ENTT_NOEXCEPT; - private: iterator_type it; - std::size_t curr; + std::size_t offset; }; template [[nodiscard]] bool operator==(const dense_hash_set_local_iterator &lhs, const dense_hash_set_local_iterator &rhs) ENTT_NOEXCEPT { - return lhs.curr == rhs.curr; + return lhs.index() == rhs.index(); } template @@ -256,7 +258,7 @@ class dense_hash_set final { [[nodiscard]] auto constrained_find(const Other &value, std::size_t bucket) { for(auto it = begin(bucket), last = end(bucket); it != last; ++it) { if(packed.second()(*it, value)) { - return iterator{it.base()}; + return begin() + it.index(); } } @@ -265,9 +267,9 @@ class dense_hash_set final { template [[nodiscard]] auto constrained_find(const Other &value, std::size_t bucket) const { - for(auto it = begin(bucket), last = end(bucket); it != last; ++it) { + for(auto it = cbegin(bucket), last = cend(bucket); it != last; ++it) { if(packed.second()(*it, value)) { - return const_iterator{it.base()}; + return cbegin() + it.index(); } } diff --git a/test/entt/container/dense_hash_map.cpp b/test/entt/container/dense_hash_map.cpp index 8c194dd24..d0b4eb68a 100644 --- a/test/entt/container/dense_hash_map.cpp +++ b/test/entt/container/dense_hash_map.cpp @@ -920,11 +920,8 @@ TEST(DenseHashMap, LocalIterator) { ASSERT_EQ(begin->first, 3u + minimum_bucket_count); ASSERT_EQ((*begin).second, 99u); - ASSERT_EQ(begin.base(), map.begin().base() + 1u); ASSERT_EQ(begin++, map.begin(3u)); - ASSERT_EQ(begin.base(), map.begin().base()); ASSERT_EQ(++begin, map.end(3u)); - ASSERT_NE(begin.base(), map.end().base()); } TEST(DenseHashMap, ConstLocalIterator) { @@ -951,11 +948,8 @@ TEST(DenseHashMap, ConstLocalIterator) { ASSERT_EQ(cbegin->first, 3u + minimum_bucket_count); ASSERT_EQ((*cbegin).second, 99u); - ASSERT_EQ(cbegin.base(), map.cbegin().base() + 1u); ASSERT_EQ(cbegin++, map.begin(3u)); - ASSERT_EQ(cbegin.base(), map.cbegin().base()); ASSERT_EQ(++cbegin, map.end(3u)); - ASSERT_NE(cbegin.base(), map.cend().base()); } TEST(DenseHashMap, LocalIteratorConversion) { diff --git a/test/entt/container/dense_hash_set.cpp b/test/entt/container/dense_hash_set.cpp index 4b847f1c7..5218b2f44 100644 --- a/test/entt/container/dense_hash_set.cpp +++ b/test/entt/container/dense_hash_set.cpp @@ -674,11 +674,8 @@ TEST(DenseHashSet, LocalIterator) { ASSERT_EQ(*begin.operator->(), 3u + minimum_bucket_count); ASSERT_EQ(*begin, 3u + minimum_bucket_count); - ASSERT_EQ(begin.base(), set.begin().base() + 1u); ASSERT_EQ(begin++, set.begin(3u)); - ASSERT_EQ(begin.base(), set.begin().base()); ASSERT_EQ(++begin, set.end(3u)); - ASSERT_NE(begin.base(), set.end().base()); } TEST(DenseHashSet, ConstLocalIterator) { @@ -705,11 +702,8 @@ TEST(DenseHashSet, ConstLocalIterator) { ASSERT_EQ(*cbegin.operator->(), 3u + minimum_bucket_count); ASSERT_EQ(*cbegin, 3u + minimum_bucket_count); - ASSERT_EQ(cbegin.base(), set.cbegin().base() + 1u); ASSERT_EQ(cbegin++, set.begin(3u)); - ASSERT_EQ(cbegin.base(), set.cbegin().base()); ASSERT_EQ(++cbegin, set.end(3u)); - ASSERT_NE(cbegin.base(), set.cend().base()); } TEST(DenseHashSet, LocalIteratorConversion) {