diff --git a/src/entt/entity/group.hpp b/src/entt/entity/group.hpp index 97c11de18..ac6e73e36 100644 --- a/src/entt/entity/group.hpp +++ b/src/entt/entity/group.hpp @@ -603,7 +603,8 @@ public: * * An iterator past the last element of the range to sort. * * 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 Sort Type of 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 args Arguments to forward to the sort function object, if any. */ - template + template + void sort(Compare compare, Sort algo = Sort{}, Args &&...args) { + sort, index_of...>(std::move(compare), std::move(algo), std::forward(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 void sort(Compare compare, Sort algo = Sort{}, Args &&...args) { if(*this) { - if constexpr(sizeof...(Type) == 0) { + if constexpr(sizeof...(Index) == 0) { static_assert(std::is_invocable_v, "Invalid comparison function"); descriptor->group().sort(std::move(compare), std::move(algo), std::forward(args)...); } else { auto comp = [&compare, cpools = pools()](const entity_type lhs, const entity_type rhs) { - if constexpr(sizeof...(Type) == 1) { - return compare((std::get>(cpools)->get(lhs), ...), (std::get>(cpools)->get(rhs), ...)); + if constexpr(sizeof...(Index) == 1) { + return compare((std::get(cpools)->get(lhs), ...), (std::get(cpools)->get(rhs), ...)); } else { - return compare(std::forward_as_tuple(std::get>(cpools)->get(lhs)...), std::forward_as_tuple(std::get>(cpools)->get(rhs)...)); + return compare(std::forward_as_tuple(std::get(cpools)->get(lhs)...), std::forward_as_tuple(std::get(cpools)->get(rhs)...)); } }; @@ -1000,7 +1019,8 @@ public: * * An iterator past the last element of the range to sort. * * 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 Sort Type of 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 args Arguments to forward to the sort function object, if any. */ - template + template + void sort(Compare compare, Sort algo = Sort{}, Args &&...args) const { + sort, index_of...>(std::move(compare), std::move(algo), std::forward(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 void sort(Compare compare, Sort algo = Sort{}, Args &&...args) const { const auto cpools = pools(); - if constexpr(sizeof...(Type) == 0) { + if constexpr(sizeof...(Index) == 0) { static_assert(std::is_invocable_v, "Invalid comparison function"); storage<0>().sort_n(descriptor->length(), std::move(compare), std::move(algo), std::forward(args)...); } else { auto comp = [&compare, &cpools](const entity_type lhs, const entity_type rhs) { - if constexpr(sizeof...(Type) == 1) { - return compare((std::get>(cpools)->get(lhs), ...), (std::get>(cpools)->get(rhs), ...)); + if constexpr(sizeof...(Index) == 1) { + return compare((std::get(cpools)->get(lhs), ...), (std::get(cpools)->get(rhs), ...)); } else { - return compare(std::forward_as_tuple(std::get>(cpools)->get(lhs)...), std::forward_as_tuple(std::get>(cpools)->get(rhs)...)); + return compare(std::forward_as_tuple(std::get(cpools)->get(lhs)...), std::forward_as_tuple(std::get(cpools)->get(rhs)...)); } };