@thi.ng/soa
Version:
SOA & AOS memory mapped structured views with optional & extensible serialization
33 lines (32 loc) • 911 B
JavaScript
import { SIZEOF } from "@thi.ng/api/typedarray";
import { align } from "@thi.ng/binary/align";
import { SOA } from "./soa.js";
import { prepareSpec } from "./utils.js";
const aos = (num, specs, buf, byteOffset = 0) => {
let total = 0;
let maxSize = 0;
const offsets = {};
const soaSpecs = {};
for (let id in specs) {
const spec = prepareSpec(specs[id]);
const tsize = SIZEOF[spec.type];
maxSize = Math.max(maxSize, tsize);
total = align(total, tsize);
offsets[id] = total;
total += tsize * spec.size;
soaSpecs[id] = spec;
}
total = align(total, maxSize);
buf = buf || new ArrayBuffer(total * num + byteOffset);
for (let id in soaSpecs) {
const spec = soaSpecs[id];
const tsize = SIZEOF[spec.type];
spec.stride = total / tsize;
spec.buf = buf;
spec.byteOffset = byteOffset + offsets[id];
}
return new SOA(num, soaSpecs);
};
export {
aos
};