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;
static const INIToSectionMap& ini_to_section = GetINIToSectionMap();
const auto it2 =
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;
});
const auto it2 = std::ranges::find_if(ini_to_section, [&location](const auto& entry) {
return entry.second.first == location.system && entry.second.second == location.section;
});
if (it2 != ini_to_section.end())
return {it2->first, location.key};

View File

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

View File

@ -331,18 +331,16 @@ private:
std::vector<AccessorMapping>::iterator FindAppropriateAccessor(const Core::CPUThreadGuard& guard,
u32 address)
{
return std::find_if(m_accessor_mappings.begin(), m_accessor_mappings.end(),
[&guard, address](const AccessorMapping& a) {
return a.accessors->IsValidAddress(guard, address - a.base);
});
return std::ranges::find_if(m_accessor_mappings, [&guard, address](const AccessorMapping& a) {
return a.accessors->IsValidAddress(guard, address - a.base);
});
}
std::vector<AccessorMapping>::const_iterator
FindAppropriateAccessor(const Core::CPUThreadGuard& guard, u32 address) const
{
return std::find_if(m_accessor_mappings.begin(), m_accessor_mappings.end(),
[&guard, address](const AccessorMapping& a) {
return a.accessors->IsValidAddress(guard, address - a.base);
});
return std::ranges::find_if(m_accessor_mappings, [&guard, address](const AccessorMapping& a) {
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))
continue;
const auto same_identity_save_it = std::find_if(
loaded_saves.begin(), loaded_saves.end(), [&gci](const Memcard::DEntry& entry) {
const auto same_identity_save_it =
std::ranges::find_if(loaded_saves, [&gci](const Memcard::DEntry& entry) {
return Memcard::HasSameIdentity(gci.m_gci_header, entry);
});
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
{
const auto i = std::find_if(s_groups_info.begin(), s_groups_info.end(),
[id](const auto& entry) { return entry.last >= id; });
const auto i =
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));
}

View File

@ -527,14 +527,13 @@ void ESDevice::DoState(PointerWrap& p)
ESDevice::ContextArray::iterator ESDevice::FindActiveContext(s32 fd)
{
return std::find_if(m_contexts.begin(), m_contexts.end(),
[fd](const auto& context) { return context.ipc_fd == fd && context.active; });
return std::ranges::find_if(
m_contexts, [fd](const auto& context) { return context.ipc_fd == fd && context.active; });
}
ESDevice::ContextArray::iterator ESDevice::FindInactiveContext()
{
return std::find_if(m_contexts.begin(), m_contexts.end(),
[](const auto& context) { return !context.active; });
return std::ranges::find_if(m_contexts, [](const auto& context) { return !context.active; });
}
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)},
}};
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() &&
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())
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;
});
if (ap_iterator == certs.end())

View File

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

View File

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

View File

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

View File

@ -6,6 +6,7 @@
#include <algorithm>
#include <cstddef>
#include <cstring>
#include <ranges>
#include "Common/Assert.h"
#include "Common/ChunkFile.h"
@ -215,8 +216,8 @@ void USBV5ResourceManager::OnDeviceChange(const ChangeEvent event,
if (interface.bAlternateSetting != 0)
continue;
auto it = std::find_if(m_usbv5_devices.rbegin(), m_usbv5_devices.rend(),
[](const USBV5Device& entry) { return !entry.in_use; });
auto it = std::ranges::find_if(m_usbv5_devices | std::views::reverse,
[](const USBV5Device& entry) { return !entry.in_use; });
if (it == m_usbv5_devices.rend())
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));
std::vector<USB::InterfaceDescriptor> interfaces = host_device->GetInterfaces(0);
auto it = std::find_if(interfaces.begin(), interfaces.end(),
[&](const USB::InterfaceDescriptor& interface) {
return interface.bInterfaceNumber == device.interface_number &&
interface.bAlternateSetting == alt_setting;
});
auto it = std::ranges::find_if(interfaces, [&](const USB::InterfaceDescriptor& interface) {
return interface.bInterfaceNumber == device.interface_number &&
interface.bAlternateSetting == alt_setting;
});
if (it == interfaces.end())
return IPCReply(IPC_EINVAL);
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));
std::vector<USB::InterfaceDescriptor> interfaces = host_device->GetInterfaces(0);
auto it = std::find_if(interfaces.begin(), interfaces.end(),
[&](const USB::InterfaceDescriptor& interface) {
return interface.bInterfaceNumber == device.interface_number &&
interface.bAlternateSetting == alt_setting;
});
auto it = std::ranges::find_if(interfaces, [&](const USB::InterfaceDescriptor& interface) {
return interface.bInterfaceNumber == device.interface_number &&
interface.bAlternateSetting == alt_setting;
});
if (it == interfaces.end())
return IPCReply(IPC_EINVAL);
it->Swap();

View File

@ -352,10 +352,9 @@ void MemChecks::Clear()
TMemCheck* MemChecks::GetMemCheck(u32 address, size_t size)
{
const auto iter =
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;
});
const auto iter = std::ranges::find_if(m_mem_checks, [address, size](const auto& mc) {
return mc.end_address >= address && address + size - 1 >= mc.start_address;
});
// None found
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)
{
const auto patch =
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;
});
const auto patch = std::ranges::find_if(s_patches, [&source, &is_kd](const NetworkPatch& p) {
return p.source == source && p.is_kd == is_kd && p.enabled;
});
if (patch == s_patches.end())
return std::nullopt;

View File

@ -84,8 +84,8 @@ SectorReader::~SectorReader()
const SectorReader::Cache* SectorReader::FindCacheLine(u64 block_num)
{
auto itr = std::find_if(m_cache.begin(), m_cache.end(),
[&](const Cache& entry) { return entry.Contains(block_num); });
auto itr =
std::ranges::find_if(m_cache, [&](const Cache& entry) { return entry.Contains(block_num); });
if (itr == m_cache.end())
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 bool is_file = path_separator == std::string_view::npos;
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);
});

View File

@ -472,8 +472,7 @@ std::vector<Partition> VolumeVerifier::CheckPartitions()
AddProblem(Severity::High, Common::GetStringT("The install partition is missing."));
if (ShouldHaveMasterpiecePartitions() &&
types.cend() ==
std::find_if(types.cbegin(), types.cend(), [](u32 type) { return type >= 0xFF; }))
types.cend() == std::ranges::find_if(types, [](u32 type) { return type >= 0xFF; }))
{
// 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:

View File

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

View File

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

View File

@ -170,10 +170,9 @@ bool ResourcePack::Install(const std::string& path)
continue;
const std::string texture_name = texture_zip_path.substr(texture_zip_path_prefix.size());
auto texture_it = std::find_if(
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;
});
auto texture_it = std::ranges::find_if(m_textures, [&texture_name](const std::string& texture) {
return mz_path_compare_wc(texture.c_str(), texture_name.c_str(), 1) == MZ_OK;
});
if (texture_it == m_textures.cend())
continue;
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)
{
auto removeme = std::find_if(m_usage.begin(), m_usage.end(),
[last_draw](UsageEntry usage) { return usage.drawno > last_draw; });
auto removeme = std::ranges::find_if(
m_usage, [last_draw](UsageEntry usage) { return usage.drawno > last_draw; });
if (removeme != m_usage.begin())
m_usage.erase(m_usage.begin(), removeme);

View File

@ -176,7 +176,7 @@ void Statistics::AddScissorRect()
}
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);
}) == scissors.end();
}