-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.h
154 lines (132 loc) · 4.63 KB
/
util.h
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#pragma once
#include <algorithm>
#include <iterator>
#include "concurrent_revisions.h"
namespace concurrent_revisions {
template <class Iterator, class F>
void parallel_foreach(Iterator begin, Iterator end, F f, size_t min_parallel = 1024)
{
size_t len = end - begin;
if (len <= min_parallel) {
std::for_each(begin, end, f);
}
else {
revision r = fork([&]{
parallel_foreach(begin, begin + len / 2, f, min_parallel);
});
parallel_foreach(begin + len / 2, end, f, min_parallel);
join(r);
}
}
template <class InputIterator, class OutputRandomAccessIterator, class F>
void parallel_transform(InputIterator begin, InputIterator end, OutputRandomAccessIterator out, F f, size_t min_parallel = 1024)
{
size_t const len = end - begin;
if (len <= min_parallel) {
std::transform(begin, end, out, f);
}
else {
revision r = fork([&begin, &out, &len, &f, &min_parallel]{
parallel_transform(begin, begin + len / 2, out, f, min_parallel);
});
parallel_transform(begin + len / 2, end, out + len / 2, f, min_parallel);
join(r);
}
}
template <class InputIterator1, class InputIterator2, class OutputRandomAccessIterator, class F>
void parallel_transform(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2, InputIterator2 end2, OutputRandomAccessIterator out, F f, size_t min_parallel = 1024)
{
size_t const len1 = end1 - begin1;
size_t const len2 = end2 - begin2;
size_t const len = std::min(len1, len2);
if (len <= min_parallel) {
std::transform(begin1, begin1+len, begin2, out, f);
}
else {
revision r = fork([&len, &begin1, &begin2, &out, &f, &min_parallel]{
parallel_transform(begin1, begin1 + len / 2, begin2, begin2 + len / 2, out, f, min_parallel);
});
parallel_transform(begin1 + len / 2, begin1 + len, begin2 + len / 2, begin2 + len, out + len / 2, f, min_parallel);
join(r);
}
}
template <class Iterator>
void parallel_max_element_impl(Iterator first, Iterator last, versioned<Iterator, max_iter_merger<Iterator> >& v, std::size_t min_parallel)
{
std::size_t len = std::distance(first, last);
if (len <= min_parallel) {
v = std::max_element(first, last);
} else {
revision r = fork([&] {
parallel_max_element_impl(first, first + len/2, v, min_parallel);
});
parallel_max_element_impl(first + len/2, last, v, min_parallel);
join(r);
}
}
template <class Iterator>
Iterator parallel_max_element(Iterator first, Iterator last, std::size_t min_parallel = 1024)
{
versioned<Iterator, max_iter_merger<Iterator> > v;
parallel_max_element_impl(first, last, v, min_parallel);
return v;
}
template <class Iterator>
void parallel_min_element_impl(Iterator first, Iterator last, versioned<Iterator, min_iter_merger<Iterator> >& v, std::size_t min_parallel)
{
std::size_t len = std::distance(first, last);
if (len <= min_parallel) {
v = std::min_element(first, last);
} else {
revision r = fork([&] {
parallel_min_element_impl(first, first + len/2, v, min_parallel);
});
parallel_min_element_impl(first + len/2, last, v, min_parallel);
join(r);
}
}
template <class Iterator>
Iterator parallel_min_element(Iterator first, Iterator last, std::size_t min_parallel = 1024)
{
versioned<Iterator, min_iter_merger<Iterator> > v;
parallel_min_element_impl(first, last, v, min_parallel);
return v;
}
template <class Iterator, class Less>
void parallel_stable_sort(Iterator first, Iterator last, Less f, std::size_t min_parallel = 1024)
{
std::size_t len = std::distance(first, last);
if (len <= min_parallel) {
std::stable_sort(first, last, f);
} else {
Iterator mid = first + len/2;
revision r = fork([&] {
parallel_stable_sort(first, mid, f, min_parallel);
});
parallel_stable_sort(mid, last, f, min_parallel);
join(r);
std::inplace_merge(first, mid, last, f);
}
}
template <class Iterator>
void parallel_stable_sort(Iterator first, Iterator last, std::size_t min_parallel = 1024)
{
parallel_stable_sort(first, last, std::less<typename std::iterator_traits<Iterator>::value_type>(), min_parallel);
}
template <class Iterator1, class Iterator2>
void parallel_swap_ranges(Iterator1 first1, Iterator1 last1, Iterator2 first2, std::size_t min_parallel = 1024)
{
std::size_t len = std::distance(first1, last1);
if (len <= min_parallel) {
std::swap_ranges(first1, last1, first2);
} else {
auto mid1 = first1 + len/2;
auto mid2 = first2 + len/2;
revision r = fork([&] {
parallel_swap_ranges(first1, mid1, first2, min_parallel);
});
parallel_swap_ranges(mid1, last1, mid2, min_parallel);
join(r);
}
}
} // namespace concurrent_revisions