Modernize std::sort with ranges and projections

In PPCTables.cpp, the code is currently unused so I was unable to test it.

In CustomPipeline.cpp, a pointer to member function cannot be used due to 16.4.5.2.1 of the C++ Standard regarding "addressable functions". https://eel.is/c++draft/namespace.std#6

In Fs.cpp and DirectoryBlob.cpp, these examples used projections in a previous iteration of this commit, but no longer do. Still, they remain in this commit because the PR they would actually belong to is already merged.
This commit is contained in:
mitaclaw 2024-09-29 11:19:33 -07:00
parent 940009a645
commit d96569a74a
9 changed files with 26 additions and 38 deletions

View File

@ -485,10 +485,7 @@ static bool Pack(const std::function<bool()>& cancelled, const File::FSTEntry& e
static void SortFST(File::FSTEntry* root)
{
std::sort(root->children.begin(), root->children.end(),
[](const File::FSTEntry& lhs, const File::FSTEntry& rhs) {
return lhs.virtualName < rhs.virtualName;
});
std::ranges::sort(root->children, {}, &File::FSTEntry::virtualName);
for (auto& child : root->children)
SortFST(&child);
}

View File

@ -686,7 +686,7 @@ Result<std::vector<std::string>> HostFileSystem::ReadDirectory(Uid uid, Gid gid,
// Now sort in reverse order because Nintendo traverses a linked list
// in which new elements are inserted at the front.
std::sort(host_entry.children.begin(), host_entry.children.end(),
std::ranges::sort(host_entry.children,
[&get_key](const File::FSTEntry& one, const File::FSTEntry& two) {
const int key1 = get_key(one.virtualName);
const int key2 = get_key(two.virtualName);

View File

@ -16,6 +16,7 @@
#include "Common/FileUtil.h"
#include "Common/IOFile.h"
#include "Common/Logging/Log.h"
#include "Common/Projection.h"
#include "Common/StringUtil.h"
#include "Core/PowerPC/PowerPC.h"
@ -694,8 +695,7 @@ void PrintInstructionRunCounts()
const GekkoOPInfo& info = s_tables.all_instructions[i];
temp[i] = std::make_pair(info.opname, info.stats->run_count);
}
std::sort(temp.begin(), temp.end(),
[](const OpInfo& a, const OpInfo& b) { return a.second > b.second; });
std::ranges::sort(temp, std::ranges::greater{}, Common::Projection::Second{});
for (auto& inst : temp)
{

View File

@ -1192,14 +1192,12 @@ void DirectoryBlobPartition::WriteDirectory(std::vector<u8>* fst_data,
std::vector<FSTBuilderNode>& sorted_entries = *parent_entries;
// Sort for determinism
std::sort(sorted_entries.begin(), sorted_entries.end(),
[](const FSTBuilderNode& one, const FSTBuilderNode& two) {
std::ranges::sort(sorted_entries, [](const FSTBuilderNode& one, const FSTBuilderNode& two) {
std::string one_upper = one.m_filename;
std::string two_upper = two.m_filename;
Common::ToUpper(&one_upper);
Common::ToUpper(&two_upper);
return one_upper == two_upper ? one.m_filename < two.m_filename :
one_upper < two_upper;
return one_upper == two_upper ? one.m_filename < two.m_filename : one_upper < two_upper;
});
for (FSTBuilderNode& entry : sorted_entries)

View File

@ -1058,8 +1058,7 @@ void VolumeVerifier::SetUpHashing()
m_scrubber.SetupScrub(m_volume);
}
std::sort(m_groups.begin(), m_groups.end(),
[](const GroupToVerify& a, const GroupToVerify& b) { return a.offset < b.offset; });
std::ranges::sort(m_groups, {}, &GroupToVerify::offset);
if (m_hashes_to_calculate.crc32)
m_crc32_context = Common::StartCRC32();

View File

@ -941,8 +941,7 @@ ConversionResultCode WIARVZFileReader<RVZ>::SetUpDataEntriesForWriting(
if (volume && volume->HasWiiHashes() && volume->HasWiiEncryption())
partitions = volume->GetPartitions();
std::sort(partitions.begin(), partitions.end(),
[](const Partition& a, const Partition& b) { return a.offset < b.offset; });
std::ranges::sort(partitions, {}, &Partition::offset);
*total_groups = 0;

View File

@ -53,9 +53,7 @@ bool Init()
pack_list_order.emplace_back(OrderHelper{i, std::move(manifest_id)});
}
std::sort(
pack_list_order.begin(), pack_list_order.end(),
[](const OrderHelper& a, const OrderHelper& b) { return a.manifest_id < b.manifest_id; });
std::ranges::sort(pack_list_order, {}, &OrderHelper::manifest_id);
bool error = false;
for (size_t i = 0; i < pack_list_order.size(); ++i)

View File

@ -164,10 +164,8 @@ std::vector<std::string> GlobalConflicts(std::string_view source)
// Sort the conflicts from largest to smallest string
// this way we can ensure smaller strings that are a substring
// of the larger string are able to be replaced appropriately
std::sort(global_result.begin(), global_result.end(),
[](const std::string& first, const std::string& second) {
return first.size() > second.size();
});
std::ranges::sort(global_result, std::ranges::greater{},
[](const std::string& s) { return s.size(); });
return global_result;
}

View File

@ -2028,8 +2028,7 @@ void TextureCacheBase::StitchXFBCopy(RcTcacheEntry& stitched_entry)
if (candidates.empty())
return;
std::sort(candidates.begin(), candidates.end(),
[](const TCacheEntry* a, const TCacheEntry* b) { return a->id < b->id; });
std::ranges::sort(candidates, {}, &TCacheEntry::id);
// We only upscale when necessary to preserve resolution. i.e. when there are upscaled partial
// copies to be stitched together.