From d92c68e1de481b509d92bbdd68f83c9a92f254aa Mon Sep 17 00:00:00 2001 From: mitaclaw <140017135+mitaclaw@users.noreply.github.com> Date: Sat, 21 Sep 2024 14:50:23 -0700 Subject: [PATCH] Simplify `std::find_if` with `Common::Contains` --- Source/Core/Core/State.cpp | 5 +-- Source/Core/Core/WiiUtils.cpp | 7 ++-- .../DolphinQt/Debugger/BreakpointWidget.cpp | 11 ++---- .../Core/VideoBackends/Vulkan/VKSwapChain.cpp | 14 ++----- .../VideoBackends/Vulkan/VulkanContext.cpp | 37 +++++++------------ 5 files changed, 25 insertions(+), 49 deletions(-) diff --git a/Source/Core/Core/State.cpp b/Source/Core/Core/State.cpp index f9964c0fa8..aac9d05221 100644 --- a/Source/Core/Core/State.cpp +++ b/Source/Core/Core/State.cpp @@ -24,6 +24,7 @@ #include "Common/ChunkFile.h" #include "Common/CommonTypes.h" +#include "Common/Contains.h" #include "Common/Event.h" #include "Common/FileUtil.h" #include "Common/IOFile.h" @@ -261,9 +262,7 @@ static int GetEmptySlot(const std::vector& used_slots) { for (int i = 1; i <= (int)NUM_STATES; i++) { - const auto it = std::find_if(used_slots.begin(), used_slots.end(), - [i](const SlotWithTimestamp& slot) { return slot.slot == i; }); - if (it == used_slots.end()) + if (!Common::Contains(used_slots, i, &SlotWithTimestamp::slot)) return i; } return -1; diff --git a/Source/Core/Core/WiiUtils.cpp b/Source/Core/Core/WiiUtils.cpp index 054f3cf0c1..2c8b090e32 100644 --- a/Source/Core/Core/WiiUtils.cpp +++ b/Source/Core/Core/WiiUtils.cpp @@ -21,6 +21,7 @@ #include "Common/Align.h" #include "Common/Assert.h" #include "Common/CommonTypes.h" +#include "Common/Contains.h" #include "Common/EnumUtils.h" #include "Common/FileUtil.h" #include "Common/HttpRequest.h" @@ -590,10 +591,8 @@ UpdateResult OnlineSystemUpdater::InstallTitleFromNUS(const std::string& prefix_ const UpdateResult import_result = [&]() { for (const IOS::ES::Content& content : tmd.first.GetContents()) { - const bool is_already_installed = std::find_if(stored_contents.begin(), stored_contents.end(), - [&content](const auto& stored_content) { - return stored_content.id == content.id; - }) != stored_contents.end(); + const bool is_already_installed = + Common::Contains(stored_contents, content.id, &IOS::ES::Content::id); // Do skip what is already installed on the NAND. if (is_already_installed) diff --git a/Source/Core/DolphinQt/Debugger/BreakpointWidget.cpp b/Source/Core/DolphinQt/Debugger/BreakpointWidget.cpp index 2737cb22ff..2c59e86f69 100644 --- a/Source/Core/DolphinQt/Debugger/BreakpointWidget.cpp +++ b/Source/Core/DolphinQt/Debugger/BreakpointWidget.cpp @@ -16,6 +16,7 @@ #include #include +#include "Common/Contains.h" #include "Common/FileUtil.h" #include "Common/IniFile.h" #include "Core/ConfigManager.h" @@ -522,10 +523,7 @@ void BreakpointWidget::OnContextMenu(const QPoint& pos) if (!is_memory_breakpoint) { const auto& inst_breakpoints = m_system.GetPowerPC().GetBreakPoints().GetBreakPoints(); - const auto bp_iter = - 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()) + if (!Common::Contains(inst_breakpoints, bp_address, &TBreakPoint::address)) return; menu->addAction(tr("Show in Code"), [this, bp_address] { emit ShowCode(bp_address); }); @@ -538,10 +536,7 @@ void BreakpointWidget::OnContextMenu(const QPoint& pos) else { const auto& memory_breakpoints = m_system.GetPowerPC().GetMemChecks().GetMemChecks(); - const auto mb_iter = - 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()) + if (!Common::Contains(memory_breakpoints, bp_address, &TMemCheck::start_address)) return; menu->addAction(tr("Show in Memory"), [this, bp_address] { emit ShowMemory(bp_address); }); diff --git a/Source/Core/VideoBackends/Vulkan/VKSwapChain.cpp b/Source/Core/VideoBackends/Vulkan/VKSwapChain.cpp index bbe94944fb..f7260bdf0c 100644 --- a/Source/Core/VideoBackends/Vulkan/VKSwapChain.cpp +++ b/Source/Core/VideoBackends/Vulkan/VKSwapChain.cpp @@ -8,6 +8,7 @@ #include "Common/Assert.h" #include "Common/CommonFuncs.h" +#include "Common/Contains.h" #include "Common/Logging/Log.h" #include "Common/MsgHandler.h" @@ -243,31 +244,24 @@ bool SwapChain::SelectPresentMode() &mode_count, present_modes.data()); 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. // 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. - 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; return true; } // 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; return true; } // 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; return true; diff --git a/Source/Core/VideoBackends/Vulkan/VulkanContext.cpp b/Source/Core/VideoBackends/Vulkan/VulkanContext.cpp index ed3b709529..3640752231 100644 --- a/Source/Core/VideoBackends/Vulkan/VulkanContext.cpp +++ b/Source/Core/VideoBackends/Vulkan/VulkanContext.cpp @@ -9,6 +9,7 @@ #include "Common/Assert.h" #include "Common/CommonFuncs.h" +#include "Common/Contains.h" #include "Common/Logging/Log.h" #include "Common/MsgHandler.h" #include "Common/StringUtil.h" @@ -170,15 +171,12 @@ bool VulkanContext::CheckValidationLayerAvailablility() res = vkEnumerateInstanceLayerProperties(&layer_count, layer_list.data()); ASSERT(res == VK_SUCCESS); - bool supports_validation_layers = - std::find_if(layer_list.begin(), layer_list.end(), [](const auto& it) { - return strcmp(it.layerName, VALIDATION_LAYER_NAME) == 0; - }) != layer_list.end(); + bool supports_validation_layers = Common::Contains( + layer_list, std::string_view{VALIDATION_LAYER_NAME}, &VkLayerProperties::layerName); bool supports_debug_utils = - std::find_if(extension_list.begin(), extension_list.end(), [](const auto& it) { - return strcmp(it.extensionName, VK_EXT_DEBUG_UTILS_EXTENSION_NAME) == 0; - }) != extension_list.end(); + Common::Contains(extension_list, std::string_view{VK_EXT_DEBUG_UTILS_EXTENSION_NAME}, + &VkExtensionProperties::extensionName); if (!supports_debug_utils && supports_validation_layers) { @@ -197,9 +195,8 @@ bool VulkanContext::CheckValidationLayerAvailablility() extension_list.data()); ASSERT(res == VK_SUCCESS); supports_debug_utils = - std::find_if(extension_list.begin(), extension_list.end(), [](const auto& it) { - return strcmp(it.extensionName, VK_EXT_DEBUG_UTILS_EXTENSION_NAME) == 0; - }) != extension_list.end(); + Common::Contains(extension_list, std::string_view{VK_EXT_DEBUG_UTILS_EXTENSION_NAME}, + &VkExtensionProperties::extensionName); } // Check for both VK_EXT_debug_utils and VK_LAYER_KHRONOS_validation @@ -330,16 +327,10 @@ bool VulkanContext::SelectInstanceExtensions(std::vector* extension auto AddExtension = [&](const char* name, bool required) { bool extension_supported = - std::find_if(available_extension_list.begin(), available_extension_list.end(), - [&](const VkExtensionProperties& properties) { - return !strcmp(name, properties.extensionName); - }) != available_extension_list.end(); - 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(); + Common::Contains(available_extension_list, std::string_view{name}, + &VkExtensionProperties::extensionName) || + Common::Contains(validation_layer_extension_list, std::string_view{name}, + &VkExtensionProperties::extensionName); if (extension_supported) { @@ -648,10 +639,8 @@ bool VulkanContext::SelectDeviceExtensions(bool enable_surface) INFO_LOG_FMT(VIDEO, "Available extension: {}", extension_properties.extensionName); auto AddExtension = [&](const char* name, bool required) { - if (std::find_if(available_extension_list.begin(), available_extension_list.end(), - [&](const VkExtensionProperties& properties) { - return !strcmp(name, properties.extensionName); - }) != available_extension_list.end()) + if (Common::Contains(available_extension_list, std::string_view{name}, + &VkExtensionProperties::extensionName)) { INFO_LOG_FMT(VIDEO, "Enabling extension: {}", name); m_device_extensions.push_back(name);