group: support for index based sort
This commit is contained in:
@@ -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<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) {
|
||||
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");
|
||||
descriptor->group().sort(std::move(compare), std::move(algo), std::forward<Args>(args)...);
|
||||
} else {
|
||||
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 {
|
||||
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.
|
||||
* * 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<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 {
|
||||
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");
|
||||
storage<0>().sort_n(descriptor->length(), std::move(compare), std::move(algo), std::forward<Args>(args)...);
|
||||
} else {
|
||||
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 {
|
||||
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)...));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user