From 846095224044b39ddd3249b6ea072e486ba1fe38 Mon Sep 17 00:00:00 2001 From: Henry Wilson Date: Sun, 23 Sep 2018 22:15:35 +0100 Subject: [PATCH] Codechange: Replaced SmallVector::Find() const with suitable alternatives The use of std::none_of in network/core/host.cpp is driven by the non-const comparison operator use by NetworkAddress. A future commit should address the const_casts in that class to ensure const-correctness. --- src/core/smallvec_type.hpp | 20 +++----------------- src/network/core/host.cpp | 8 ++++---- 2 files changed, 7 insertions(+), 21 deletions(-) diff --git a/src/core/smallvec_type.hpp b/src/core/smallvec_type.hpp index 289cc9e1d6..1c713470f6 100644 --- a/src/core/smallvec_type.hpp +++ b/src/core/smallvec_type.hpp @@ -102,20 +102,6 @@ public: return this->Begin() + start; } - /** - * Search for the first occurrence of an item. - * The '!=' operator of T is used for comparison. - * @param item Item to search for - * @return The position of the item, or End() when not present - */ - inline const T *Find(const T &item) const - { - const T *pos = this->Begin(); - const T *end = this->End(); - while (pos != end && *pos != item) pos++; - return pos; - } - /** * Search for the first occurrence of an item. * The '!=' operator of T is used for comparison. @@ -124,8 +110,8 @@ public: */ inline int FindIndex(const T &item) const { - auto const it = this->Find(item); - return it == this->End() ? -1 : it - this->Begin(); + auto const it = std::find(std::vector::begin(), std::vector::end(), item); + return it == std::vector::end() ? -1 : it - std::vector::begin(); } /** @@ -136,7 +122,7 @@ public: */ inline bool Contains(const T &item) const { - return this->Find(item) != this->End(); + return std::find(std::vector::begin(), std::vector::end(), item) != std::vector::end(); } /** diff --git a/src/network/core/host.cpp b/src/network/core/host.cpp index 05ad84153b..c0365742b0 100644 --- a/src/network/core/host.cpp +++ b/src/network/core/host.cpp @@ -76,7 +76,7 @@ static void NetworkFindBroadcastIPsInternal(NetworkAddressList *broadcast) // BE memset(&address, 0, sizeof(address)); ((sockaddr_in*)&address)->sin_addr.s_addr = htonl(ip | ~netmask); NetworkAddress addr(address, sizeof(sockaddr)); - if (!broadcast->Contains(addr)) *broadcast->Append() = addr; + if (std::none_of(broadcast->begin(), broadcast->end(), [&addr](NetworkAddress const& elem) -> bool { return elem == addr; })) *broadcast->Append() = addr; } if (read < 0) { break; @@ -100,7 +100,7 @@ static void NetworkFindBroadcastIPsInternal(NetworkAddressList *broadcast) // GE if (ifa->ifa_broadaddr->sa_family != AF_INET) continue; NetworkAddress addr(ifa->ifa_broadaddr, sizeof(sockaddr)); - if (!broadcast->Contains(addr)) *broadcast->Append() = addr; + if (std::none_of(broadcast->begin(), broadcast->end(), [&addr](NetworkAddress const& elem) -> bool { return elem == addr; })) *broadcast->Append() = addr; } freeifaddrs(ifap); } @@ -136,7 +136,7 @@ static void NetworkFindBroadcastIPsInternal(NetworkAddressList *broadcast) // Wi memcpy(&address, &ifo[j].iiAddress.Address, sizeof(sockaddr)); ((sockaddr_in*)&address)->sin_addr.s_addr = ifo[j].iiAddress.AddressIn.sin_addr.s_addr | ~ifo[j].iiNetmask.AddressIn.sin_addr.s_addr; NetworkAddress addr(address, sizeof(sockaddr)); - if (!broadcast->Contains(addr)) *broadcast->Append() = addr; + if (std::none_of(broadcast->begin(), broadcast->end(), [&addr](NetworkAddress const& elem) -> bool { return elem == addr; })) *broadcast->Append() = addr; } free(ifo); @@ -174,7 +174,7 @@ static void NetworkFindBroadcastIPsInternal(NetworkAddressList *broadcast) // !G (r.ifr_flags & IFF_BROADCAST) && ioctl(sock, SIOCGIFBRDADDR, &r) != -1) { NetworkAddress addr(&r.ifr_broadaddr, sizeof(sockaddr)); - if (!broadcast->Contains(addr)) *broadcast->Append() = addr; + if (std::none_of(broadcast->begin(), broadcast->end(), [&addr](NetworkAddress const& elem) -> bool { return elem == addr; })) *broadcast->Append() = addr; } }