UNPKG

@thi.ng/vector-pools

Version:

Data structures for managing & working with strided, memory mapped vectors

61 lines (60 loc) 1.28 kB
import { wrap } from "./wrap.js"; class AVecList { buffer; factory; start; capacity; curr; size; estride; cstride; freeIDs; /** * * @param buffer - * @param capacity - * @param size - * @param start - * @param cstride - * @param estride - * @param factory - */ constructor(buffer, capacity, size, start = 0, cstride = 1, estride = size, factory = wrap) { this.buffer = buffer || new Float32Array(size * capacity); this.size = size; this.factory = factory; this.cstride = cstride; this.estride = estride; this.start = this.curr = start; this.capacity = capacity; this.freeIDs = []; } indices(res = [], i = 0, local = true) { const { start, estride } = this; if (local) { for (let v of this) { res[i++] = (v.offset - start) / estride; } } else { for (let v of this) { res[i++] = v.offset; } } return res; } alloc() { let idx; if (this.freeIDs.length > 0) { idx = this.freeIDs.pop(); } else if (this.length >= this.capacity) { return; } else { idx = this.curr; this.curr += this.estride; } return this.factory(this.buffer, this.size, idx, this.cstride); } } export { AVecList };