Browse Source

group: support for index based sort

Michele Caini 3 years ago
parent
commit
ada19432f6
1 changed files with 50 additions and 12 deletions
  1. 50 12
      src/entt/entity/group.hpp

+ 50 - 12
src/entt/entity/group.hpp

@@ -603,7 +603,8 @@ public:
      * * An iterator past the last element of the range to sort.
      * * An iterator past the last element of the range to sort.
      * * A comparison function to use to compare the elements.
      * * A comparison function to use to compare the elements.
      *
      *
-     * @tparam Type Optional types of components to compare.
+     * @tparam Type Optional type of component to compare.
+     * @tparam Other Other optional types of components to compare.
      * @tparam Compare Type of comparison function object.
      * @tparam Compare Type of comparison function object.
      * @tparam Sort Type of sort function object.
      * @tparam Sort Type of sort function object.
      * @tparam Args Types of arguments to forward to the sort function object.
      * @tparam Args Types of arguments to forward to the sort function object.
@@ -611,18 +612,36 @@ public:
      * @param algo A valid sort function object.
      * @param algo A valid sort function object.
      * @param args Arguments to forward to the sort function object, if any.
      * @param args Arguments to forward to the sort function object, if any.
      */
      */
-    template<typename... Type, typename Compare, typename Sort = std_sort, typename... Args>
+    template<typename Type, typename... Other, typename Compare, typename Sort = std_sort, typename... Args>
+    void sort(Compare compare, Sort algo = Sort{}, Args &&...args) {
+        sort<index_of<Type>, index_of<Other>...>(std::move(compare), std::move(algo), std::forward<Args>(args)...);
+    }
+
+    /**
+     * @brief Sort a group according to the given comparison function.
+     *
+     * @sa sort
+     *
+     * @tparam Index Optional indexes of components to compare.
+     * @tparam Compare Type of comparison function object.
+     * @tparam Sort Type of sort function object.
+     * @tparam Args Types of arguments to forward to the sort function object.
+     * @param compare A valid comparison function object.
+     * @param algo A valid sort function object.
+     * @param args Arguments to forward to the sort function object, if any.
+     */
+    template<std::size_t... Index, typename Compare, typename Sort = std_sort, typename... Args>
     void sort(Compare compare, Sort algo = Sort{}, Args &&...args) {
     void sort(Compare compare, Sort algo = Sort{}, Args &&...args) {
         if(*this) {
         if(*this) {
-            if constexpr(sizeof...(Type) == 0) {
+            if constexpr(sizeof...(Index) == 0) {
                 static_assert(std::is_invocable_v<Compare, const entity_type, const entity_type>, "Invalid comparison function");
                 static_assert(std::is_invocable_v<Compare, const entity_type, const entity_type>, "Invalid comparison function");
                 descriptor->group().sort(std::move(compare), std::move(algo), std::forward<Args>(args)...);
                 descriptor->group().sort(std::move(compare), std::move(algo), std::forward<Args>(args)...);
             } else {
             } else {
                 auto comp = [&compare, cpools = pools()](const entity_type lhs, const entity_type rhs) {
                 auto comp = [&compare, cpools = pools()](const entity_type lhs, const entity_type rhs) {
-                    if constexpr(sizeof...(Type) == 1) {
-                        return compare((std::get<index_of<Type>>(cpools)->get(lhs), ...), (std::get<index_of<Type>>(cpools)->get(rhs), ...));
+                    if constexpr(sizeof...(Index) == 1) {
+                        return compare((std::get<Index>(cpools)->get(lhs), ...), (std::get<Index>(cpools)->get(rhs), ...));
                     } else {
                     } else {
-                        return compare(std::forward_as_tuple(std::get<index_of<Type>>(cpools)->get(lhs)...), std::forward_as_tuple(std::get<index_of<Type>>(cpools)->get(rhs)...));
+                        return compare(std::forward_as_tuple(std::get<Index>(cpools)->get(lhs)...), std::forward_as_tuple(std::get<Index>(cpools)->get(rhs)...));
                     }
                     }
                 };
                 };
 
 
@@ -1000,7 +1019,8 @@ public:
      * * An iterator past the last element of the range to sort.
      * * An iterator past the last element of the range to sort.
      * * A comparison function to use to compare the elements.
      * * A comparison function to use to compare the elements.
      *
      *
-     * @tparam Type Optional types of components to compare.
+     * @tparam Type Optional type of component to compare.
+     * @tparam Other Other optional types of components to compare.
      * @tparam Compare Type of comparison function object.
      * @tparam Compare Type of comparison function object.
      * @tparam Sort Type of sort function object.
      * @tparam Sort Type of sort function object.
      * @tparam Args Types of arguments to forward to the sort function object.
      * @tparam Args Types of arguments to forward to the sort function object.
@@ -1008,19 +1028,37 @@ public:
      * @param algo A valid sort function object.
      * @param algo A valid sort function object.
      * @param args Arguments to forward to the sort function object, if any.
      * @param args Arguments to forward to the sort function object, if any.
      */
      */
-    template<typename... Type, typename Compare, typename Sort = std_sort, typename... Args>
+    template<typename Type, typename... Other, typename Compare, typename Sort = std_sort, typename... Args>
+    void sort(Compare compare, Sort algo = Sort{}, Args &&...args) const {
+        sort<index_of<Type>, index_of<Other>...>(std::move(compare), std::move(algo), std::forward<Args>(args)...);
+    }
+
+    /**
+     * @brief Sort a group according to the given comparison function.
+     *
+     * @sa sort
+     *
+     * @tparam Index Optional indexes of components to compare.
+     * @tparam Compare Type of comparison function object.
+     * @tparam Sort Type of sort function object.
+     * @tparam Args Types of arguments to forward to the sort function object.
+     * @param compare A valid comparison function object.
+     * @param algo A valid sort function object.
+     * @param args Arguments to forward to the sort function object, if any.
+     */
+    template<std::size_t... Index, typename Compare, typename Sort = std_sort, typename... Args>
     void sort(Compare compare, Sort algo = Sort{}, Args &&...args) const {
     void sort(Compare compare, Sort algo = Sort{}, Args &&...args) const {
         const auto cpools = pools();
         const auto cpools = pools();
 
 
-        if constexpr(sizeof...(Type) == 0) {
+        if constexpr(sizeof...(Index) == 0) {
             static_assert(std::is_invocable_v<Compare, const entity_type, const entity_type>, "Invalid comparison function");
             static_assert(std::is_invocable_v<Compare, const entity_type, const entity_type>, "Invalid comparison function");
             storage<0>().sort_n(descriptor->length(), std::move(compare), std::move(algo), std::forward<Args>(args)...);
             storage<0>().sort_n(descriptor->length(), std::move(compare), std::move(algo), std::forward<Args>(args)...);
         } else {
         } else {
             auto comp = [&compare, &cpools](const entity_type lhs, const entity_type rhs) {
             auto comp = [&compare, &cpools](const entity_type lhs, const entity_type rhs) {
-                if constexpr(sizeof...(Type) == 1) {
-                    return compare((std::get<index_of<Type>>(cpools)->get(lhs), ...), (std::get<index_of<Type>>(cpools)->get(rhs), ...));
+                if constexpr(sizeof...(Index) == 1) {
+                    return compare((std::get<Index>(cpools)->get(lhs), ...), (std::get<Index>(cpools)->get(rhs), ...));
                 } else {
                 } else {
-                    return compare(std::forward_as_tuple(std::get<index_of<Type>>(cpools)->get(lhs)...), std::forward_as_tuple(std::get<index_of<Type>>(cpools)->get(rhs)...));
+                    return compare(std::forward_as_tuple(std::get<Index>(cpools)->get(lhs)...), std::forward_as_tuple(std::get<Index>(cpools)->get(rhs)...));
                 }
                 }
             };
             };