Michele Caini 4 лет назад
Родитель
Сommit
dae5f86d4e
2 измененных файлов с 48 добавлено и 28 удалено
  1. 39 28
      src/entt/entity/storage.hpp
  2. 9 0
      test/entt/entity/storage.cpp

+ 39 - 28
src/entt/entity/storage.hpp

@@ -96,38 +96,10 @@ public:
         return (*this + -value);
     }
 
-    difference_type operator-(const storage_iterator &other) const ENTT_NOEXCEPT {
-        return other.index - index;
-    }
-
     [[nodiscard]] reference operator[](const difference_type value) const ENTT_NOEXCEPT {
         return *operator+(value);
     }
 
-    [[nodiscard]] bool operator==(const storage_iterator &other) const ENTT_NOEXCEPT {
-        return other.index == index;
-    }
-
-    [[nodiscard]] bool operator!=(const storage_iterator &other) const ENTT_NOEXCEPT {
-        return !(*this == other);
-    }
-
-    [[nodiscard]] bool operator<(const storage_iterator &other) const ENTT_NOEXCEPT {
-        return index > other.index;
-    }
-
-    [[nodiscard]] bool operator>(const storage_iterator &other) const ENTT_NOEXCEPT {
-        return index < other.index;
-    }
-
-    [[nodiscard]] bool operator<=(const storage_iterator &other) const ENTT_NOEXCEPT {
-        return !(*this > other);
-    }
-
-    [[nodiscard]] bool operator>=(const storage_iterator &other) const ENTT_NOEXCEPT {
-        return !(*this < other);
-    }
-
     [[nodiscard]] pointer operator->() const ENTT_NOEXCEPT {
         const auto pos = index - 1;
         return (*packed)[pos / packed_page_v] + fast_mod(pos, packed_page_v);
@@ -137,11 +109,50 @@ public:
         return *operator->();
     }
 
+    [[nodiscard]] difference_type base() const ENTT_NOEXCEPT {
+        return index;
+    }
+
 private:
     Container *packed;
     difference_type index;
 };
 
+template<typename CLhs, typename CRhs>
+[[nodiscard]] auto operator-(const storage_iterator<CLhs> &lhs, const storage_iterator<CRhs> &rhs) ENTT_NOEXCEPT {
+    return rhs.base() - lhs.base();
+}
+
+template<typename CLhs, typename CRhs>
+[[nodiscard]] bool operator==(const storage_iterator<CLhs> &lhs, const storage_iterator<CRhs> &rhs) ENTT_NOEXCEPT {
+    return lhs.base() == rhs.base();
+}
+
+template<typename CLhs, typename CRhs>
+[[nodiscard]] bool operator!=(const storage_iterator<CLhs> &lhs, const storage_iterator<CRhs> &rhs) ENTT_NOEXCEPT {
+    return !(lhs == rhs);
+}
+
+template<typename CLhs, typename CRhs>
+[[nodiscard]] bool operator<(const storage_iterator<CLhs> &lhs, const storage_iterator<CRhs> &rhs) ENTT_NOEXCEPT {
+    return lhs.base() > rhs.base();
+}
+
+template<typename CLhs, typename CRhs>
+[[nodiscard]] bool operator>(const storage_iterator<CLhs> &lhs, const storage_iterator<CRhs> &rhs) ENTT_NOEXCEPT {
+    return lhs.base() < rhs.base();
+}
+
+template<typename CLhs, typename CRhs>
+[[nodiscard]] bool operator<=(const storage_iterator<CLhs> &lhs, const storage_iterator<CRhs> &rhs) ENTT_NOEXCEPT {
+    return !(lhs > rhs);
+}
+
+template<typename CLhs, typename CRhs>
+[[nodiscard]] bool operator>=(const storage_iterator<CLhs> &lhs, const storage_iterator<CRhs> &rhs) ENTT_NOEXCEPT {
+    return !(lhs < rhs);
+}
+
 } // namespace internal
 
 /**

+ 9 - 0
test/entt/entity/storage.cpp

@@ -943,6 +943,15 @@ TEST(Storage, IteratorConversion) {
 
     ASSERT_EQ(it->value, 42);
     ASSERT_EQ(it->value, cit->value);
+
+    ASSERT_EQ(it - cit, 0);
+    ASSERT_EQ(cit - it, 0);
+    ASSERT_LE(it, cit);
+    ASSERT_LE(cit, it);
+    ASSERT_GE(it, cit);
+    ASSERT_GE(cit, it);
+    ASSERT_EQ(it, cit);
+    ASSERT_NE(++cit, it);
 }
 
 TEST(Storage, Raw) {