Function sort

Synopsis

#include <src/entt/entity/group.hpp>

template <typename... Component, typename Compare, typename Sort = std_sort, typename... Args>
void sort(Compare compare, Sort algo=Sort{}, Args &&... args)

Description

Sort a group according to the given comparison function.

Sort the group so that iterating it with a couple of iterators returns entities and components in the expected order. See begin and end for more details.

The comparison function object must return true if the first element is less than the second one, false otherwise. The signature of the comparison function should be equivalent to one of the following:

bool(std::tuple<Component &...>, std::tuple<Component &...>);
bool(const Component &, const Component &);
bool(const Entity, const Entity);

Where Component are either owned types or not but still such that they are iterated by the group.
Moreover, the comparison function object shall induce a strict weak ordering on the values.

The sort function oject must offer a member function template operator() that accepts three arguments:

  • An iterator to the first 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.
Template Parameters

Component - Optional types of components to compare.

Compare - Type of comparison function object.

Sort - Type of sort function object.

Args - Types of arguments to forward to the sort function object.

Parameters

compare - A valid comparison function object.

algo - A valid sort function object.

args - Arguments to forward to the sort function object, if any.

Source

Lines 907-931 in src/entt/entity/group.hpp.

template<typename... Component, typename Compare, typename Sort = std_sort, typename... Args>
void sort(Compare compare, Sort algo = Sort{}, Args &&... args) {
    auto *cpool = std::get<0>(pools);
    if constexpr(sizeof...(Component) == 0) {
        static_assert(std::is_invocable_v<Compare, const entity_type, const entity_type>, "Invalid comparison function");
        cpool->sort_n(*length, std::move(compare), std::move(algo), std::forward<Args>(args)...);
    } else if constexpr(sizeof...(Component) == 1) {
        cpool->sort_n(*length, [this, compare = std::move(compare)](const entity_type lhs, const entity_type rhs) {
            return compare((std::get<storage_type<Component> *>(pools)->get(lhs), ...), (std::get<storage_type<Component> *>(pools)->get(rhs), ...));
        }, std::move(algo), std::forward<Args>(args)...);
    } else {
        cpool->sort_n(*length, [this, compare = std::move(compare)](const entity_type lhs, const entity_type rhs) {
            return compare(std::forward_as_tuple(std::get<storage_type<Component> *>(pools)->get(lhs)...), std::forward_as_tuple(std::get<storage_type<Component> *>(pools)->get(rhs)...));
        }, std::move(algo), std::forward<Args>(args)...);
    }
    [this](auto *head, auto *... other) {
        for(auto next = *length; next; --next) {
            const auto pos = next - 1;
            [[maybe_unused]] const auto entt = head->data()[pos];
            (other->swap(other->data()[pos], entt), ...);
        }
    }(std::get<storage_type<Owned> *>(pools)...);
}





Add Discussion

Log in to comment