Merge pull request #13117 from mitaclaw/ranges-modernization-9-trivial-find

Ranges Algorithms Modernization - Find
This commit is contained in:
Tilka 2024-10-11 20:27:18 +01:00 committed by GitHub
commit c1832d17f6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
32 changed files with 71 additions and 84 deletions

View File

@ -85,7 +85,7 @@ bool IniFile::Section::Delete(std::string_view key)
return false;
values.erase(it);
keys_order.erase(std::find(keys_order.begin(), keys_order.end(), key));
keys_order.erase(std::ranges::find(keys_order, key));
return true;
}

View File

@ -59,7 +59,7 @@ static std::optional<DiscIO::Language> TryParseLanguage(const std::string& local
"ja", "en", "de", "fr", "es", "it", "nl", "zh", "zh", "ko",
};
const auto it = std::find(LANGUAGES.cbegin(), LANGUAGES.cend(), split_locale[0]);
const auto it = std::ranges::find(LANGUAGES, split_locale[0]);
if (it == LANGUAGES.cend())
return std::nullopt;
@ -142,7 +142,7 @@ static std::optional<u8> ComputeDefaultCountry()
if (country == "BQ" || country == "CW" || country == "SX")
country = "AN";
const auto it = std::find(COUNTRIES.cbegin(), COUNTRIES.cend(), country);
const auto it = std::ranges::find(COUNTRIES, country);
if (it == COUNTRIES.cend())
return std::nullopt;

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

@ -252,7 +252,7 @@ HitType CodeTrace::TraceLogic(const TraceOutput& current_instr, bool first_hit)
return HitType::SKIP;
// The reg_itr will be used later for erasing.
auto reg_itr = std::find(m_reg_autotrack.begin(), m_reg_autotrack.end(), instr.reg0);
auto reg_itr = std::ranges::find(m_reg_autotrack, instr.reg0);
const bool match_reg123 =
(!instr.reg1.empty() && std::find(m_reg_autotrack.begin(), m_reg_autotrack.end(),
instr.reg1) != m_reg_autotrack.end()) ||

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

@ -721,7 +721,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

@ -77,7 +77,7 @@ std::optional<PatchEntry> DeserializeLine(std::string line)
entry.conditional = true;
}
const auto iter = std::find(s_patch_type_strings.begin(), s_patch_type_strings.end(), items[1]);
const auto iter = std::ranges::find(s_patch_type_strings, items[1]);
if (iter == s_patch_type_strings.end())
return std::nullopt;
entry.type = static_cast<PatchType>(std::distance(s_patch_type_strings.begin(), iter));

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

@ -396,7 +396,7 @@ void GeckoCodeWidget::DownloadCodes()
for (const auto& code : codes)
{
auto it = std::find(m_gecko_codes.begin(), m_gecko_codes.end(), code);
auto it = std::ranges::find(m_gecko_codes, code);
if (it == m_gecko_codes.end())
{

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

@ -145,8 +145,7 @@ void GameDigestDialog::SetResult(int pid, const std::string& result)
auto client = Settings::Instance().GetNetPlayClient();
if (client && m_results.size() >= client->GetPlayers().size())
{
if (std::adjacent_find(m_results.begin(), m_results.end(), std::not_equal_to<>()) ==
m_results.end())
if (std::ranges::adjacent_find(m_results, std::ranges::not_equal_to{}) == m_results.end())
{
m_check_label->setText(tr("The hashes match!"));
}

View File

@ -85,7 +85,7 @@ std::vector<ResourcePack>& GetPacks()
std::vector<ResourcePack*> GetLowerPriorityPacks(const ResourcePack& pack)
{
std::vector<ResourcePack*> list;
for (auto it = std::find(packs.begin(), packs.end(), pack) + 1; it != packs.end(); ++it)
for (auto it = std::ranges::find(packs, pack) + 1; it != packs.end(); ++it)
{
auto& entry = *it;
if (!IsInstalled(pack))
@ -100,7 +100,7 @@ std::vector<ResourcePack*> GetLowerPriorityPacks(const ResourcePack& pack)
std::vector<ResourcePack*> GetHigherPriorityPacks(const ResourcePack& pack)
{
std::vector<ResourcePack*> list;
auto end = std::find(packs.begin(), packs.end(), pack);
auto end = std::ranges::find(packs, pack);
for (auto it = packs.begin(); it != end; ++it)
{
@ -145,7 +145,7 @@ bool Remove(ResourcePack& pack)
if (!result)
return false;
auto pack_iterator = std::find(packs.begin(), packs.end(), pack);
auto pack_iterator = std::ranges::find(packs, pack);
if (pack_iterator == packs.end())
return false;

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();
}

View File

@ -2814,7 +2814,7 @@ TextureCacheBase::InvalidateTexture(TexAddrCache::iterator iter, bool discard_pe
// Xenoblade's sunset scene, where 35 copies are done per frame, and 25 of them are
// copied to the same address, and can be skipped.
ReleaseEFBCopyStagingTexture(std::move(entry->pending_efb_copy));
auto pending_it = std::find(m_pending_efb_copies.begin(), m_pending_efb_copies.end(), entry);
auto pending_it = std::ranges::find(m_pending_efb_copies, entry);
if (pending_it != m_pending_efb_copies.end())
m_pending_efb_copies.erase(pending_it);
}