Repository navigation
Expand file tree
/
Copy pathalgorithms.cpp
More file actions
129 lines (90 loc) · 3.1 KB
/
Copy pathalgorithms.cpp
File metadata and controls
129 lines (90 loc) · 3.1 KB
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
#include "vector.h"
#include <algorithm>
#include <numeric>
#include <execution>
//wrong to check what happens, not associative, will vary
struct my_custom_op{
int operator()(int a, int b){
return -10;
}
};
struct reverse{
bool operator()(int a, int b){
return a>b;
}
};
struct sort_according_to_another_vector{
vector<int> vector;
bool operator()(int ia, int ib){
return vector[ia] < vector[ib];
}
};
int main(){
vector<int> v;
for (int i=0; i<10; i++){
v.push_back(10-i);
}
v.print();
int sum = std::reduce(v.begin(), v.end(), 0, std::plus<>());
std::cout<<"sum= "<< sum<<std::endl;
int seeded_sum = std::reduce(v.begin(), v.end(), 10, std::plus<>());
std::cout<<"seeded_sum= "<< seeded_sum<<std::endl;
int custom_sum = std::reduce(v.begin(), v.end(), 10, my_custom_op()); //op is not associative: UB, result vary with impl & policy
std::cout<<"custom_sum= "<< custom_sum<<std::endl;
//seq: seq
//unseq: simd
//par: openmp style share of the work
//par_unseq: both
int fast_sum = std::reduce(std::execution::par, v.begin(), v.end(), 0, std::plus<>());
std::cout<<"fast_sum= "<< fast_sum<<std::endl;
std::sort(v.begin(), v.end());
v.print();
std::sort(std::execution::par, v.begin(), v.end(), reverse());
v.print();
vector<int> another_vector;
for (int i=0; i<10; i++){
auto x = (i%2) ? i :-i;
another_vector.push_back(x);
}
std::cout<<"another vector\n";
another_vector.print();
vector<int> index(10), out(10);
std::iota(index.begin(), index.end(), 0);
index.print();
std::sort(std::execution::par, index.begin(), index.end(), sort_according_to_another_vector{another_vector}); //no () there Oo
std::cout<<"sorted index:\n";
index.print();
//put back into out
std::transform(index.begin(), index.end(), /*not v here !*/ out.begin(), [&](int i) { return v[i]; });
out.print();
//in place transformation
//no return
//[](int &a){ return (a<0) ? -a:a;} does nothing (a never touched)
//[](int a){ {a=(a<0) ? -a:a;} does nothing (only local copy is modified)
std::for_each(another_vector.begin(), another_vector.end(), [](int &a){a=(a<0) ? -a:a;});
another_vector.print();
vector<int> result(10);
std::sort(v.begin(), v.end());
v.print();
//this time a return is needed bc we write somewhere else
std::transform(v.begin(), v.end(), result.begin(), [](int&a){return a+1;});
result.print();
for (int i=0; i<10; i++){
v[i] = 0;
}
v.print();
int result_ = std::transform_reduce(v.begin(), v.end(), 0,
[](int a, int b){return a+b;}, //reduce
[](int&a){return a+1;} // transform
);
std::cout<<result_<<std::endl;
//important: v is not transformed! (no W)
v.print();
for (int i=0; i<10; i++){
v[i] = 1;
}
v.print();
// transform version too
std::inclusive_scan(v.begin(), v.end(), result.begin(), std::plus<>());
result.print();
}