/* * Copyright 2008-2012 NVIDIA Corporation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /*! \file functional.h * \brief Function objects and tools for manipulating them */ #pragma once #include #include #include namespace thrust { /*! \addtogroup function_objects Function Objects */ template struct unary_traits; template struct binary_traits; /*! \addtogroup function_object_adaptors Function Object Adaptors * \ingroup function_objects * \{ */ /*! \p unary_function is an empty base class: it contains no member functions * or member variables, but only type information. The only reason it exists * is to make it more convenient to define types that are models of the * concept Adaptable Unary Function. Specifically, any model of Adaptable * Unary Function must define nested \c typedefs. Those \c typedefs are * provided by the base class \p unary_function. * * The following code snippet demonstrates how to construct an * Adaptable Unary Function using \p unary_function. * * \code * struct sine : public thrust::unary_function * { * __host__ __device__ * float operator()(float x) { return sinf(x); } * }; * \endcode * * \note unary_function is currently redundant with the C++ STL type * \c std::unary_function. We reserve it here for potential additional * functionality at a later date. * * \see http://www.sgi.com/tech/stl/unary_function.html * \see binary_function */ template struct unary_function : public std::unary_function { }; // end unary_function /*! \p binary_function is an empty base class: it contains no member functions * or member variables, but only type information. The only reason it exists * is to make it more convenient to define types that are models of the * concept Adaptable Binary Function. Specifically, any model of Adaptable * Binary Function must define nested \c typedefs. Those \c typedefs are * provided by the base class \p binary_function. * * The following code snippet demonstrates how to construct an * Adaptable Binary Function using \p binary_function. * * \code * struct exponentiate : public thrust::binary_function * { * __host__ __device__ * float operator()(float x, float y) { return powf(x,y); } * }; * \endcode * * \note binary_function is currently redundant with the C++ STL type * \c std::binary_function. We reserve it here for potential additional * functionality at a later date. * * \see http://www.sgi.com/tech/stl/binary_function.html * \see unary_function */ template struct binary_function : public std::binary_function { }; // end binary_function /*! \} */ /*! \addtogroup predefined_function_objects Predefined Function Objects * \ingroup function_objects */ /*! \addtogroup arithmetic_operations Arithmetic Operations * \ingroup predefined_function_objects * \{ */ /*! \p plus is a function object. Specifically, it is an Adaptable Binary Function. * If \c f is an object of class plus, and \c x and \c y are objects * of class \c T, then f(x,y) returns x+y. * * \tparam T is a model of Assignable, * and if \c x and \c y are objects of type \p T, then x+y must be defined and must have a return type that is convertible to \c T. * * The following code snippet demonstrates how to use plus to sum two * device_vectors of \c floats. * * \code * #include * #include * #include * #include * #include * ... * const int N = 1000; * thrust::device_vector V1(N); * thrust::device_vector V2(N); * thrust::device_vector V3(N); * * thrust::sequence(V1.begin(), V1.end(), 1); * thrust::fill(V2.begin(), V2.end(), 75); * * thrust::transform(V1.begin(), V1.end(), V2.begin(), V3.begin(), * thrust::plus()); * // V3 is now {76, 77, 78, ..., 1075} * \endcode * * \see http://www.sgi.com/tech/stl/plus.html * \see binary_function */ template struct plus : public binary_function { /*! Function call operator. The return value is lhs + rhs. */ __host__ __device__ T operator()(const T &lhs, const T &rhs) const {return lhs + rhs;} }; // end plus /*! \p minus is a function object. Specifically, it is an Adaptable Binary Function. * If \c f is an object of class minus, and \c x and \c y are objects * of class \c T, then f(x,y) returns x-y. * * \tparam T is a model of Assignable, * and if \c x and \c y are objects of type \p T, then x-y must be defined and must have a return type that is convertible to \c T. * * The following code snippet demonstrates how to use minus to subtract * a device_vector of \c floats from another. * * \code * #include * #include * #include * #include * #include * ... * const int N = 1000; * thrust::device_vector V1(N); * thrust::device_vector V2(N); * thrust::device_vector V3(N); * * thrust::sequence(V1.begin(), V1.end(), 1); * thrust::fill(V2.begin(), V2.end(), 75); * * thrust::transform(V1.begin(), V1.end(), V2.begin(), V3.begin(), * thrust::minus()); * // V3 is now {-74, -75, -76, ..., -925} * \endcode * * \see http://www.sgi.com/tech/stl/minus.html * \see binary_function */ template struct minus : public binary_function { /*! Function call operator. The return value is lhs - rhs. */ __host__ __device__ T operator()(const T &lhs, const T &rhs) const {return lhs - rhs;} }; // end minus /*! \p multiplies is a function object. Specifically, it is an Adaptable Binary Function. * If \c f is an object of class minus, and \c x and \c y are objects * of class \c T, then f(x,y) returns x*y. * * \tparam T is a model of Assignable, * and if \c x and \c y are objects of type \p T, then x*y must be defined and must have a return type that is convertible to \c T. * * The following code snippet demonstrates how to use multiplies to multiply * two device_vectors of \c floats. * * \code * #include * #include * #include * #include * #include * ... * const int N = 1000; * thrust::device_vector V1(N); * thrust::device_vector V2(N); * thrust::device_vector V3(N); * * thrust::sequence(V1.begin(), V1.end(), 1); * thrust::fill(V2.begin(), V2.end(), 75); * * thrust::transform(V1.begin(), V1.end(), V2.begin(), V3.begin(), * thrust::multiplies()); * // V3 is now {75, 150, 225, ..., 75000} * \endcode * * \see http://www.sgi.com/tech/stl/multiplies.html * \see binary_function */ template struct multiplies : public binary_function { /*! Function call operator. The return value is lhs * rhs. */ __host__ __device__ T operator()(const T &lhs, const T &rhs) const {return lhs * rhs;} }; // end multiplies /*! \p divides is a function object. Specifically, it is an Adaptable Binary Function. * If \c f is an object of class divides, and \c x and \c y are objects * of class \c T, then f(x,y) returns x/y. * * \tparam T is a model of Assignable, * and if \c x and \c y are objects of type \p T, then x/y must be defined and must have a return type that is convertible to \c T. * * The following code snippet demonstrates how to use divides to divide * one device_vectors of \c floats by another. * * \code * #include * #include * #include * #include * #include * ... * const int N = 1000; * thrust::device_vector V1(N); * thrust::device_vector V2(N); * thrust::device_vector V3(N); * * thrust::sequence(V1.begin(), V1.end(), 1); * thrust::fill(V2.begin(), V2.end(), 75); * * thrust::transform(V1.begin(), V1.end(), V2.begin(), V3.begin(), * thrust::divides()); * // V3 is now {1/75, 2/75, 3/75, ..., 1000/75} * \endcode * * \see http://www.sgi.com/tech/stl/divides.html * \see binary_function */ template struct divides : public binary_function { /*! Function call operator. The return value is lhs / rhs. */ __host__ __device__ T operator()(const T &lhs, const T &rhs) const {return lhs / rhs;} }; // end divides /*! \p modulus is a function object. Specifically, it is an Adaptable Binary Function. * If \c f is an object of class divides, and \c x and \c y are objects * of class \c T, then f(x,y) returns x%y. * * \tparam T is a model of Assignable, * and if \c x and \c y are objects of type \p T, then x%y must be defined and must have a return type that is convertible to \c T. * * The following code snippet demonstrates how to use modulus to take * the modulus of one device_vectors of \c floats by another. * * \code * #include * #include * #include * #include * #include * ... * const int N = 1000; * thrust::device_vector V1(N); * thrust::device_vector V2(N); * thrust::device_vector V3(N); * * thrust::sequence(V1.begin(), V1.end(), 1); * thrust::fill(V2.begin(), V2.end(), 75); * * thrust::transform(V1.begin(), V1.end(), V2.begin(), V3.begin(), * thrust::modulus()); * // V3 is now {1%75, 2%75, 3%75, ..., 1000%75} * \endcode * * \see http://www.sgi.com/tech/stl/modulus.html * \see binary_function */ template struct modulus : public binary_function { /*! Function call operator. The return value is lhs % rhs. */ __host__ __device__ T operator()(const T &lhs, const T &rhs) const {return lhs % rhs;} }; // end modulus /*! \p negate is a function object. Specifically, it is an Adaptable Unary Function. * If \c f is an object of class negate, and \c x is an object * of class \c T, then f(x) returns -x. * * \tparam T is a model of Assignable, * and if \c x is an object of type \p T, then -x must be defined and must have a return type that is convertible to \c T. * * The following code snippet demonstrates how to use negate to negate * the element of a device_vector of \c floats. * * \code * #include * #include * #include * #include * ... * const int N = 1000; * thrust::device_vector V1(N); * thrust::device_vector V2(N); * * thrust::sequence(V1.begin(), V1.end(), 1); * * thrust::transform(V1.begin(), V1.end(), V2.begin(), * thrust::negate()); * // V2 is now {-1, -2, -3, ..., -1000} * \endcode * * \see http://www.sgi.com/tech/stl/negate.html * \see unary_function */ template struct negate : public unary_function { /*! Function call operator. The return value is -x. */ __host__ __device__ T operator()(const T &x) const {return -x;} }; // end negate /*! \} */ /*! \addtogroup comparison_operations Comparison Operations * \ingroup predefined_function_objects * \{ */ /*! \p equal_to is a function object. Specifically, it is an Adaptable Binary * Predicate, which means it is a function object that tests the truth or falsehood * of some condition. If \c f is an object of class equal_to and \c x * and \c y are objects of class \c T, then f(x,y) returns \c true if * x == y and \c false otherwise. * * \tparam T is a model of Equality Comparable. * * \see http://www.sgi.com/tech/stl/equal_to.html * \see binary_function */ template struct equal_to : public binary_function { /*! Function call operator. The return value is lhs == rhs. */ __host__ __device__ bool operator()(const T &lhs, const T &rhs) const {return lhs == rhs;} }; // end equal_to /*! \p not_equal_to is a function object. Specifically, it is an Adaptable Binary * Predicate, which means it is a function object that tests the truth or falsehood * of some condition. If \c f is an object of class not_equal_to and \c x * and \c y are objects of class \c T, then f(x,y) returns \c true if * x != y and \c false otherwise. * * \tparam T is a model of Equality Comparable. * * \see http://www.sgi.com/tech/stl/not_equal_to.html * \see binary_function */ template struct not_equal_to : public binary_function { /*! Function call operator. The return value is lhs != rhs. */ __host__ __device__ bool operator()(const T &lhs, const T &rhs) const {return lhs != rhs;} }; // end not_equal_to /*! \p greater is a function object. Specifically, it is an Adaptable Binary * Predicate, which means it is a function object that tests the truth or falsehood * of some condition. If \c f is an object of class greater and \c x * and \c y are objects of class \c T, then f(x,y) returns \c true if * x > y and \c false otherwise. * * \tparam T is a model of LessThan Comparable. * * \see http://www.sgi.com/tech/stl/greater.html * \see binary_function */ template struct greater : public binary_function { /*! Function call operator. The return value is lhs > rhs. */ __host__ __device__ bool operator()(const T &lhs, const T &rhs) const {return lhs > rhs;} }; // end greater /*! \p less is a function object. Specifically, it is an Adaptable Binary * Predicate, which means it is a function object that tests the truth or falsehood * of some condition. If \c f is an object of class less and \c x * and \c y are objects of class \c T, then f(x,y) returns \c true if * x < y and \c false otherwise. * * \tparam T is a model of LessThan Comparable. * * \see http://www.sgi.com/tech/stl/less.html * \see binary_function */ template struct less : public binary_function { /*! Function call operator. The return value is lhs < rhs. */ __host__ __device__ bool operator()(const T &lhs, const T &rhs) const {return lhs < rhs;} }; // end less /*! \p greater_equal is a function object. Specifically, it is an Adaptable Binary * Predicate, which means it is a function object that tests the truth or falsehood * of some condition. If \c f is an object of class greater_equal and \c x * and \c y are objects of class \c T, then f(x,y) returns \c true if * x >= y and \c false otherwise. * * \tparam T is a model of LessThan Comparable. * * \see http://www.sgi.com/tech/stl/greater_equal.html * \see binary_function */ template struct greater_equal : public binary_function { /*! Function call operator. The return value is lhs >= rhs. */ __host__ __device__ bool operator()(const T &lhs, const T &rhs) const {return lhs >= rhs;} }; // end greater_equal /*! \p less_equal is a function object. Specifically, it is an Adaptable Binary * Predicate, which means it is a function object that tests the truth or falsehood * of some condition. If \c f is an object of class less_equal and \c x * and \c y are objects of class \c T, then f(x,y) returns \c true if * x <= y and \c false otherwise. * * \tparam T is a model of LessThan Comparable. * * \see http://www.sgi.com/tech/stl/less_equal.html * \see binary_function */ template struct less_equal : public binary_function { /*! Function call operator. The return value is lhs <= rhs. */ __host__ __device__ bool operator()(const T &lhs, const T &rhs) const {return lhs <= rhs;} }; // end less_equal /*! \} */ /*! \addtogroup logical_operations Logical Operations * \ingroup predefined_function_objects * \{ */ /*! \p logical_and is a function object. Specifically, it is an Adaptable Binary Predicate, * which means it is a function object that tests the truth or falsehood of some condition. * If \c f is an object of class logical_and and \c x and \c y are objects of * class \c T (where \c T is convertible to \c bool) then f(x,y) returns \c true * if and only if both \c x and \c y are \c true. * * \tparam T must be convertible to \c bool. * * \see http://www.sgi.com/tech/stl/logical_and.html * \see binary_function */ template struct logical_and : public binary_function { /*! Function call operator. The return value is lhs && rhs. */ __host__ __device__ bool operator()(const T &lhs, const T &rhs) const {return lhs && rhs;} }; // end logical_and /*! \p logical_or is a function object. Specifically, it is an Adaptable Binary Predicate, * which means it is a function object that tests the truth or falsehood of some condition. * If \c f is an object of class logical_or and \c x and \c y are objects of * class \c T (where \c T is convertible to \c bool) then f(x,y) returns \c true * if and only if either \c x or \c y are \c true. * * \tparam T must be convertible to \c bool. * * \see http://www.sgi.com/tech/stl/logical_or.html * \see binary_function */ template struct logical_or : public binary_function { /*! Function call operator. The return value is lhs || rhs. */ __host__ __device__ bool operator()(const T &lhs, const T &rhs) const {return lhs || rhs;} }; // end logical_or /*! \p logical_not is a function object. Specifically, it is an Adaptable Predicate, * which means it is a function object that tests the truth or falsehood of some condition. * If \c f is an object of class logical_not and \c x is an object of * class \c T (where \c T is convertible to \c bool) then f(x) returns \c true * if and only if \c x is \c false. * * \tparam T must be convertible to \c bool. * * The following code snippet demonstrates how to use \p logical_not to transform * a device_vector of \c bools into its logical complement. * * \code * #include * #include * #include * ... * thrust::device_vector V; * ... * thrust::transform(V.begin(), V.end(), V.begin(), thrust::logical_not()); * // The elements of V are now the logical complement of what they were prior * \endcode * * \see http://www.sgi.com/tech/stl/logical_not.html * \see unary_function */ template struct logical_not : public unary_function { /*! Function call operator. The return value is !x. */ __host__ __device__ bool operator()(const T &x) const {return !x;} }; // end logical_not /*! \} */ /*! \addtogroup bitwise_operations Bitwise Operations * \ingroup predefined_function_objects * \{ */ /*! \p bit_and is a function object. Specifically, it is an Adaptable Binary Function. * If \c f is an object of class bit_and, and \c x and \c y are objects * of class \c T, then f(x,y) returns x&y. * * \tparam T is a model of Assignable, * and if \c x and \c y are objects of type \p T, then x&y must be defined and must have a return type that is convertible to \c T. * * The following code snippet demonstrates how to use bit_and to take * the bitwise AND of one device_vector of \c ints by another. * * \code * #include * #include * #include * #include * #include * ... * const int N = 1000; * thrust::device_vector V1(N); * thrust::device_vector V2(N); * thrust::device_vector V3(N); * * thrust::sequence(V1.begin(), V1.end(), 1); * thrust::fill(V2.begin(), V2.end(), 13); * * thrust::transform(V1.begin(), V1.end(), V2.begin(), V3.begin(), * thrust::bit_and()); * // V3 is now {1&13, 2&13, 3&13, ..., 1000%13} * \endcode * * \see binary_function */ template struct bit_and : public binary_function { /*! Function call operator. The return value is lhs & rhs. */ __host__ __device__ T operator()(const T &lhs, const T &rhs) const {return lhs & rhs;} }; // end bit_and /*! \p bit_or is a function object. Specifically, it is an Adaptable Binary Function. * If \c f is an object of class bit_and, and \c x and \c y are objects * of class \c T, then f(x,y) returns x|y. * * \tparam T is a model of Assignable, * and if \c x and \c y are objects of type \p T, then x|y must be defined and must have a return type that is convertible to \c T. * * The following code snippet demonstrates how to use bit_or to take * the bitwise OR of one device_vector of \c ints by another. * * \code * #include * #include * #include * #include * #include * ... * const int N = 1000; * thrust::device_vector V1(N); * thrust::device_vector V2(N); * thrust::device_vector V3(N); * * thrust::sequence(V1.begin(), V1.end(), 1); * thrust::fill(V2.begin(), V2.end(), 13); * * thrust::transform(V1.begin(), V1.end(), V2.begin(), V3.begin(), * thrust::bit_or()); * // V3 is now {1|13, 2|13, 3|13, ..., 1000|13} * \endcode * * \see binary_function */ template struct bit_or : public binary_function { /*! Function call operator. The return value is lhs | rhs. */ __host__ __device__ T operator()(const T &lhs, const T &rhs) const {return lhs | rhs;} }; // end bit_or /*! \p bit_xor is a function object. Specifically, it is an Adaptable Binary Function. * If \c f is an object of class bit_and, and \c x and \c y are objects * of class \c T, then f(x,y) returns x^y. * * \tparam T is a model of Assignable, * and if \c x and \c y are objects of type \p T, then x^y must be defined and must have a return type that is convertible to \c T. * * The following code snippet demonstrates how to use bit_xor to take * the bitwise XOR of one device_vector of \c ints by another. * * \code * #include * #include * #include * #include * #include * ... * const int N = 1000; * thrust::device_vector V1(N); * thrust::device_vector V2(N); * thrust::device_vector V3(N); * * thrust::sequence(V1.begin(), V1.end(), 1); * thrust::fill(V2.begin(), V2.end(), 13); * * thrust::transform(V1.begin(), V1.end(), V2.begin(), V3.begin(), * thrust::bit_xor()); * // V3 is now {1^13, 2^13, 3^13, ..., 1000^13} * \endcode * * \see binary_function */ template struct bit_xor : public binary_function { /*! Function call operator. The return value is lhs ^ rhs. */ __host__ __device__ T operator()(const T &lhs, const T &rhs) const {return lhs ^ rhs;} }; // end bit_xor /*! \} */ /*! \addtogroup generalized_identity_operations Generalized Identity Operations * \ingroup predefined_function_objects * \{ */ /*! \p identity is a Unary Function that represents the identity function: it takes * a single argument \c x, and returns \c x. * * \tparam T No requirements on \p T. * * The following code snippet demonstrates that \p identity returns its * argument. * * \code * #include * #include * ... * int x = 137; * thrust::identity id; * assert(x == id(x)); * \endcode * * \see http://www.sgi.com/tech/stl/identity.html * \see unary_function */ template struct identity : public unary_function { /*! Function call operator. The return value is x. */ __host__ __device__ const T &operator()(const T &x) const {return x;} }; // end identity /*! \p maximum is a function object that takes two arguments and returns the greater * of the two. Specifically, it is an Adaptable Binary Function. If \c f is an * object of class maximum and \c x and \c y are objects of class \c T * f(x,y) returns \c x if x > y and \c y, otherwise. * * \tparam T is a model of LessThan Comparable. * * The following code snippet demonstrates that \p maximum returns its * greater argument. * * \code * #include * #include * ... * int x = 137; * int y = -137; * thrust::maximum mx; * assert(x == mx(x,y)); * \endcode * * \see minimum * \see min * \see binary_function */ template struct maximum : public binary_function { /*! Function call operator. The return value is rhs < lhs ? lhs : rhs. */ __host__ __device__ T operator()(const T &lhs, const T &rhs) const {return lhs < rhs ? rhs : lhs;} }; // end maximum /*! \p minimum is a function object that takes two arguments and returns the lesser * of the two. Specifically, it is an Adaptable Binary Function. If \c f is an * object of class minimum and \c x and \c y are objects of class \c T * f(x,y) returns \c x if x < y and \c y, otherwise. * * \tparam T is a model of LessThan Comparable. * * The following code snippet demonstrates that \p minimum returns its * lesser argument. * * \code * #include * #include * ... * int x = 137; * int y = -137; * thrust::minimum mn; * assert(y == mn(x,y)); * \endcode * * \see maximum * \see max * \see binary_function */ template struct minimum : public binary_function { /*! Function call operator. The return value is lhs < rhs ? lhs : rhs. */ __host__ __device__ T operator()(const T &lhs, const T &rhs) const {return lhs < rhs ? lhs : rhs;} }; // end minimum /*! \p project1st is a function object that takes two arguments and returns * its first argument; the second argument is unused. It is essentially a * generalization of identity to the case of a Binary Function. * * \code * #include * #include * ... * int x = 137; * int y = -137; * thrust::project1st pj1; * assert(x == pj1(x,y)); * \endcode * * \see identity * \see project2nd * \see binary_function */ template struct project1st : public binary_function { /*! Function call operator. The return value is lhs. */ __host__ __device__ const T1 &operator()(const T1 &lhs, const T2 &rhs) const {return lhs;} }; // end project1st /*! \p project2nd is a function object that takes two arguments and returns * its second argument; the first argument is unused. It is essentially a * generalization of identity to the case of a Binary Function. * * \code * #include * #include * ... * int x = 137; * int y = -137; * thrust::project2nd pj2; * assert(y == pj2(x,y)); * \endcode * * \see identity * \see project1st * \see binary_function */ template struct project2nd : public binary_function { /*! Function call operator. The return value is rhs. */ __host__ __device__ const T2 &operator()(const T1 &lhs, const T2 &rhs) const {return rhs;} }; // end project2nd /*! \} */ // odds and ends /*! \addtogroup function_object_adaptors * \{ */ /*! \p unary_negate is a function object adaptor: it is an Adaptable Predicate * that represents the logical negation of some other Adaptable Predicate. * That is: if \c f is an object of class unary_negate, * then there exists an object \c pred of class \c AdaptablePredicate such * that f(x) always returns the same value as !pred(x). * There is rarely any reason to construct a unary_negate directly; * it is almost always easier to use the helper function not1. * * \see http://www.sgi.com/tech/stl/unary_negate.html * \see not1 */ template struct unary_negate : public thrust::unary_function { /*! Constructor takes a \p Predicate object to negate. * \param p The \p Predicate object to negate. */ __host__ __device__ explicit unary_negate(Predicate p) : pred(p){} /*! Function call operator. The return value is !pred(x). */ __host__ __device__ bool operator()(const typename Predicate::argument_type& x) { return !pred(x); } /*! \cond */ Predicate pred; /*! \endcond */ }; // end unary_negate /*! \p not1 is a helper function to simplify the creation of Adaptable Predicates: * it takes an Adaptable Predicate \p pred as an argument and returns a new Adaptable * Predicate that represents the negation of \p pred. That is: if \c pred is an object * of a type which models Adaptable Predicate, then the the type of the result * \c npred of not1(pred) is also a model of Adaptable Predicate and * npred(x) always returns the same value as !pred(x). * * \param pred The Adaptable Predicate to negate. * \return A new object, npred such that npred(x) always returns * the same value as !pred(x). * * \tparam Predicate is a model of Adaptable Predicate. * * \see unary_negate * \see not2 */ template __host__ __device__ unary_negate not1(const Predicate &pred); /*! \p binary_negate is a function object adaptor: it is an Adaptable Binary * Predicate that represents the logical negation of some other Adaptable * Binary Predicate. That is: if \c f is an object of class binary_negate, * then there exists an object \c pred of class \c AdaptableBinaryPredicate * such that f(x,y) always returns the same value as !pred(x,y). * There is rarely any reason to construct a binary_negate directly; * it is almost always easier to use the helper function not2. * * \see http://www.sgi.com/tech/stl/binary_negate.html */ template struct binary_negate : public thrust::binary_function { /*! Constructor takes a \p Predicate object to negate. * \param p The \p Predicate object to negate. */ __host__ __device__ explicit binary_negate(Predicate p) : pred(p){} /*! Function call operator. The return value is !pred(x,y). */ __host__ __device__ bool operator()(const typename Predicate::first_argument_type& x, const typename Predicate::second_argument_type& y) { return !pred(x,y); } /*! \cond */ Predicate pred; /*! \endcond */ }; // end binary_negate /*! \p not2 is a helper function to simplify the creation of Adaptable Binary Predicates: * it takes an Adaptable Binary Predicate \p pred as an argument and returns a new Adaptable * Binary Predicate that represents the negation of \p pred. That is: if \c pred is an object * of a type which models Adaptable Binary Predicate, then the the type of the result * \c npred of not2(pred) is also a model of Adaptable Binary Predicate and * npred(x,y) always returns the same value as !pred(x,y). * * \param pred The Adaptable Binary Predicate to negate. * \return A new object, npred such that npred(x,y) always returns * the same value as !pred(x,y). * * \tparam Binary Predicate is a model of Adaptable Binary Predicate. * * \see binary_negate * \see not1 */ template __host__ __device__ binary_negate not2(const BinaryPredicate &pred); /*! \} */ /*! \addtogroup placeholder_objects Placeholder Objects * \ingroup function_objects * \{ */ /*! \namespace placeholders * \brief Facilities for constructing simple functions inline. * * Objects in the \p thrust::placeholders namespace may be used to create simple arithmetic functions inline * in an algorithm invocation. Combining placeholders such as \p _1 and \p _2 with arithmetic operations such as \c + * creates an unnamed function object which applies the operation to their arguments. * * The type of placeholder objects is implementation-defined. * * The following code snippet demonstrates how to use the placeholders \p _1 and \p _2 with \p thrust::transform * to implement the SAXPY computation: * * \code * #include * #include * #include * * int main() * { * thrust::device_vector x(4), y(4); * x[0] = 1; * x[1] = 2; * x[2] = 3; * x[3] = 4; * * y[0] = 1; * y[1] = 1; * y[2] = 1; * y[3] = 1; * * float a = 2.0f; * * using namespace thrust::placeholders; * * thrust::transform(x.begin(), x.end(), y.begin(), y.begin(), * a * _1 + 2 * ); * * // y is now {3, 5, 7, 9} * } * \endcode */ namespace placeholders { /*! \p thrust::placeholders::_1 is the placeholder for the first function parameter. */ static const thrust::detail::functional::placeholder<0>::type _1; /*! \p thrust::placeholders::_2 is the placeholder for the second function parameter. */ static const thrust::detail::functional::placeholder<1>::type _2; /*! \p thrust::placeholders::_3 is the placeholder for the third function parameter. */ static const thrust::detail::functional::placeholder<2>::type _3; /*! \p thrust::placeholders::_4 is the placeholder for the fourth function parameter. */ static const thrust::detail::functional::placeholder<3>::type _4; /*! \p thrust::placeholders::_5 is the placeholder for the fifth function parameter. */ static const thrust::detail::functional::placeholder<4>::type _5; /*! \p thrust::placeholders::_6 is the placeholder for the sixth function parameter. */ static const thrust::detail::functional::placeholder<5>::type _6; /*! \p thrust::placeholders::_7 is the placeholder for the seventh function parameter. */ static const thrust::detail::functional::placeholder<6>::type _7; /*! \p thrust::placeholders::_8 is the placeholder for the eighth function parameter. */ static const thrust::detail::functional::placeholder<7>::type _8; /*! \p thrust::placeholders::_9 is the placeholder for the ninth function parameter. */ static const thrust::detail::functional::placeholder<8>::type _9; /*! \p thrust::placeholders::_10 is the placeholder for the tenth function parameter. */ static const thrust::detail::functional::placeholder<9>::type _10; } // end placeholders /*! \} // placeholder_objects */ } // end thrust #include #include