logical.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013 Kyle Lutz <[email protected]>
  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. #ifndef BOOST_COMPUTE_FUNCTIONAL_LOGICAL_HPP
  11. #define BOOST_COMPUTE_FUNCTIONAL_LOGICAL_HPP
  12. namespace boost {
  13. namespace compute {
  14. namespace detail {
  15. template<class Predicate, class Expr>
  16. class invoked_unary_negate_function
  17. {
  18. public:
  19. typedef int result_type;
  20. invoked_unary_negate_function(const Predicate &pred,
  21. const Expr &expr)
  22. : m_pred(pred),
  23. m_expr(expr)
  24. {
  25. }
  26. Predicate pred() const
  27. {
  28. return m_pred;
  29. }
  30. Expr expr() const
  31. {
  32. return m_expr;
  33. }
  34. private:
  35. Predicate m_pred;
  36. Expr m_expr;
  37. };
  38. template<class Predicate, class Expr1, class Expr2>
  39. class invoked_binary_negate_function
  40. {
  41. public:
  42. typedef int result_type;
  43. invoked_binary_negate_function(const Predicate &pred,
  44. const Expr1 &expr1,
  45. const Expr2 &expr2)
  46. : m_pred(pred),
  47. m_expr1(expr1),
  48. m_expr2(expr2)
  49. {
  50. }
  51. Predicate pred() const
  52. {
  53. return m_pred;
  54. }
  55. Expr1 expr1() const
  56. {
  57. return m_expr1;
  58. }
  59. Expr2 expr2() const
  60. {
  61. return m_expr2;
  62. }
  63. private:
  64. Predicate m_pred;
  65. Expr1 m_expr1;
  66. Expr2 m_expr2;
  67. };
  68. } // end detail namespace
  69. /// \internal_
  70. template<class Arg, class Result>
  71. struct unary_function
  72. {
  73. typedef Arg argument_type;
  74. typedef Result result_type;
  75. };
  76. /// \internal_
  77. template<class Arg1, class Arg2, class Result>
  78. struct binary_function
  79. {
  80. typedef Arg1 first_argument_type;
  81. typedef Arg2 second_argument_type;
  82. typedef Result result_type;
  83. };
  84. /// \internal_
  85. template<class Arg1, class Arg2, class Arg3, class Result>
  86. struct ternary_function
  87. {
  88. typedef Arg1 first_argument_type;
  89. typedef Arg2 second_argument_type;
  90. typedef Arg3 third_argument_type;
  91. typedef Result result_type;
  92. };
  93. /// The unary_negate function adaptor negates a unary function.
  94. ///
  95. /// \see not1()
  96. template<class Predicate>
  97. class unary_negate : public unary_function<void, int>
  98. {
  99. public:
  100. explicit unary_negate(Predicate pred)
  101. : m_pred(pred)
  102. {
  103. }
  104. /// \internal_
  105. template<class Arg>
  106. detail::invoked_unary_negate_function<Predicate, Arg>
  107. operator()(const Arg &arg) const
  108. {
  109. return detail::invoked_unary_negate_function<
  110. Predicate,
  111. Arg
  112. >(m_pred, arg);
  113. }
  114. private:
  115. Predicate m_pred;
  116. };
  117. /// The binnary_negate function adaptor negates a binary function.
  118. ///
  119. /// \see not2()
  120. template<class Predicate>
  121. class binary_negate : public binary_function<void, void, int>
  122. {
  123. public:
  124. explicit binary_negate(Predicate pred)
  125. : m_pred(pred)
  126. {
  127. }
  128. /// \internal_
  129. template<class Arg1, class Arg2>
  130. detail::invoked_binary_negate_function<Predicate, Arg1, Arg2>
  131. operator()(const Arg1 &arg1, const Arg2 &arg2) const
  132. {
  133. return detail::invoked_binary_negate_function<
  134. Predicate,
  135. Arg1,
  136. Arg2
  137. >(m_pred, arg1, arg2);
  138. }
  139. private:
  140. Predicate m_pred;
  141. };
  142. /// Returns a unary_negate adaptor around \p predicate.
  143. ///
  144. /// \param predicate the unary function to wrap
  145. ///
  146. /// \return a unary_negate wrapper around \p predicate
  147. template<class Predicate>
  148. inline unary_negate<Predicate> not1(const Predicate &predicate)
  149. {
  150. return unary_negate<Predicate>(predicate);
  151. }
  152. /// Returns a binary_negate adaptor around \p predicate.
  153. ///
  154. /// \param predicate the binary function to wrap
  155. ///
  156. /// \return a binary_negate wrapper around \p predicate
  157. template<class Predicate>
  158. inline binary_negate<Predicate> not2(const Predicate &predicate)
  159. {
  160. return binary_negate<Predicate>(predicate);
  161. }
  162. /// The logical_not function negates its argument and returns it.
  163. ///
  164. /// \see not1(), not2()
  165. template<class T>
  166. struct logical_not : public unary_function<T, int>
  167. {
  168. /// \internal_
  169. template<class Expr>
  170. detail::invoked_function<int, boost::tuple<Expr> >
  171. operator()(const Expr &expr) const
  172. {
  173. return detail::invoked_function<int, boost::tuple<Expr> >(
  174. "!", std::string(), boost::make_tuple(expr)
  175. );
  176. }
  177. };
  178. } // end compute namespace
  179. } // end boost namespace
  180. #endif // BOOST_COMPUTE_FUNCTIONAL_LOGICAL_HPP