/
p1897.cpp
131 lines (109 loc) · 3.06 KB
/
p1897.cpp
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
/*
* Copyright 2019-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <unifex/just.hpp>
#include <unifex/let.hpp>
#include <unifex/transform.hpp>
#include <unifex/scheduler_concepts.hpp>
#include <unifex/sync_wait.hpp>
#include <unifex/timed_single_thread_context.hpp>
#include <unifex/indexed_for.hpp>
#include <unifex/when_all.hpp>
#include <chrono>
#include <iostream>
using namespace unifex;
using namespace std::chrono;
using namespace std::chrono_literals;
namespace execution {
class sequenced_policy {};
class parallel_policy{};
inline constexpr sequenced_policy seq{};
inline constexpr parallel_policy par{};
}
namespace ranges {
struct int_iterator {
using value_type = int;
using reference = value_type&;
using difference_type = size_t;
using pointer = value_type*;
using iterator_category = std::random_access_iterator_tag;
int operator[](size_t offset) const {
return base_+offset;
}
int operator*() const {
return base_;
}
int_iterator operator++() {
++base_;
return *this;
}
int_iterator operator++(int) {
auto cur = *this;
++base_;
return cur;
}
bool operator!=(const int_iterator& rhs) const {
return base_ != rhs.base_;
}
int base_;
};
struct iota_view {
int size_;
using iterator = int_iterator;
int_iterator begin() {
return int_iterator{0};
}
int_iterator end() {
return int_iterator{size_};
}
size_t size() const {
return size_;
}
};
} // namespace ranges
int main() {
// use seq, which supports a forward range
auto result = sync_wait(indexed_for(
just(42),
execution::seq,
ranges::iota_view{10},
[](int idx, int& x) {
x = x + idx;
}));
std::cout << "all done " << *result << "\n";
// indexed_for example from P1897R2:
auto just_sender =
just(std::vector<int>{3, 4, 5}, 10);
// Use par which requires range to be random access
auto indexed_for_sender =
indexed_for(
std::move(just_sender),
execution::par,
ranges::iota_view{3},
[](int idx, std::vector<int>& vec, const int& i){
vec[idx] = vec[idx] + i + idx;
});
auto transform_sender = transform(
std::move(indexed_for_sender), [](std::vector<int> vec, int /*i*/){return vec;});
// Slight difference from p1897R2 because unifex's sync_wait returns an optional
// to account for cancellation
std::vector<int> vector_result =
*sync_wait(std::move(transform_sender));
std::cout << "vector result:\n";
for(auto v : vector_result) {
std::cout << "\t" << v << "\n";
}
return 0;
}