Modernize `std::find_if` with ranges

In BTEmu.cpp, `std::mem_fn` was not necessary for the predicate to compile.
This commit is contained in:
mitaclaw 2024-09-21 18:09:34 -07:00
parent 6ca7e2856b
commit e4fb837f4b
24 changed files with 60 additions and 72 deletions

View File

@ -153,10 +153,9 @@ static std::pair<std::string, std::string> GetINILocationFromConfig(const Locati
return it->first; return it->first;
static const INIToSectionMap& ini_to_section = GetINIToSectionMap(); static const INIToSectionMap& ini_to_section = GetINIToSectionMap();
const auto it2 = const auto it2 = std::ranges::find_if(ini_to_section, [&location](const auto& entry) {
std::find_if(ini_to_section.begin(), ini_to_section.end(), [&location](const auto& entry) { return entry.second.first == location.system && entry.second.second == location.section;
return entry.second.first == location.system && entry.second.second == location.section; });
});
if (it2 != ini_to_section.end()) if (it2 != ini_to_section.end())
return {it2->first, location.key}; return {it2->first, location.key};

View File

@ -113,8 +113,7 @@ const DSPOPCTemplate* GetExtOpTemplate(UDSPInstruction inst);
template <typename T, size_t N> template <typename T, size_t N>
auto FindByOpcode(UDSPInstruction opcode, const std::array<T, N>& data) auto FindByOpcode(UDSPInstruction opcode, const std::array<T, N>& data)
{ {
return std::find_if(data.cbegin(), data.cend(), [opcode](const auto& info) { return std::ranges::find_if(
return (opcode & info.opcode_mask) == info.opcode; data, [opcode](const auto& info) { return (opcode & info.opcode_mask) == info.opcode; });
});
} }
} // namespace DSP } // namespace DSP

View File

@ -331,18 +331,16 @@ private:
std::vector<AccessorMapping>::iterator FindAppropriateAccessor(const Core::CPUThreadGuard& guard, std::vector<AccessorMapping>::iterator FindAppropriateAccessor(const Core::CPUThreadGuard& guard,
u32 address) u32 address)
{ {
return std::find_if(m_accessor_mappings.begin(), m_accessor_mappings.end(), return std::ranges::find_if(m_accessor_mappings, [&guard, address](const AccessorMapping& a) {
[&guard, address](const AccessorMapping& a) { return a.accessors->IsValidAddress(guard, address - a.base);
return a.accessors->IsValidAddress(guard, address - a.base); });
});
} }
std::vector<AccessorMapping>::const_iterator std::vector<AccessorMapping>::const_iterator
FindAppropriateAccessor(const Core::CPUThreadGuard& guard, u32 address) const FindAppropriateAccessor(const Core::CPUThreadGuard& guard, u32 address) const
{ {
return std::find_if(m_accessor_mappings.begin(), m_accessor_mappings.end(), return std::ranges::find_if(m_accessor_mappings, [&guard, address](const AccessorMapping& a) {
[&guard, address](const AccessorMapping& a) { return a.accessors->IsValidAddress(guard, address - a.base);
return a.accessors->IsValidAddress(guard, address - a.base); });
});
} }
}; };

View File

@ -146,8 +146,8 @@ std::vector<std::string> GCMemcardDirectory::GetFileNamesForGameID(const std::st
if (!gci_file.ReadBytes(&gci.m_gci_header, Memcard::DENTRY_SIZE)) if (!gci_file.ReadBytes(&gci.m_gci_header, Memcard::DENTRY_SIZE))
continue; continue;
const auto same_identity_save_it = std::find_if( const auto same_identity_save_it =
loaded_saves.begin(), loaded_saves.end(), [&gci](const Memcard::DEntry& entry) { std::ranges::find_if(loaded_saves, [&gci](const Memcard::DEntry& entry) {
return Memcard::HasSameIdentity(gci.m_gci_header, entry); return Memcard::HasSameIdentity(gci.m_gci_header, entry);
}); });
if (same_identity_save_it != loaded_saves.end()) if (same_identity_save_it != loaded_saves.end())

View File

@ -420,8 +420,8 @@ ControllerEmu::ControlGroup* HotkeyManager::GetHotkeyGroup(HotkeyGroup group) co
int HotkeyManager::FindGroupByID(int id) const int HotkeyManager::FindGroupByID(int id) const
{ {
const auto i = std::find_if(s_groups_info.begin(), s_groups_info.end(), const auto i =
[id](const auto& entry) { return entry.last >= id; }); std::ranges::find_if(s_groups_info, [id](const auto& entry) { return entry.last >= id; });
return static_cast<int>(std::distance(s_groups_info.begin(), i)); return static_cast<int>(std::distance(s_groups_info.begin(), i));
} }

View File

@ -527,14 +527,13 @@ void ESDevice::DoState(PointerWrap& p)
ESDevice::ContextArray::iterator ESDevice::FindActiveContext(s32 fd) ESDevice::ContextArray::iterator ESDevice::FindActiveContext(s32 fd)
{ {
return std::find_if(m_contexts.begin(), m_contexts.end(), return std::ranges::find_if(
[fd](const auto& context) { return context.ipc_fd == fd && context.active; }); m_contexts, [fd](const auto& context) { return context.ipc_fd == fd && context.active; });
} }
ESDevice::ContextArray::iterator ESDevice::FindInactiveContext() ESDevice::ContextArray::iterator ESDevice::FindInactiveContext()
{ {
return std::find_if(m_contexts.begin(), m_contexts.end(), return std::ranges::find_if(m_contexts, [](const auto& context) { return !context.active; });
[](const auto& context) { return !context.active; });
} }
std::optional<IPCReply> ESDevice::Open(const OpenRequest& request) std::optional<IPCReply> ESDevice::Open(const OpenRequest& request)

View File

@ -726,7 +726,7 @@ CertReader::CertReader(std::vector<u8>&& bytes) : SignedBlobReader(std::move(byt
{SignatureType::ECC, PublicKeyType::ECC, sizeof(CertECC)}, {SignatureType::ECC, PublicKeyType::ECC, sizeof(CertECC)},
}}; }};
const auto info = std::find_if(types.cbegin(), types.cend(), [this](const CertStructInfo& entry) { const auto info = std::ranges::find_if(types, [this](const CertStructInfo& entry) {
return m_bytes.size() >= std::get<2>(entry) && std::get<0>(entry) == GetSignatureType() && return m_bytes.size() >= std::get<2>(entry) && std::get<0>(entry) == GetSignatureType() &&
std::get<1>(entry) == GetPublicKeyType(); std::get<1>(entry) == GetPublicKeyType();
}); });

View File

@ -139,7 +139,7 @@ ReturnCode ESCore::VerifySign(const std::vector<u8>& hash, const std::vector<u8>
if (certs.empty()) if (certs.empty())
return ES_EINVAL; return ES_EINVAL;
const auto ap_iterator = std::find_if(certs.begin(), certs.end(), [](const auto& entry) { const auto ap_iterator = std::ranges::find_if(certs, [](const auto& entry) {
return entry.first.length() > 2 && entry.first.compare(0, 2, "AP") == 0; return entry.first.length() > 2 && entry.first.compare(0, 2, "AP") == 0;
}); });
if (ap_iterator == certs.end()) if (ap_iterator == certs.end())

View File

@ -207,8 +207,8 @@ Result<FileStatus> HostFileSystem::GetFileStatus(Fd fd)
HostFileSystem::Handle* HostFileSystem::AssignFreeHandle() HostFileSystem::Handle* HostFileSystem::AssignFreeHandle()
{ {
const auto it = std::find_if(m_handles.begin(), m_handles.end(), const auto it =
[](const Handle& handle) { return !handle.opened; }); std::ranges::find_if(m_handles, [](const Handle& handle) { return !handle.opened; });
if (it == m_handles.end()) if (it == m_handles.end())
return nullptr; return nullptr;

View File

@ -669,8 +669,7 @@ IOSC::KeyEntry::KeyEntry(ObjectType type_, ObjectSubType subtype_, std::vector<u
IOSC::KeyEntries::iterator IOSC::FindFreeEntry() IOSC::KeyEntries::iterator IOSC::FindFreeEntry()
{ {
return std::find_if(m_key_entries.begin(), m_key_entries.end(), return std::ranges::find_if(m_key_entries, [](const auto& entry) { return !entry.in_use; });
[](const auto& entry) { return !entry.in_use; });
} }
IOSC::KeyEntry* IOSC::FindEntry(Handle handle) IOSC::KeyEntry* IOSC::FindEntry(Handle handle)

View File

@ -473,8 +473,7 @@ bool BluetoothEmuDevice::SendEventInquiryResponse()
static_assert( static_assert(
sizeof(SHCIEventInquiryResult) - 2 + (num_responses * sizeof(hci_inquiry_response)) < 256); sizeof(SHCIEventInquiryResult) - 2 + (num_responses * sizeof(hci_inquiry_response)) < 256);
const auto iter = std::find_if(m_wiimotes.begin(), m_wiimotes.end(), const auto iter = std::ranges::find_if(m_wiimotes, &WiimoteDevice::IsInquiryScanEnabled);
std::mem_fn(&WiimoteDevice::IsInquiryScanEnabled));
if (iter == m_wiimotes.end()) if (iter == m_wiimotes.end())
{ {
// No remotes are discoverable. // No remotes are discoverable.

View File

@ -6,6 +6,7 @@
#include <algorithm> #include <algorithm>
#include <cstddef> #include <cstddef>
#include <cstring> #include <cstring>
#include <ranges>
#include "Common/Assert.h" #include "Common/Assert.h"
#include "Common/ChunkFile.h" #include "Common/ChunkFile.h"
@ -215,8 +216,8 @@ void USBV5ResourceManager::OnDeviceChange(const ChangeEvent event,
if (interface.bAlternateSetting != 0) if (interface.bAlternateSetting != 0)
continue; continue;
auto it = std::find_if(m_usbv5_devices.rbegin(), m_usbv5_devices.rend(), auto it = std::ranges::find_if(m_usbv5_devices | std::views::reverse,
[](const USBV5Device& entry) { return !entry.in_use; }); [](const USBV5Device& entry) { return !entry.in_use; });
if (it == m_usbv5_devices.rend()) if (it == m_usbv5_devices.rend())
return; return;

View File

@ -169,11 +169,10 @@ IPCReply USB_HIDv5::GetDeviceInfo(USBV5Device& device, const IOCtlRequest& reque
memory.CopyToEmu(request.buffer_out + 56, &config_descriptor, sizeof(config_descriptor)); memory.CopyToEmu(request.buffer_out + 56, &config_descriptor, sizeof(config_descriptor));
std::vector<USB::InterfaceDescriptor> interfaces = host_device->GetInterfaces(0); std::vector<USB::InterfaceDescriptor> interfaces = host_device->GetInterfaces(0);
auto it = std::find_if(interfaces.begin(), interfaces.end(), auto it = std::ranges::find_if(interfaces, [&](const USB::InterfaceDescriptor& interface) {
[&](const USB::InterfaceDescriptor& interface) { return interface.bInterfaceNumber == device.interface_number &&
return interface.bInterfaceNumber == device.interface_number && interface.bAlternateSetting == alt_setting;
interface.bAlternateSetting == alt_setting; });
});
if (it == interfaces.end()) if (it == interfaces.end())
return IPCReply(IPC_EINVAL); return IPCReply(IPC_EINVAL);
it->Swap(); it->Swap();

View File

@ -152,11 +152,10 @@ IPCReply USB_VEN::GetDeviceInfo(USBV5Device& device, const IOCtlRequest& request
memory.CopyToEmu(request.buffer_out + 40, &config_descriptor, sizeof(config_descriptor)); memory.CopyToEmu(request.buffer_out + 40, &config_descriptor, sizeof(config_descriptor));
std::vector<USB::InterfaceDescriptor> interfaces = host_device->GetInterfaces(0); std::vector<USB::InterfaceDescriptor> interfaces = host_device->GetInterfaces(0);
auto it = std::find_if(interfaces.begin(), interfaces.end(), auto it = std::ranges::find_if(interfaces, [&](const USB::InterfaceDescriptor& interface) {
[&](const USB::InterfaceDescriptor& interface) { return interface.bInterfaceNumber == device.interface_number &&
return interface.bInterfaceNumber == device.interface_number && interface.bAlternateSetting == alt_setting;
interface.bAlternateSetting == alt_setting; });
});
if (it == interfaces.end()) if (it == interfaces.end())
return IPCReply(IPC_EINVAL); return IPCReply(IPC_EINVAL);
it->Swap(); it->Swap();

View File

@ -352,10 +352,9 @@ void MemChecks::Clear()
TMemCheck* MemChecks::GetMemCheck(u32 address, size_t size) TMemCheck* MemChecks::GetMemCheck(u32 address, size_t size)
{ {
const auto iter = const auto iter = std::ranges::find_if(m_mem_checks, [address, size](const auto& mc) {
std::find_if(m_mem_checks.begin(), m_mem_checks.end(), [address, size](const auto& mc) { return mc.end_address >= address && address + size - 1 >= mc.start_address;
return mc.end_address >= address && address + size - 1 >= mc.start_address; });
});
// None found // None found
if (iter == m_mem_checks.cend()) if (iter == m_mem_checks.cend())

View File

@ -117,10 +117,9 @@ void Reload()
std::optional<std::string> GetNetworkPatch(std::string_view source, IsKD is_kd) std::optional<std::string> GetNetworkPatch(std::string_view source, IsKD is_kd)
{ {
const auto patch = const auto patch = std::ranges::find_if(s_patches, [&source, &is_kd](const NetworkPatch& p) {
std::find_if(s_patches.begin(), s_patches.end(), [&source, &is_kd](const NetworkPatch& p) { return p.source == source && p.is_kd == is_kd && p.enabled;
return p.source == source && p.is_kd == is_kd && p.enabled; });
});
if (patch == s_patches.end()) if (patch == s_patches.end())
return std::nullopt; return std::nullopt;

View File

@ -84,8 +84,8 @@ SectorReader::~SectorReader()
const SectorReader::Cache* SectorReader::FindCacheLine(u64 block_num) const SectorReader::Cache* SectorReader::FindCacheLine(u64 block_num)
{ {
auto itr = std::find_if(m_cache.begin(), m_cache.end(), auto itr =
[&](const Cache& entry) { return entry.Contains(block_num); }); std::ranges::find_if(m_cache, [&](const Cache& entry) { return entry.Contains(block_num); });
if (itr == m_cache.end()) if (itr == m_cache.end())
return nullptr; return nullptr;

View File

@ -366,7 +366,7 @@ static FSTBuilderNode* FindFileNodeInFST(std::string_view path, std::vector<FSTB
const size_t path_separator = path.find('/'); const size_t path_separator = path.find('/');
const bool is_file = path_separator == std::string_view::npos; const bool is_file = path_separator == std::string_view::npos;
const std::string_view name = is_file ? path : path.substr(0, path_separator); const std::string_view name = is_file ? path : path.substr(0, path_separator);
const auto it = std::find_if(fst->begin(), fst->end(), [&](const FSTBuilderNode& node) { const auto it = std::ranges::find_if(*fst, [&](const FSTBuilderNode& node) {
return Common::CaseInsensitiveEquals(node.m_filename, name); return Common::CaseInsensitiveEquals(node.m_filename, name);
}); });

View File

@ -472,8 +472,7 @@ std::vector<Partition> VolumeVerifier::CheckPartitions()
AddProblem(Severity::High, Common::GetStringT("The install partition is missing.")); AddProblem(Severity::High, Common::GetStringT("The install partition is missing."));
if (ShouldHaveMasterpiecePartitions() && if (ShouldHaveMasterpiecePartitions() &&
types.cend() == types.cend() == std::ranges::find_if(types, [](u32 type) { return type >= 0xFF; }))
std::find_if(types.cbegin(), types.cend(), [](u32 type) { return type >= 0xFF; }))
{ {
// i18n: This string is referring to a game mode in Super Smash Bros. Brawl called Masterpieces // i18n: This string is referring to a game mode in Super Smash Bros. Brawl called Masterpieces
// where you play demos of NES/SNES/N64 games. Official translations: // where you play demos of NES/SNES/N64 games. Official translations:

View File

@ -162,10 +162,9 @@ void GekkoSyntaxHighlight::highlightBlock(const QString& text)
} }
else if (m_mode == 1) else if (m_mode == 1)
{ {
auto paren_it = std::find_if(info->parens.begin(), info->parens.end(), auto paren_it = std::ranges::find_if(info->parens, [this](const std::pair<int, int>& p) {
[this](const std::pair<int, int>& p) { return p.first == m_cursor_loc || p.second == m_cursor_loc;
return p.first == m_cursor_loc || p.second == m_cursor_loc; });
});
if (paren_it != info->parens.end()) if (paren_it != info->parens.end())
{ {
HighlightSubstr(paren_it->first, 1, HighlightFormat::Paren); HighlightSubstr(paren_it->first, 1, HighlightFormat::Paren);

View File

@ -5,6 +5,7 @@
#include <algorithm> #include <algorithm>
#include <bit> #include <bit>
#include <ranges>
#include <QGroupBox> #include <QGroupBox>
#include <QHBoxLayout> #include <QHBoxLayout>
@ -472,9 +473,8 @@ void FIFOAnalyzer::FindNext()
const int index = m_detail_list->currentRow(); const int index = m_detail_list->currentRow();
ASSERT(index >= 0); ASSERT(index >= 0);
auto next_result = auto next_result = std::ranges::find_if(
std::find_if(m_search_results.begin(), m_search_results.end(), m_search_results, [index](auto& result) { return result.m_cmd > static_cast<u32>(index); });
[index](auto& result) { return result.m_cmd > static_cast<u32>(index); });
if (next_result != m_search_results.end()) if (next_result != m_search_results.end())
{ {
ShowSearchResult(next_result - m_search_results.begin()); ShowSearchResult(next_result - m_search_results.begin());
@ -487,8 +487,9 @@ void FIFOAnalyzer::FindPrevious()
ASSERT(index >= 0); ASSERT(index >= 0);
auto prev_result = auto prev_result =
std::find_if(m_search_results.rbegin(), m_search_results.rend(), std::ranges::find_if(m_search_results | std::views::reverse, [index](auto& result) {
[index](auto& result) { return result.m_cmd < static_cast<u32>(index); }); return result.m_cmd < static_cast<u32>(index);
});
if (prev_result != m_search_results.rend()) if (prev_result != m_search_results.rend())
{ {
ShowSearchResult((m_search_results.rend() - prev_result) - 1); ShowSearchResult((m_search_results.rend() - prev_result) - 1);

View File

@ -170,10 +170,9 @@ bool ResourcePack::Install(const std::string& path)
continue; continue;
const std::string texture_name = texture_zip_path.substr(texture_zip_path_prefix.size()); const std::string texture_name = texture_zip_path.substr(texture_zip_path_prefix.size());
auto texture_it = std::find_if( auto texture_it = std::ranges::find_if(m_textures, [&texture_name](const std::string& texture) {
m_textures.cbegin(), m_textures.cend(), [&texture_name](const std::string& texture) { return mz_path_compare_wc(texture.c_str(), texture_name.c_str(), 1) == MZ_OK;
return mz_path_compare_wc(texture.c_str(), texture_name.c_str(), 1) == MZ_OK; });
});
if (texture_it == m_textures.cend()) if (texture_it == m_textures.cend())
continue; continue;
const auto texture = *texture_it; const auto texture = *texture_it;

View File

@ -61,8 +61,8 @@ static NSString* GetName(Metal::StateTracker::UploadBuffer buffer)
bool Metal::StateTracker::UsageTracker::PrepareForAllocation(u64 last_draw, size_t amt) bool Metal::StateTracker::UsageTracker::PrepareForAllocation(u64 last_draw, size_t amt)
{ {
auto removeme = std::find_if(m_usage.begin(), m_usage.end(), auto removeme = std::ranges::find_if(
[last_draw](UsageEntry usage) { return usage.drawno > last_draw; }); m_usage, [last_draw](UsageEntry usage) { return usage.drawno > last_draw; });
if (removeme != m_usage.begin()) if (removeme != m_usage.begin())
m_usage.erase(m_usage.begin(), removeme); m_usage.erase(m_usage.begin(), removeme);

View File

@ -176,7 +176,7 @@ void Statistics::AddScissorRect()
} }
else else
{ {
add = std::find_if(scissors.begin(), scissors.end(), [&](auto& s) { add = std::ranges::find_if(scissors, [&](auto& s) {
return s.Matches(scissor, show_scissors, show_viewports); return s.Matches(scissor, show_scissors, show_viewports);
}) == scissors.end(); }) == scissors.end();
} }