Michele Caini %!s(int64=8) %!d(string=hai) anos
pai
achega
13d0b0940c

+ 146 - 103
README.md

@@ -7,43 +7,44 @@
 
 # Table of Contents
 
-   * [Introduction](#introduction)
-   * [Build Instructions](#build-instructions)
-   * [Crash Course: entity-component system](#crash-course-entity-component-system)
-      * [Design choices](#design-choices)
-         * [A bitset-free entity-component system](#a-bitset-free-entity-component-system)
-         * [Pay per use](#pay-per-use)
-      * [Vademecum](#vademecum)
-      * [The Registry, the Entity and the Component](#the-registry-the-entity-and-the-component)
-         * [Single instance components](#single-instance-components)
-         * [Runtime components](#runtime-components)
-            * [A journey through a plugin](#a-journey-through-a-plugin)
-         * [Sorting: is it possible?](#sorting-is-it-possible)
-      * [View: to persist or not to persist?](#view-to-persist-or-not-to-persist)
-         * [Standard View](#standard-view)
-            * [Single component standard view](#single-component-standard-view)
-            * [Multi component standard view](#multi-component-standard-view)
-         * [Persistent View](#persistent-view)
-         * [Give me everything](#give-me-everything)
-      * [Side notes](#side-notes)
-   * [Crash Course: core functionalities](#crash-course-core-functionalities)
-      * [Compile-time identifiers](#compile-time-identifiers)
-      * [Runtime identifiers](#runtime-identifiers)
-      * [Hashed strings](#hashed-strings)
-   * [Crash Course: service locator](#crash-course-service-locator)
-   * [Crash Course: cooperative scheduler](#crash-course-cooperative-scheduler)
-      * [The process](#the-process)
-      * [The scheduler](#the-scheduler)
-   * [Crash Course: resource management](#crash-course-resource-management)
-      * [The resource, the loader and the cache](#the-resource-the-loader-and-the-cache)
-   * [Crash Course: events, signals and everything in between](#crash-course-events-signals-and-everything-in-between)
-      * [Signals](#signals)
-      * [Compile-time event bus](#compile-time-event-bus)
-      * [Delegate](#delegate)
-      * [Event dispatcher](#event-dispatcher)
-      * [Event emitter](#event-emitter)
-   * [License](#license)
-   * [Support](#support)
+* [Introduction](#introduction)
+* [Build Instructions](#build-instructions)
+* [Crash Course: entity-component system](#crash-course-entity-component-system)
+   * [Design choices](#design-choices)
+      * [A bitset-free entity-component system](#a-bitset-free-entity-component-system)
+      * [Pay per use](#pay-per-use)
+   * [Vademecum](#vademecum)
+   * [The Registry, the Entity and the Component](#the-registry-the-entity-and-the-component)
+      * [Single instance components](#single-instance-components)
+      * [Runtime components](#runtime-components)
+         * [A journey through a plugin](#a-journey-through-a-plugin)
+      * [Sorting: is it possible?](#sorting-is-it-possible)
+   * [View: to persist or not to persist?](#view-to-persist-or-not-to-persist)
+      * [Standard View](#standard-view)
+         * [Single component standard view](#single-component-standard-view)
+         * [Multi component standard view](#multi-component-standard-view)
+      * [Persistent View](#persistent-view)
+      * [Raw View](#raw-view)
+      * [Give me everything](#give-me-everything)
+   * [Side notes](#side-notes)
+* [Crash Course: core functionalities](#crash-course-core-functionalities)
+   * [Compile-time identifiers](#compile-time-identifiers)
+   * [Runtime identifiers](#runtime-identifiers)
+   * [Hashed strings](#hashed-strings)
+* [Crash Course: service locator](#crash-course-service-locator)
+* [Crash Course: cooperative scheduler](#crash-course-cooperative-scheduler)
+   * [The process](#the-process)
+   * [The scheduler](#the-scheduler)
+* [Crash Course: resource management](#crash-course-resource-management)
+   * [The resource, the loader and the cache](#the-resource-the-loader-and-the-cache)
+* [Crash Course: events, signals and everything in between](#crash-course-events-signals-and-everything-in-between)
+   * [Signals](#signals)
+   * [Compile-time event bus](#compile-time-event-bus)
+   * [Delegate](#delegate)
+   * [Event dispatcher](#event-dispatcher)
+   * [Event emitter](#event-emitter)
+* [License](#license)
+* [Support](#support)
 
 # Introduction
 
@@ -57,8 +58,8 @@ that is used mostly in game development. For further details:
 * [ECS on Wikipedia](https://en.wikipedia.org/wiki/Entity%E2%80%93component%E2%80%93system)
 
 A long time ago, the sole entity-component system was part of the project. After
-a while the codebase has grown and more and more classes have become part
-of the repository.<br/>
+a while the codebase has grown and more and more classes have become part of the
+repository.<br/>
 That's why today it's called _the EnTT Framework_.
 
 Currently, `EnTT` is tested on Linux, Microsoft Windows and OS X. It has proven
@@ -86,11 +87,11 @@ assure that I'll do my best to take them all seriously.
 Here is a brief list of what it offers today:
 
 * Statically generated integer identifiers for types (assigned either at
-compile-time or at runtime).
+  compile-time or at runtime).
 * A constexpr utility for human readable resource identifiers.
 * An incredibly fast entity-component system based on sparse sets, with its own
-views and a _pay for what you use_ policy to adjust performance and memory usage
-according to the users' requirements.
+  views and a _pay for what you use_ policy to adjust performance and memory
+  usage according to the users' requirements.
 * Actor class for those who aren't confident with entity-component systems.
 * The smallest and most basic implementation of a service locator ever seen.
 * A cooperative scheduler for processes of any type.
@@ -103,8 +104,8 @@ according to the users' requirements.
 
 Consider it a work in progress. For more details and an updated list, please
 refer to the [online documentation](https://skypjack.github.io/entt/). It
-probably contains much more. Moreover, the whole API is fully documented
-in-code for those who are brave enough to read it.<br/>
+probably contains much more. Moreover, the whole API is fully documented in-code
+for those who are brave enough to read it.<br/>
 Continue reading to know how the different parts of the project work or follow
 the link above to take a look at the API reference.
 
@@ -203,12 +204,12 @@ Dell XPS 13 out of the mid 2014):
 | Standard view, 1M entities, ten components<br/>Half of the entities have all the components | 0.0010s | **1.2e-06s** |
 | Standard view, 1M entities, ten components<br/>One of the entities has all the components | 0.0008s | **1.2e-06s** |
 | Persistent view, 1M entities, ten components | 0.0011s | **3.0e-07s** |
+| Raw view, 1M entities | - | **2.2e-07s** |
 | Sort 150k entities, one component<br/>Arrays are in reverse order | - | **0.0036s** |
 | Sort 150k entities, enforce permutation<br/>Arrays are in reverse order | - | **0.0005s** |
 
 Note: The default version of `EntityX` (`master` branch) wasn't added to the
-comparison because it's already much slower than its compile-time
-counterpart.
+comparison because it's already much slower than its compile-time counterpart.
 
 Pretty interesting, aren't them? In fact, these benchmarks are the same used by
 `EntityX` to show _how fast it is_. To be honest, they aren't so good and these
@@ -336,7 +337,7 @@ many others besides me.
 
 ## Vademecum
 
-The `Registry` to store, the `View` to iterate. That's all.
+The `Registry` to store, the views to iterate. That's all.
 
 An entity (the _E_ of an _ECS_) is an opaque identifier that users should just
 use as-is and store around if needed. Do not try to inspect an entity
@@ -352,10 +353,9 @@ and move assignable. They are list initialized by using the parameters provided
 to construct the component itself. No need to register components or their types
 neither with the registry nor with the entity-component system at all.<br/>
 Systems (the _S_ of an _ECS_) are just plain functions, functors, lambdas or
-whatever the users want. They can accept a `Registry`, a `View` or a
-`PersistentView` and use them the way they prefer. No need to register systems
-or their types neither with the registry nor with the entity-component system at
-all.
+whatever the users want. They can accept a `Registry` or a view of any type and
+use them the way they prefer. No need to register systems or their types neither
+with the registry nor with the entity-component system at all.
 
 The following sections will explain in short how to use the entity-component
 system, the core part of the whole framework.<br/>
@@ -491,14 +491,14 @@ registry.reset<Position>(entity);
 There exist also two other _versions_ of the `reset` member function:
 
 * If no entity is passed to it, `reset` will remove the given component from
-each entity that has it:
+  each entity that has it:
 
   ```cpp
   registry.reset<Position>();
   ```
 
 * If neither the entity nor the component are specified, all the entities still
-in use and their components are destroyed:
+  in use and their components are destroyed:
 
   ```cpp
   registry.reset();
@@ -663,60 +663,74 @@ First of all, it is worth answering an obvious question: why views?<br/>
 Roughly speaking, they are a good tool to enforce single responsibility. A
 system that has access to a registry can create and destroy entities, as well as
 assign and remove components. On the other side, a system that has access to a
-view can only iterate entities and their components as well as modify their data
-members.<br/>
+view can only iterate entities and their components, then read or update the
+data members of the latter.<br/>
 It is a subtle difference that can help designing a better software sometimes.
 
-There are mainly two kinds of views: standard (also known as `View`) and
-persistent (also known as `PersistentView`).<br/>
-Both of them have pros and cons to take in consideration. In particular:
+There are mainly three kinds of views: standard (also known as `View`),
+persistent (also known as `PersistentView`) and raw (also known as
+`RawView`).<br/>
+All of them have pros and cons to take in consideration. In particular:
 
 * Standard views:
 
   Pros:
-  * They work out-of-the-box and don't require any dedicated data
-    structure.
-  * Creating and destroying them isn't expensive at all because they don't
-    have any type of initialization.
-  * They are the best tool to iterate single components.
-  * They are the best tool to iterate multiple components at once when one of
-    the components is assigned to a significantly low number of entities.
+  * They work out-of-the-box and don't require any dedicated data structure.
+  * Creating and destroying them isn't expensive at all because they don't have
+    any type of initialization.
+  * They are the best tool for iterating entities for a single component.
+  * They are the best tool for iterating entities for multiple components when
+    one of the components is assigned to a significantly low number of entities.
   * They don't affect any other operations of the registry.
 
   Cons:
-  * Their performance tend to degenerate when the number of components
-    to iterate grows up and the most of the entities have all of them.
+  * Their performance tend to degenerate when the number of components to
+    iterate grows up and the most of the entities have all of them.
 
 * Persistent views:
 
   Pros:
-  * Once prepared, creating and destroying them isn't expensive at all
-    because they don't have any type of initialization.
-  * They are the best tool to iterate multiple components at once when
-    the most of the entities have all of them.
+  * Once prepared, creating and destroying them isn't expensive at all because
+    they don't have any type of initialization.
+  * They are the best tool for iterating entities for mmultiple components and
+    most entities have them all.
 
   Cons:
-    * They have dedicated data structures and thus affect the memory usage to a
-      minimal extent.
-    * If not previously prepared, the first time they are used they go
-      through an initialization step that could take a while.
-    * They affect to a minimum the creation and destruction of entities and
-      components. In other terms: the more persistent views there will be,
-      the less performing will be creating and destroying entities and
-      components.
-
-To sum up and as a rule of thumb, use a standard view:
-* To iterate entities for a single component.
-* To iterate entities for multiple components when a significantly low
-  number of entities have one of the components.
-* In all those cases where a persistent view would give a boost to
-  performance but the iteration isn't performed frequently.
-
-Use a persistent view in all the other cases.
-
-To easily iterate entities, all the views offer the common `begin` and `end`
-member functions that allow users to use a view in a typical range-for
-loop.<br/>
+  * They have dedicated data structures and thus affect the memory usage to a
+    minimal extent.
+  * If not previously prepared, the first time they are used they go through an
+    initialization step that could take a while.
+  * They affect to a minimum the creation and destruction of entities and
+    components. In other terms: the more persistent views there will be, the
+    less performing will be creating and destroying entities and components.
+
+* Raw views:
+
+  Pros:
+  * They work out-of-the-box and don't require any dedicated data structure.
+  * Creating and destroying them isn't expensive at all because they don't have
+    any type of initialization.
+  * They are the best tool for iterating components when it is not necessary to
+    know which entities they belong to.
+  * They don't affect any other operations of the registry.
+
+  Cons:
+  * They can be used to iterate only one type of component at a time.
+  * They don't return the entity to which a component belongs to the caller.
+
+To sum up and as a rule of thumb:
+* Use a raw view to iterate components only (no entities) for a given type.
+* Use a standard view to iterate entities for a single component.
+* Use a standard view to iterate entities for multiple components when a
+  significantly low number of entities have one of the components.
+* Use a standard view in all those cases where a persistent view would give a
+  boost to performance but the iteration isn't performed frequently.
+* Prepare and use a persistent view in all the other cases.
+
+To easily iterate entities and components, all the views offer the common
+`begin` and `end` member functions that allow users to use a view in a typical
+range-for loop. Almost all the views offer also a *more functional* `each`
+member function that accepts a callback for convenience.<br/>
 Continue reading for more details or refer to the
 [official documentation](https://skypjack.github.io/entt/).
 
@@ -749,9 +763,9 @@ Refer to the [official documentation](https://skypjack.github.io/entt/) for all
 the details.
 
 There is no need to store views around for they are extremely cheap to
-construct, even though they can be copied without problems and reused
-freely. In fact, they return newly created and correctly initialized iterators
-whenever `begin` or `end` are invoked.<br/>
+construct, even though they can be copied without problems and reused freely. In
+fact, they return newly created and correctly initialized iterators whenever
+`begin` or `end` are invoked.<br/>
 To iterate a single component standard view, either use it in range-for loop:
 
 ```cpp
@@ -783,8 +797,8 @@ mind that it works only with the components of the view itself.
 #### Multi component standard view
 
 Multi component standard views iterate entities that have at least all the given
-components in their bags. During construction, these views look at the number
-of entities available for each component and pick up a reference to the smallest
+components in their bags. During construction, these views look at the number of
+entities available for each component and pick up a reference to the smallest
 set of candidates in order to speed up iterations.<br/>
 They offer fewer functionalities than their companion views for single
 component. In particular, a multi component standard view exposes utility
@@ -795,9 +809,9 @@ Refer to the [official documentation](https://skypjack.github.io/entt/) for all
 the details.
 
 There is no need to store views around for they are extremely cheap to
-construct, even though they can be copied without problems and reused
-freely. In fact, they return newly created and correctly initialized iterators
-whenever `begin` or `end` are invoked.<br/>
+construct, even though they can be copied without problems and reused freely. In
+fact, they return newly created and correctly initialized iterators whenever
+`begin` or `end` are invoked.<br/>
 To iterate a multi component standard view, either use it in range-for loop:
 
 ```cpp
@@ -847,9 +861,9 @@ auto view = registry.persistent<Position, Velocity>();
 ```
 
 There is no need to store views around for they are extremely cheap to
-construct, even though they can be copied without problems and reused
-freely. In fact, they return newly created and correctly initialized iterators
-whenever `begin` or `end` are invoked.<br/>
+construct, even though they can be copied without problems and reused freely. In
+fact, they return newly created and correctly initialized iterators whenever
+`begin` or `end` are invoked.<br/>
 That being said, persistent views perform an initialization step the very first
 time they are constructed and this could be quite costly. To avoid it, consider
 asking to the registry to _prepare_ them when no entities have been created yet:
@@ -904,6 +918,35 @@ whether all the components have to be accessed or not.
 function template of a registry during iterations, if possible. However, keep in
 mind that it works only with the components of the view itself.
 
+### Raw View
+
+Raw views return all the components of a given type. This kind of views can
+access components directly and avoid extra indirections as if components were
+accessed via an entity identifier.<br/>
+They offer a bunch of functionalities to get the number of instances they are
+going to return and a raw access to the entity list as well as to the component
+list.<br/>
+Refer to the [official documentation](https://skypjack.github.io/entt/) for all
+the details.
+
+There is no need to store views around for they are extremely cheap to
+construct, even though they can be copied without problems and reused freely. In
+fact, they return newly created and correctly initialized iterators whenever
+`begin` or `end` are invoked.<br/>
+To iterate a raw view, use it in range-for loop:
+
+```cpp
+auto view = registry.raw<Renderable>();
+
+for(auto &&component: raw) {
+    // ...
+}
+```
+
+**Note**: raw views don't have the `each` nor the `get` member function for
+obvious reasons. The former would only return the components and therefore it
+would be redundant, the latter isn't required at all.
+
 ### Give me everything
 
 Views are narrow windows on the entire list of entities. They work by filtering

+ 0 - 2
TODO

@@ -5,7 +5,5 @@
 * parent-child relationships between entities directly managed by the registry. is it possible to do that in a clean and safe way?
 * scene management (I prefer the concept of spaces, that is a kind of scene anyway)
 * blueprint registry - kind of factory to create entitites template for initialization (get rid of the extra versions of Registry::create)
-* 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
 * review doc: separate it in multiple md/dox files, reduce the readme to a minimum and provide users with links to the online documentation on gh-pages
 * AOB

+ 38 - 8
src/entt/entity/registry.hpp

@@ -848,9 +848,9 @@ public:
      * the following rules:
      *
      * * All the entities in `A` that are also in `B` are returned first
-     * according to the order they have in `B`.
+     *   according to the order they have in `B`.
      * * All the entities in `A` that are not in `B` are returned in no
-     * particular order after all the other entities.
+     *   particular order after all the other entities.
      *
      * Any subsequent change to `B` won't affect the order in `A`.
      *
@@ -1027,10 +1027,10 @@ public:
      * entities. In particular:
      *
      * * Single component views are incredibly fast and iterate a packed array
-     * of entities, all of which has the given component.
+     *   of entities, all of which has the given component.
      * * Multi component views look at the number of entities available for each
-     * component and pick up a reference to the smallest set of candidates to
-     * test for the given components.
+     *   component and pick up a reference to the smallest set of candidates to
+     *   test for the given components.
      *
      * @note
      * Multi component views are pretty fast. However their performance tend to
@@ -1041,6 +1041,7 @@ public:
      * @see View
      * @see View<Entity, Component>
      * @see PersistentView
+     * @see RawView
      *
      * @tparam Component Type of components used to construct the view.
      * @return A newly created standard view.
@@ -1127,10 +1128,10 @@ public:
      * However they have also drawbacks:
      *
      * * Each kind of persistent view requires a dedicated data structure that
-     * is allocated within the registry and it increases memory pressure.
+     *   is allocated within the registry and it increases memory pressure.
      * * Internal data structures used to construct persistent views must be
-     * kept updated and it affects slightly construction and destruction of
-     * entities and components.
+     *   kept updated and it affects slightly construction and destruction of
+     *   entities and components.
      *
      * That being said, persistent views are an incredibly powerful tool if used
      * with care and offer a boost of performance undoubtedly.
@@ -1144,6 +1145,7 @@ public:
      * @see View
      * @see View<Entity, Component>
      * @see PersistentView
+     * @see RawView
      *
      * @tparam Component Types of components used to construct the view.
      * @return A newly created persistent view.
@@ -1154,6 +1156,34 @@ public:
         return PersistentView<Entity, Component...>{handler<Component...>(), pool<Component>()...};
     }
 
+    /**
+     * @brief Returns a raw view for the given component.
+     *
+     * This kind of views are created on the fly and share with the registry its
+     * internal data structures.<br/>
+     * Feel free to discard a view after the use. Creating and destroying a view
+     * is an incredibly cheap operation because they do not require any type of
+     * initialization.<br/>
+     * As a rule of thumb, storing a view should never be an option.
+     *
+     * Raw views are incredibly fast and must be considered the best tool to
+     * iterate components whenever knowing the entities to which they belong
+     * isn't required.
+     *
+     * @see View
+     * @see View<Entity, Component>
+     * @see PersistentView
+     * @see RawView
+     *
+     * @tparam Component Type of component used to construct the view.
+     * @return A newly created raw view.
+     */
+    template<typename Component>
+    RawView<Entity, Component> raw() {
+        return RawView<Entity, Component>{ensure<Component>()};
+    }
+
+
 private:
     std::vector<std::unique_ptr<SparseSet<Entity>>> handlers;
     std::vector<std::unique_ptr<SparseSet<Entity>>> pools;

+ 106 - 27
src/entt/entity/sparse_set.hpp

@@ -182,7 +182,7 @@ public:
      * always a valid range, even if the container is empty.
      *
      * @note
-     * There are no guarantees on the order, even though `sort` has been
+     * There are no guarantees on the order, even though `respect` has been
      * previously invoked. Internal data structures arrange elements to maximize
      * performance. Accessing them directly gives a performance boost but less
      * guarantees. Use `begin` and `end` if you want to iterate the sparse set
@@ -197,14 +197,14 @@ public:
     /**
      * @brief Returns an iterator to the beginning.
      *
-     * The returned iterator points to the first element of the internal packed
+     * The returned iterator points to the first entity of the internal packed
      * array. If the sparse set is empty, the returned iterator will be equal to
      * `end()`.
      *
      * @note
-     * Input iterators stay true to the order imposed by a call to `sort`.
+     * Input iterators stay true to the order imposed by a call to `respect`.
      *
-     * @return An iterator to the first element of the internal packed array.
+     * @return An iterator to the first entity of the internal packed array.
      */
     iterator_type begin() const noexcept {
         return Iterator{direct, direct.size()};
@@ -213,14 +213,14 @@ public:
     /**
      * @brief Returns an iterator to the end.
      *
-     * The returned iterator points to the element following the last element in
+     * The returned iterator points to the element following the last entity in
      * the internal packed array. Attempting to dereference the returned
      * iterator results in undefined behavior.
      *
      * @note
-     * Input iterators stay true to the order imposed by a call to `sort`.
+     * Input iterators stay true to the order imposed by a call to `respect`.
      *
-     * @return An iterator to the element following the last element of the
+     * @return An iterator to the element following the last entity of the
      * internal packed array.
      */
     iterator_type end() const noexcept {
@@ -364,12 +364,12 @@ public:
      * sets by using one of them as a master and the other one as a slave.
      *
      * Iterating the sparse set with a couple of iterators returns elements in
-     * the expected order after a call to `sort`. See `begin` and `end` for more
-     * details.
+     * the expected order after a call to `respect`. See `begin` and `end` for
+     * more details.
      *
      * @note
      * Attempting to iterate elements using the raw pointer returned by `data`
-     * gives no guarantees on the order, even though `sort` has been invoked.
+     * gives no guarantees on the order, even though `respect` has been invoked.
      *
      * @param other The sparse sets that imposes the order of the entities.
      */
@@ -432,6 +432,49 @@ template<typename Entity, typename Type>
 class SparseSet<Entity, Type>: public SparseSet<Entity> {
     using underlying_type = SparseSet<Entity>;
 
+    struct Iterator final {
+        using difference_type = std::size_t;
+        using value_type = Type;
+
+        Iterator(const std::vector<value_type> &instances, std::size_t pos)
+            : instances{instances}, pos{pos}
+        {}
+
+        Iterator & operator++() noexcept {
+            return --pos, *this;
+        }
+
+        Iterator operator++(int) noexcept {
+            Iterator orig = *this;
+            return ++(*this), orig;
+        }
+
+        Iterator & operator+=(difference_type value) noexcept {
+            pos -= value;
+            return *this;
+        }
+
+        Iterator operator+(difference_type value) noexcept {
+            return Iterator{instances, pos-value};
+        }
+
+        bool operator==(const Iterator &other) const noexcept {
+            return other.pos == pos;
+        }
+
+        bool operator!=(const Iterator &other) const noexcept {
+            return !(*this == other);
+        }
+
+        value_type operator*() const noexcept {
+            return instances[pos-1];
+        }
+
+    private:
+        const std::vector<value_type> &instances;
+        std::size_t pos;
+    };
+
 public:
     /*! @brief Type of the objects associated to the entities. */
     using object_type = Type;
@@ -442,7 +485,7 @@ public:
     /*! @brief Unsigned integer type. */
     using size_type = typename underlying_type::size_type;
     /*! @brief Input iterator type. */
-    using iterator_type = typename underlying_type::iterator_type;
+    using iterator_type = Iterator;
 
     /*! @brief Default constructor. */
     SparseSet() noexcept = default;
@@ -477,11 +520,11 @@ public:
      * always a valid range, even if the container is empty.
      *
      * @note
-     * There are no guarantees on the order, even though `sort` has been
-     * previously invoked. Internal data structures arrange elements to maximize
-     * performance. Accessing them directly gives a performance boost but less
-     * guarantees. Use `begin` and `end` if you want to iterate the sparse set
-     * in the expected order.
+     * There are no guarantees on the order, even though either `sort` or
+     * `respect` has been previously invoked. Internal data structures arrange
+     * elements to maximize performance. Accessing them directly gives a
+     * performance boost but less guarantees. Use `begin` and `end` if you want
+     * to iterate the sparse set in the expected order.
      *
      * @return A pointer to the array of objects.
      */
@@ -496,11 +539,11 @@ public:
      * always a valid range, even if the container is empty.
      *
      * @note
-     * There are no guarantees on the order, even though `sort` has been
-     * previously invoked. Internal data structures arrange elements to maximize
-     * performance. Accessing them directly gives a performance boost but less
-     * guarantees. Use `begin` and `end` if you want to iterate the sparse set
-     * in the expected order.
+     * There are no guarantees on the order, even though either `sort` or
+     * `respect` has been previously invoked. Internal data structures arrange
+     * elements to maximize performance. Accessing them directly gives a
+     * performance boost but less guarantees. Use `begin` and `end` if you want
+     * to iterate the sparse set in the expected order.
      *
      * @return A pointer to the array of objects.
      */
@@ -508,6 +551,40 @@ public:
         return instances.data();
     }
 
+    /**
+     * @brief Returns an iterator to the beginning.
+     *
+     * The returned iterator points to the first instance of the given type. If
+     * the sparse set is empty, the returned iterator will be equal to `end()`.
+     *
+     * @note
+     * Input iterators stay true to the order imposed by a call to either `sort`
+     * or `respect`.
+     *
+     * @return An iterator to the first instance of the given type.
+     */
+    iterator_type begin() const noexcept {
+        return Iterator{instances, instances.size()};
+    }
+
+    /**
+     * @brief Returns an iterator to the end.
+     *
+     * The returned iterator points to the element following the last instance
+     * of the given type. Attempting to dereference the returned iterator
+     * results in undefined behavior.
+     *
+     * @note
+     * Input iterators stay true to the order imposed by a call to either `sort`
+     * or `respect`.
+     *
+     * @return An iterator to the element following the last instance of the
+     * given type.
+     */
+    iterator_type end() const noexcept {
+        return Iterator{instances, 0};
+    }
+
     /**
      * @brief Returns the object associated to an entity.
      *
@@ -632,8 +709,9 @@ public:
      * @endcode
      *
      * @note
-     * Attempting to iterate elements using the raw pointer returned by `data`
-     * gives no guarantees on the order, even though `sort` has been invoked.
+     * Attempting to iterate elements using a raw pointer returned by a call to
+     * either `data` or `raw` gives no guarantees on the order, even though
+     * `sort` has been invoked.
      *
      * @tparam Compare Type of comparison function object.
      * @param compare A valid comparison function object.
@@ -676,12 +754,13 @@ public:
      * sets by using one of them as a master and the other one as a slave.
      *
      * Iterating the sparse set with a couple of iterators returns elements in
-     * the expected order after a call to `sort`. See `begin` and `end` for more
-     * details.
+     * the expected order after a call to `respect`. See `begin` and `end` for
+     * more details.
      *
      * @note
-     * Attempting to iterate elements using the raw pointer returned by `data`
-     * gives no guarantees on the order, even though `sort` has been invoked.
+     * Attempting to iterate elements using a raw pointer returned by a call to
+     * either `data` or `raw` gives no guarantees on the order, even though
+     * `respect` has been invoked.
      *
      * @param other The sparse sets that imposes the order of the entities.
      */

+ 196 - 27
src/entt/entity/view.hpp

@@ -37,10 +37,11 @@ class Registry;
  *
  * * New instances of the given components are created and assigned to entities.
  * * The entity currently pointed is modified (as an example, if one of the
- * given components is removed from the entity to which the iterator points).
+ *   given components is removed from the entity to which the iterator points).
  *
- * In all the other cases, modify the pools of the given components somehow
- * invalidates all the iterators and using them results in undefined behavior.
+ * In all the other cases, modifying the pools of the given components in any
+ * way invalidates all the iterators and using them results in undefined
+ * behavior.
  *
  * @note
  * Views share references to the underlying data structures with the Registry
@@ -57,6 +58,7 @@ class Registry;
  *
  * @sa View
  * @sa View<Entity, Component>
+ * @sa RawView
  *
  * @tparam Entity A valid entity type (see entt_traits for more details).
  * @tparam Component Types of components iterated by the view.
@@ -169,7 +171,7 @@ public:
      * An assertion will abort the execution at runtime in debug mode if the
      * view doesn't contain the given entity.
      *
-     * @tparam Comp Type of the component to get.
+     * @tparam Comp Type of component to get.
      * @param entity A valid entity identifier.
      * @return The component assigned to the entity.
      */
@@ -192,7 +194,7 @@ public:
      * An assertion will abort the execution at runtime in debug mode if the
      * view doesn't contain the given entity.
      *
-     * @tparam Comp Type of the component to get.
+     * @tparam Comp Type of component to get.
      * @param entity A valid entity identifier.
      * @return The component assigned to the entity.
      */
@@ -250,7 +252,8 @@ public:
     }
 
     /**
-     * @brief Iterate the entities and applies them the given function object.
+     * @brief Iterates entities and components and applies the given function
+     * object to them.
      *
      * The function object is invoked for each entity. It is provided with the
      * entity itself and a set of const references to all the components of the
@@ -272,7 +275,8 @@ public:
     }
 
     /**
-     * @brief Iterate the entities and applies them the given function object.
+     * @brief Iterates entities and components and applies the given function
+     * object to them.
      *
      * The function object is invoked for each entity. It is provided with the
      * entity itself and a set of references to all the components of the
@@ -307,7 +311,7 @@ public:
      * can quickly ruin the order imposed to the pool of entities shared between
      * the persistent views.
      *
-     * @tparam Comp Type of the component to use to impose the order.
+     * @tparam Comp Type of component to use to impose the order.
      */
     template<typename Comp>
     void sort() {
@@ -338,10 +342,11 @@ private:
  *
  * * New instances of the given components are created and assigned to entities.
  * * The entity currently pointed is modified (as an example, if one of the
- * given components is removed from the entity to which the iterator points).
+ *   given components is removed from the entity to which the iterator points).
  *
- * In all the other cases, modify the pools of the given components somehow
- * invalidates all the iterators and using them results in undefined behavior.
+ * In all the other cases, modifying the pools of the given components in any
+ * way invalidates all the iterators and using them results in undefined
+ * behavior.
  *
  * @note
  * Views share references to the underlying data structures with the Registry
@@ -354,6 +359,7 @@ private:
  *
  * @sa View<Entity, Component>
  * @sa PersistentView
+ * @sa RawView
  *
  * @tparam Entity A valid entity type (see entt_traits for more details).
  * @tparam Component Types of components iterated by the view.
@@ -532,7 +538,7 @@ public:
      * An assertion will abort the execution at runtime in debug mode if the
      * view doesn't contain the given entity.
      *
-     * @tparam Comp Type of the component to get.
+     * @tparam Comp Type of component to get.
      * @param entity A valid entity identifier.
      * @return The component assigned to the entity.
      */
@@ -555,7 +561,7 @@ public:
      * An assertion will abort the execution at runtime in debug mode if the
      * view doesn't contain the given entity.
      *
-     * @tparam Comp Type of the component to get.
+     * @tparam Comp Type of component to get.
      * @param entity A valid entity identifier.
      * @return The component assigned to the entity.
      */
@@ -613,7 +619,8 @@ public:
     }
 
     /**
-     * @brief Iterate the entities and applies them the given function object.
+     * @brief Iterates entities and components and applies the given function
+     * object to them.
      *
      * The function object is invoked for each entity. It is provided with the
      * entity itself and a set of const references to all the components of the
@@ -647,7 +654,8 @@ public:
     }
 
     /**
-     * @brief Iterate the entities and applies them the given function object.
+     * @brief Iterates entities and components and applies the given function
+     * object to them.
      *
      * The function object is invoked for each entity. It is provided with the
      * entity itself and a set of references to all the components of the
@@ -719,11 +727,11 @@ private:
  *
  * Iterators aren't invalidated if:
  *
- * * New instances of the given components are created and assigned to entities.
- * * The entity currently pointed is modified (as an example, if one of the
- * given components is removed from the entity to which the iterator points).
+ * * New instances of the given component are created and assigned to entities.
+ * * The entity currently pointed is modified (as an example, the given
+ *   component is removed from the entity to which the iterator points).
  *
- * In all the other cases, modify the pools of the given components somehow
+ * In all the other cases, modifying the pool of the given component in any way
  * invalidates all the iterators and using them results in undefined behavior.
  *
  * @note
@@ -737,9 +745,10 @@ private:
  *
  * @sa View
  * @sa PersistentView
+ * @sa RawView
  *
  * @tparam Entity A valid entity type (see entt_traits for more details).
- * @tparam Component Type of the component iterated by the view.
+ * @tparam Component Type of component iterated by the view.
  */
 template<typename Entity, typename Component>
 class View<Entity, Component> final {
@@ -755,12 +764,12 @@ class View<Entity, Component> final {
 
 public:
     /*! @brief Input iterator type. */
-    using iterator_type = typename pool_type::iterator_type;
+    using iterator_type = typename view_type::iterator_type;
     /*! @brief Underlying entity identifier. */
     using entity_type = typename pool_type::entity_type;
     /*! @brief Unsigned integer type. */
     using size_type = typename pool_type::size_type;
-    /*! @brief Type of the component iterated by the view. */
+    /*! @brief Type of component iterated by the view. */
     using raw_type = typename pool_type::object_type;
 
     /**
@@ -834,7 +843,7 @@ public:
      * @return An iterator to the first entity that has the given component.
      */
     iterator_type begin() const noexcept {
-        return pool.begin();
+        return pool.view_type::begin();
     }
 
     /**
@@ -853,7 +862,7 @@ public:
      * given component.
      */
     iterator_type end() const noexcept {
-        return pool.end();
+        return pool.view_type::end();
     }
 
     /**
@@ -905,7 +914,8 @@ public:
     }
 
     /**
-     * @brief Iterate the entities and applies them the given function object.
+     * @brief Iterates entities and components and applies the given function
+     * object to them.
      *
      * The function object is invoked for each entity. It is provided with the
      * entity itself and a const reference to the component of the view.<br/>
@@ -920,13 +930,16 @@ public:
      */
     template<typename Func>
     void each(Func func) const {
-        for(auto entity: pool) {
+        const view_type &view = pool;
+
+        for(auto entity: view) {
             func(entity, get(entity));
         }
     }
 
     /**
-     * @brief Iterate the entities and applies them the given function object.
+     * @brief Iterates entities and components and applies the given function
+     * object to them.
      *
      * The function object is invoked for each entity. It is provided with the
      * entity itself and a reference to the component of the view.<br/>
@@ -951,6 +964,162 @@ private:
 };
 
 
+/**
+ * @brief Raw view.
+ *
+ * Raw views are meant to easily iterate components without having to resort to
+ * using any other member function, so as to further increase the performance.
+ * Whenever knowing the entity to which a component belongs isn't required, this
+ * should be the preferred tool.<br/>
+ * Order of elements during iterations are highly dependent on the order of the
+ * underlying data structure. See SparseSet and its specializations for more
+ * details.
+ *
+ * @b Important
+ *
+ * Iterators aren't invalidated if:
+ *
+ * * New instances of the given component are created and assigned to entities.
+ * * The entity to which the component belongs is modified (as an example, the
+ *   given component is destroyed).
+ *
+ * In all the other cases, modifying the pool of the given component in any way
+ * invalidates all the iterators and using them results in undefined behavior.
+ *
+ * @note
+ * Views share a reference to the underlying data structure with the Registry
+ * that generated them. Therefore any change to the entities and to the
+ * components made by means of the registry are immediately reflected by views.
+ *
+ * @warning
+ * Lifetime of a view must overcome the one of the registry that generated it.
+ * In any other case, attempting to use a view results in undefined behavior.
+ *
+ * @sa View
+ * @sa View<Entity, Component>
+ * @sa PersistentView
+ *
+ * @tparam Entity A valid entity type (see entt_traits for more details).
+ * @tparam Component Type of component iterated by the view.
+ */
+template<typename Entity, typename Component>
+class RawView final {
+    /*! @brief A registry is allowed to create views. */
+    friend class Registry<Entity>;
+
+    using view_type = SparseSet<Entity>;
+    using pool_type = SparseSet<Entity, Component>;
+
+    RawView(pool_type &pool) noexcept
+        : pool{pool}
+    {}
+
+public:
+    /*! @brief Input iterator type. */
+    using iterator_type = typename pool_type::iterator_type;
+    /*! @brief Underlying entity identifier. */
+    using entity_type = typename pool_type::entity_type;
+    /*! @brief Unsigned integer type. */
+    using size_type = typename pool_type::size_type;
+    /*! @brief Type of component iterated by the view. */
+    using raw_type = typename pool_type::object_type;
+
+    /**
+     * @brief Returns the number of instances of the given type.
+     * @return Number of instances of the given component.
+     */
+    size_type size() const noexcept {
+        return pool.size();
+    }
+
+    /**
+     * @brief Direct access to the list of components.
+     *
+     * The returned pointer is such that range `[raw(), raw() + size()]` is
+     * always a valid range, even if the container is empty.
+     *
+     * @note
+     * There are no guarantees on the order of the components. Use `begin` and
+     * `end` if you want to iterate the view in the expected order.
+     *
+     * @return A pointer to the array of components.
+     */
+    raw_type * raw() noexcept {
+        return pool.raw();
+    }
+
+    /**
+     * @brief Direct access to the list of components.
+     *
+     * The returned pointer is such that range `[raw(), raw() + size()]` is
+     * always a valid range, even if the container is empty.
+     *
+     * @note
+     * There are no guarantees on the order of the components. Use `begin` and
+     * `end` if you want to iterate the view in the expected order.
+     *
+     * @return A pointer to the array of components.
+     */
+    const raw_type * raw() const noexcept {
+        return pool.raw();
+    }
+
+    /**
+     * @brief Direct access to the list of entities.
+     *
+     * The returned pointer is such that range `[data(), data() + size()]` is
+     * always a valid range, even if the container is empty.
+     *
+     * @note
+     * There are no guarantees on the order of the entities. Use `begin` and
+     * `end` if you want to iterate the view in the expected order.
+     *
+     * @return A pointer to the array of entities.
+     */
+    const entity_type * data() const noexcept {
+        return pool.data();
+    }
+
+    /**
+     * @brief Returns an iterator to the first instance of the given type.
+     *
+     * The returned iterator points to the first instance of the given type. If
+     * the view is empty, the returned iterator will be equal to `end()`.
+     *
+     * @note
+     * Input iterators stay true to the order imposed to the underlying data
+     * structures.
+     *
+     * @return An iterator to the first instance of the given type.
+     */
+    iterator_type begin() const noexcept {
+        return pool.begin();
+    }
+
+    /**
+     * @brief Returns an iterator that is past the last instance of the given
+     * type.
+     *
+     * The returned iterator points to the element following the last instance
+     * of the given type. Attempting to dereference the returned iterator
+     * results in undefined behavior.
+     *
+     * @note
+     * Input iterators stay true to the order imposed to the underlying data
+     * structures.
+     *
+     * @return An iterator to the element following the last instance of the
+     * given type.
+     */
+    iterator_type end() const noexcept {
+        return pool.end();
+    }
+
+private:
+    pool_type &pool;
+};
+
+
 }
 
 

+ 21 - 2
test/benchmark/benchmark.cpp

@@ -76,11 +76,14 @@ TEST(Benchmark, IterateCreateDeleteSingleComponent) {
             registry.create<Position>();
         }
 
-        view.each([&registry](auto entity, auto &&...) {
+        for(auto entity: view) {
+            const auto &position = view.get(entity);
+            (void)position;
+
             if(rand() % 2 == 0) {
                 registry.destroy(entity);
             }
-        });
+        };
     }
 
     timer.elapsed();
@@ -100,6 +103,22 @@ TEST(Benchmark, IterateSingleComponent1M) {
     timer.elapsed();
 }
 
+TEST(Benchmark, IterateSingleComponentRaw1M) {
+    entt::DefaultRegistry registry;
+
+    std::cout << "Iterating over 1000000 entities, one component, raw view" << std::endl;
+
+    for(std::uint64_t i = 0; i < 1000000L; i++) {
+        registry.create<Position>();
+    }
+
+    Timer timer;
+    for(auto &&component: registry.raw<Position>()) {
+        (void)component;
+    }
+    timer.elapsed();
+}
+
 TEST(Benchmark, IterateTwoComponents1M) {
     entt::DefaultRegistry registry;
 

+ 30 - 30
test/entt/entity/sparse_set.cpp

@@ -320,9 +320,9 @@ TEST(SparseSetWithType, RawBeginEnd) {
     auto begin = set.begin();
     auto end = set.end();
 
-    ASSERT_EQ(set.get(*(begin++)), 9);
-    ASSERT_EQ(set.get(*(begin++)), 6);
-    ASSERT_EQ(set.get(*(begin++)), 3);
+    ASSERT_EQ(*(begin++), 9);
+    ASSERT_EQ(*(begin++), 6);
+    ASSERT_EQ(*(begin++), 3);
     ASSERT_EQ(begin, end);
 }
 
@@ -354,11 +354,11 @@ TEST(SparseSetWithType, SortOrdered) {
     auto begin = set.begin();
     auto end = set.end();
 
-    ASSERT_EQ(set.get(*(begin++)), 1);
-    ASSERT_EQ(set.get(*(begin++)), 3);
-    ASSERT_EQ(set.get(*(begin++)), 6);
-    ASSERT_EQ(set.get(*(begin++)), 9);
-    ASSERT_EQ(set.get(*(begin++)), 12);
+    ASSERT_EQ(*(begin++), 1);
+    ASSERT_EQ(*(begin++), 3);
+    ASSERT_EQ(*(begin++), 6);
+    ASSERT_EQ(*(begin++), 9);
+    ASSERT_EQ(*(begin++), 12);
     ASSERT_EQ(begin, end);
 }
 
@@ -390,11 +390,11 @@ TEST(SparseSetWithType, SortReverse) {
     auto begin = set.begin();
     auto end = set.end();
 
-    ASSERT_EQ(set.get(*(begin++)), 1);
-    ASSERT_EQ(set.get(*(begin++)), 3);
-    ASSERT_EQ(set.get(*(begin++)), 6);
-    ASSERT_EQ(set.get(*(begin++)), 9);
-    ASSERT_EQ(set.get(*(begin++)), 12);
+    ASSERT_EQ(*(begin++), 1);
+    ASSERT_EQ(*(begin++), 3);
+    ASSERT_EQ(*(begin++), 6);
+    ASSERT_EQ(*(begin++), 9);
+    ASSERT_EQ(*(begin++), 12);
     ASSERT_EQ(begin, end);
 }
 
@@ -426,11 +426,11 @@ TEST(SparseSetWithType, SortUnordered) {
     auto begin = set.begin();
     auto end = set.end();
 
-    ASSERT_EQ(set.get(*(begin++)), 1);
-    ASSERT_EQ(set.get(*(begin++)), 3);
-    ASSERT_EQ(set.get(*(begin++)), 6);
-    ASSERT_EQ(set.get(*(begin++)), 9);
-    ASSERT_EQ(set.get(*(begin++)), 12);
+    ASSERT_EQ(*(begin++), 1);
+    ASSERT_EQ(*(begin++), 3);
+    ASSERT_EQ(*(begin++), 6);
+    ASSERT_EQ(*(begin++), 9);
+    ASSERT_EQ(*(begin++), 12);
     ASSERT_EQ(begin, end);
 }
 
@@ -443,9 +443,9 @@ TEST(SparseSetWithType, RespectDisjoint) {
     lhs.construct(12, 6);
     lhs.construct(42, 9);
 
-    ASSERT_EQ(lhs.get(3), 3);
-    ASSERT_EQ(lhs.get(12), 6);
-    ASSERT_EQ(lhs.get(42), 9);
+    ASSERT_EQ(clhs.get(3), 3);
+    ASSERT_EQ(clhs.get(12), 6);
+    ASSERT_EQ(clhs.get(42), 9);
 
     lhs.respect(rhs);
 
@@ -456,9 +456,9 @@ TEST(SparseSetWithType, RespectDisjoint) {
     auto begin = clhs.begin();
     auto end = clhs.end();
 
-    ASSERT_EQ(clhs.get(*(begin++)), 9);
-    ASSERT_EQ(clhs.get(*(begin++)), 6);
-    ASSERT_EQ(clhs.get(*(begin++)), 3);
+    ASSERT_EQ(*(begin++), 9);
+    ASSERT_EQ(*(begin++), 6);
+    ASSERT_EQ(*(begin++), 3);
     ASSERT_EQ(begin, end);
 }
 
@@ -472,9 +472,9 @@ TEST(SparseSetWithType, RespectOverlap) {
     lhs.construct(42, 9);
     rhs.construct(12, 6);
 
-    ASSERT_EQ(lhs.get(3), 3);
-    ASSERT_EQ(lhs.get(12), 6);
-    ASSERT_EQ(lhs.get(42), 9);
+    ASSERT_EQ(clhs.get(3), 3);
+    ASSERT_EQ(clhs.get(12), 6);
+    ASSERT_EQ(clhs.get(42), 9);
     ASSERT_EQ(rhs.get(12), 6);
 
     lhs.respect(rhs);
@@ -486,9 +486,9 @@ TEST(SparseSetWithType, RespectOverlap) {
     auto begin = clhs.begin();
     auto end = clhs.end();
 
-    ASSERT_EQ(clhs.get(*(begin++)), 6);
-    ASSERT_EQ(clhs.get(*(begin++)), 9);
-    ASSERT_EQ(clhs.get(*(begin++)), 3);
+    ASSERT_EQ(*(begin++), 6);
+    ASSERT_EQ(*(begin++), 9);
+    ASSERT_EQ(*(begin++), 3);
     ASSERT_EQ(begin, end);
 }
 

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

@@ -343,3 +343,64 @@ TEST(PersistentView, Sort) {
         ASSERT_EQ(view.get<int>(entity), ival++);
     }
 }
+
+
+
+
+
+
+
+////////////////////////////////////////////////////////////////////////////////
+
+TEST(RawView, Functionalities) {
+    entt::DefaultRegistry registry;
+
+    auto e0 = registry.create();
+    auto e1 = registry.create<int, char>();
+
+    ASSERT_NO_THROW(registry.raw<char>().begin()++);
+    ASSERT_NO_THROW(++registry.raw<char>().begin());
+
+    auto view = registry.raw<char>();
+
+    ASSERT_NE(view.begin(), view.end());
+    ASSERT_EQ(view.size(), typename decltype(view)::size_type{1});
+
+    registry.assign<char>(e0);
+
+    ASSERT_EQ(view.size(), typename decltype(view)::size_type{2});
+
+    registry.get<char>(e0) = '1';
+    registry.get<char>(e1) = '2';
+
+    for(auto &&component: view) {
+        ASSERT_TRUE(component == '1' || component == '2');
+    }
+
+    ASSERT_EQ(*(view.data() + 0), e1);
+    ASSERT_EQ(*(view.data() + 1), e0);
+
+    ASSERT_EQ(*(view.raw() + 0), '2');
+    ASSERT_EQ(*(static_cast<const decltype(view) &>(view).raw() + 1), '1');
+
+    registry.remove<char>(e0);
+    registry.remove<char>(e1);
+
+    ASSERT_EQ(view.begin(), view.end());
+}
+
+TEST(RawView, Empty) {
+    entt::DefaultRegistry registry;
+
+    registry.create<char, double>();
+    registry.create<char>();
+
+    auto view = registry.raw<int>();
+
+    ASSERT_EQ(view.size(), entt::DefaultRegistry::size_type{0});
+
+    for(auto &&component: view) {
+        (void)component;
+        FAIL();
+    }
+}