Android-cuttlefish cvd tool
contains.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <algorithm>
20#include <iterator>
21#include <string>
22#include <type_traits>
23
24#include "absl/strings/string_view.h"
25
37namespace cuttlefish {
38namespace contains_internal_impl {
39
40/*
41 * If Container does not have find key, will be a compiler error used
42 * by SFINAE. If it does have one, this is equivalent to the "void" type.
43 */
44template <typename Container, typename Key>
46 decltype(void(std::declval<Container&>().find(std::declval<Key&>())));
47
48/*
49 * Here is how this works:
50 *
51 * Given that
52 * HasFindImpl<Container, T> is used in the code
53 *
54 * 1. The input is effectively regarded as HasFindImpl<Container, T, void>.
55 * The specialized version below isn't looked up yet; whether the specialized
56 * version below is used or not, the compiler front-end needs all three
57 * template parameters to match against either special or generic version.
58 * When obtaining "all three," the front-end only looks up the base template
59 * definition. The default type of the third template parameter is void, so
60 * the given type is expanded/deduced to HasFindImpl<Container, T, void>.
61 *
62 * 2. Now, given HasFindImpl<Container, T, void>, the compiler front-end
63 * tries matching against the specialized and generic/original versions. If
64 * the input could matches both a generic and a specialized one, the compiler
65 * chooses the specialized one. Thus, particularly, HasFindImpl
66 * implementation's third parameter in the specialized version must be the
67 * same as the default type of the third template parameter to the original/
68 * generic version, which is "void."
69 */
70template <typename Container, typename T, typename = void>
71struct HasFindImpl : std::false_type {};
72
73template <typename Container, typename T>
74struct HasFindImpl<Container, T, VoidTypeIfHasFind<Container, T>>
75 : std::true_type {};
76
77template <typename T>
79 typename std::remove_cv_t<typename std::remove_reference_t<T>>;
80
81template <typename T, typename U>
82using IsSame = typename std::is_same<RemoveCvref<T>, RemoveCvref<U>>;
83
84template <typename T>
85struct IsString : IsSame<std::string, T> {};
86
87template <typename T>
88struct IsStringView : IsSame<absl::string_view, T> {};
89
90} // namespace contains_internal_impl
91
92// TODO(kwstephenkim): Replace these when C++20 starts to be used.
93template <typename Container, typename U,
94 typename = std::enable_if_t<
98 void>>
99constexpr bool Contains(Container&& container, U&& u) {
100 // using O(1) or O(lgN) find()
101 return container.find(std::forward<U>(u)) != container.end();
102}
103
104template <
105 typename Container, typename U,
106 std::enable_if_t<!contains_internal_impl::HasFindImpl<Container, U>::value,
107 int> = 0>
108constexpr bool Contains(Container&& container, U&& u) {
109 // falls back to a generic, likely linear search
110 const auto itr =
111 std::find(std::begin(container), std::end(container), std::forward<U>(u));
112 return itr != std::end(container);
113}
114
115// std::string:: or absl::string_view::find() returns index, not iterator
116template <typename T>
117constexpr bool Contains(const std::string& s, T&& t) {
118 return s.find(std::forward<T>(t)) != std::string::npos;
119}
120
121template <typename T>
122constexpr bool Contains(const absl::string_view& s, T&& t) {
123 return s.find(std::forward<T>(t)) != absl::string_view::npos;
124}
125
126} // namespace cuttlefish
typename std::remove_cv_t< typename std::remove_reference_t< T > > RemoveCvref
Definition: contains.h:79
decltype(void(std::declval< Container & >().find(std::declval< Key & >()))) VoidTypeIfHasFind
Definition: contains.h:46
typename std::is_same< RemoveCvref< T >, RemoveCvref< U > > IsSame
Definition: contains.h:82
Definition: alloc_driver.h:21
constexpr bool Contains(Container &&container, U &&u)
Definition: contains.h:99