From 1a3ebe5b99b57de7ca762401bc8038949e8fa94d Mon Sep 17 00:00:00 2001 From: Azgaar Date: Fri, 3 Jun 2022 00:18:18 +0300 Subject: [PATCH] refactor - create types array to have named attributes --- index.html | 6 +++--- main.js | 13 +++++++++---- utils/arrayUtils.js | 16 +++++++++------- utils/graphUtils.js | 5 ++--- versioning.js | 2 +- 5 files changed, 24 insertions(+), 18 deletions(-) diff --git a/index.html b/index.html index d8adfdd1..dc3ff6ed 100644 --- a/index.html +++ b/index.html @@ -6085,9 +6085,9 @@ - + - + @@ -6121,7 +6121,7 @@ - + diff --git a/main.js b/main.js index b10ad0c4..f443111d 100644 --- a/main.js +++ b/main.js @@ -13,6 +13,11 @@ const ERROR = true; // detect device const MOBILE = window.innerWidth < 600 || navigator.userAgentData?.mobile; +// typed arrays max values +const UINT8_MAX = 255; +const UINT16_MAX = 65535; +const UINT32_MAX = 4294967295; + if (PRODUCTION && "serviceWorker" in navigator) { window.addEventListener("load", () => { navigator.serviceWorker.register("./sw.js").catch(err => { @@ -1198,17 +1203,17 @@ function reGraph() { function getCellArea(i) { const area = Math.abs(d3.polygonArea(getPackPolygon(i))); - return Math.min(area, 65535); + return Math.min(area, UINT16_MAX); } const {cells: packCells, vertices} = calculateVoronoi(newCells.p, grid.boundary); pack.vertices = vertices; pack.cells = packCells; pack.cells.p = newCells.p; - pack.cells.g = getTypedArray(grid.points.length).from(newCells.g); + pack.cells.g = createTypedArray({maxValue: grid.points.length, from: newCells.g}); pack.cells.q = d3.quadtree(newCells.p.map(([x, y], i) => [x, y, i])); - pack.cells.h = getTypedArray(100).from(newCells.h); - pack.cells.area = getTypedArray(65535).from(pack.cells.i).map(getCellArea); + pack.cells.h = createTypedArray({maxValue: 100, from: newCells.h}); + pack.cells.area = createTypedArray({maxValue: UINT16_MAX, from: pack.cells.i}).map(getCellArea); TIME && console.timeEnd("reGraph"); } diff --git a/utils/arrayUtils.js b/utils/arrayUtils.js index 5fa9c81d..d872d086 100644 --- a/utils/arrayUtils.js +++ b/utils/arrayUtils.js @@ -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); } diff --git a/utils/graphUtils.js b/utils/graphUtils.js index b345e286..75b64253 100644 --- a/utils/graphUtils.js +++ b/utils/graphUtils.js @@ -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"); diff --git a/versioning.js b/versioning.js index c904ead1..1431ec83 100644 --- a/versioning.js +++ b/versioning.js @@ -1,7 +1,7 @@ "use strict"; // version and caching control -const version = "1.84.08"; // generator version, update each time +const version = "1.84.09"; // generator version, update each time { document.title += " v" + version;