refactor - create types array to have named attributes

This commit is contained in:
Azgaar 2022-06-03 00:18:18 +03:00
parent 0c2ec8d97e
commit 1a3ebe5b99
5 changed files with 24 additions and 18 deletions

View file

@ -43,16 +43,18 @@ function deepCopy(obj) {
function getTypedArray(maxValue) {
console.assert(
Number.isInteger(maxValue) && maxValue >= 0 && maxValue <= 4294967295,
`Array maxValue must be an integer between 0 and 4294967295, got ${maxValue}`
Number.isInteger(maxValue) && maxValue >= 0 && maxValue <= UINT32_MAX,
`Array maxValue must be an integer between 0 and ${UINT32_MAX}, got ${maxValue}`
);
if (maxValue <= 255) return Uint8Array;
if (maxValue <= 65535) return Uint16Array;
if (maxValue <= 4294967295) return Uint32Array;
if (maxValue <= UINT8_MAX) return Uint8Array;
if (maxValue <= UINT16_MAX) return Uint16Array;
if (maxValue <= UINT32_MAX) return Uint32Array;
return Uint32Array;
}
function createTypedArray({maxValue, length}) {
return new (getTypedArray(maxValue))(length);
function createTypedArray({maxValue, length, from}) {
const typedArray = getTypedArray(maxValue);
if (!from) return new typedArray(length);
return typedArray.from(from);
}

View file

@ -43,11 +43,10 @@ function calculateVoronoi(points, boundary) {
TIME && console.timeEnd("calculateDelaunay");
TIME && console.time("calculateVoronoi");
const n = points.length;
const voronoi = new Voronoi(delaunay, allPoints, n);
const voronoi = new Voronoi(delaunay, allPoints, points.length);
const cells = voronoi.cells;
cells.i = getTypedArray(n).from(d3.range(n)); // array of indexes
cells.i = createTypedArray({maxValue: points.length, length: points.length}).map((_, i) => i); // array of indexes
const vertices = voronoi.vertices;
TIME && console.timeEnd("calculateVoronoi");