| // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| // Derived from google3/util/gtl/stl_util.h | |
| #ifndef BASE_STL_UTIL_H_ | |
| #define BASE_STL_UTIL_H_ | |
| #include <algorithm> | |
| #include <iterator> | |
| #include <type_traits> | |
| namespace base { | |
| namespace internal { | |
| template <typename Collection> | |
| class HasKeyType { | |
| template <typename C> | |
| static std::true_type test(typename C::key_type*); | |
| template <typename C> | |
| static std::false_type test(...); | |
| public: | |
| static constexpr bool value = decltype(test<Collection>(nullptr))::value; | |
| }; | |
| } // namespace internal | |
| // Test to see if a collection like a vector contains a particular value. | |
| // Returns true if the value is in the collection. | |
| // Don't use this on collections such as sets or maps. This is enforced by | |
| // disabling this method if the collection defines a key_type. | |
| // TODO: Replace with std::ranges::contains once macOS CI uses Xcode 16+. | |
| template <typename Collection, | |
| typename Value, | |
| typename std::enable_if<!internal::HasKeyType<Collection>::value, | |
| int>::type = 0> | |
| bool ContainsValue(const Collection& collection, const Value& value) { | |
| return std::find(std::begin(collection), std::end(collection), value) != | |
| std::end(collection); | |
| } | |
| } // namespace base | |
| #endif // BASE_STL_UTIL_H_ |