Function sort

Summary

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

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

(2) template <typename Component>
    void sort() const

Function overload

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 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 424-438 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) {
    if constexpr(sizeof...(Component) == 0) {
        static_assert(std::is_invocable_v<Compare, const entity_type, const entity_type>, "Invalid comparison function");
        handler->sort(std::move(compare), std::move(algo), std::forward<Args>(args)...);
    }  else if constexpr(sizeof...(Component) == 1) {
        handler->sort([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 {
        handler->sort([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)...);
    }
}

Synopsis

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

template <typename Component>
void sort() const

Description

Sort the shared pool of entities according to the given component.

Non-owning groups of the same type share with the registry a pool of entities with its own order that doesn't depend on the order of any pool of components. Users can order the underlying data structure so that it respects the order of the pool of the given component.

Note
The shared pool of entities and thus its order is affected by the changes to each and every pool that it tracks. Therefore changes to those pools can quickly ruin the order imposed to the pool of entities shared between the non-owning groups.
Template Parameters

Component - Type of component to use to impose the order.

Source

Lines 456-459 in src/entt/entity/group.hpp.

template<typename Component>
void sort() const {
    handler->respect(*std::get<storage_type<Component> *>(pools));
}





Add Discussion

Log in to comment