Simplify std::find_if with Common::Contains

This commit is contained in:
mitaclaw 2024-09-21 14:50:23 -07:00
parent 110d32729e
commit d92c68e1de
5 changed files with 25 additions and 49 deletions

View File

@ -24,6 +24,7 @@
#include "Common/ChunkFile.h" #include "Common/ChunkFile.h"
#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
#include "Common/Contains.h"
#include "Common/Event.h" #include "Common/Event.h"
#include "Common/FileUtil.h" #include "Common/FileUtil.h"
#include "Common/IOFile.h" #include "Common/IOFile.h"
@ -261,9 +262,7 @@ static int GetEmptySlot(const std::vector<SlotWithTimestamp>& used_slots)
{ {
for (int i = 1; i <= (int)NUM_STATES; i++) for (int i = 1; i <= (int)NUM_STATES; i++)
{ {
const auto it = std::find_if(used_slots.begin(), used_slots.end(), if (!Common::Contains(used_slots, i, &SlotWithTimestamp::slot))
[i](const SlotWithTimestamp& slot) { return slot.slot == i; });
if (it == used_slots.end())
return i; return i;
} }
return -1; return -1;

View File

@ -21,6 +21,7 @@
#include "Common/Align.h" #include "Common/Align.h"
#include "Common/Assert.h" #include "Common/Assert.h"
#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
#include "Common/Contains.h"
#include "Common/EnumUtils.h" #include "Common/EnumUtils.h"
#include "Common/FileUtil.h" #include "Common/FileUtil.h"
#include "Common/HttpRequest.h" #include "Common/HttpRequest.h"
@ -590,10 +591,8 @@ UpdateResult OnlineSystemUpdater::InstallTitleFromNUS(const std::string& prefix_
const UpdateResult import_result = [&]() { const UpdateResult import_result = [&]() {
for (const IOS::ES::Content& content : tmd.first.GetContents()) for (const IOS::ES::Content& content : tmd.first.GetContents())
{ {
const bool is_already_installed = std::find_if(stored_contents.begin(), stored_contents.end(), const bool is_already_installed =
[&content](const auto& stored_content) { Common::Contains(stored_contents, content.id, &IOS::ES::Content::id);
return stored_content.id == content.id;
}) != stored_contents.end();
// Do skip what is already installed on the NAND. // Do skip what is already installed on the NAND.
if (is_already_installed) if (is_already_installed)

View File

@ -16,6 +16,7 @@
#include <QToolBar> #include <QToolBar>
#include <QVBoxLayout> #include <QVBoxLayout>
#include "Common/Contains.h"
#include "Common/FileUtil.h" #include "Common/FileUtil.h"
#include "Common/IniFile.h" #include "Common/IniFile.h"
#include "Core/ConfigManager.h" #include "Core/ConfigManager.h"
@ -522,10 +523,7 @@ void BreakpointWidget::OnContextMenu(const QPoint& pos)
if (!is_memory_breakpoint) if (!is_memory_breakpoint)
{ {
const auto& inst_breakpoints = m_system.GetPowerPC().GetBreakPoints().GetBreakPoints(); const auto& inst_breakpoints = m_system.GetPowerPC().GetBreakPoints().GetBreakPoints();
const auto bp_iter = if (!Common::Contains(inst_breakpoints, bp_address, &TBreakPoint::address))
std::find_if(inst_breakpoints.begin(), inst_breakpoints.end(),
[bp_address](const auto& bp) { return bp.address == bp_address; });
if (bp_iter == inst_breakpoints.end())
return; return;
menu->addAction(tr("Show in Code"), [this, bp_address] { emit ShowCode(bp_address); }); menu->addAction(tr("Show in Code"), [this, bp_address] { emit ShowCode(bp_address); });
@ -538,10 +536,7 @@ void BreakpointWidget::OnContextMenu(const QPoint& pos)
else else
{ {
const auto& memory_breakpoints = m_system.GetPowerPC().GetMemChecks().GetMemChecks(); const auto& memory_breakpoints = m_system.GetPowerPC().GetMemChecks().GetMemChecks();
const auto mb_iter = if (!Common::Contains(memory_breakpoints, bp_address, &TMemCheck::start_address))
std::find_if(memory_breakpoints.begin(), memory_breakpoints.end(),
[bp_address](const auto& bp) { return bp.start_address == bp_address; });
if (mb_iter == memory_breakpoints.end())
return; return;
menu->addAction(tr("Show in Memory"), [this, bp_address] { emit ShowMemory(bp_address); }); menu->addAction(tr("Show in Memory"), [this, bp_address] { emit ShowMemory(bp_address); });

View File

@ -8,6 +8,7 @@
#include "Common/Assert.h" #include "Common/Assert.h"
#include "Common/CommonFuncs.h" #include "Common/CommonFuncs.h"
#include "Common/Contains.h"
#include "Common/Logging/Log.h" #include "Common/Logging/Log.h"
#include "Common/MsgHandler.h" #include "Common/MsgHandler.h"
@ -243,31 +244,24 @@ bool SwapChain::SelectPresentMode()
&mode_count, present_modes.data()); &mode_count, present_modes.data());
ASSERT(res == VK_SUCCESS); ASSERT(res == VK_SUCCESS);
// Checks if a particular mode is supported, if it is, returns that mode.
auto CheckForMode = [&present_modes](VkPresentModeKHR check_mode) {
auto it = std::find_if(present_modes.begin(), present_modes.end(),
[check_mode](VkPresentModeKHR mode) { return check_mode == mode; });
return it != present_modes.end();
};
// If vsync is enabled, use VK_PRESENT_MODE_FIFO_KHR. // If vsync is enabled, use VK_PRESENT_MODE_FIFO_KHR.
// This check should not fail with conforming drivers, as the FIFO present mode is mandated by // This check should not fail with conforming drivers, as the FIFO present mode is mandated by
// the specification (VK_KHR_swapchain). In case it isn't though, fall through to any other mode. // the specification (VK_KHR_swapchain). In case it isn't though, fall through to any other mode.
if (m_vsync_enabled && CheckForMode(VK_PRESENT_MODE_FIFO_KHR)) if (m_vsync_enabled && Common::Contains(present_modes, VK_PRESENT_MODE_FIFO_KHR))
{ {
m_present_mode = VK_PRESENT_MODE_FIFO_KHR; m_present_mode = VK_PRESENT_MODE_FIFO_KHR;
return true; return true;
} }
// Prefer screen-tearing, if possible, for lowest latency. // Prefer screen-tearing, if possible, for lowest latency.
if (CheckForMode(VK_PRESENT_MODE_IMMEDIATE_KHR)) if (Common::Contains(present_modes, VK_PRESENT_MODE_IMMEDIATE_KHR))
{ {
m_present_mode = VK_PRESENT_MODE_IMMEDIATE_KHR; m_present_mode = VK_PRESENT_MODE_IMMEDIATE_KHR;
return true; return true;
} }
// Use optimized-vsync above vsync. // Use optimized-vsync above vsync.
if (CheckForMode(VK_PRESENT_MODE_MAILBOX_KHR)) if (Common::Contains(present_modes, VK_PRESENT_MODE_MAILBOX_KHR))
{ {
m_present_mode = VK_PRESENT_MODE_MAILBOX_KHR; m_present_mode = VK_PRESENT_MODE_MAILBOX_KHR;
return true; return true;

View File

@ -9,6 +9,7 @@
#include "Common/Assert.h" #include "Common/Assert.h"
#include "Common/CommonFuncs.h" #include "Common/CommonFuncs.h"
#include "Common/Contains.h"
#include "Common/Logging/Log.h" #include "Common/Logging/Log.h"
#include "Common/MsgHandler.h" #include "Common/MsgHandler.h"
#include "Common/StringUtil.h" #include "Common/StringUtil.h"
@ -170,15 +171,12 @@ bool VulkanContext::CheckValidationLayerAvailablility()
res = vkEnumerateInstanceLayerProperties(&layer_count, layer_list.data()); res = vkEnumerateInstanceLayerProperties(&layer_count, layer_list.data());
ASSERT(res == VK_SUCCESS); ASSERT(res == VK_SUCCESS);
bool supports_validation_layers = bool supports_validation_layers = Common::Contains(
std::find_if(layer_list.begin(), layer_list.end(), [](const auto& it) { layer_list, std::string_view{VALIDATION_LAYER_NAME}, &VkLayerProperties::layerName);
return strcmp(it.layerName, VALIDATION_LAYER_NAME) == 0;
}) != layer_list.end();
bool supports_debug_utils = bool supports_debug_utils =
std::find_if(extension_list.begin(), extension_list.end(), [](const auto& it) { Common::Contains(extension_list, std::string_view{VK_EXT_DEBUG_UTILS_EXTENSION_NAME},
return strcmp(it.extensionName, VK_EXT_DEBUG_UTILS_EXTENSION_NAME) == 0; &VkExtensionProperties::extensionName);
}) != extension_list.end();
if (!supports_debug_utils && supports_validation_layers) if (!supports_debug_utils && supports_validation_layers)
{ {
@ -197,9 +195,8 @@ bool VulkanContext::CheckValidationLayerAvailablility()
extension_list.data()); extension_list.data());
ASSERT(res == VK_SUCCESS); ASSERT(res == VK_SUCCESS);
supports_debug_utils = supports_debug_utils =
std::find_if(extension_list.begin(), extension_list.end(), [](const auto& it) { Common::Contains(extension_list, std::string_view{VK_EXT_DEBUG_UTILS_EXTENSION_NAME},
return strcmp(it.extensionName, VK_EXT_DEBUG_UTILS_EXTENSION_NAME) == 0; &VkExtensionProperties::extensionName);
}) != extension_list.end();
} }
// Check for both VK_EXT_debug_utils and VK_LAYER_KHRONOS_validation // Check for both VK_EXT_debug_utils and VK_LAYER_KHRONOS_validation
@ -330,16 +327,10 @@ bool VulkanContext::SelectInstanceExtensions(std::vector<const char*>* extension
auto AddExtension = [&](const char* name, bool required) { auto AddExtension = [&](const char* name, bool required) {
bool extension_supported = bool extension_supported =
std::find_if(available_extension_list.begin(), available_extension_list.end(), Common::Contains(available_extension_list, std::string_view{name},
[&](const VkExtensionProperties& properties) { &VkExtensionProperties::extensionName) ||
return !strcmp(name, properties.extensionName); Common::Contains(validation_layer_extension_list, std::string_view{name},
}) != available_extension_list.end(); &VkExtensionProperties::extensionName);
extension_supported =
extension_supported ||
std::find_if(validation_layer_extension_list.begin(), validation_layer_extension_list.end(),
[&](const VkExtensionProperties& properties) {
return !strcmp(name, properties.extensionName);
}) != validation_layer_extension_list.end();
if (extension_supported) if (extension_supported)
{ {
@ -648,10 +639,8 @@ bool VulkanContext::SelectDeviceExtensions(bool enable_surface)
INFO_LOG_FMT(VIDEO, "Available extension: {}", extension_properties.extensionName); INFO_LOG_FMT(VIDEO, "Available extension: {}", extension_properties.extensionName);
auto AddExtension = [&](const char* name, bool required) { auto AddExtension = [&](const char* name, bool required) {
if (std::find_if(available_extension_list.begin(), available_extension_list.end(), if (Common::Contains(available_extension_list, std::string_view{name},
[&](const VkExtensionProperties& properties) { &VkExtensionProperties::extensionName))
return !strcmp(name, properties.extensionName);
}) != available_extension_list.end())
{ {
INFO_LOG_FMT(VIDEO, "Enabling extension: {}", name); INFO_LOG_FMT(VIDEO, "Enabling extension: {}", name);
m_device_extensions.push_back(name); m_device_extensions.push_back(name);