UNPKG

@thi.ng/vectors

Version:

Optimized 2d/3d/4d and arbitrary length vector operations, support for memory mapping/layouts

40 lines (39 loc) 1.02 kB
import { SIZEOF, typedArray } from "@thi.ng/api/typedarray"; const mapBuffer = (type, buf, num, size, byteOffset = 0, byteStride = size * SIZEOF[type]) => { const res = []; for (; num-- > 0; byteOffset += byteStride) { res.push(typedArray(type, buf, byteOffset, size)); } return res; }; const intoBuffer = (type, buf, src, byteOffset, byteStride) => { const view = typedArray(type, buf); const size = SIZEOF[type]; byteOffset /= size; byteStride /= size; for (let x of src) { view.set(x, byteOffset); byteOffset += byteStride; } }; const mapStridedBuffer = (ctor, buf, num, start, cstride, estride) => { const res = []; while (num-- > 0) { res.push(new ctor(buf, start, cstride)); start += estride; } return res; }; const intoStridedBuffer = (set, buf, src, start, cstride, estride) => { for (let v of src) { set(buf, v, start, 0, cstride, 1); start += estride; } return buf; }; export { intoBuffer, intoStridedBuffer, mapBuffer, mapStridedBuffer };