mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 11:28:12 +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:
parent
058cd1241e
commit
686ade6b5a
14 changed files with 35 additions and 23 deletions
|
@ -30,14 +30,8 @@
|
||||||
|
|
||||||
namespace AK {
|
namespace AK {
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
bool is_less_than(const T& a, const T& b)
|
|
||||||
{
|
|
||||||
return a < b;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename Iterator, typename LessThan>
|
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;
|
int size = end - start;
|
||||||
if (size <= 1)
|
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);
|
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;
|
using AK::quick_sort;
|
||||||
|
|
|
@ -542,7 +542,7 @@ void IRCClient::handle_rpl_namreply(const Message& msg)
|
||||||
auto& channel = ensure_channel(channel_name);
|
auto& channel = ensure_channel(channel_name);
|
||||||
auto members = msg.arguments[3].split(' ');
|
auto members = msg.arguments[3].split(' ');
|
||||||
|
|
||||||
quick_sort(members.begin(), members.end(), [](auto& a, auto& b) {
|
quick_sort(members, [](auto& a, auto& b) {
|
||||||
return strcasecmp(a.characters(), b.characters()) < 0;
|
return strcasecmp(a.characters(), b.characters()) < 0;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -123,7 +123,7 @@ NonnullRefPtr<GUI::Menu> build_system_menu()
|
||||||
Vector<String> sorted_app_categories;
|
Vector<String> sorted_app_categories;
|
||||||
for (auto& category : seen_app_categories)
|
for (auto& category : seen_app_categories)
|
||||||
sorted_app_categories.append(category);
|
sorted_app_categories.append(category);
|
||||||
quick_sort(sorted_app_categories.begin(), sorted_app_categories.end(), [](auto& a, auto& b) { return a < b; });
|
quick_sort(sorted_app_categories);
|
||||||
|
|
||||||
u8 system_menu_name[] = { 0xc3, 0xb8, 0 };
|
u8 system_menu_name[] = { 0xc3, 0xb8, 0 };
|
||||||
auto system_menu = GUI::Menu::construct(String((const char*)system_menu_name));
|
auto system_menu = GUI::Menu::construct(String((const char*)system_menu_name));
|
||||||
|
@ -175,7 +175,7 @@ NonnullRefPtr<GUI::Menu> build_system_menu()
|
||||||
auto theme_path = String::format("/res/themes/%s", theme_name.characters());
|
auto theme_path = String::format("/res/themes/%s", theme_name.characters());
|
||||||
g_themes.append({ FileSystemPath(theme_name).title(), theme_path });
|
g_themes.append({ FileSystemPath(theme_name).title(), theme_path });
|
||||||
}
|
}
|
||||||
quick_sort(g_themes.begin(), g_themes.end(), [](auto& a, auto& b) { return a.name < b.name; });
|
quick_sort(g_themes, [](auto& a, auto& b) { return a.name < b.name; });
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
|
|
|
@ -60,7 +60,7 @@ struct Project::ProjectTreeNode : public RefCounted<ProjectTreeNode> {
|
||||||
{
|
{
|
||||||
if (type == Type::File)
|
if (type == Type::File)
|
||||||
return;
|
return;
|
||||||
quick_sort(children.begin(), children.end(), [](auto& a, auto& b) {
|
quick_sort(children, [](auto& a, auto& b) {
|
||||||
return a->name < b->name;
|
return a->name < b->name;
|
||||||
});
|
});
|
||||||
for (auto& child : children)
|
for (auto& child : children)
|
||||||
|
|
|
@ -401,7 +401,7 @@ bool Scheduler::pick_next()
|
||||||
sorted_runnables.append(&thread);
|
sorted_runnables.append(&thread);
|
||||||
return IterationDecision::Continue;
|
return IterationDecision::Continue;
|
||||||
});
|
});
|
||||||
quick_sort(sorted_runnables.begin(), sorted_runnables.end(), [](auto& a, auto& b) { return a->effective_priority() >= b->effective_priority(); });
|
quick_sort(sorted_runnables, [](auto& a, auto& b) { return a->effective_priority() >= b->effective_priority(); });
|
||||||
|
|
||||||
Thread* thread_to_schedule = nullptr;
|
Thread* thread_to_schedule = nullptr;
|
||||||
|
|
||||||
|
|
|
@ -179,7 +179,7 @@ String ELFLoader::symbolicate(u32 address, u32* out_offset) const
|
||||||
m_sorted_symbols.append({ symbol.value(), symbol.name() });
|
m_sorted_symbols.append({ symbol.value(), symbol.name() });
|
||||||
return IterationDecision::Continue;
|
return IterationDecision::Continue;
|
||||||
});
|
});
|
||||||
quick_sort(m_sorted_symbols.begin(), m_sorted_symbols.end(), [](auto& a, auto& b) {
|
quick_sort(m_sorted_symbols, [](auto& a, auto& b) {
|
||||||
return a.address < b.address;
|
return a.address < b.address;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -71,7 +71,7 @@ void GFontDatabase::for_each_font(Function<void(const StringView&)> callback)
|
||||||
names.ensure_capacity(m_name_to_metadata.size());
|
names.ensure_capacity(m_name_to_metadata.size());
|
||||||
for (auto& it : m_name_to_metadata)
|
for (auto& it : m_name_to_metadata)
|
||||||
names.append(it.key);
|
names.append(it.key);
|
||||||
quick_sort(names.begin(), names.end(), AK::is_less_than<String>);
|
quick_sort(names);
|
||||||
for (auto& name : names)
|
for (auto& name : names)
|
||||||
callback(name);
|
callback(name);
|
||||||
}
|
}
|
||||||
|
@ -84,7 +84,7 @@ void GFontDatabase::for_each_fixed_width_font(Function<void(const StringView&)>
|
||||||
if (it.value.is_fixed_width)
|
if (it.value.is_fixed_width)
|
||||||
names.append(it.key);
|
names.append(it.key);
|
||||||
}
|
}
|
||||||
quick_sort(names.begin(), names.end(), AK::is_less_than<String>);
|
quick_sort(names);
|
||||||
for (auto& name : names)
|
for (auto& name : names)
|
||||||
callback(name);
|
callback(name);
|
||||||
}
|
}
|
||||||
|
|
|
@ -121,7 +121,7 @@ void SortingProxyModel::resort()
|
||||||
did_update();
|
did_update();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
quick_sort(m_row_mappings.begin(), m_row_mappings.end(), [&](auto row1, auto row2) -> bool {
|
quick_sort(m_row_mappings, [&](auto row1, auto row2) -> bool {
|
||||||
auto data1 = target().data(target().index(row1, m_key_column), Model::Role::Sort);
|
auto data1 = target().data(target().index(row1, m_key_column), Model::Role::Sort);
|
||||||
auto data2 = target().data(target().index(row2, m_key_column), Model::Role::Sort);
|
auto data2 = target().data(target().index(row2, m_key_column), Model::Role::Sort);
|
||||||
if (data1 == data2)
|
if (data1 == data2)
|
||||||
|
|
|
@ -71,7 +71,7 @@ void AppletManager::add_applet(Window& applet)
|
||||||
{
|
{
|
||||||
m_applets.append(applet.make_weak_ptr());
|
m_applets.append(applet.make_weak_ptr());
|
||||||
|
|
||||||
quick_sort(m_applets.begin(), m_applets.end(), [](auto& a, auto& b) {
|
quick_sort(m_applets, [](auto& a, auto& b) {
|
||||||
auto index_a = order_vector.find_first_index(a->title());
|
auto index_a = order_vector.find_first_index(a->title());
|
||||||
auto index_b = order_vector.find_first_index(b->title());
|
auto index_b = order_vector.find_first_index(b->title());
|
||||||
ASSERT(index_a.has_value());
|
ASSERT(index_a.has_value());
|
||||||
|
|
|
@ -127,7 +127,7 @@ void LineEditor::cache_path()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
quick_sort(m_path.begin(), m_path.end(), AK::is_less_than<String>);
|
quick_sort(m_path);
|
||||||
}
|
}
|
||||||
|
|
||||||
void LineEditor::cut_mismatching_chars(String& completion, const String& other, size_t start_compare)
|
void LineEditor::cut_mismatching_chars(String& completion, const String& other, size_t start_compare)
|
||||||
|
|
|
@ -221,7 +221,7 @@ int main(int argc, char** argv)
|
||||||
|
|
||||||
Vector<Index> byte_vector;
|
Vector<Index> byte_vector;
|
||||||
expand_list(tokens, byte_vector);
|
expand_list(tokens, byte_vector);
|
||||||
quick_sort(byte_vector.begin(), byte_vector.end(), [](auto& a, auto& b) { return a.m_from < b.m_from; });
|
quick_sort(byte_vector, [](auto& a, auto& b) { return a.m_from < b.m_from; });
|
||||||
/* Process each file */
|
/* Process each file */
|
||||||
for (auto& file : files) {
|
for (auto& file : files) {
|
||||||
cut_file(file, byte_vector);
|
cut_file(file, byte_vector);
|
||||||
|
|
|
@ -326,7 +326,7 @@ int do_file_system_object_long(const char* path)
|
||||||
files.append(move(metadata));
|
files.append(move(metadata));
|
||||||
}
|
}
|
||||||
|
|
||||||
quick_sort(files.begin(), files.end(), [](auto& a, auto& b) {
|
quick_sort(files, [](auto& a, auto& b) {
|
||||||
if (flag_sort_by_timestamp) {
|
if (flag_sort_by_timestamp) {
|
||||||
if (flag_reverse_sort)
|
if (flag_reverse_sort)
|
||||||
return a.stat.st_mtime > b.stat.st_mtime;
|
return a.stat.st_mtime > b.stat.st_mtime;
|
||||||
|
@ -382,7 +382,7 @@ int do_file_system_object_short(const char* path)
|
||||||
if (names.last().length() > longest_name)
|
if (names.last().length() > longest_name)
|
||||||
longest_name = name.length();
|
longest_name = name.length();
|
||||||
}
|
}
|
||||||
quick_sort(names.begin(), names.end(), [](auto& a, auto& b) { return a < b; });
|
quick_sort(names);
|
||||||
|
|
||||||
size_t printed_on_row = 0;
|
size_t printed_on_row = 0;
|
||||||
size_t nprinted = 0;
|
size_t nprinted = 0;
|
||||||
|
|
|
@ -50,7 +50,7 @@ int main(int argc, char** argv)
|
||||||
lines.append(buffer);
|
lines.append(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
quick_sort(lines.begin(), lines.end(), [](auto& a, auto& b) {
|
quick_sort(lines, [](auto& a, auto& b) {
|
||||||
return strcmp(a.characters(), b.characters()) < 0;
|
return strcmp(a.characters(), b.characters()) < 0;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -184,7 +184,7 @@ int main(int, char**)
|
||||||
threads.append(&it.value);
|
threads.append(&it.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
quick_sort(threads.begin(), threads.end(), [](auto* p1, auto* p2) {
|
quick_sort(threads, [](auto* p1, auto* p2) {
|
||||||
return p2->times_scheduled_since_prev < p1->times_scheduled_since_prev;
|
return p2->times_scheduled_since_prev < p1->times_scheduled_since_prev;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue