storage: removed ::sort_n, capture pool and use ::get if needed

This commit is contained in:
Michele Caini
2021-07-18 23:25:38 +02:00
parent 5d2af3f9e1
commit 834f7feb27
3 changed files with 11 additions and 50 deletions

View File

@@ -1437,7 +1437,14 @@ public:
void sort(Compare compare, Sort algo = Sort{}, Args &&... args) {
ENTT_ASSERT(sortable<Component>(), "Cannot sort owned storage");
auto *cpool = assure<Component>();
cpool->sort_n(cpool->size(), std::move(compare), std::move(algo), std::forward<Args>(args)...);
if constexpr(std::is_invocable_v<Compare, decltype(cpool->get({})), 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>(args)...);
} else {
cpool->sort(std::move(compare), std::move(algo), std::forward<Args>(args)...);
}
}
/**

View File

@@ -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<typename Compare, typename Sort = std_sort, typename... Args>
void sort_n(const size_type length, Compare compare, Sort algo = Sort{}, Args &&... args) {
if constexpr(std::is_invocable_v<Compare, const value_type &, const value_type &>) {
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>(args)...);
} else {
underlying_type::sort_n(length, std::move(compare), std::move(algo), std::forward<Args>(args)...);
}
}
private:
compressed_pair<alloc, size_type> bucket;
alloc_ptr_pointer packed;

View File

@@ -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();