xref: /openbmc/telemetry/src/utils/contains.hpp (revision 7e114c045e44dcf916db6399bd684283fa84785d)
1 #pragma once
2 
3 #include <algorithm>
4 
5 namespace utils
6 {
7 namespace detail
8 {
9 
10 template <class T>
11 concept HasMemberFind =
12     requires(T container) { container.find(container.begin()->first); };
13 
14 template <class T>
15 concept HasMemberContains =
16     requires(T container) { container.contains(*container.begin()); };
17 
18 } // namespace detail
19 
20 template <detail::HasMemberFind T>
contains(const T & container,const typename T::value_type::first_type & key)21 inline bool contains(const T& container,
22                      const typename T::value_type::first_type& key)
23 {
24     return container.find(key) != container.end();
25 }
26 
27 template <detail::HasMemberContains T>
contains(const T & container,const typename T::value_type & key)28 inline bool contains(const T& container, const typename T::value_type& key)
29 {
30     return container.contains(key);
31 }
32 
33 template <class T>
contains(const T & container,const typename T::value_type & key)34 inline bool contains(const T& container, const typename T::value_type& key)
35 {
36     return std::find(container.begin(), container.end(), key) !=
37            container.end();
38 }
39 
40 } // namespace utils
41