• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
3 //
4 // Distributed under the Boost Software License, Version 1.0
5 // See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt
7 //
8 // See http://boostorg.github.com/compute for more information.
9 //---------------------------------------------------------------------------//
10 
11 #ifndef BOOST_COMPUTE_FUNCTIONAL_LOGICAL_HPP
12 #define BOOST_COMPUTE_FUNCTIONAL_LOGICAL_HPP
13 
14 namespace boost {
15 namespace compute {
16 namespace detail {
17 
18 template<class Predicate, class Expr>
19 class invoked_unary_negate_function
20 {
21 public:
22     typedef int result_type;
23 
invoked_unary_negate_function(const Predicate & pred,const Expr & expr)24     invoked_unary_negate_function(const Predicate &pred,
25                                   const Expr &expr)
26         : m_pred(pred),
27           m_expr(expr)
28     {
29     }
30 
pred() const31     Predicate pred() const
32     {
33         return m_pred;
34     }
35 
expr() const36     Expr expr() const
37     {
38         return m_expr;
39     }
40 
41 private:
42     Predicate m_pred;
43     Expr m_expr;
44 };
45 
46 template<class Predicate, class Expr1, class Expr2>
47 class invoked_binary_negate_function
48 {
49 public:
50     typedef int result_type;
51 
invoked_binary_negate_function(const Predicate & pred,const Expr1 & expr1,const Expr2 & expr2)52     invoked_binary_negate_function(const Predicate &pred,
53                                    const Expr1 &expr1,
54                                    const Expr2 &expr2)
55         : m_pred(pred),
56           m_expr1(expr1),
57           m_expr2(expr2)
58     {
59     }
60 
pred() const61     Predicate pred() const
62     {
63         return m_pred;
64     }
65 
expr1() const66     Expr1 expr1() const
67     {
68         return m_expr1;
69     }
70 
expr2() const71     Expr2 expr2() const
72     {
73         return m_expr2;
74     }
75 
76 private:
77     Predicate m_pred;
78     Expr1 m_expr1;
79     Expr2 m_expr2;
80 };
81 
82 } // end detail namespace
83 
84 /// \internal_
85 template<class Arg, class Result>
86 struct unary_function
87 {
88     typedef Arg argument_type;
89     typedef Result result_type;
90 };
91 
92 /// \internal_
93 template<class Arg1, class Arg2, class Result>
94 struct binary_function
95 {
96     typedef Arg1 first_argument_type;
97     typedef Arg2 second_argument_type;
98     typedef Result result_type;
99 };
100 
101 /// \internal_
102 template<class Arg1, class Arg2, class Arg3, class Result>
103 struct ternary_function
104 {
105     typedef Arg1 first_argument_type;
106     typedef Arg2 second_argument_type;
107     typedef Arg3 third_argument_type;
108     typedef Result result_type;
109 };
110 
111 /// The unary_negate function adaptor negates a unary function.
112 ///
113 /// \see not1()
114 template<class Predicate>
115 class unary_negate : public unary_function<void, int>
116 {
117 public:
unary_negate(Predicate pred)118     explicit unary_negate(Predicate pred)
119         : m_pred(pred)
120     {
121     }
122 
123     /// \internal_
124     template<class Arg>
125     detail::invoked_unary_negate_function<Predicate, Arg>
operator ()(const Arg & arg) const126     operator()(const Arg &arg) const
127     {
128         return detail::invoked_unary_negate_function<
129                    Predicate,
130                    Arg
131                 >(m_pred, arg);
132     }
133 
134 private:
135     Predicate m_pred;
136 };
137 
138 /// The binnary_negate function adaptor negates a binary function.
139 ///
140 /// \see not2()
141 template<class Predicate>
142 class binary_negate : public binary_function<void, void, int>
143 {
144 public:
binary_negate(Predicate pred)145     explicit binary_negate(Predicate pred)
146         : m_pred(pred)
147     {
148     }
149 
150     /// \internal_
151     template<class Arg1, class Arg2>
152     detail::invoked_binary_negate_function<Predicate, Arg1, Arg2>
operator ()(const Arg1 & arg1,const Arg2 & arg2) const153     operator()(const Arg1 &arg1, const Arg2 &arg2) const
154     {
155         return detail::invoked_binary_negate_function<
156                    Predicate,
157                    Arg1,
158                    Arg2
159                 >(m_pred, arg1, arg2);
160     }
161 
162 private:
163     Predicate m_pred;
164 };
165 
166 /// Returns a unary_negate adaptor around \p predicate.
167 ///
168 /// \param predicate the unary function to wrap
169 ///
170 /// \return a unary_negate wrapper around \p predicate
171 template<class Predicate>
not1(const Predicate & predicate)172 inline unary_negate<Predicate> not1(const Predicate &predicate)
173 {
174     return unary_negate<Predicate>(predicate);
175 }
176 
177 /// Returns a binary_negate adaptor around \p predicate.
178 ///
179 /// \param predicate the binary function to wrap
180 ///
181 /// \return a binary_negate wrapper around \p predicate
182 template<class Predicate>
not2(const Predicate & predicate)183 inline binary_negate<Predicate> not2(const Predicate &predicate)
184 {
185     return binary_negate<Predicate>(predicate);
186 }
187 
188 /// The logical_not function negates its argument and returns it.
189 ///
190 /// \see not1(), not2()
191 template<class T>
192 struct logical_not : public unary_function<T, int>
193 {
194     /// \internal_
195     template<class Expr>
196     detail::invoked_function<int, boost::tuple<Expr> >
operator ()boost::compute::logical_not197     operator()(const Expr &expr) const
198     {
199         return detail::invoked_function<int, boost::tuple<Expr> >(
200             "!", std::string(), boost::make_tuple(expr)
201         );
202     }
203 };
204 
205 } // end compute namespace
206 } // end boost namespace
207 
208 #endif // BOOST_COMPUTE_FUNCTIONAL_LOGICAL_HPP
209