ソースを参照

entity/*: iterators review

Michele Caini 6 年 前
コミット
b888757092

+ 4 - 4
src/entt/entity/runtime_view.hpp

@@ -65,8 +65,8 @@ class basic_runtime_view {
 
         using direct_type = std::vector<const sparse_set<Entity> *>;
 
-        iterator(const direct_type *all, underlying_iterator_type curr) ENTT_NOEXCEPT
-            : pools{all},
+        iterator(const direct_type &all, underlying_iterator_type curr) ENTT_NOEXCEPT
+            : pools{&all},
               it{curr}
         {
             if(it != (*pools)[0]->end() && !valid()) {
@@ -187,7 +187,7 @@ public:
         iterator_type it{};
 
         if(valid()) {
-            it = { &pools, pools[0]->begin() };
+            it = { pools, pools[0]->begin() };
         }
 
         return it;
@@ -212,7 +212,7 @@ public:
         iterator_type it{};
 
         if(valid()) {
-            it = { &pools, pools[0]->end() };
+            it = { pools, pools[0]->end() };
         }
 
         return it;

+ 6 - 5
src/entt/entity/sparse_set.hpp

@@ -58,8 +58,8 @@ class sparse_set {
         using direct_type = std::vector<Entity>;
         using index_type = typename traits_type::difference_type;
 
-        iterator(const direct_type *ref, const index_type idx) ENTT_NOEXCEPT
-            : direct{ref}, index{idx}
+        iterator(const direct_type &ref, const index_type idx) ENTT_NOEXCEPT
+            : direct{&ref}, index{idx}
         {}
 
     public:
@@ -95,7 +95,8 @@ class sparse_set {
         }
 
         iterator operator+(const difference_type value) const ENTT_NOEXCEPT {
-            return iterator{direct, index-value};
+            iterator copy = *this;
+            return (copy += value);
         }
 
         iterator & operator-=(const difference_type value) ENTT_NOEXCEPT {
@@ -297,7 +298,7 @@ public:
      */
     iterator_type begin() const ENTT_NOEXCEPT {
         const typename traits_type::difference_type pos = direct.size();
-        return iterator_type{&direct, pos};
+        return iterator_type{direct, pos};
     }
 
     /**
@@ -315,7 +316,7 @@ public:
      * internal packed array.
      */
     iterator_type end() const ENTT_NOEXCEPT {
-        return iterator_type{&direct, {}};
+        return iterator_type{direct, {}};
     }
 
     /**

+ 8 - 7
src/entt/entity/storage.hpp

@@ -57,8 +57,8 @@ class storage: public sparse_set<Entity> {
         using instance_type = std::conditional_t<Const, const std::vector<Type>, std::vector<Type>>;
         using index_type = typename traits_type::difference_type;
 
-        iterator(instance_type *ref, const index_type idx) ENTT_NOEXCEPT
-            : instances{ref}, index{idx}
+        iterator(instance_type &ref, const index_type idx) ENTT_NOEXCEPT
+            : instances{&ref}, index{idx}
         {}
 
     public:
@@ -94,7 +94,8 @@ class storage: public sparse_set<Entity> {
         }
 
         iterator operator+(const difference_type value) const ENTT_NOEXCEPT {
-            return iterator{instances, index-value};
+            iterator copy = *this;
+            return (copy += value);
         }
 
         iterator & operator-=(const difference_type value) ENTT_NOEXCEPT {
@@ -221,7 +222,7 @@ public:
      */
     const_iterator_type cbegin() const ENTT_NOEXCEPT {
         const typename traits_type::difference_type pos = underlying_type::size();
-        return const_iterator_type{&instances, pos};
+        return const_iterator_type{instances, pos};
     }
 
     /*! @copydoc cbegin */
@@ -232,7 +233,7 @@ public:
     /*! @copydoc begin */
     iterator_type begin() ENTT_NOEXCEPT {
         const typename traits_type::difference_type pos = underlying_type::size();
-        return iterator_type{&instances, pos};
+        return iterator_type{instances, pos};
     }
 
     /**
@@ -250,7 +251,7 @@ public:
      * given type.
      */
     const_iterator_type cend() const ENTT_NOEXCEPT {
-        return const_iterator_type{&instances, {}};
+        return const_iterator_type{instances, {}};
     }
 
     /*! @copydoc cend */
@@ -260,7 +261,7 @@ public:
 
     /*! @copydoc end */
     iterator_type end() ENTT_NOEXCEPT {
-        return iterator_type{&instances, {}};
+        return iterator_type{instances, {}};
     }
 
     /**

+ 14 - 14
src/entt/entity/view.hpp

@@ -81,8 +81,8 @@ class basic_view<Entity, exclude_t<Exclude...>, Component...> {
     class iterator final {
         friend class basic_view<Entity, exclude_t<Exclude...>, Component...>;
 
-        iterator(const sparse_set<Entity> *candidate, unchecked_type other, filter_type ignore, underlying_iterator_type curr) ENTT_NOEXCEPT
-            : view{candidate},
+        iterator(const sparse_set<Entity> &candidate, unchecked_type other, filter_type ignore, underlying_iterator_type curr) ENTT_NOEXCEPT
+            : view{&candidate},
               unchecked{other},
               filter{ignore},
               it{curr}
@@ -154,16 +154,16 @@ class basic_view<Entity, exclude_t<Exclude...>, Component...> {
         : pools{&component..., &epool...}
     {}
 
-    const sparse_set<Entity> * candidate() const ENTT_NOEXCEPT {
-        return std::min({ static_cast<const sparse_set<Entity> *>(std::get<pool_type<Component> *>(pools))... }, [](const auto *lhs, const auto *rhs) {
+    const sparse_set<Entity> & candidate() const ENTT_NOEXCEPT {
+        return *std::min({ static_cast<const sparse_set<Entity> *>(std::get<pool_type<Component> *>(pools))... }, [](const auto *lhs, const auto *rhs) {
             return lhs->size() < rhs->size();
         });
     }
 
-    unchecked_type unchecked(const sparse_set<Entity> *view) const {
+    unchecked_type unchecked(const sparse_set<Entity> &view) const {
         std::size_t pos{};
         unchecked_type other{};
-        ((std::get<pool_type<Component> *>(pools) == view ? nullptr : (other[pos++] = std::get<pool_type<Component> *>(pools))), ...);
+        ((std::get<pool_type<Component> *>(pools) == &view ? nullptr : (other[pos++] = std::get<pool_type<Component> *>(pools))), ...);
         return other;
     }
 
@@ -306,9 +306,9 @@ public:
      * @return An iterator to the first entity that has the given components.
      */
     iterator_type begin() const {
-        const auto *view = candidate();
+        const auto &view = candidate();
         const filter_type ignore{std::get<pool_type<Exclude> *>(pools)...};
-        return iterator_type{view, unchecked(view), ignore, view->begin()};
+        return iterator_type{view, unchecked(view), ignore, view.begin()};
     }
 
     /**
@@ -327,9 +327,9 @@ public:
      * given components.
      */
     iterator_type end() const {
-        const auto *view = candidate();
+        const auto &view = candidate();
         const filter_type ignore{std::get<pool_type<Exclude> *>(pools)...};
-        return iterator_type{view, unchecked(view), ignore, view->end()};
+        return iterator_type{view, unchecked(view), ignore, view.end()};
     }
 
     /**
@@ -359,9 +359,9 @@ public:
      * iterator otherwise.
      */
     iterator_type find(const entity_type entt) const {
-        const auto *view = candidate();
+        const auto &view = candidate();
         const filter_type ignore{std::get<pool_type<Exclude> *>(pools)...};
-        iterator_type it{view, unchecked(view), ignore, view->find(entt)};
+        iterator_type it{view, unchecked(view), ignore, view.find(entt)};
         return (it != end() && *it == entt) ? it : end();
     }
 
@@ -430,8 +430,8 @@ public:
      */
     template<typename Func>
     void each(Func func) const {
-        const auto *view = candidate();
-        ((std::get<pool_type<Component> *>(pools) == view ? each<Component>(std::move(func)) : void()), ...);
+        const auto &view = candidate();
+        ((std::get<pool_type<Component> *>(pools) == &view ? each<Component>(std::move(func)) : void()), ...);
     }
 
     /**