Menu

#94 use "tuples" to index arrays

open
nobody
5
2012-01-14
2012-01-14
No

Index arrays with "tuples", or similar structures of the form int* (meaning tuples of ints) or long*. This support is three: first, "tuple" variables must be supported in do() loops (in all forms). Second, they must be allowed to index arrays. And third, arithmetic operations must be supported on "tuples", as they are on ordinary ints. Working with tuples allows to simplify working with multi-dimensional array.

3d convolution of the form:
nuwork(1, 1, 64) do((i, j, k) in (n, n, n)) {
mutable r = 0f;
inline do((p, q, s) in (-1 <> 1, -1 <> 1, -1 <> 1))
r += a[clamp0(i + p, n), clamp0(j + q, n), clamp0(k + s, n)] * b[p + 1, q + 1, s + 1];
c[i, j, k] = r;
}
is shortened to
nuwork(1, 1, 64) do(i in (n, n, n)) {
mutable r = 0f;
inline do(p in (-1 <> 1, -1 <> 1, -1 <> 1))
r += a[clamp0(i + p, n)] * b[p + 1];
c[i] = r;
}
or, using "built-in" domain functions
nuwork(1, 1, 64) do(i in dom3(n)) {
mutable r = 0f;
inline do(p in dom3(-1 <> 1))
r += a[clamp0(i + p, n)] * b[p + 1];
c[i] = r;
}
// clamp0 also better be built-in

Discussion