Simplify std::find_if with std::ranges::find and projections

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

In WiiUtils.cpp, the magic value `1u` was replaced by the constant value `DiscIO::PARTITION_UPDATE`.
This commit is contained in:
mitaclaw 2024-09-21 14:43:21 -07:00
parent d77e0c665c
commit ab24e9d380
21 changed files with 46 additions and 81 deletions

View File

@ -11,6 +11,8 @@
#include <utility> #include <utility>
#include <vector> #include <vector>
#include "Common/Projection.h"
namespace Config namespace Config
{ {
using Layers = std::map<LayerType, std::shared_ptr<Layer>>; 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) std::optional<System> GetSystemFromName(const std::string& name)
{ {
const auto system = std::find_if(system_to_name.begin(), system_to_name.end(), const auto system = std::ranges::find(system_to_name, name, Common::Projection::Value{});
[&name](const auto& entry) { return entry.second == name; });
if (system != system_to_name.end()) if (system != system_to_name.end())
return system->first; 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) void MemoryPatches::UnsetPatch(const Core::CPUThreadGuard& guard, u32 address)
{ {
const auto it = std::find_if(m_patches.begin(), m_patches.end(), const auto it = std::ranges::find(m_patches, address, &MemoryPatch::address);
[address](const auto& patch) { return patch.address == address; });
if (it == m_patches.end()) if (it == m_patches.end())
return; return;

View File

@ -22,6 +22,7 @@
#include "Common/IniFile.h" #include "Common/IniFile.h"
#include "Common/Logging/Log.h" #include "Common/Logging/Log.h"
#include "Common/MsgHandler.h" #include "Common/MsgHandler.h"
#include "Common/Projection.h"
#include "Common/StringUtil.h" #include "Common/StringUtil.h"
#include "Core/Config/MainSettings.h" #include "Core/Config/MainSettings.h"
@ -147,8 +148,7 @@ static Location MapINIToRealLocation(const std::string& section, const std::stri
static std::pair<std::string, std::string> GetINILocationFromConfig(const Location& location) static std::pair<std::string, std::string> GetINILocationFromConfig(const Location& location)
{ {
static const INIToLocationMap& ini_to_location = GetINIToLocationMap(); static const INIToLocationMap& ini_to_location = GetINIToLocationMap();
const auto it = std::find_if(ini_to_location.begin(), ini_to_location.end(), const auto it = std::ranges::find(ini_to_location, location, Common::Projection::Value{});
[&location](const auto& entry) { return entry.second == location; });
if (it != ini_to_location.end()) if (it != ini_to_location.end())
return it->first; return it->first;

View File

@ -525,8 +525,7 @@ std::array<const DSPOPCTemplate*, EXT_OPTABLE_SIZE> s_ext_op_table;
template <size_t N> template <size_t N>
auto FindByName(std::string_view name, const std::array<DSPOPCTemplate, N>& data) auto FindByName(std::string_view name, const std::array<DSPOPCTemplate, N>& data)
{ {
return std::find_if(data.cbegin(), data.cend(), return std::ranges::find(data, name, &DSPOPCTemplate::name);
[&name](const auto& info) { return name == info.name; });
} }
} // Anonymous namespace } // Anonymous namespace

View File

@ -63,8 +63,7 @@ bool LabelMap::RegisterLabel(std::string label, u16 lval, LabelType type)
void LabelMap::DeleteLabel(std::string_view label) void LabelMap::DeleteLabel(std::string_view label)
{ {
const auto iter = std::find_if(labels.cbegin(), labels.cend(), const auto iter = std::ranges::find(labels, label, &Label::name);
[&label](const auto& entry) { return entry.name == label; });
if (iter == labels.cend()) if (iter == labels.cend())
return; return;

View File

@ -240,8 +240,7 @@ bool IsEnabled(HookFlag flag, PowerPC::CoreMode mode)
u32 UnPatch(Core::System& system, std::string_view patch_name) 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 auto patch = std::ranges::find(os_patches, patch_name, &Hook::name);
[&](const Hook& p) { return patch_name == p.name; });
if (patch == std::end(os_patches)) if (patch == std::end(os_patches))
return 0; return 0;

View File

@ -22,6 +22,7 @@
#include "Common/Crypto/SHA1.h" #include "Common/Crypto/SHA1.h"
#include "Common/Logging/Log.h" #include "Common/Logging/Log.h"
#include "Common/NandPaths.h" #include "Common/NandPaths.h"
#include "Common/Projection.h"
#include "Common/StringUtil.h" #include "Common/StringUtil.h"
#include "Common/Swap.h" #include "Common/Swap.h"
#include "Core/CommonTitles.h" #include "Core/CommonTitles.h"
@ -571,8 +572,7 @@ SharedContentMap::~SharedContentMap() = default;
std::optional<std::string> std::optional<std::string>
SharedContentMap::GetFilenameFromSHA1(const std::array<u8, 20>& sha1) const SharedContentMap::GetFilenameFromSHA1(const std::array<u8, 20>& sha1) const
{ {
const auto it = std::find_if(m_entries.begin(), m_entries.end(), const auto it = std::ranges::find(m_entries, sha1, &Entry::sha1);
[&sha1](const auto& entry) { return entry.sha1 == sha1; });
if (it == m_entries.end()) if (it == m_entries.end())
return {}; return {};
@ -670,8 +670,7 @@ UIDSys::UIDSys(HLE::FSCore& fs_core) : m_fs{fs_core.GetFS()}
u32 UIDSys::GetUIDFromTitle(u64 title_id) const u32 UIDSys::GetUIDFromTitle(u64 title_id) const
{ {
const auto it = std::find_if(m_entries.begin(), m_entries.end(), const auto it = std::ranges::find(m_entries, title_id, Common::Projection::Value{});
[title_id](const auto& entry) { return entry.second == title_id; });
return (it == m_entries.end()) ? 0 : it->first; return (it == m_entries.end()) ? 0 : it->first;
} }

View File

@ -86,11 +86,6 @@ auto GetMetadataFields(T& obj)
return std::tie(obj.uid, obj.gid, obj.is_file, obj.modes, obj.attribute); 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. // Convert the host directory entries into ones that can be exposed to the emulated system.
static u64 FixupDirectoryEntries(File::FSTEntry* dir, bool is_root) 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)), '/')) for (const std::string& component : SplitString(std::string(path.substr(1)), '/'))
{ {
complete_path += '/' + component; complete_path += '/' + component;
const auto next = const auto next = std::ranges::find(entry->children, component, &FstEntry::name);
std::find_if(entry->children.begin(), entry->children.end(), GetNamePredicate(component));
if (next != entry->children.end()) if (next != entry->children.end())
{ {
entry = &*next; entry = &*next;
@ -552,8 +546,7 @@ ResultCode HostFileSystem::Delete(Uid uid, Gid gid, const std::string& path)
else else
return ResultCode::InUse; return ResultCode::InUse;
const auto it = std::find_if(parent->children.begin(), parent->children.end(), const auto it = std::ranges::find(parent->children, split_path.file_name, &FstEntry::name);
GetNamePredicate(split_path.file_name));
if (it != parent->children.end()) if (it != parent->children.end())
parent->children.erase(it); parent->children.erase(it);
SaveFst(); 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; new_entry->name = split_new_path.file_name;
// Finally, remove the child from the old parent and move it to the new parent. // 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(), const auto it =
GetNamePredicate(split_old_path.file_name)); std::ranges::find(old_parent->children, split_old_path.file_name, &FstEntry::name);
if (it != old_parent->children.end()) if (it != old_parent->children.end())
{ {
new_entry->data = it->data; new_entry->data = it->data;

View File

@ -105,9 +105,8 @@ constexpr u32 PLACEHOLDER = 0xDEADBEEF;
static bool SetupMemory(Memory::MemoryManager& memory, u64 ios_title_id, MemorySetupType setup_type) static bool SetupMemory(Memory::MemoryManager& memory, u64 ios_title_id, MemorySetupType setup_type)
{ {
auto target_imv = std::find_if( auto target_imv = std::ranges::find(GetMemoryValues(), static_cast<u16>(ios_title_id & 0xffff),
GetMemoryValues().begin(), GetMemoryValues().end(), &MemoryValues::ios_number);
[&](const MemoryValues& imv) { return imv.ios_number == (ios_title_id & 0xffff); });
if (target_imv == GetMemoryValues().end()) if (target_imv == GetMemoryValues().end())
{ {

View File

@ -16,6 +16,7 @@
#include "Common/FileUtil.h" #include "Common/FileUtil.h"
#include "Common/Logging/Log.h" #include "Common/Logging/Log.h"
#include "Common/NandPaths.h" #include "Common/NandPaths.h"
#include "Common/Projection.h"
#include "Common/SettingsHandler.h" #include "Common/SettingsHandler.h"
#include "Common/StringUtil.h" #include "Common/StringUtil.h"
@ -62,8 +63,7 @@ u8 GetAreaCode(std::string_view area)
{"CHN", 6}, {"CHN", 6},
}}; }};
const auto entry_pos = std::find_if(regions.cbegin(), regions.cend(), const auto entry_pos = std::ranges::find(regions, area, Common::Projection::Key{});
[&area](const auto& entry) { return entry.first == area; });
if (entry_pos != regions.end()) if (entry_pos != regions.end())
return entry_pos->second; return entry_pos->second;
@ -79,8 +79,7 @@ HardwareModel GetHardwareModel(std::string_view model)
{"RVD", HardwareModel::RVD}, {"RVD", HardwareModel::RVD},
}}; }};
const auto entry_pos = std::find_if(models.cbegin(), models.cend(), const auto entry_pos = std::ranges::find(models, model, Common::Projection::Key{});
[&model](const auto& entry) { return entry.first == model; });
if (entry_pos != models.cend()) if (entry_pos != models.cend())
return entry_pos->second; return entry_pos->second;

View File

@ -49,8 +49,7 @@ const TBreakPoint* BreakPoints::GetBreakpoint(u32 address) const
const TBreakPoint* BreakPoints::GetRegularBreakpoint(u32 address) const const TBreakPoint* BreakPoints::GetRegularBreakpoint(u32 address) const
{ {
auto bp = std::find_if(m_breakpoints.begin(), m_breakpoints.end(), auto bp = std::ranges::find(m_breakpoints, address, &TBreakPoint::address);
[address](const auto& bp_) { return bp_.address == address; });
if (bp == m_breakpoints.end()) if (bp == m_breakpoints.end())
return nullptr; 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. // Check for existing breakpoint, and overwrite with new info.
// This is assuming we usually want the new breakpoint over an old one. // This is assuming we usually want the new breakpoint over an old one.
auto iter = std::find_if(m_breakpoints.begin(), m_breakpoints.end(), auto iter = std::ranges::find(m_breakpoints, address, &TBreakPoint::address);
[address](const auto& bp) { return bp.address == address; });
TBreakPoint bp; // breakpoint settings TBreakPoint bp; // breakpoint settings
bp.is_enabled = true; bp.is_enabled = true;
@ -176,8 +174,7 @@ bool BreakPoints::ToggleBreakPoint(u32 address)
bool BreakPoints::ToggleEnable(u32 address) bool BreakPoints::ToggleEnable(u32 address)
{ {
auto iter = std::find_if(m_breakpoints.begin(), m_breakpoints.end(), auto iter = std::ranges::find(m_breakpoints, address, &TBreakPoint::address);
[address](const auto& bp) { return bp.address == address; });
if (iter == m_breakpoints.end()) if (iter == m_breakpoints.end())
return false; return false;
@ -188,8 +185,7 @@ bool BreakPoints::ToggleEnable(u32 address)
bool BreakPoints::Remove(u32 address) bool BreakPoints::Remove(u32 address)
{ {
const auto iter = std::find_if(m_breakpoints.begin(), m_breakpoints.end(), const auto iter = std::ranges::find(m_breakpoints, address, &TBreakPoint::address);
[address](const auto& bp) { return bp.address == address; });
if (iter == m_breakpoints.cend()) if (iter == m_breakpoints.cend())
return false; return false;
@ -293,9 +289,7 @@ void MemChecks::Add(TMemCheck memory_check)
// Check for existing breakpoint, and overwrite with new info. // Check for existing breakpoint, and overwrite with new info.
// This is assuming we usually want the new breakpoint over an old one. // This is assuming we usually want the new breakpoint over an old one.
const u32 address = memory_check.start_address; const u32 address = memory_check.start_address;
auto old_mem_check = auto old_mem_check = std::ranges::find(m_mem_checks, address, &TMemCheck::start_address);
std::find_if(m_mem_checks.begin(), m_mem_checks.end(),
[address](const auto& check) { return check.start_address == address; });
if (old_mem_check != m_mem_checks.end()) if (old_mem_check != m_mem_checks.end())
{ {
memory_check.is_enabled = old_mem_check->is_enabled; // Preserve enabled status memory_check.is_enabled = old_mem_check->is_enabled; // Preserve enabled status
@ -315,8 +309,7 @@ void MemChecks::Add(TMemCheck memory_check)
bool MemChecks::ToggleEnable(u32 address) bool MemChecks::ToggleEnable(u32 address)
{ {
auto iter = std::find_if(m_mem_checks.begin(), m_mem_checks.end(), auto iter = std::ranges::find(m_mem_checks, address, &TMemCheck::start_address);
[address](const auto& bp) { return bp.start_address == address; });
if (iter == m_mem_checks.end()) if (iter == m_mem_checks.end())
return false; return false;
@ -327,9 +320,7 @@ bool MemChecks::ToggleEnable(u32 address)
bool MemChecks::Remove(u32 address) bool MemChecks::Remove(u32 address)
{ {
const auto iter = const auto iter = std::ranges::find(m_mem_checks, address, &TMemCheck::start_address);
std::find_if(m_mem_checks.cbegin(), m_mem_checks.cend(),
[address](const auto& check) { return check.start_address == address; });
if (iter == m_mem_checks.cend()) if (iter == m_mem_checks.cend())
return false; return false;

View File

@ -228,15 +228,13 @@ SysConf::Entry& SysConf::AddEntry(Entry&& entry)
SysConf::Entry* SysConf::GetEntry(std::string_view key) SysConf::Entry* SysConf::GetEntry(std::string_view key)
{ {
const auto iterator = std::find_if(m_entries.begin(), m_entries.end(), const auto iterator = std::ranges::find(m_entries, key, &Entry::name);
[&key](const auto& entry) { return entry.name == key; });
return iterator != m_entries.end() ? &*iterator : nullptr; return iterator != m_entries.end() ? &*iterator : nullptr;
} }
const SysConf::Entry* SysConf::GetEntry(std::string_view key) const const SysConf::Entry* SysConf::GetEntry(std::string_view key) const
{ {
const auto iterator = std::find_if(m_entries.begin(), m_entries.end(), const auto iterator = std::ranges::find(m_entries, key, &Entry::name);
[&key](const auto& entry) { return entry.name == key; });
return iterator != m_entries.end() ? &*iterator : nullptr; return iterator != m_entries.end() ? &*iterator : nullptr;
} }

View File

@ -42,6 +42,7 @@
#include "Core/SysConf.h" #include "Core/SysConf.h"
#include "Core/System.h" #include "Core/System.h"
#include "DiscIO/DiscExtractor.h" #include "DiscIO/DiscExtractor.h"
#include "DiscIO/DiscUtils.h"
#include "DiscIO/Enums.h" #include "DiscIO/Enums.h"
#include "DiscIO/Filesystem.h" #include "DiscIO/Filesystem.h"
#include "DiscIO/VolumeDisc.h" #include "DiscIO/VolumeDisc.h"
@ -749,10 +750,9 @@ UpdateResult DiscSystemUpdater::DoDiscUpdate()
return UpdateResult::RegionMismatch; return UpdateResult::RegionMismatch;
const auto partitions = m_volume->GetPartitions(); const auto partitions = m_volume->GetPartitions();
const auto update_partition = const auto update_partition = std::ranges::find(
std::find_if(partitions.cbegin(), partitions.cend(), [&](const DiscIO::Partition& partition) { partitions, DiscIO::PARTITION_UPDATE,
return m_volume->GetPartitionType(partition) == 1u; [&](const DiscIO::Partition& partition) { return m_volume->GetPartitionType(partition); });
});
if (update_partition == partitions.cend()) if (update_partition == partitions.cend())
{ {

View File

@ -862,10 +862,7 @@ static std::vector<u8> ExtractNodeToVector(std::vector<FSTBuilderNode>* nodes, v
DirectoryBlobReader* blob) DirectoryBlobReader* blob)
{ {
std::vector<u8> data; std::vector<u8> data;
const auto it = const auto it = std::ranges::find(*nodes, userdata, &FSTBuilderNode::m_user_data);
std::find_if(nodes->begin(), nodes->end(), [&userdata](const FSTBuilderNode& node) {
return node.m_user_data == userdata;
});
if (it == nodes->end() || !it->IsFile()) if (it == nodes->end() || !it->IsFile())
return data; return data;

View File

@ -146,8 +146,7 @@ std::optional<Disc> ParseString(std::string_view xml, std::string xml_path)
const std::string macro_id = macro_node.attribute("id").as_string(); const std::string macro_id = macro_node.attribute("id").as_string();
for (auto& section : disc.m_sections) for (auto& section : disc.m_sections)
{ {
auto option_to_clone = std::find_if(section.m_options.begin(), section.m_options.end(), auto option_to_clone = std::ranges::find(section.m_options, macro_id, &Option::m_id);
[&](const Option& o) { return o.m_id == macro_id; });
if (option_to_clone == section.m_options.end()) if (option_to_clone == section.m_options.end())
continue; continue;
@ -305,8 +304,7 @@ std::vector<Patch> Disc::GeneratePatches(const std::string& game_id) const
const Choice& choice = option.m_choices[selected - 1]; const Choice& choice = option.m_choices[selected - 1];
for (const auto& patch_ref : choice.m_patch_references) for (const auto& patch_ref : choice.m_patch_references)
{ {
const auto patch = std::find_if(m_patches.begin(), m_patches.end(), const auto patch = std::ranges::find(m_patches, patch_ref.m_id, &Patch::m_id);
[&](const Patch& p) { return patch_ref.m_id == p.m_id; });
if (patch == m_patches.end()) if (patch == m_patches.end())
continue; continue;

View File

@ -1372,8 +1372,8 @@ WIARVZFileReader<RVZ>::ProcessAndCompress(CompressThreadState* state, CompressPa
TryReuse(reusable_groups, reusable_groups_mutex, &entry); TryReuse(reusable_groups, reusable_groups_mutex, &entry);
if (!entry.reused_group && reuse_id) if (!entry.reused_group && reuse_id)
{ {
const auto it = std::find_if(output_entries.begin(), output_entries.begin() + i, const auto it = std::ranges::find(output_entries.begin(), output_entries.begin() + i,
[reuse_id](const auto& e) { return e.reuse_id == reuse_id; }); reuse_id, &OutputParametersEntry::reuse_id);
if (it != output_entries.begin() + i) if (it != output_entries.begin() + i)
entry.reused_group = it->reused_group; entry.reused_group = it->reused_group;
} }

View File

@ -181,8 +181,8 @@ QGroupBox* NewPatchDialog::CreateEntry(const PatchEngine::PatchEntry& entry)
m_entry_layout->removeWidget(box); m_entry_layout->removeWidget(box);
box->deleteLater(); box->deleteLater();
m_entries.erase(std::find_if(m_entries.begin(), m_entries.end(), m_entries.erase(
[new_entry](const auto& e) { return e.get() == new_entry; })); std::ranges::find(m_entries, new_entry, [](const auto& e) { return e.get(); }));
} }
}); });

View File

@ -148,9 +148,8 @@ bool UI::IsTestMode()
bool Platform::VersionCheck(const std::vector<TodoList::UpdateOp>& to_update, bool Platform::VersionCheck(const std::vector<TodoList::UpdateOp>& to_update,
const std::string& install_base_path, const std::string& temp_dir) const std::string& install_base_path, const std::string& temp_dir)
{ {
const auto op_it = std::find_if(to_update.cbegin(), to_update.cend(), [&](const auto& op) { const auto op_it = std::ranges::find(to_update, "Dolphin.app/Contents/Info.plist",
return op.filename == "Dolphin.app/Contents/Info.plist"; &TodoList::UpdateOp::filename);
});
if (op_it == to_update.cend()) if (op_it == to_update.cend())
return true; return true;

View File

@ -65,9 +65,7 @@ void GameFileCache::Clear(DeleteOnDisk delete_on_disk)
std::shared_ptr<const GameFile> GameFileCache::AddOrGet(const std::string& path, std::shared_ptr<const GameFile> GameFileCache::AddOrGet(const std::string& path,
bool* cache_changed) bool* cache_changed)
{ {
auto it = std::find_if( auto it = std::ranges::find(m_cached_files, path, &GameFile::GetFilePath);
m_cached_files.begin(), m_cached_files.end(),
[&path](const std::shared_ptr<GameFile>& file) { return file->GetFilePath() == path; });
const bool found = it != m_cached_files.cend(); const bool found = it != m_cached_files.cend();
if (!found) if (!found)
{ {

View File

@ -278,9 +278,7 @@ void VideoBackendBase::ActivateBackend(const std::string& name)
g_video_backend = GetDefaultVideoBackend(); g_video_backend = GetDefaultVideoBackend();
const auto& backends = GetAvailableBackends(); const auto& backends = GetAvailableBackends();
const auto iter = std::find_if(backends.begin(), backends.end(), [&name](const auto& backend) { const auto iter = std::ranges::find(backends, name, &VideoBackendBase::GetName);
return name == backend->GetName();
});
if (iter == backends.end()) if (iter == backends.end())
return; return;

View File

@ -250,8 +250,7 @@ std::optional<BuildInfos> InitBuildInfos(const std::vector<TodoList::UpdateOp>&
const std::string& install_base_path, const std::string& install_base_path,
const std::string& temp_dir) const std::string& temp_dir)
{ {
const auto op_it = std::find_if(to_update.cbegin(), to_update.cend(), const auto op_it = std::ranges::find(to_update, "build_info.txt", &TodoList::UpdateOp::filename);
[&](const auto& op) { return op.filename == "build_info.txt"; });
if (op_it == to_update.cend()) if (op_it == to_update.cend())
return {}; return {};