-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathpack2.cxx
86 lines (70 loc) · 1.97 KB
/
pack2.cxx
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
#include <vector>
#include <algorithm>
#include <cstdio>
inline int sq(int x) {
return x * x;
}
int main() {
// std::vector = [ @range(10)... ];
std::vector<int> x;
{
// The expansion count is inferred from the range expression.
size_t count = 10;
// Declare an object for the current index in the range.
int begin = 0;
// Loop until we've exhausted the range.
while(count--) {
x.push_back(begin);
// The begin index for positive step is inclusive. Decrement it at
// the end of the loop.
++begin;
}
}
// std::vector y = [ @range(10::-1)... ];
std::vector<int> y;
{
size_t count = 10;
int begin = 10;
while(count--) {
// The begin index for negative step is exclusive. Decrement it at
// the start of the loop.
--begin;
y.push_back(begin);
}
}
// std::vector z = [ sq(x[:]) + 5 * y[:] ... ];
std::vector<int> z;
{
// Find the size for each slice in the pack expression.
size_t x_count = x.size();
size_t y_count = y.size();
// Use the minimum slice size to set the loop count.
size_t count = std::min(x_count, y_count);
// Declare iterators for the current item in each slice.
auto x_begin = x.begin();
auto y_begin = y.begin();
while(count--) {
z.push_back(sq(*x_begin) + 5 * *y_begin);
// Both slices have an implicit +1 step, so perform post-step increment.
++x_begin;
++y_begin;
}
}
// printf("sq(%d) + 5 * %d -> %2d\n", x[:], y[:], z[:]) ...;
{
// Find the size for each slice in the pack expression.
size_t x_count = x.size();
size_t y_count = y.size();
size_t z_count = z.size();
size_t count = std::min(std::min(x_count, y_count), z_count);
auto x_begin = x.begin();
auto y_begin = y.begin();
auto z_begin = z.begin();
while(count--) {
printf("sq(%d) + 5 * %d -> %2d\n", *x_begin, *y_begin, *z_begin);
++x_begin;
++y_begin;
++z_begin;
}
}
}