Merge pull request #13093 from mitaclaw/ranges-modernization-4-projection

Ranges Algorithms Modernization - Projection
This commit is contained in:
JMC47
2025-03-23 15:56:13 -04:00
committed by GitHub
36 changed files with 128 additions and 176 deletions

View File

@ -114,6 +114,7 @@ add_library(common
PcapFile.h
Profiler.cpp
Profiler.h
Projection.h
QoSSession.cpp
QoSSession.h
Random.cpp

View File

@ -11,6 +11,8 @@
#include <utility>
#include <vector>
#include "Common/Projection.h"
namespace Config
{
using Layers = std::map<LayerType, std::shared_ptr<Layer>>;
@ -168,8 +170,7 @@ const std::string& GetSystemName(System system)
std::optional<System> GetSystemFromName(const std::string& name)
{
const auto system = std::find_if(system_to_name.begin(), system_to_name.end(),
[&name](const auto& entry) { return entry.second == name; });
const auto system = std::ranges::find(system_to_name, name, Common::Projection::Value{});
if (system != system_to_name.end())
return system->first;

View File

@ -63,8 +63,7 @@ const std::vector<MemoryPatch>& MemoryPatches::GetPatches() const
void MemoryPatches::UnsetPatch(const Core::CPUThreadGuard& guard, u32 address)
{
const auto it = std::find_if(m_patches.begin(), m_patches.end(),
[address](const auto& patch) { return patch.address == address; });
const auto it = std::ranges::find(m_patches, address, &MemoryPatch::address);
if (it == m_patches.end())
return;

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

@ -220,9 +220,7 @@ WindowsMemoryRegion* MemArena::EnsureSplitRegionForMapping(void* start_address,
}
// find closest region that is <= the given address by using upper bound and decrementing
auto it = std::upper_bound(
regions.begin(), regions.end(), address,
[](u8* addr, const WindowsMemoryRegion& region) { return addr < region.m_start; });
auto it = std::ranges::upper_bound(regions, address, {}, &WindowsMemoryRegion::m_start);
if (it == regions.begin())
{
// this should never happen, implies that the given address is before the start of the
@ -363,9 +361,7 @@ bool MemArena::JoinRegionsAfterUnmap(void* start_address, size_t size)
}
// there should be a mapping that matches the request exactly, find it
auto it = std::lower_bound(
regions.begin(), regions.end(), address,
[](const WindowsMemoryRegion& region, u8* addr) { return region.m_start < addr; });
auto it = std::ranges::lower_bound(regions, address, {}, &WindowsMemoryRegion::m_start);
if (it == regions.end() || it->m_start != address || it->m_size != size)
{
// didn't find it, we were given bogus input

View File

@ -0,0 +1,30 @@
// Copyright 2025 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include <utility>
namespace Common::Projection
{
struct First
{
// TODO C++23: static operator()
template <class T>
[[nodiscard]] constexpr auto&& operator()(T&& t) const noexcept
{
return std::forward<T>(t).first;
}
};
struct Second
{
// TODO C++23: static operator()
template <class T>
[[nodiscard]] constexpr auto&& operator()(T&& t) const noexcept
{
return std::forward<T>(t).second;
}
};
using Key = First;
using Value = Second;
} // namespace Common::Projection