Vec2/3/4 view based buffer transformation for VecOpVV type ops and supporting arbitrary component and element layouts of all input and output buffers.
remarks
The given pre-initialized vectors MUST be separate instances, are used as sliding cursors / views of their respective backing buffers and will be modified as part of the transformation process (though the input buffers themselves are treated as immutable, unless out is configured to use one of the input buffers).
In each iteration op is called via op(out, a, b), followed by cursor updates to process the next vector view. No bounds checking is performed.
This function returns out's backing buffer.
example
// each input buffer contains 2 2D vectors, but using// different strided data layouts
mapVV(
// transformation function
add,
// init output buffer viewnew Vec2(),
// wrap 1st input buffer & configure offset & component stridenew Vec2([1,0,2,0,0,0,0,0,3,0,4,0,0,0,0,0], 0, 2),
// wrap 2nd input buffernew Vec2([0,10,0,0,20,0,0,30,0,0,40], 1, 3),
2, // num vectors2, // output element stride8, // input #1 element stride6// input #2 element stride
);
// [ 11, 22, 33, 44 ]
Alternatively, Vec2/3/4.iterator() combined with transducers can be used to achieve the same (and more flexible) transformations, but will incur more intermediate object allocations. mapV*() functions only use (and mutate) the provided vector instances and do not allocate any further objects.
// output bufferconst out = newArray(4);
tx.run(
tx.map(([o, a, b]) => add(o, a, b)),
tx.zip(
Vec2.iterator(out, 2),
Vec2.iterator([1,0,2,0,0,0,0,0,3,0,4,0,0,0,0,0], 2, 0, 2, 8),
Vec2.iterator([0,10,0,0,20,0,0,30,0,0,40], 2, 1, 3, 6),
)
);
out
// [ 11, 22, 33, 44 ]
Like mapVV, but for VecOpV type ops and hence only using single input.
// 4x 2D vectors in SOA layout // i.e. [x1, x2, x3, x4, y1, y2, y3, y4] buf = [1, 3, 5, 7, 2, 4, 6, 8]; // use `swapXY` to swizzle each vector and use AOS for output res = mapV(swapXY, new Vec2(), new Vec2(buf, 0, 4), 4, 2, 1); // [ 2, 1, 4, 3, 6, 5, 8, 7 ] // unpack result for demonstration purposes [...Vec2.iterator(res, 4)].map(v => [...v]); // [ [ 2, 1 ], [ 4, 3 ], [ 6, 5 ], [ 8, 7 ] ]