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
+1
View File
@@ -114,6 +114,7 @@ add_library(common
PcapFile.h
Profiler.cpp
Profiler.h
Projection.h
QoSSession.cpp
QoSSession.h
Random.cpp
+3 -2
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;
+1 -2
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;
+1 -4
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);
}
+2 -6
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
+30
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
+1 -2
View File
@@ -525,8 +525,7 @@ std::array<const DSPOPCTemplate*, EXT_OPTABLE_SIZE> s_ext_op_table;
template <size_t N>
auto FindByName(std::string_view name, const std::array<DSPOPCTemplate, N>& data)
{
return std::find_if(data.cbegin(), data.cend(),
[&name](const auto& info) { return name == info.name; });
return std::ranges::find(data, name, &DSPOPCTemplate::name);
}
} // Anonymous namespace
+1 -2
View File
@@ -63,8 +63,7 @@ bool LabelMap::RegisterLabel(std::string label, u16 lval, LabelType type)
void LabelMap::DeleteLabel(std::string_view label)
{
const auto iter = std::find_if(labels.cbegin(), labels.cend(),
[&label](const auto& entry) { return entry.name == label; });
const auto iter = std::ranges::find(labels, label, &Label::name);
if (iter == labels.cend())
return;
+1 -2
View File
@@ -240,8 +240,7 @@ bool IsEnabled(HookFlag flag, PowerPC::CoreMode mode)
u32 UnPatch(Core::System& system, std::string_view patch_name)
{
const auto patch = std::find_if(std::begin(os_patches), std::end(os_patches),
[&](const Hook& p) { return patch_name == p.name; });
const auto patch = std::ranges::find(os_patches, patch_name, &Hook::name);
if (patch == std::end(os_patches))
return 0;
+3 -4
View File
@@ -22,6 +22,7 @@
#include "Common/Crypto/SHA1.h"
#include "Common/Logging/Log.h"
#include "Common/NandPaths.h"
#include "Common/Projection.h"
#include "Common/StringUtil.h"
#include "Common/Swap.h"
#include "Core/CommonTitles.h"
@@ -571,8 +572,7 @@ SharedContentMap::~SharedContentMap() = default;
std::optional<std::string>
SharedContentMap::GetFilenameFromSHA1(const std::array<u8, 20>& sha1) const
{
const auto it = std::find_if(m_entries.begin(), m_entries.end(),
[&sha1](const auto& entry) { return entry.sha1 == sha1; });
const auto it = std::ranges::find(m_entries, sha1, &Entry::sha1);
if (it == m_entries.end())
return {};
@@ -670,8 +670,7 @@ UIDSys::UIDSys(HLE::FSCore& fs_core) : m_fs{fs_core.GetFS()}
u32 UIDSys::GetUIDFromTitle(u64 title_id) const
{
const auto it = std::find_if(m_entries.begin(), m_entries.end(),
[title_id](const auto& entry) { return entry.second == title_id; });
const auto it = std::ranges::find(m_entries, title_id, Common::Projection::Value{});
return (it == m_entries.end()) ? 0 : it->first;
}
+14 -21
View File
@@ -86,11 +86,6 @@ auto GetMetadataFields(T& obj)
return std::tie(obj.uid, obj.gid, obj.is_file, obj.modes, obj.attribute);
}
auto GetNamePredicate(const std::string& name)
{
return [&name](const auto& entry) { return entry.name == name; };
}
// Convert the host directory entries into ones that can be exposed to the emulated system.
static u64 FixupDirectoryEntries(File::FSTEntry* dir, bool is_root)
{
@@ -254,8 +249,7 @@ HostFileSystem::FstEntry* HostFileSystem::GetFstEntryForPath(const std::string&
for (const std::string& component : SplitString(std::string(path.substr(1)), '/'))
{
complete_path += '/' + component;
const auto next =
std::find_if(entry->children.begin(), entry->children.end(), GetNamePredicate(component));
const auto next = std::ranges::find(entry->children, component, &FstEntry::name);
if (next != entry->children.end())
{
entry = &*next;
@@ -552,8 +546,7 @@ ResultCode HostFileSystem::Delete(Uid uid, Gid gid, const std::string& path)
else
return ResultCode::InUse;
const auto it = std::find_if(parent->children.begin(), parent->children.end(),
GetNamePredicate(split_path.file_name));
const auto it = std::ranges::find(parent->children, split_path.file_name, &FstEntry::name);
if (it != parent->children.end())
parent->children.erase(it);
SaveFst();
@@ -642,8 +635,8 @@ ResultCode HostFileSystem::Rename(Uid uid, Gid gid, const std::string& old_path,
new_entry->name = split_new_path.file_name;
// Finally, remove the child from the old parent and move it to the new parent.
const auto it = std::find_if(old_parent->children.begin(), old_parent->children.end(),
GetNamePredicate(split_old_path.file_name));
const auto it =
std::ranges::find(old_parent->children, split_old_path.file_name, &FstEntry::name);
if (it != old_parent->children.end())
{
new_entry->data = it->data;
@@ -693,17 +686,17 @@ 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(),
[&get_key](const File::FSTEntry& one, const File::FSTEntry& two) {
const int key1 = get_key(one.virtualName);
const int key2 = get_key(two.virtualName);
if (key1 != key2)
return key1 > key2;
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);
if (key1 != key2)
return key1 > key2;
// For files that are not in the FST, sort lexicographically to ensure that
// results are consistent no matter what the underlying filesystem is.
return one.virtualName > two.virtualName;
});
// For files that are not in the FST, sort lexicographically to ensure that
// results are consistent no matter what the underlying filesystem is.
return one.virtualName > two.virtualName;
});
std::vector<std::string> output;
for (const File::FSTEntry& child : host_entry.children)
+2 -3
View File
@@ -105,9 +105,8 @@ constexpr u32 PLACEHOLDER = 0xDEADBEEF;
static bool SetupMemory(Memory::MemoryManager& memory, u64 ios_title_id, MemorySetupType setup_type)
{
auto target_imv = std::find_if(
GetMemoryValues().begin(), GetMemoryValues().end(),
[&](const MemoryValues& imv) { return imv.ios_number == (ios_title_id & 0xffff); });
auto target_imv = std::ranges::find(GetMemoryValues(), static_cast<u16>(ios_title_id & 0xffff),
&MemoryValues::ios_number);
if (target_imv == GetMemoryValues().end())
{
@@ -16,6 +16,7 @@
#include "Common/FileUtil.h"
#include "Common/Logging/Log.h"
#include "Common/NandPaths.h"
#include "Common/Projection.h"
#include "Common/SettingsHandler.h"
#include "Common/StringUtil.h"
@@ -62,8 +63,7 @@ u8 GetAreaCode(std::string_view area)
{"CHN", 6},
}};
const auto entry_pos = std::find_if(regions.cbegin(), regions.cend(),
[&area](const auto& entry) { return entry.first == area; });
const auto entry_pos = std::ranges::find(regions, area, Common::Projection::Key{});
if (entry_pos != regions.end())
return entry_pos->second;
@@ -79,8 +79,7 @@ HardwareModel GetHardwareModel(std::string_view model)
{"RVD", HardwareModel::RVD},
}};
const auto entry_pos = std::find_if(models.cbegin(), models.cend(),
[&model](const auto& entry) { return entry.first == model; });
const auto entry_pos = std::ranges::find(models, model, Common::Projection::Key{});
if (entry_pos != models.cend())
return entry_pos->second;
+3 -13
View File
@@ -1540,9 +1540,7 @@ void NetPlayClient::DisplayPlayersPing()
u32 NetPlayClient::GetPlayersMaxPing() const
{
return std::max_element(
m_players.begin(), m_players.end(),
[](const auto& a, const auto& b) { return a.second.ping < b.second.ping; })
return std::ranges::max_element(m_players, {}, [](const auto& kv) { return kv.second.ping; })
->second.ping;
}
@@ -2409,22 +2407,14 @@ bool NetPlayClient::IsFirstInGamePad(int ingame_pad) const
[](auto mapping) { return mapping > 0; });
}
static int CountLocalPads(const PadMappingArray& pad_map, const PlayerId& local_player_pid)
{
return static_cast<int>(
std::count_if(pad_map.begin(), pad_map.end(), [&local_player_pid](const auto& mapping) {
return mapping == local_player_pid;
}));
}
int NetPlayClient::NumLocalPads() const
{
return CountLocalPads(m_pad_map, m_local_player->pid);
return std::ranges::count(m_pad_map, m_local_player->pid);
}
int NetPlayClient::NumLocalWiimotes() const
{
return CountLocalPads(m_wiimote_map, m_local_player->pid);
return std::ranges::count(m_wiimote_map, m_local_player->pid);
}
static int InGameToLocal(int ingame_pad, const PadMappingArray& pad_map, PlayerId local_player_pid)
+7 -16
View File
@@ -49,8 +49,7 @@ const TBreakPoint* BreakPoints::GetBreakpoint(u32 address) const
const TBreakPoint* BreakPoints::GetRegularBreakpoint(u32 address) const
{
auto bp = std::find_if(m_breakpoints.begin(), m_breakpoints.end(),
[address](const auto& bp_) { return bp_.address == address; });
auto bp = std::ranges::find(m_breakpoints, address, &TBreakPoint::address);
if (bp == m_breakpoints.end())
return nullptr;
@@ -127,8 +126,7 @@ void BreakPoints::Add(u32 address, bool break_on_hit, bool log_on_hit,
{
// Check for existing breakpoint, and overwrite with new info.
// This is assuming we usually want the new breakpoint over an old one.
auto iter = std::find_if(m_breakpoints.begin(), m_breakpoints.end(),
[address](const auto& bp) { return bp.address == address; });
auto iter = std::ranges::find(m_breakpoints, address, &TBreakPoint::address);
TBreakPoint bp; // breakpoint settings
bp.is_enabled = true;
@@ -176,8 +174,7 @@ bool BreakPoints::ToggleBreakPoint(u32 address)
bool BreakPoints::ToggleEnable(u32 address)
{
auto iter = std::find_if(m_breakpoints.begin(), m_breakpoints.end(),
[address](const auto& bp) { return bp.address == address; });
auto iter = std::ranges::find(m_breakpoints, address, &TBreakPoint::address);
if (iter == m_breakpoints.end())
return false;
@@ -188,8 +185,7 @@ bool BreakPoints::ToggleEnable(u32 address)
bool BreakPoints::Remove(u32 address)
{
const auto iter = std::find_if(m_breakpoints.begin(), m_breakpoints.end(),
[address](const auto& bp) { return bp.address == address; });
const auto iter = std::ranges::find(m_breakpoints, address, &TBreakPoint::address);
if (iter == m_breakpoints.cend())
return false;
@@ -296,9 +292,7 @@ void MemChecks::Add(TMemCheck memory_check, bool update)
// Check for existing breakpoint, and overwrite with new info.
// This is assuming we usually want the new breakpoint over an old one.
const u32 address = memory_check.start_address;
auto old_mem_check =
std::find_if(m_mem_checks.begin(), m_mem_checks.end(),
[address](const auto& check) { return check.start_address == address; });
auto old_mem_check = std::ranges::find(m_mem_checks, address, &TMemCheck::start_address);
if (old_mem_check != m_mem_checks.end())
{
memory_check.is_enabled = old_mem_check->is_enabled; // Preserve enabled status
@@ -316,8 +310,7 @@ void MemChecks::Add(TMemCheck memory_check, bool update)
bool MemChecks::ToggleEnable(u32 address)
{
auto iter = std::find_if(m_mem_checks.begin(), m_mem_checks.end(),
[address](const auto& bp) { return bp.start_address == address; });
auto iter = std::ranges::find(m_mem_checks, address, &TMemCheck::start_address);
if (iter == m_mem_checks.end())
return false;
@@ -328,9 +321,7 @@ bool MemChecks::ToggleEnable(u32 address)
bool MemChecks::Remove(u32 address, bool update)
{
const auto iter =
std::find_if(m_mem_checks.cbegin(), m_mem_checks.cend(),
[address](const auto& check) { return check.start_address == address; });
const auto iter = std::ranges::find(m_mem_checks, address, &TMemCheck::start_address);
if (iter == m_mem_checks.cend())
return false;
@@ -521,10 +521,9 @@ void RegCache::BindToRegister(preg_t i, bool doLoad, bool makeDirty)
}
ASSERT_MSG(DYNA_REC,
std::none_of(m_regs.begin(), m_regs.end(),
[xr](const auto& r) {
return r.Location().has_value() && r.Location()->IsSimpleReg(xr);
}),
std::ranges::none_of(
m_regs, [xr](const auto& l) { return l.has_value() && l->IsSimpleReg(xr); },
&PPCCachedReg::Location),
"Xreg {} already bound", Common::ToUnderlying(xr));
m_regs[i].SetBoundTo(xr);
+2 -2
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)
{
+2 -7
View File
@@ -312,11 +312,6 @@ static std::vector<SlotWithTimestamp> 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;
@@ -999,7 +994,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);
}
@@ -1015,7 +1010,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);
}
+2 -4
View File
@@ -228,15 +228,13 @@ SysConf::Entry& SysConf::AddEntry(Entry&& entry)
SysConf::Entry* SysConf::GetEntry(std::string_view key)
{
const auto iterator = std::find_if(m_entries.begin(), m_entries.end(),
[&key](const auto& entry) { return entry.name == key; });
const auto iterator = std::ranges::find(m_entries, key, &Entry::name);
return iterator != m_entries.end() ? &*iterator : nullptr;
}
const SysConf::Entry* SysConf::GetEntry(std::string_view key) const
{
const auto iterator = std::find_if(m_entries.begin(), m_entries.end(),
[&key](const auto& entry) { return entry.name == key; });
const auto iterator = std::ranges::find(m_entries, key, &Entry::name);
return iterator != m_entries.end() ? &*iterator : nullptr;
}
+4 -4
View File
@@ -43,6 +43,7 @@
#include "Core/SysConf.h"
#include "Core/System.h"
#include "DiscIO/DiscExtractor.h"
#include "DiscIO/DiscUtils.h"
#include "DiscIO/Enums.h"
#include "DiscIO/Filesystem.h"
#include "DiscIO/VolumeDisc.h"
@@ -743,10 +744,9 @@ UpdateResult DiscSystemUpdater::DoDiscUpdate()
return UpdateResult::RegionMismatch;
const auto partitions = m_volume->GetPartitions();
const auto update_partition =
std::find_if(partitions.cbegin(), partitions.cend(), [&](const DiscIO::Partition& partition) {
return m_volume->GetPartitionType(partition) == 1u;
});
const auto update_partition = std::ranges::find(
partitions, DiscIO::PARTITION_UPDATE,
[&](const DiscIO::Partition& partition) { return m_volume->GetPartitionType(partition); });
if (update_partition == partitions.cend())
{

Some files were not shown because too many files have changed in this diff Show More