From 6a1be188f2fe1b5f0d1a000b5b083ef5e5be0722 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=C5=81ukasz=20Matysiak?= <december0123@gmail.com>
Date: Sat, 4 Apr 2015 00:33:56 +0200
Subject: [PATCH] Use std::find_if instead of for loops

---
 src/Data.cpp | 90 +++++++++++-----------------------------------------
 src/Mhwd.cpp | 40 +++++++++++------------
 2 files changed, 38 insertions(+), 92 deletions(-)

diff --git a/src/Data.cpp b/src/Data.cpp
index 4b56a7e..7b85129 100644
--- a/src/Data.cpp
+++ b/src/Data.cpp
@@ -133,17 +133,10 @@ void Data::getAllDevicesOfConfig(const std::vector<std::shared_ptr<Device>>& dev
         for (auto&& i_device = devices.begin(); i_device != devices.end();
                 ++i_device)
         {
-            bool found = false;
             // Check class ids
-            for (auto&& classID = hwdID->classIDs.begin();
-                    classID != hwdID->classIDs.end(); ++classID)
-            {
-                if (*classID == "*" || *classID == (*i_device)->classID_)
-                {
-                    found = true;
-                    break;
-                }
-            }
+            bool found = std::find_if(hwdID->classIDs.begin(), hwdID->classIDs.end(), [i_device](const std::string& classID){
+            					return (("*" == classID) || (classID == (*i_device)->classID_));
+            				}) != hwdID->classIDs.end();
 
             if (!found)
             {
@@ -152,19 +145,9 @@ void Data::getAllDevicesOfConfig(const std::vector<std::shared_ptr<Device>>& dev
             else
             {
                 // Check blacklisted class ids
-                found = false;
-
-                for (auto&& blacklistedClassID =
-                        (*hwdID).blacklistedClassIDs.begin();
-                        blacklistedClassID != (*hwdID).blacklistedClassIDs.end();
-                        ++blacklistedClassID)
-                {
-                    if (*blacklistedClassID == (*i_device)->classID_)
-                    {
-                        found = true;
-                        break;
-                    }
-                }
+                found = std::find_if(hwdID->blacklistedClassIDs.begin(), hwdID->blacklistedClassIDs.end(), [i_device](const std::string& blacklistedClassID){
+                				return (blacklistedClassID == (*i_device)->classID_);
+                			}) != hwdID->blacklistedClassIDs.end();
 
                 if (found)
                 {
@@ -173,17 +156,9 @@ void Data::getAllDevicesOfConfig(const std::vector<std::shared_ptr<Device>>& dev
                 else
                 {
                     // Check vendor ids
-                    found = false;
-
-                    for (auto&& vendorID = hwdID->vendorIDs.begin();
-                            vendorID != hwdID->vendorIDs.end(); ++vendorID)
-                    {
-                        if (("*" == *vendorID) || (*vendorID == (*i_device)->vendorID_))
-                        {
-                            found = true;
-                            break;
-                        }
-                    }
+                	found = std::find_if(hwdID->vendorIDs.begin(), hwdID->vendorIDs.end(), [i_device](const std::string& vendorID){
+                	            	return (("*" == vendorID) || (vendorID == (*i_device)->vendorID_));
+                	            }) != hwdID->vendorIDs.end();
 
                     if (!found)
                     {
@@ -192,19 +167,9 @@ void Data::getAllDevicesOfConfig(const std::vector<std::shared_ptr<Device>>& dev
                     else
                     {
                         // Check blacklisted vendor ids
-                        found = false;
-
-                        for (auto&& blacklistedVendorID =
-                                hwdID->blacklistedVendorIDs.begin();
-                                blacklistedVendorID != hwdID->blacklistedVendorIDs.end();
-                                ++blacklistedVendorID)
-                        {
-                            if (*blacklistedVendorID == (*i_device)->vendorID_)
-                            {
-                                found = true;
-                                break;
-                            }
-                        }
+                        found = std::find_if(hwdID->blacklistedVendorIDs.begin(), hwdID->blacklistedVendorIDs.end(), [i_device](const std::string& blacklistedVendorID){
+                        				return (blacklistedVendorID == (*i_device)->vendorID_);
+                        			}) != hwdID->blacklistedVendorIDs.end();
 
                         if (found)
                         {
@@ -213,17 +178,9 @@ void Data::getAllDevicesOfConfig(const std::vector<std::shared_ptr<Device>>& dev
                         else
                         {
                             // Check device ids
-                            found = false;
-
-                            for (auto&& deviceID = hwdID->deviceIDs.begin();
-                                    deviceID != hwdID->deviceIDs.end(); ++deviceID)
-                            {
-                                if (("*" == *deviceID) || (*deviceID == (*i_device)->deviceID_))
-                                {
-                                    found = true;
-                                    break;
-                                }
-                            }
+                        	found = std::find_if(hwdID->deviceIDs.begin(), hwdID->deviceIDs.end(), [i_device](const std::string& deviceID){
+                        	        		return (("*" == deviceID) || (deviceID == (*i_device)->deviceID_));
+                        	            }) != hwdID->deviceIDs.end();
 
                             if (!found)
                             {
@@ -232,20 +189,9 @@ void Data::getAllDevicesOfConfig(const std::vector<std::shared_ptr<Device>>& dev
                             else
                             {
                                 // Check blacklisted device ids
-                                found = false;
-
-                                for (auto&& blacklistedDeviceID =
-                                        hwdID->blacklistedDeviceIDs.begin();
-                                        blacklistedDeviceID != hwdID->blacklistedDeviceIDs.end();
-                                        ++blacklistedDeviceID)
-                                {
-                                    if (*blacklistedDeviceID == (*i_device)->deviceID_)
-                                    {
-                                        found = true;
-                                        break;
-                                    }
-                                }
-
+                                found = std::find_if(hwdID->blacklistedDeviceIDs.begin(), hwdID->blacklistedDeviceIDs.end(), [i_device](const std::string& blacklistedDeviceID){
+                                				return (blacklistedDeviceID == (*i_device)->deviceID_);
+                                			}) != hwdID->blacklistedDeviceIDs.end();
                                 if (found)
                                 {
                                     continue;
diff --git a/src/Mhwd.cpp b/src/Mhwd.cpp
index bf32811..8a9dda8 100644
--- a/src/Mhwd.cpp
+++ b/src/Mhwd.cpp
@@ -225,15 +225,15 @@ std::shared_ptr<Config> Mhwd::getInstalledConfig(const std::string& configName,
         installedConfigs = &data_.installedPCIConfigs;
     }
 
-    for (auto&& installedConfig = installedConfigs->begin();
-            installedConfig != installedConfigs->end(); installedConfig++)
+    auto installedConfig = std::find_if(installedConfigs->begin(), installedConfigs->end(),
+            [configName](const std::shared_ptr<Config>& config) {
+                return configName == config->name_;
+            });
+
+    if (installedConfig != installedConfigs->end())
     {
-        if (configName == (*installedConfig)->name_)
-        {
-            return (*installedConfig);
-        }
+        return *installedConfig;
     }
-
     return nullptr;
 }
 
@@ -252,15 +252,14 @@ std::shared_ptr<Config> Mhwd::getDatabaseConfig(const std::string& configName,
         allConfigs = &data_.allPCIConfigs;
     }
 
-    for (auto&& iterator = allConfigs->begin();
-            iterator != allConfigs->end(); ++iterator)
+    auto config = std::find_if(allConfigs->begin(), allConfigs->end(),
+    		[configName](const std::shared_ptr<Config>& config) {
+                return config->name_ == configName;
+            });
+    if (config != allConfigs->end())
     {
-        if (configName == (*iterator)->name_)
-        {
-            return (*iterator);
-        }
+        return *config;
     }
-
     return nullptr;
 }
 
@@ -288,13 +287,14 @@ std::shared_ptr<Config> Mhwd::getAvailableConfig(const std::string& configName,
         }
         else
         {
-            for (auto&& availableConfig = (*device)->availableConfigs_.begin();
-                    availableConfig != (*device)->availableConfigs_.end(); availableConfig++)
+            auto& availableConfigs = (*device)->availableConfigs_;
+            auto availableConfig = std::find_if(availableConfigs.begin(), availableConfigs.end(),
+                    [configName](const std::shared_ptr<Config>& config){
+                        return config->name_ == configName;
+                    });
+            if (availableConfig != availableConfigs.end())
             {
-                if (configName == (*availableConfig)->name_)
-                {
-                    return (*availableConfig);
-                }
+                return *availableConfig;
             }
         }
     }
-- 
GitLab