]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/hof/example/in.cpp
1 /*=============================================================================
2 Copyright (c) 2017 Paul Fultz II
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 ==============================================================================*/
7 /*=============================================================================
8 Copyright (c) 2016 Paul Fultz II
10 Distributed under the Boost Software License, Version 1.0. (See accompanying
11 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
12 ==============================================================================*/
16 using namespace boost::hof
;
19 template<class R
, class T
>
20 auto member_find(const R
& r
, const T
& x
) BOOST_HOF_RETURNS(r
.find(x
));
23 // Function to find an iterator using a containers built-in find if available
24 BOOST_HOF_STATIC_LAMBDA_FUNCTION(find_iterator
) = first_of(
25 [](const std::string
& s
, const auto& x
)
27 auto index
= s
.find(x
);
28 if (index
== std::string::npos
) return s
.end();
29 else return s
.begin() + index
;
32 // On MSVC, trailing decltype doesn't work with generic lambdas, so a
33 // seperate function can be used instead.
34 BOOST_HOF_LIFT(member_find
),
36 [](const auto& r
, const auto& x
) BOOST_HOF_RETURNS(r
.find(x
)),
38 [](const auto& r
, const auto& x
)
42 return std::find(begin(r
), end(r
), x
);
45 // Implement an infix `in` operator to check if a range contains an element
46 BOOST_HOF_STATIC_LAMBDA_FUNCTION(in
) = infix(
47 [](const auto& x
, const auto& r
)
50 return find_iterator(r
, x
) != end(r
);
53 // Negate version of `in`
54 BOOST_HOF_STATIC_LAMBDA_FUNCTION(not_in
) = infix(compose(not _
, in
));
58 // Check if vector contains element
59 std::vector
<int> numbers
= { 1, 2, 3, 4, 5 };
60 if (5 <in
> numbers
) std::cout
<< "Yes" << std::endl
;
62 // Check if string contains element
63 std::string s
= "hello world";
64 if ("hello" <in
> s
) std::cout
<< "Yes" << std::endl
;
66 // Check if map contains element
67 std::map
<int, std::string
> number_map
= {
74 if (4 <in
> number_map
) std::cout
<< "Yes" << std::endl
;
76 // Check if map doesn't contains element
77 if (not (8 <in
> numbers
)) std::cout
<< "No" << std::endl
;
78 if (8 <not_in
> numbers
) std::cout
<< "No" << std::endl
;