// SPDX-FileCopyrightText: Copyright (c) 2008-2013, NVIDIA Corporation. All rights reserved. // SPDX-License-Identifier: Apache-2.0 /*! \file extrema.h * \brief Functions for computing computing extremal values */ #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 #include #include #include THRUST_NAMESPACE_BEGIN using ::cuda::std::max; using ::cuda::std::min; /*! \addtogroup reductions * \{ * \addtogroup extrema * \ingroup reductions * \{ */ /*! \p min_element finds the smallest element in the range [first, last). * It returns the first iterator \c i in [first, last) * such that no other iterator in [first, last) points to a value smaller * than \c *i. The return value is \p last if and only if [first, last) is an * empty range. * * The two versions of \p min_element differ in how they define whether one element is * less than another. This version compares objects using \c operator<. Specifically, * this version of \p min_element returns the first iterator \c i in [first, last) * such that, for every iterator \c j in [first, last), *j < *i is * \c false. * * The algorithm's execution is parallelized as determined by \p exec. * * \param exec The execution policy to use for parallelization. * \param first The beginning of the sequence. * \param last The end of the sequence. * \return An iterator pointing to the smallest element of the range [first, last), * if it is not an empty range; \p last, otherwise. * * \tparam ForwardIterator is a model of Forward * Iterator, and \c ForwardIterator's \c value_type is a model of LessThan Comparable. * * \code * #include * #include * ... * int data[6] = {1, 0, 2, 2, 1, 3}; * int *result = thrust::min_element(thrust::host, data, data + 6); * * // result is data + 1 * // *result is 0 * \endcode * * \see https://en.cppreference.com/w/cpp/algorithm/min_element * * \verbatim embed:rst:leading-asterisk * .. versionadded:: 2.2.0 * \endverbatim */ template _CCCL_HOST_DEVICE ForwardIterator min_element( const thrust::detail::execution_policy_base& exec, ForwardIterator first, ForwardIterator last); /*! \p min_element finds the smallest element in the range [first, last). * It returns the first iterator \c i in [first, last) * such that no other iterator in [first, last) points to a value smaller * than \c *i. The return value is \p last if and only if [first, last) is an * empty range. * * The two versions of \p min_element differ in how they define whether one element is * less than another. This version compares objects using \c operator<. Specifically, * this version of \p min_element returns the first iterator \c i in [first, last) * such that, for every iterator \c j in [first, last), *j < *i is * \c false. * * \param first The beginning of the sequence. * \param last The end of the sequence. * \return An iterator pointing to the smallest element of the range [first, last), * if it is not an empty range; \p last, otherwise. * * \tparam ForwardIterator is a model of Forward * Iterator, and \c ForwardIterator's \c value_type is a model of LessThan Comparable. * * \code * #include * ... * int data[6] = {1, 0, 2, 2, 1, 3}; * int *result = thrust::min_element(data, data + 6); * * // result is data + 1 * // *result is 0 * \endcode * * \see https://en.cppreference.com/w/cpp/algorithm/min_element * * \verbatim embed:rst:leading-asterisk * .. versionadded:: 2.2.0 * \endverbatim */ template ForwardIterator min_element(ForwardIterator first, ForwardIterator last); /*! \p min_element finds the smallest element in the range [first, last). * It returns the first iterator \c i in [first, last) * such that no other iterator in [first, last) points to a value smaller * than \c *i. The return value is \p last if and only if [first, last) is an * empty range. * * The two versions of \p min_element differ in how they define whether one element is * less than another. This version compares objects using a function object \p comp. * Specifically, this version of \p min_element returns the first iterator \c i in [first, last) * such that, for every iterator \c j in [first, last), comp(*j, *i) is * \c false. * * The algorithm's execution is parallelized as determined by \p exec. * * \param exec The execution policy to use for parallelization. * \param first The beginning of the sequence. * \param last The end of the sequence. * \param comp A binary predicate used for comparison. * \return An iterator pointing to the smallest element of the range [first, last), * if it is not an empty range; \p last, otherwise. * * \tparam DerivedPolicy The name of the derived execution policy. * \tparam ForwardIterator is a model of Forward * Iterator, and \p ForwardIterator's \c value_type is convertible to both \p comp's first and second argument type. * \tparam BinaryPredicate is a model of Binary Predicate. * * The following code snippet demonstrates how to use \p min_element to find the smallest element * of a collection of key-value pairs using the \p thrust::host execution policy for parallelization: * * \code * #include * #include * ... * * struct key_value * { * int key; * int value; * }; * * struct compare_key_value * { * __host__ __device__ * bool operator()(key_value lhs, key_value rhs) * { * return lhs.key < rhs.key; * } * }; * * ... * key_value data[4] = { {4,5}, {0,7}, {2,3}, {6,1} }; * * key_value *smallest = thrust::min_element(thrust::host, data, data + 4, compare_key_value()); * * // smallest == data + 1 * // *smallest == {0,7} * \endcode * * \see https://en.cppreference.com/w/cpp/algorithm/min_element * * \verbatim embed:rst:leading-asterisk * .. versionadded:: 2.2.0 * \endverbatim */ template _CCCL_HOST_DEVICE ForwardIterator min_element( const thrust::detail::execution_policy_base& exec, ForwardIterator first, ForwardIterator last, BinaryPredicate comp); /*! \p min_element finds the smallest element in the range [first, last). * It returns the first iterator \c i in [first, last) * such that no other iterator in [first, last) points to a value smaller * than \c *i. The return value is \p last if and only if [first, last) is an * empty range. * * The two versions of \p min_element differ in how they define whether one element is * less than another. This version compares objects using a function object \p comp. * Specifically, this version of \p min_element returns the first iterator \c i in [first, last) * such that, for every iterator \c j in [first, last), comp(*j, *i) is * \c false. * * \param first The beginning of the sequence. * \param last The end of the sequence. * \param comp A binary predicate used for comparison. * \return An iterator pointing to the smallest element of the range [first, last), * if it is not an empty range; \p last, otherwise. * * \tparam ForwardIterator is a model of Forward * Iterator, and \p ForwardIterator's \c value_type is convertible to both \p comp's first and second argument type. * \tparam BinaryPredicate is a model of Binary * Predicate. * * The following code snippet demonstrates how to use \p min_element to find the smallest element * of a collection of key-value pairs. * * \code * #include * * struct key_value * { * int key; * int value; * }; * * struct compare_key_value * { * __host__ __device__ * bool operator()(key_value lhs, key_value rhs) * { * return lhs.key < rhs.key; * } * }; * * ... * key_value data[4] = { {4,5}, {0,7}, {2,3}, {6,1} }; * * key_value *smallest = thrust::min_element(data, data + 4, compare_key_value()); * * // smallest == data + 1 * // *smallest == {0,7} * \endcode * * \see https://en.cppreference.com/w/cpp/algorithm/min_element * * \verbatim embed:rst:leading-asterisk * .. versionadded:: 2.2.0 * \endverbatim */ template ForwardIterator min_element(ForwardIterator first, ForwardIterator last, BinaryPredicate comp); /*! \p max_element finds the largest element in the range [first, last). * It returns the first iterator \c i in [first, last) * such that no other iterator in [first, last) points to a value larger * than \c *i. The return value is \p last if and only if [first, last) is an * empty range. * * The two versions of \p max_element differ in how they define whether one element is * greater than another. This version compares objects using \c operator<. Specifically, * this version of \p max_element returns the first iterator \c i in [first, last) * such that, for every iterator \c j in [first, last), *i < *j is * \c false. * * The algorithm's execution is parallelized as determined by \p exec. * * \param exec The execution policy to use for parallelization. * \param first The beginning of the sequence. * \param last The end of the sequence. * \return An iterator pointing to the largest element of the range [first, last), * if it is not an empty range; \p last, otherwise. * * \tparam A Thrust backend system. * \tparam ForwardIterator is a model of Forward * Iterator, and \c ForwardIterator's \c value_type is a model of LessThan Comparable. * * \code * #include * #include * ... * int data[6] = {1, 0, 2, 2, 1, 3}; * int *result = thrust::max_element(thrust::host, data, data + 6); * * // *result == 3 * \endcode * * \see https://en.cppreference.com/w/cpp/algorithm/max_element * * \verbatim embed:rst:leading-asterisk * .. versionadded:: 2.2.0 * \endverbatim */ template _CCCL_HOST_DEVICE ForwardIterator max_element( const thrust::detail::execution_policy_base& exec, ForwardIterator first, ForwardIterator last); /*! \p max_element finds the largest element in the range [first, last). * It returns the first iterator \c i in [first, last) * such that no other iterator in [first, last) points to a value larger * than \c *i. The return value is \p last if and only if [first, last) is an * empty range. * * The two versions of \p max_element differ in how they define whether one element is * greater than another. This version compares objects using \c operator<. Specifically, * this version of \p max_element returns the first iterator \c i in [first, last) * such that, for every iterator \c j in [first, last), *i < *j is * \c false. * * \param first The beginning of the sequence. * \param last The end of the sequence. * \return An iterator pointing to the largest element of the range [first, last), * if it is not an empty range; \p last, otherwise. * * \tparam ForwardIterator is a model of Forward * Iterator, and \c ForwardIterator's \c value_type is a model of LessThan Comparable. * * \code * #include * ... * int data[6] = {1, 0, 2, 2, 1, 3}; * int *result = thrust::max_element(data, data + 6); * * // *result == 3 * \endcode * * \see https://en.cppreference.com/w/cpp/algorithm/max_element * * \verbatim embed:rst:leading-asterisk * .. versionadded:: 2.2.0 * \endverbatim */ template ForwardIterator max_element(ForwardIterator first, ForwardIterator last); /*! \p max_element finds the largest element in the range [first, last). * It returns the first iterator \c i in [first, last) * such that no other iterator in [first, last) points to a value larger * than \c *i. The return value is \p last if and only if [first, last) is an * empty range. * * The two versions of \p max_element differ in how they define whether one element is * less than another. This version compares objects using a function object \p comp. * Specifically, this version of \p max_element returns the first iterator \c i in [first, last) * such that, for every iterator \c j in [first, last), comp(*i, *j) is * \c false. * * The algorithm's execution is parallelized as determined by \p exec. * * \param exec The execution policy to use for parallelization. * \param first The beginning of the sequence. * \param last The end of the sequence. * \param comp A binary predicate used for comparison. * \return An iterator pointing to the largest element of the range [first, last), * if it is not an empty range; \p last, otherwise. * * \tparam DerivedPolicy The name of the derived execution policy. * \tparam ForwardIterator is a model of Forward * Iterator, and \p ForwardIterator's \c value_type is convertible to both \p comp's first and second argument type. * \tparam BinaryPredicate is a model of Binary * Predicate. * * The following code snippet demonstrates how to use \p max_element to find the largest element * of a collection of key-value pairs using the \p thrust::host execution policy for parallelization. * * \code * #include * #include * ... * * struct key_value * { * int key; * int value; * }; * * struct compare_key_value * { * __host__ __device__ * bool operator()(key_value lhs, key_value rhs) * { * return lhs.key < rhs.key; * } * }; * * ... * key_value data[4] = { {4,5}, {0,7}, {2,3}, {6,1} }; * * key_value *largest = thrust::max_element(thrust::host, data, data + 4, compare_key_value()); * * // largest == data + 3 * // *largest == {6,1} * \endcode * * \see https://en.cppreference.com/w/cpp/algorithm/max_element * * \verbatim embed:rst:leading-asterisk * .. versionadded:: 2.2.0 * \endverbatim */ template _CCCL_HOST_DEVICE ForwardIterator max_element( const thrust::detail::execution_policy_base& exec, ForwardIterator first, ForwardIterator last, BinaryPredicate comp); /*! \p max_element finds the largest element in the range [first, last). * It returns the first iterator \c i in [first, last) * such that no other iterator in [first, last) points to a value larger * than \c *i. The return value is \p last if and only if [first, last) is an * empty range. * * The two versions of \p max_element differ in how they define whether one element is * less than another. This version compares objects using a function object \p comp. * Specifically, this version of \p max_element returns the first iterator \c i in [first, last) * such that, for every iterator \c j in [first, last), comp(*i, *j) is * \c false. * * \param first The beginning of the sequence. * \param last The end of the sequence. * \param comp A binary predicate used for comparison. * \return An iterator pointing to the largest element of the range [first, last), * if it is not an empty range; \p last, otherwise. * * \tparam ForwardIterator is a model of Forward * Iterator, and \p ForwardIterator's \c value_type is convertible to both \p comp's first and second argument type. * \tparam BinaryPredicate is a model of Binary * Predicate. * * The following code snippet demonstrates how to use \p max_element to find the largest element * of a collection of key-value pairs. * * \code * #include * * struct key_value * { * int key; * int value; * }; * * struct compare_key_value * { * __host__ __device__ * bool operator()(key_value lhs, key_value rhs) * { * return lhs.key < rhs.key; * } * }; * * ... * key_value data[4] = { {4,5}, {0,7}, {2,3}, {6,1} }; * * key_value *largest = thrust::max_element(data, data + 4, compare_key_value()); * * // largest == data + 3 * // *largest == {6,1} * \endcode * * \see https://en.cppreference.com/w/cpp/algorithm/max_element * * \verbatim embed:rst:leading-asterisk * .. versionadded:: 2.2.0 * \endverbatim */ template ForwardIterator max_element(ForwardIterator first, ForwardIterator last, BinaryPredicate comp); /*! \p minmax_element finds the smallest and largest elements in the range [first, last). * It returns a pair of iterators (imin, imax) where \c imin is the same iterator * returned by \p min_element and \c imax is the same iterator returned by \p max_element. * This function is potentially more efficient than separate calls to \p min_element and \p max_element. * * The algorithm's execution is parallelized as determined by \p exec. * * \param exec The execution policy to use for parallelization. * \param first The beginning of the sequence. * \param last The end of the sequence. * \return A pair of iterator pointing to the smallest and largest elements of the range [first, last), * if it is not an empty range; \p last, otherwise. * * \tparam DerivedPolicy The name of the derived execution policy. * \tparam ForwardIterator is a model of Forward * Iterator, and \c ForwardIterator's \c value_type is a model of LessThan Comparable. * * \code * #include * #include * ... * int data[6] = {1, 0, 2, 2, 1, 3}; * cuda::std::pair result = thrust::minmax_element(thrust::host, data, data + 6); * * // result.first is data + 1 * // result.second is data + 5 * // *result.first is 0 * // *result.second is 3 * \endcode * * \see min_element * \see max_element * \see http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1840.pdf * * \verbatim embed:rst:leading-asterisk * .. versionadded:: 2.2.0 * \endverbatim */ template _CCCL_HOST_DEVICE ::cuda::std::pair minmax_element( const thrust::detail::execution_policy_base& exec, ForwardIterator first, ForwardIterator last); /*! \p minmax_element finds the smallest and largest elements in the range [first, last). * It returns a pair of iterators (imin, imax) where \c imin is the same iterator * returned by \p min_element and \c imax is the same iterator returned by \p max_element. * This function is potentially more efficient than separate calls to \p min_element and \p max_element. * * \param first The beginning of the sequence. * \param last The end of the sequence. * \return A pair of iterator pointing to the smallest and largest elements of the range [first, last), * if it is not an empty range; \p last, otherwise. * * \tparam ForwardIterator is a model of Forward * Iterator, and \c ForwardIterator's \c value_type is a model of LessThan Comparable. * * \code * #include * ... * int data[6] = {1, 0, 2, 2, 1, 3}; * cuda::std::pair result = thrust::minmax_element(data, data + 6); * * // result.first is data + 1 * // result.second is data + 5 * // *result.first is 0 * // *result.second is 3 * \endcode * * \see min_element * \see max_element * \see http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1840.pdf * * \verbatim embed:rst:leading-asterisk * .. versionadded:: 2.2.0 * \endverbatim */ template ::cuda::std::pair minmax_element(ForwardIterator first, ForwardIterator last); /*! \p minmax_element finds the smallest and largest elements in the range [first, last). * It returns a pair of iterators (imin, imax) where \c imin is the same iterator * returned by \p min_element and \c imax is the same iterator returned by \p max_element. * This function is potentially more efficient than separate calls to \p min_element and \p max_element. * * The algorithm's execution is parallelized as determined by \p exec. * * \param exec The execution policy to use for parallelization. * \param first The beginning of the sequence. * \param last The end of the sequence. * \param comp A binary predicate used for comparison. * \return A pair of iterator pointing to the smallest and largest elements of the range [first, last), * if it is not an empty range; \p last, otherwise. * * \tparam DerivedPolicy The name of the derived execution policy. * \tparam ForwardIterator is a model of Forward * Iterator, and \p ForwardIterator's \c value_type is convertible to both \p comp's first and second argument type. * \tparam BinaryPredicate is a model of Binary * Predicate. * * The following code snippet demonstrates how to use \p minmax_element to find the smallest and largest elements * of a collection of key-value pairs using the \p thrust::host execution policy for parallelization: * * \code * #include * #include * #include * ... * * struct key_value * { * int key; * int value; * }; * * struct compare_key_value * { * __host__ __device__ * bool operator()(key_value lhs, key_value rhs) * { * return lhs.key < rhs.key; * } * }; * * ... * key_value data[4] = { {4,5}, {0,7}, {2,3}, {6,1} }; * * cuda::std::pair extrema = thrust::minmax_element(thrust::host, data, data + 4, * compare_key_value()); * * // extrema.first == data + 1 * // *extrema.first == {0,7} * // extrema.second == data + 3 * // *extrema.second == {6,1} * \endcode * * \see min_element * \see max_element * \see http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1840.pdf * * \verbatim embed:rst:leading-asterisk * .. versionadded:: 2.2.0 * \endverbatim */ template _CCCL_HOST_DEVICE ::cuda::std::pair minmax_element( const thrust::detail::execution_policy_base& exec, ForwardIterator first, ForwardIterator last, BinaryPredicate comp); /*! \p minmax_element finds the smallest and largest elements in the range [first, last). * It returns a pair of iterators (imin, imax) where \c imin is the same iterator * returned by \p min_element and \c imax is the same iterator returned by \p max_element. * This function is potentially more efficient than separate calls to \p min_element and \p max_element. * * \param first The beginning of the sequence. * \param last The end of the sequence. * \param comp A binary predicate used for comparison. * \return A pair of iterator pointing to the smallest and largest elements of the range [first, last), * if it is not an empty range; \p last, otherwise. * * \tparam ForwardIterator is a model of Forward * Iterator, and \p ForwardIterator's \c value_type is convertible to both \p comp's first and second argument type. * \tparam BinaryPredicate is a model of Binary * Predicate. * * The following code snippet demonstrates how to use \p minmax_element to find the smallest and largest elements * of a collection of key-value pairs. * * \code * #include * #include * * struct key_value * { * int key; * int value; * }; * * struct compare_key_value * { * __host__ __device__ * bool operator()(key_value lhs, key_value rhs) * { * return lhs.key < rhs.key; * } * }; * * ... * key_value data[4] = { {4,5}, {0,7}, {2,3}, {6,1} }; * * cuda::std::pair extrema = thrust::minmax_element(data, data + 4, compare_key_value()); * * // extrema.first == data + 1 * // *extrema.first == {0,7} * // extrema.second == data + 3 * // *extrema.second == {6,1} * \endcode * * \see min_element * \see max_element * \see http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1840.pdf * * \verbatim embed:rst:leading-asterisk * .. versionadded:: 2.2.0 * \endverbatim */ template ::cuda::std::pair minmax_element(ForwardIterator first, ForwardIterator last, BinaryPredicate comp); /*! \} // end extrema * \} // end reductions */ THRUST_NAMESPACE_END #include