File: sort.dox

package info (click to toggle)
taskflow 3.9.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 45,948 kB
  • sloc: cpp: 39,058; xml: 35,572; python: 12,935; javascript: 1,732; makefile: 59; sh: 16
file content (102 lines) | stat: -rw-r--r-- 2,663 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
namespace tf {

/** @page ParallelSort Parallel Sort

%Taskflow provides template functions for constructing tasks
to sort ranges of items in parallel.

@tableofcontents

@section ParallelSortInclude Include the Header

You need to include the header file, <tt>taskflow/algorithm/sort.hpp</tt>,
for creating a parallel-sort task.

@code{.cpp}
#include <taskflow/algorithm/sort.hpp>
@endcode

@section SortARangeOfItems Sort a Range of Items

The task created by tf::Taskflow::sort(B first, E last) 
performs parallel sort to rank a range of elements specified by 
<tt>[first, last)</tt> in increasing order.
The given iterators must be @em random-accessible.
The following example creates a task to sort a data vector in increasing order.

@code{.cpp}
tf::Taskflow taskflow;
tf::Executor executor;

std::vector<int> data = {1, 4, 9, 2, 3, 11, -8};

tf::Task sort = taskflow.sort(data.begin(), data.end());

executor.run(taskflow).wait();

assert(std::is_sorted(data.begin(), data.end()));
@endcode

@attention
Elements are compared using the operator @c <.

@section SortARangeOfItemsWithACustomComparator Sort a Range of Items with a Custom Comparator

tf::Taskflow::sort(B first, E last, C cmp) is an overload of 
parallel sort that allows users to specify a custom comparator.
The following example sorts a data vector in decreasing order.

@code{.cpp}
tf::Taskflow taskflow;
tf::Executor executor;

std::vector<int> data = {1, 4, 9, 2, 3, 11, -8};

tf::Task sort = taskflow.sort(data.begin(), data.end(), 
  [](int a, int b) { return a > b; }
);

executor.run(taskflow).wait();

assert(std::is_sorted(data.begin(), data.end(), std::greater<int>{}));
@endcode

@attention
tf::Taskflow::sort is not stable. That is, two or more objects with equal keys
may not appear in the same order before sorting.

@section ParallelSortEnableStatefulDataPassing Enable Stateful Data Passing

The iterators taken by tf::Taskflow::sort are templated.
You can use std::reference_wrapper to enable stateful data passing
between the sort task and others.
The following example creates a task @c init to initialize the data vector
and a task @c sort to sort the data in parallel after @c init finishes.

@code{.cpp}
tf::Taskflow taskflow;
tf::Executor executor;

std::vector<int> data;
std::vector<int>::iterator first, last;

tf::Task init = taskflow.emplace([&](){ 
  data  = {1, 4, 9, 2, 3, 11, -8}; 
  first = data.begin();
  last  = data.end();
});
tf::Task sort = taskflow.sort(
  std::ref(first), std::ref(last), [] (int l, int r) { return l < r; }
);
init.precede(sort);

executor.run(taskflow).wait();

assert(std::is_sorted(data.begin(), data.end()));
@endcode

*/

}