Repository navigation
Expand file tree
/
Copy pathiteratorC.c
More file actions
90 lines (78 loc) · 2.77 KB
/
Copy pathiteratorC.c
File metadata and controls
90 lines (78 loc) · 2.77 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
#include <stdio.h>
#include <stdlib.h>
#include "cfparray.h"
void print1(cfp_ptr1d p, size_t n)
{
size_t i;
const cfp_array1d_api _ = cfp.array1d;
for (i = 0; i < n; i++)
printf("%e\n", _.reference.get(_.pointer.ref_at(p, i)));
}
void print2(cfp_ptr2d p, size_t n)
{
const cfp_array2d_api _ = cfp.array2d;
while (n--) {
printf("%lf\n", _.reference.get(_.pointer.ref(p)));
p = _.pointer.inc(p);
}
}
void print3(cfp_iter1d begin, cfp_iter1d end)
{
const cfp_array1d_api _ = cfp.array1d;
cfp_iter1d p;
for (p = begin; !_.iterator.eq(p, end); p = _.iterator.inc(p))
printf("%e\n", _.reference.get(_.iterator.ref(p)));
}
int main()
{
const cfp_array1d_api _1d = cfp.array1d;
const cfp_array2d_api _2d = cfp.array2d;
const cfp_array3d_api _3d = cfp.array3d;
size_t i, j, k;
/* some fun with 1D arrays */
cfp_array1d v = _1d.ctor(10, 64.0, 0, 0);
/* initialize and print array of random values */
cfp_iter1d it1;
for (it1 = _1d.begin(v); !_1d.iterator.eq(it1, _1d.end(v)); it1 = _1d.iterator.inc(it1))
_1d.reference.set(_1d.iterator.ref(it1), rand());
printf("random array\n");
print1(_1d.ptr(v, 0), _1d.size(v));
printf("\n");
/* some fun with 2D arrays */
cfp_array2d a = _2d.ctor(5, 7, 64.0, 0, 0);
/* print array indices visited in block-order traversal*/
printf("block order (x, y) indices\n");
cfp_iter2d it2;
for (it2 = _2d.begin(a); !_2d.iterator.eq(it2, _2d.end(a)); it2 = _2d.iterator.inc(it2)) {
i = _2d.iterator.i(it2);
j = _2d.iterator.j(it2);
printf("(%lu, %lu)\n", i, j);
_2d.reference.set(_2d.iterator.ref(it2), i + 10 * j);
}
printf("\n");
/* print array contents in row-major order */
printf("row-major order yx indices\n");
print2(_2d.ptr_flat(a, 0), _2d.size(a));
printf("\n");
/* pointer arithmetic */
cfp_ptr2d pb2 = _2d.reference.ptr(_2d.iterator.ref(_2d.begin(a)));
cfp_ptr2d pe2 = _2d.reference.ptr(_2d.iterator.ref(_2d.end(a)));
printf("%lu * %lu = %lld\n", _2d.size_x(a), _2d.size_y(a), (long long int)_2d.pointer.distance(pe2, pb2));
/* some fun with 3D arrays */
cfp_array3d b = _3d.ctor(7, 2, 5, 64.0, 0, 0);
/* print array indices visited in block-order traversal */
printf("block order (x, y, z) indices\n");
cfp_iter3d it3;
for (it3 = _3d.begin(b); !_3d.iterator.eq(it3, _3d.end(b)); it3 = _3d.iterator.inc(it3)) {
i = _3d.iterator.i(it3);
j = _3d.iterator.j(it3);
k = _3d.iterator.k(it3);
printf("(%lu, %lu, %lu)\n", i, j, k);
}
printf("\n");
/* pointer arithmetic */
cfp_ptr3d pb3 = _3d.reference.ptr(_3d.iterator.ref(_3d.begin(b)));
cfp_ptr3d pe3 = _3d.reference.ptr(_3d.iterator.ref(_3d.end(b)));
printf("%lu * %lu * %lu = %lld\n", _3d.size_x(b), _3d.size_y(b), _3d.size_z(b), (long long int)_3d.pointer.distance(pe3, pb3));
return 0;
}