Skip to content

Commit 13362b4

Browse files
[oneDPL] Add sorting-by-key algorithms (#564)
Signed-off-by: Dmitriy Sobolev <[email protected]> Co-authored-by: Alexey Kukanov <[email protected]>
1 parent e9e4673 commit 13362b4

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

source/elements/oneDPL/source/parallel_api.rst

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,59 @@ satisfy a given predicate, and stores the result to the output. Depending on the
508508
modified from its initial value. The return value is an iterator targeting past the last
509509
considered element of the output sequence, that is, ``result + (end1 - start1)``.
510510

511+
.. code:: cpp
512+
513+
template<typename Policy, typename KeyIt, typename ValueIt,
514+
typename Comparator = std::less<typename std::iterator_traits<KeyIt>::value_type>>
515+
void
516+
sort_by_key(Policy&& policy, KeyIt keys_first, KeyIt keys_last,
517+
ValueIt values_first,
518+
Comparator comp = std::less<typename std::iterator_traits<KeyIt>::value_type>());
519+
520+
``oneapi::dpl::sort_by_key`` sorts the sequence of keys ``[keys_first, keys_last)``
521+
and simultaneously permutes associated values at the same positions in the range
522+
``[values_first, values_first + std::distance(keys_first, keys_last))``
523+
to match the order of the sorted keys. That is, a key and its associated value
524+
will have the same index in their respective sequences after sorting.
525+
526+
Keys are sorted with respect to the provided comparator object ``comp``. That means, for any
527+
two iterators ``i`` and ``j`` to the sorted sequence of keys such that ``i`` precedes ``j``,
528+
``comp(*j, *i) == false``.
529+
If no ``comp`` object is provided, keys are sorted with respect to ``std::less``.
530+
531+
Sorting is unstable. That means, keys which do not precede one another with respect to the given
532+
comparator and their associated values might be ordered arbitrarily relative to each other.
533+
534+
``KeyIt`` and ``ValueIt`` must satisfy the requirements of ``ValueSwappable``,
535+
and ``Comparator`` must satisfy the requirements for the ``Compare`` parameter of ``std::sort``,
536+
as defined by the `C++ Standard`_.
537+
538+
.. code:: cpp
539+
540+
template<typename Policy, typename KeyIt, typename ValueIt,
541+
typename Comparator = std::less<typename std::iterator_traits<KeyIt>::value_type>>
542+
void
543+
stable_sort_by_key(Policy&& policy, KeyIt keys_first, KeyIt keys_last,
544+
ValueIt values_first,
545+
Comparator comp = std::less<typename std::iterator_traits<KeyIt>::value_type>());
546+
547+
``oneapi::dpl::stable_sort_by_key`` sorts the sequence of keys ``[keys_first, keys_last)``
548+
and simultaneously permutes associated values at the same positions in the range
549+
``[values_first, values_first + std::distance(keys_first, keys_last))``
550+
to match the order of the sorted keys. That is, a key and its associated value
551+
will have the same index in their respective sequences after sorting.
552+
553+
Keys are sorted with respect to the provided comparator object ``comp``. That means, for any
554+
two iterators ``i`` and ``j`` to the sorted sequence of keys such that ``i`` precedes ``j``,
555+
``comp(*j, *i) == false``.
556+
If no ``comp`` object is provided, keys are sorted with respect to ``std::less``.
557+
558+
Sorting is stable. That means, keys which do not precede one another with respect to the given
559+
comparator and their associated values maintain the relative order as in the original sequences.
560+
561+
``KeyIt`` and ``ValueIt`` must satisfy the requirements of ``ValueSwappable``,
562+
and ``Comparator`` must satisfy the requirements for the ``Compare`` parameter of ``std::sort``,
563+
as defined by the `C++ Standard`_.
511564

512565
.. _`C++ Standard`: https://isocpp.org/std/the-standard
513566
.. _`SYCL`: https://registry.khronos.org/SYCL/specs/sycl-2020/html/sycl-2020.html

0 commit comments

Comments
 (0)