1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 00:37:45 +00:00

AK: Make quick_sort() a little more ergonomic

Now it actually defaults to "a < b" comparison, instead of forcing you
to provide a trivial less-than comparator. Also you can pass in any
collection type that has .begin() and .end() and we'll sort it for you.
This commit is contained in:
Andreas Kling 2020-03-03 16:01:37 +01:00
parent 058cd1241e
commit 686ade6b5a
14 changed files with 35 additions and 23 deletions

View file

@ -30,14 +30,8 @@
namespace AK {
template<typename T>
bool is_less_than(const T& a, const T& b)
{
return a < b;
}
template<typename Iterator, typename LessThan>
void quick_sort(Iterator start, Iterator end, LessThan less_than = is_less_than)
void quick_sort(Iterator start, Iterator end, LessThan less_than)
{
int size = end - start;
if (size <= 1)
@ -62,6 +56,24 @@ void quick_sort(Iterator start, Iterator end, LessThan less_than = is_less_than)
quick_sort(start + i, end, less_than);
}
template<typename Iterator>
void quick_sort(Iterator start, Iterator end)
{
quick_sort(start, end, [](auto& a, auto& b) { return a < b; });
}
template<typename Collection, typename LessThan>
void quick_sort(Collection& collection, LessThan less_than)
{
quick_sort(collection.begin(), collection.end(), move(less_than));
}
template<typename Collection>
void quick_sort(Collection& collection)
{
quick_sort(collection.begin(), collection.end());
}
}
using AK::quick_sort;