Ver código fonte

iterators from sparse sets and views have now operator+/operator+=

Michele Caini 8 anos atrás
pai
commit
866c18200a

+ 0 - 1
TODO

@@ -7,5 +7,4 @@
 * scene management (I prefer the concept of spaces, that is a kind of scene anyway)
 * raw view (affects sparse sets - custom iterator in derived one - and introduces a new kind of view): single component view to iterate components only instead of entities (in the right order!!)
   it should speed up systems like rendering or whatever requires a single component and isn't interested in the entity, for it avoids the double check of the get
-* operator+/operator+= (-/-=) on sparse set iterators (required to partition arrays and split iteration on multiple threads)
 * AOB

+ 10 - 0
src/entt/entity/sparse_set.hpp

@@ -57,6 +57,7 @@ class SparseSet<Entity> {
     using traits_type = entt_traits<Entity>;
 
     struct Iterator final {
+        using difference_type = std::size_t;
         using value_type = Entity;
 
         Iterator(const std::vector<value_type> &direct, std::size_t pos)
@@ -72,6 +73,15 @@ class SparseSet<Entity> {
             return ++(*this), orig;
         }
 
+        Iterator & operator+=(difference_type value) noexcept {
+            pos -= value;
+            return *this;
+        }
+
+        Iterator operator+(difference_type value) noexcept {
+            return Iterator{direct, pos-value};
+        }
+
         bool operator==(const Iterator &other) const noexcept {
             return other.pos == pos;
         }

+ 10 - 0
src/entt/entity/view.hpp

@@ -366,6 +366,7 @@ class View final {
         }
 
     public:
+        using difference_type = typename underlying_iterator_type::difference_type;
         using value_type = typename view_type::entity_type;
 
         Iterator(unchecked_type unchecked, underlying_iterator_type begin, underlying_iterator_type end) noexcept
@@ -385,6 +386,15 @@ class View final {
             return ++(*this), orig;
         }
 
+        Iterator & operator+=(difference_type value) noexcept {
+            begin += value;
+            return *this;
+        }
+
+        Iterator operator+(difference_type value) noexcept {
+            return Iterator{unchecked, begin+value, end};
+        }
+
         bool operator==(const Iterator &other) const noexcept {
             return other.begin == begin;
         }

+ 8 - 0
test/entt/entity/sparse_set.cpp

@@ -63,12 +63,20 @@ TEST(SparseSetNoType, DataBeginEnd) {
     ASSERT_EQ(*(set.data() + 1u), 12u);
     ASSERT_EQ(*(set.data() + 2u), 42u);
 
+    auto it = set.begin();
+
+    ASSERT_EQ(*it, 42u);
+    ASSERT_EQ(*(it+1), 12u);
+    ASSERT_EQ(*(it+2), 3u);
+    ASSERT_EQ(it += 3, set.end());
+
     auto begin = set.begin();
     auto end = set.end();
 
     ASSERT_EQ(*(begin++), 42u);
     ASSERT_EQ(*(begin++), 12u);
     ASSERT_EQ(*(begin++), 3u);
+
     ASSERT_EQ(begin, end);
 }
 

+ 6 - 0
test/entt/entity/view.cpp

@@ -81,6 +81,12 @@ TEST(View, MultipleComponent) {
     auto e0 = registry.create<char>();
     auto e1 = registry.create<int, char>();
 
+    auto it = registry.view<char>().begin();
+
+    ASSERT_EQ(*it, e1);
+    ASSERT_EQ(*(it+1), e0);
+    ASSERT_EQ(it += 2, registry.view<char>().end());
+
     ASSERT_NO_THROW((registry.view<int, char>().begin()++));
     ASSERT_NO_THROW((++registry.view<int, char>().begin()));