Modernize std::stable_sort with ranges and projections

This commit is contained in:
mitaclaw 2024-09-28 22:20:48 -07:00
parent d96569a74a
commit 1c668350a0
4 changed files with 13 additions and 27 deletions

View File

@ -313,11 +313,6 @@ static std::vector<SlotWithTimestamp> GetUsedSlotsWithTimestamp()
return result; 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) static void CompressBufferToFile(const u8* raw_buffer, u64 size, File::IOFile& f)
{ {
u64 total_bytes_compressed = 0; u64 total_bytes_compressed = 0;
@ -1000,7 +995,7 @@ void LoadLastSaved(Core::System& system, int i)
return; 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); Load(system, (used_slots.end() - i)->slot);
} }
@ -1016,7 +1011,7 @@ void SaveFirstSaved(Core::System& system)
} }
// overwrite the oldest state // 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); Save(system, used_slots.front().slot, true);
} }

View File

@ -1340,8 +1340,7 @@ void VolumeVerifier::Finish()
} }
// Show the most serious problems at the top // Show the most serious problems at the top
std::stable_sort(m_result.problems.begin(), m_result.problems.end(), std::ranges::stable_sort(m_result.problems, std::ranges::greater{}, &Problem::severity);
[](const Problem& p1, const Problem& p2) { return p1.severity > p2.severity; });
const Severity highest_severity = const Severity highest_severity =
m_result.problems.empty() ? Severity::None : m_result.problems[0].severity; m_result.problems.empty() ? Severity::None : m_result.problems[0].severity;

View File

@ -433,12 +433,8 @@ void CodeViewWidget::CalculateBranchIndentation()
// process in order of how much vertical space the drawn arrow would take up // process in order of how much vertical space the drawn arrow would take up
// so shorter arrows go further to the left // 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)); 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 // build a 2D lookup table representing the columns and rows the arrow could be drawn in

View File

@ -273,18 +273,14 @@ bool ControllerInterface::AddDevice(std::shared_ptr<ciface::Core::Device> device
NOTICE_LOG_FMT(CONTROLLERINTERFACE, "Added device: {}", device->GetQualifiedName()); NOTICE_LOG_FMT(CONTROLLERINTERFACE, "Added device: {}", device->GetQualifiedName());
m_devices.emplace_back(std::move(device)); 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 // We can't (and don't want) to control the order in which devices are added, but we need
// need their order to be consistent, and we need the same one to always be the first, where // their order to be consistent, and we need the same one to always be the first, where present
// present (the keyboard and mouse device usually). This is because when defaulting a // (the keyboard and mouse device usually). This is because when defaulting a controller
// controller profile, it will automatically select the first device in the list as its default. // profile, it will automatically select the first device in the list as its default. It would
std::stable_sort(m_devices.begin(), m_devices.end(), // be nice to sort devices by Source then Name then ID, but it's better to leave them sorted by
[](const std::shared_ptr<ciface::Core::Device>& a, // the add order. This also avoids breaking the order on other platforms that are less tested.
const std::shared_ptr<ciface::Core::Device>& b) { std::ranges::stable_sort(m_devices, std::ranges::greater{},
// It would be nice to sort devices by Source then Name then ID but it's &ciface::Core::Device::GetSortPriority);
// 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();
});
} }
if (!m_populating_devices_counter) if (!m_populating_devices_counter)