]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/hof/example/in.cpp
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / boost / libs / hof / example / in.cpp
1 /*=============================================================================
2 Copyright (c) 2017 Paul Fultz II
3 in.cpp
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
9 in.cpp
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 ==============================================================================*/
13
14 #include "example.h"
15
16 using namespace boost::hof;
17
18 #ifdef _MSC_VER
19 template<class R, class T>
20 auto member_find(const R& r, const T& x) BOOST_HOF_RETURNS(r.find(x));
21 #endif
22
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)
26 {
27 auto index = s.find(x);
28 if (index == std::string::npos) return s.end();
29 else return s.begin() + index;
30 },
31 #ifdef _MSC_VER
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),
35 #else
36 [](const auto& r, const auto& x) BOOST_HOF_RETURNS(r.find(x)),
37 #endif
38 [](const auto& r, const auto& x)
39 {
40 using std::begin;
41 using std::end;
42 return std::find(begin(r), end(r), x);
43 }
44 );
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)
48 {
49 using std::end;
50 return find_iterator(r, x) != end(r);
51 }
52 );
53 // Negate version of `in`
54 BOOST_HOF_STATIC_LAMBDA_FUNCTION(not_in) = infix(compose(not _, in));
55
56 int main()
57 {
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;
61
62 // Check if string contains element
63 std::string s = "hello world";
64 if ("hello" <in> s) std::cout << "Yes" << std::endl;
65
66 // Check if map contains element
67 std::map<int, std::string> number_map = {
68 { 1, "1" },
69 { 2, "2" },
70 { 3, "3" },
71 { 4, "4" }
72 };
73
74 if (4 <in> number_map) std::cout << "Yes" << std::endl;
75
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;
79
80 }
81