// SPDX-FileCopyrightText: Copyright (c) 2008-2013, NVIDIA Corporation. All rights reserved. // SPDX-License-Identifier: Apache-2.0 /*! \file find.h * \brief Locating values in (unsorted) ranges */ #pragma once #include #if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) # pragma GCC system_header #elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) # pragma clang system_header #elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) # pragma system_header #endif // no system header #include THRUST_NAMESPACE_BEGIN /*! \addtogroup algorithms */ /*! \addtogroup searching * \ingroup algorithms * \{ */ /*! \p find returns the first iterator \c i in the range * [first, last) such that *i == value * or \c last if no such iterator exists. * * The algorithm's execution is parallelized as determined by \p exec. * * \param exec The execution policy to use for parallelization. * \param first Beginning of the sequence to search. * \param last End of the sequence to search. * \param value The value to find. * \return The first iterator \c i such that *i == value or \c last. * * \tparam DerivedPolicy The name of the derived execution policy. * \tparam InputIterator is a model of Input * Iterator and \p InputIterator's \c value_type is equality comparable to type \c T. \tparam T is a model of EqualityComparable. * * \code * #include * #include * #include * ... * thrust::device_vector input(4); * * input[0] = 0; * input[1] = 5; * input[2] = 3; * input[3] = 7; * * thrust::device_vector::iterator iter; * * iter = thrust::find(thrust::device, input.begin(), input.end(), 3); // returns input.first() + 2 * iter = thrust::find(thrust::device, input.begin(), input.end(), 5); // returns input.first() + 1 * iter = thrust::find(thrust::device, input.begin(), input.end(), 9); // returns input.end() * \endcode * * \see find_if * \see mismatch * * \verbatim embed:rst:leading-asterisk * .. versionadded:: 2.2.0 * \endverbatim */ template _CCCL_HOST_DEVICE InputIterator find(const thrust::detail::execution_policy_base& exec, InputIterator first, InputIterator last, const T& value); /*! \p find returns the first iterator \c i in the range * [first, last) such that *i == value * or \c last if no such iterator exists. * * \param first Beginning of the sequence to search. * \param last End of the sequence to search. * \param value The value to find. * \return The first iterator \c i such that *i == value or \c last. * * \tparam InputIterator is a model of Input * Iterator and \p InputIterator's \c value_type is equality comparable to type \c T. \tparam T is a model of EqualityComparable. * * \code * #include * #include * ... * thrust::device_vector input(4); * * input[0] = 0; * input[1] = 5; * input[2] = 3; * input[3] = 7; * * thrust::device_vector::iterator iter; * * iter = thrust::find(input.begin(), input.end(), 3); // returns input.first() + 2 * iter = thrust::find(input.begin(), input.end(), 5); // returns input.first() + 1 * iter = thrust::find(input.begin(), input.end(), 9); // returns input.end() * \endcode * * \see find_if * \see mismatch * * \verbatim embed:rst:leading-asterisk * .. versionadded:: 2.2.0 * \endverbatim */ template InputIterator find(InputIterator first, InputIterator last, const T& value); /*! \p find_if returns the first iterator \c i in the range * [first, last) such that pred(*i) is \c true * or \c last if no such iterator exists. * * The algorithm's execution is parallelized as determined by \p exec. * * \param exec The execution policy to use for parallelization. * \param first Beginning of the sequence to search. * \param last End of the sequence to search. * \param pred A predicate used to test range elements. * \return The first iterator \c i such that pred(*i) is \c true, or \c last. * * \tparam DerivedPolicy The name of the derived execution policy. * \tparam InputIterator is a model of Input * Iterator. \tparam Predicate is a model of Predicate. * * \code * #include * #include * #include * ... * * struct greater_than_four * { * __host__ __device__ * bool operator()(int x) * { * return x > 4; * } * }; * * struct greater_than_ten * { * __host__ __device__ * bool operator()(int x) * { * return x > 10; * } * }; * * ... * thrust::device_vector input(4); * * input[0] = 0; * input[1] = 5; * input[2] = 3; * input[3] = 7; * * thrust::device_vector::iterator iter; * * iter = thrust::find_if(thrust::device, input.begin(), input.end(), greater_than_four()); // returns input.first() + * 1 * * iter = thrust::find_if(thrust::device, input.begin(), input.end(), greater_than_ten()); // returns input.end() * \endcode * * \see find * \see find_if_not * \see mismatch * * \verbatim embed:rst:leading-asterisk * .. versionadded:: 2.2.0 * \endverbatim */ template _CCCL_HOST_DEVICE InputIterator find_if( const thrust::detail::execution_policy_base& exec, InputIterator first, InputIterator last, Predicate pred); /*! \p find_if returns the first iterator \c i in the range * [first, last) such that pred(*i) is \c true * or \c last if no such iterator exists. * * \param first Beginning of the sequence to search. * \param last End of the sequence to search. * \param pred A predicate used to test range elements. * \return The first iterator \c i such that pred(*i) is \c true, or \c last. * * \tparam InputIterator is a model of Input * Iterator. \tparam Predicate is a model of Predicate. * * \code * #include * #include * * struct greater_than_four * { * __host__ __device__ * bool operator()(int x) * { * return x > 4; * } * }; * * struct greater_than_ten * { * __host__ __device__ * bool operator()(int x) * { * return x > 10; * } * }; * * ... * thrust::device_vector input(4); * * input[0] = 0; * input[1] = 5; * input[2] = 3; * input[3] = 7; * * thrust::device_vector::iterator iter; * * iter = thrust::find_if(input.begin(), input.end(), greater_than_four()); // returns input.first() + 1 * * iter = thrust::find_if(input.begin(), input.end(), greater_than_ten()); // returns input.end() * \endcode * * \see find * \see find_if_not * \see mismatch * * \verbatim embed:rst:leading-asterisk * .. versionadded:: 2.2.0 * \endverbatim */ template InputIterator find_if(InputIterator first, InputIterator last, Predicate pred); /*! \p find_if_not returns the first iterator \c i in the range * [first, last) such that pred(*i) is \c false * or \c last if no such iterator exists. * * The algorithm's execution is parallelized as determined by \p exec. * * \param exec The execution policy to use for parallelization. * \param first Beginning of the sequence to search. * \param last End of the sequence to search. * \param pred A predicate used to test range elements. * \return The first iterator \c i such that pred(*i) is \c false, or \c last. * * \tparam DerivedPolicy The name of the derived execution policy. * \tparam InputIterator is a model of Input * Iterator. \tparam Predicate is a model of Predicate. * * \code * #include * #include * #include * ... * * struct greater_than_four * { * __host__ __device__ * bool operator()(int x) * { * return x > 4; * } * }; * * struct greater_than_ten * { * __host__ __device__ * bool operator()(int x) * { * return x > 10; * } * }; * * ... * thrust::device_vector input(4); * * input[0] = 0; * input[1] = 5; * input[2] = 3; * input[3] = 7; * * thrust::device_vector::iterator iter; * * iter = thrust::find_if_not(thrust::device, input.begin(), input.end(), greater_than_four()); // returns * input.first() * * iter = thrust::find_if_not(thrust::device, input.begin(), input.end(), greater_than_ten()); // returns * input.first() \endcode * * \see find * \see find_if * \see mismatch * * \verbatim embed:rst:leading-asterisk * .. versionadded:: 2.2.0 * \endverbatim */ template _CCCL_HOST_DEVICE InputIterator find_if_not( const thrust::detail::execution_policy_base& exec, InputIterator first, InputIterator last, Predicate pred); /*! \p find_if_not returns the first iterator \c i in the range * [first, last) such that pred(*i) is \c false * or \c last if no such iterator exists. * * \param first Beginning of the sequence to search. * \param last End of the sequence to search. * \param pred A predicate used to test range elements. * \return The first iterator \c i such that pred(*i) is \c false, or \c last. * * \tparam InputIterator is a model of Input * Iterator. \tparam Predicate is a model of Predicate. * * \code * #include * #include * * struct greater_than_four * { * __host__ __device__ * bool operator()(int x) * { * return x > 4; * } * }; * * struct greater_than_ten * { * __host__ __device__ * bool operator()(int x) * { * return x > 10; * } * }; * * ... * thrust::device_vector input(4); * * input[0] = 0; * input[1] = 5; * input[2] = 3; * input[3] = 7; * * thrust::device_vector::iterator iter; * * iter = thrust::find_if_not(input.begin(), input.end(), greater_than_four()); // returns input.first() * * iter = thrust::find_if_not(input.begin(), input.end(), greater_than_ten()); // returns input.first() * \endcode * * \see find * \see find_if * \see mismatch * * \verbatim embed:rst:leading-asterisk * .. versionadded:: 2.2.0 * \endverbatim */ template InputIterator find_if_not(InputIterator first, InputIterator last, Predicate pred); /*! \} // end searching */ THRUST_NAMESPACE_END #include