1 | ////////////////////////////////////////////////////////////////////////////// |
2 | // |
3 | // (C) Copyright Ion Gaztanaga 2014-2014. |
4 | // |
5 | // Distributed under the Boost Software License, Version 1.0. |
6 | // (See accompanying file LICENSE_1_0.txt or copy at |
7 | // http://www.boost.org/LICENSE_1_0.txt) |
8 | // |
9 | // See http://www.boost.org/libs/container for documentation. |
10 | // |
11 | ////////////////////////////////////////////////////////////////////////////// |
12 | |
13 | #ifndef BOOST_CONTAINER_DETAIL_ALGORITHM_HPP |
14 | #define BOOST_CONTAINER_DETAIL_ALGORITHM_HPP |
15 | |
16 | #ifndef BOOST_CONFIG_HPP |
17 | # include <boost/config.hpp> |
18 | #endif |
19 | |
20 | #if defined(BOOST_HAS_PRAGMA_ONCE) |
21 | # pragma once |
22 | #endif |
23 | |
24 | #include <boost/intrusive/detail/algorithm.hpp> |
25 | |
26 | namespace boost { |
27 | namespace container { |
28 | |
29 | using boost::intrusive::algo_equal; |
30 | using boost::intrusive::algo_lexicographical_compare; |
31 | |
32 | template<class Func> |
33 | class binder1st |
34 | { |
35 | public: |
36 | typedef typename Func::second_argument_type argument_type; |
37 | typedef typename Func::result_type result_type; |
38 | |
39 | binder1st(const Func& func, const typename Func::first_argument_type& arg) |
40 | : op(func), value(arg) |
41 | {} |
42 | |
43 | result_type operator()(const argument_type& arg) const |
44 | { return op(value, arg); } |
45 | |
46 | result_type operator()(argument_type& arg) const |
47 | { return op(value, arg); } |
48 | |
49 | private: |
50 | Func op; |
51 | typename Func::first_argument_type value; |
52 | }; |
53 | |
54 | template<class Func, class T> |
55 | inline binder1st<Func> bind1st(const Func& func, const T& arg) |
56 | { return boost::container::binder1st<Func>(func, arg); } |
57 | |
58 | template<class Func> |
59 | class binder2nd |
60 | { |
61 | public: |
62 | typedef typename Func::first_argument_type argument_type; |
63 | typedef typename Func::result_type result_type; |
64 | |
65 | binder2nd(const Func& func, const typename Func::second_argument_type& arg) |
66 | : op(func), value(arg) |
67 | {} |
68 | |
69 | result_type operator()(const argument_type& arg) const |
70 | { return op(arg, value); } |
71 | |
72 | result_type operator()(argument_type& arg) const |
73 | { return op(arg, value); } |
74 | |
75 | private: |
76 | Func op; |
77 | typename Func::second_argument_type value; |
78 | }; |
79 | |
80 | template<class Func, class T> |
81 | inline binder2nd<Func> bind2nd(const Func& func, const T& arg) |
82 | { |
83 | return (boost::container::binder2nd<Func>(func, arg)); |
84 | } |
85 | |
86 | template<class Func> |
87 | class unary_negate |
88 | { |
89 | public: |
90 | typedef typename Func::argument_type argument_type; |
91 | typedef typename Func::result_type result_type; |
92 | |
93 | explicit unary_negate(const Func& func) |
94 | : m_func(func) |
95 | {} |
96 | |
97 | bool operator()(const typename Func::argument_type& arg) const |
98 | { return !m_func(arg); } |
99 | |
100 | private: |
101 | Func m_func; |
102 | }; |
103 | |
104 | template<class Func> inline |
105 | unary_negate<Func> not1(const Func& func) |
106 | { |
107 | return boost::container::unary_negate<Func>(func); |
108 | } |
109 | |
110 | template<class InputIt, class UnaryPredicate> |
111 | InputIt find_if(InputIt first, InputIt last, UnaryPredicate p) |
112 | { |
113 | for (; first != last; ++first) { |
114 | if (p(*first)) { |
115 | return first; |
116 | } |
117 | } |
118 | return last; |
119 | } |
120 | |
121 | template<class InputIt, class ForwardIt, class BinaryPredicate> |
122 | InputIt find_first_of(InputIt first1, InputIt last1, ForwardIt first2, ForwardIt last2, BinaryPredicate p) |
123 | { |
124 | for (; first1 != last1; ++first1) { |
125 | for (ForwardIt it = first2; it != last2; ++it) { |
126 | if (p(*first1, *it)) { |
127 | return first1; |
128 | } |
129 | } |
130 | } |
131 | return last1; |
132 | } |
133 | |
134 | template<class ForwardIt1, class ForwardIt2, class BinaryPredicate> |
135 | ForwardIt1 search(ForwardIt1 first1, ForwardIt1 last1, |
136 | ForwardIt2 first2, ForwardIt2 last2, BinaryPredicate p) |
137 | { |
138 | for (; ; ++first1) { |
139 | ForwardIt1 it = first1; |
140 | for (ForwardIt2 it2 = first2; ; ++it, ++it2) { |
141 | if (it2 == last2) { |
142 | return first1; |
143 | } |
144 | if (it == last1) { |
145 | return last1; |
146 | } |
147 | if (!p(*it, *it2)) { |
148 | break; |
149 | } |
150 | } |
151 | } |
152 | } |
153 | |
154 | } //namespace container { |
155 | } //namespace boost { |
156 | |
157 | #endif //#ifndef BOOST_CONTAINER_DETAIL_ALGORITHM_HPP |
158 | |