From 1c668350a04c0f1b9d5bb45c28dc2712813c35e6 Mon Sep 17 00:00:00 2001 From: mitaclaw <140017135+mitaclaw@users.noreply.github.com> Date: Sat, 28 Sep 2024 22:20:48 -0700 Subject: [PATCH] Modernize `std::stable_sort` with ranges and projections --- Source/Core/Core/State.cpp | 9 ++------- Source/Core/DiscIO/VolumeVerifier.cpp | 3 +-- .../DolphinQt/Debugger/CodeViewWidget.cpp | 8 ++------ .../ControllerInterface.cpp | 20 ++++++++----------- 4 files changed, 13 insertions(+), 27 deletions(-) diff --git a/Source/Core/Core/State.cpp b/Source/Core/Core/State.cpp index f9964c0fa8..3937be01e3 100644 --- a/Source/Core/Core/State.cpp +++ b/Source/Core/Core/State.cpp @@ -313,11 +313,6 @@ static std::vector GetUsedSlotsWithTimestamp() return result; } -static bool CompareTimestamp(const SlotWithTimestamp& lhs, const SlotWithTimestamp& rhs) -{ - return lhs.timestamp < rhs.timestamp; -} - static void CompressBufferToFile(const u8* raw_buffer, u64 size, File::IOFile& f) { u64 total_bytes_compressed = 0; @@ -1000,7 +995,7 @@ void LoadLastSaved(Core::System& system, int i) return; } - std::stable_sort(used_slots.begin(), used_slots.end(), CompareTimestamp); + std::ranges::stable_sort(used_slots, {}, &SlotWithTimestamp::timestamp); Load(system, (used_slots.end() - i)->slot); } @@ -1016,7 +1011,7 @@ void SaveFirstSaved(Core::System& system) } // overwrite the oldest state - std::stable_sort(used_slots.begin(), used_slots.end(), CompareTimestamp); + std::ranges::stable_sort(used_slots, {}, &SlotWithTimestamp::timestamp); Save(system, used_slots.front().slot, true); } diff --git a/Source/Core/DiscIO/VolumeVerifier.cpp b/Source/Core/DiscIO/VolumeVerifier.cpp index 5ad535955e..467aea6639 100644 --- a/Source/Core/DiscIO/VolumeVerifier.cpp +++ b/Source/Core/DiscIO/VolumeVerifier.cpp @@ -1340,8 +1340,7 @@ void VolumeVerifier::Finish() } // Show the most serious problems at the top - std::stable_sort(m_result.problems.begin(), m_result.problems.end(), - [](const Problem& p1, const Problem& p2) { return p1.severity > p2.severity; }); + std::ranges::stable_sort(m_result.problems, std::ranges::greater{}, &Problem::severity); const Severity highest_severity = m_result.problems.empty() ? Severity::None : m_result.problems[0].severity; diff --git a/Source/Core/DolphinQt/Debugger/CodeViewWidget.cpp b/Source/Core/DolphinQt/Debugger/CodeViewWidget.cpp index 792d78d9b0..696ff60d47 100644 --- a/Source/Core/DolphinQt/Debugger/CodeViewWidget.cpp +++ b/Source/Core/DolphinQt/Debugger/CodeViewWidget.cpp @@ -433,13 +433,9 @@ void CodeViewWidget::CalculateBranchIndentation() // process in order of how much vertical space the drawn arrow would take up // so shorter arrows go further to the left - const auto priority = [](const CodeViewBranch& b) { + std::ranges::stable_sort(m_branches, {}, [](const CodeViewBranch& b) { return b.is_link ? 0 : (std::max(b.src_addr, b.dst_addr) - std::min(b.src_addr, b.dst_addr)); - }; - std::stable_sort(m_branches.begin(), m_branches.end(), - [&priority](const CodeViewBranch& lhs, const CodeViewBranch& rhs) { - return priority(lhs) < priority(rhs); - }); + }); // build a 2D lookup table representing the columns and rows the arrow could be drawn in // and try to place all branch arrows in it as far left as possible diff --git a/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp b/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp index 10dd492ceb..b8651c608d 100644 --- a/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp +++ b/Source/Core/InputCommon/ControllerInterface/ControllerInterface.cpp @@ -273,18 +273,14 @@ bool ControllerInterface::AddDevice(std::shared_ptr device NOTICE_LOG_FMT(CONTROLLERINTERFACE, "Added device: {}", device->GetQualifiedName()); m_devices.emplace_back(std::move(device)); - // We can't (and don't want) to control the order in which devices are added, but we - // need their order to be consistent, and we need the same one to always be the first, where - // present (the keyboard and mouse device usually). This is because when defaulting a - // controller profile, it will automatically select the first device in the list as its default. - std::stable_sort(m_devices.begin(), m_devices.end(), - [](const std::shared_ptr& a, - const std::shared_ptr& b) { - // It would be nice to sort devices by Source then Name then ID but it's - // better to leave them sorted by the add order, which also avoids breaking - // the order on other platforms that are less tested. - return a->GetSortPriority() > b->GetSortPriority(); - }); + // We can't (and don't want) to control the order in which devices are added, but we need + // their order to be consistent, and we need the same one to always be the first, where present + // (the keyboard and mouse device usually). This is because when defaulting a controller + // profile, it will automatically select the first device in the list as its default. It would + // be nice to sort devices by Source then Name then ID, but it's better to leave them sorted by + // the add order. This also avoids breaking the order on other platforms that are less tested. + std::ranges::stable_sort(m_devices, std::ranges::greater{}, + &ciface::Core::Device::GetSortPriority); } if (!m_populating_devices_counter)