diff --git a/src/entt/entity/registry.hpp b/src/entt/entity/registry.hpp index c000488b7..8dd809f4b 100644 --- a/src/entt/entity/registry.hpp +++ b/src/entt/entity/registry.hpp @@ -1437,7 +1437,14 @@ public: void sort(Compare compare, Sort algo = Sort{}, Args &&... args) { ENTT_ASSERT(sortable(), "Cannot sort owned storage"); auto *cpool = assure(); - cpool->sort_n(cpool->size(), std::move(compare), std::move(algo), std::forward(args)...); + + if constexpr(std::is_invocable_vget({})), decltype(cpool->get({}))>) { + cpool->sort([this, cpool, compare = std::move(compare)](const auto lhs, const auto rhs) { + return compare(std::as_const(cpool->get(lhs)), std::as_const(cpool->get(rhs))); + }, std::move(algo), std::forward(args)...); + } else { + cpool->sort(std::move(compare), std::move(algo), std::forward(args)...); + } } /** diff --git a/src/entt/entity/storage.hpp b/src/entt/entity/storage.hpp index 81845a49a..a5876f5cd 100644 --- a/src/entt/entity/storage.hpp +++ b/src/entt/entity/storage.hpp @@ -647,52 +647,6 @@ public: } } - /** - * @brief Sort elements according to the given comparison function. - * - * 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: - * - * @code{.cpp} - * bool(const Entity, const Entity); - * bool(const Type &, const Type &); - * @endcode - * - * 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. - * - * @warning - * Empty types are never instantiated. Therefore, only comparison function - * objects that require to return entities rather than components are - * accepted. - * - * @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 length Number of elements to sort. - * @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_n(const size_type length, Compare compare, Sort algo = Sort{}, Args &&... args) { - if constexpr(std::is_invocable_v) { - underlying_type::sort_n(length, [this, compare = std::move(compare)](const auto lhs, const auto rhs) { - return compare(std::as_const(get(lhs)), std::as_const(get(rhs))); - }, std::move(algo), std::forward(args)...); - } else { - underlying_type::sort_n(length, std::move(compare), std::move(algo), std::forward(args)...); - } - } - private: compressed_pair bucket; alloc_ptr_pointer packed; diff --git a/test/entt/entity/storage.cpp b/test/entt/entity/storage.cpp index 31143d9ca..c309545dc 100644 --- a/test/entt/entity/storage.cpp +++ b/test/entt/entity/storage.cpp @@ -817,12 +817,12 @@ TEST(Storage, SortRange) { boxed_int values[5u]{{3}, {6}, {1}, {9}, {12}}; pool.insert(std::begin(entities), std::end(entities), values); - pool.sort_n(0u, [](auto lhs, auto rhs) { return lhs.value < rhs.value; }); + pool.sort_n(0u, [&pool](auto lhs, auto rhs) { return pool.get(lhs).value < pool.get(rhs).value; }); ASSERT_TRUE(std::equal(std::rbegin(entities), std::rend(entities), pool.entt::sparse_set::begin(), pool.entt::sparse_set::end())); ASSERT_TRUE(std::equal(std::rbegin(values), std::rend(values), pool.begin(), pool.end())); - pool.sort_n(2u, [](auto lhs, auto rhs) { return lhs.value < rhs.value; }); + pool.sort_n(2u, [&pool](auto lhs, auto rhs) { return pool.get(lhs).value < pool.get(rhs).value; }); ASSERT_EQ(pool.raw()[0u][0u], boxed_int{6}); ASSERT_EQ(pool.raw()[0u][1u], boxed_int{3}); @@ -832,7 +832,7 @@ TEST(Storage, SortRange) { ASSERT_EQ(pool.data()[1u], entt::entity{12}); ASSERT_EQ(pool.data()[2u], entt::entity{7}); - pool.sort_n(5u, [](auto lhs, auto rhs) { return lhs.value < rhs.value; }); + pool.sort_n(5u, [&pool](auto lhs, auto rhs) { return pool.get(lhs).value < pool.get(rhs).value; }); auto begin = pool.begin(); auto end = pool.end();