diff --git a/.eslintrc.json b/.eslintrc.json index c2bd6c5..4b9180a 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -4,6 +4,9 @@ "sourceType": "module", "ecmaVersion": 8 }, + "env": { + "es6": true + }, "rules": { "no-cond-assign": 0, "no-constant-condition": 0 diff --git a/README.md b/README.md index 0a930a2..510b64d 100644 --- a/README.md +++ b/README.md @@ -34,14 +34,14 @@ const tree = d3.quadtree(); # d3.quadtree([data[, x, y]]) [<>](https://github.com/d3/d3-quadtree/blob/master/src/quadtree.js "Source") -Creates a new, empty quadtree with an empty [extent](#quadtree_extent) and the default [*x*-](#quadtree_x) and [*y*-](#quadtree_y)accessors. If *data* is specified, [adds](#quadtree_addAll) the specified array of data to the quadtree. This is equivalent to: +Creates a new, empty quadtree with an empty [extent](#quadtree_extent) and the default [*x*-](#quadtree_x) and [*y*-](#quadtree_y)accessors. If *data* is specified, [adds](#quadtree_addAll) the specified iterable of data to the quadtree. This is equivalent to: ```js const tree = d3.quadtree() .addAll(data); ``` -If *x* and *y* are also specified, sets the [*x*-](#quadtree_x) and [*y*-](#quadtree_y) accessors to the specified functions before adding the specified array of data to the quadtree, equivalent to: +If *x* and *y* are also specified, sets the [*x*-](#quadtree_x) and [*y*-](#quadtree_y) accessors to the specified functions before adding the specified iterable of data to the quadtree, equivalent to: ```js const tree = d3.quadtree() @@ -88,7 +88,7 @@ Adds the specified *datum* to the quadtree, deriving its coordinates ⟨*x*,*y* # quadtree.addAll(data) [<>](https://github.com/d3/d3-quadtree/blob/master/src/add.js "Source") -Adds the specified array of *data* to the quadtree, deriving each element’s coordinates ⟨*x*,*y*⟩ using the current [*x*-](#quadtree_x) and [*y*-](#quadtree_y)accessors, and return this quadtree. This is approximately equivalent to calling [*quadtree*.add](#quadtree_add) repeatedly: +Adds the specified iterable of *data* to the quadtree, deriving each element’s coordinates ⟨*x*,*y*⟩ using the current [*x*-](#quadtree_x) and [*y*-](#quadtree_y)accessors, and return this quadtree. This is approximately equivalent to calling [*quadtree*.add](#quadtree_add) repeatedly: ```js for (let i = 0, n = data.length; i < n; ++i) { diff --git a/src/add.js b/src/add.js index 985adfc..3d7c6ba 100644 --- a/src/add.js +++ b/src/add.js @@ -48,18 +48,14 @@ function add(tree, x, y, d) { } export function addAll(data) { - var d, i, n = data.length, - x, - y, - xz = new Array(n), - yz = new Array(n), - x0 = Infinity, - y0 = Infinity, - x1 = -Infinity, - y1 = -Infinity; + if (!Array.isArray(data)) data = Array.from(data); + const n = data.length; + const xz = new Float64Array(n); + const yz = new Float64Array(n); + let x0 = Infinity, y0 = x0, x1 = -x0, y1 = x1; // Compute the points and their extent. - for (i = 0; i < n; ++i) { + for (let i = 0, d, x, y; i < n; ++i) { if (isNaN(x = +this._x.call(null, d = data[i])) || isNaN(y = +this._y.call(null, d))) continue; xz[i] = x; yz[i] = y; @@ -76,7 +72,7 @@ export function addAll(data) { this.cover(x0, y0).cover(x1, y1); // Add the new points. - for (i = 0; i < n; ++i) { + for (let i = 0; i < n; ++i) { add(this, xz[i], yz[i], data[i]); } diff --git a/test/addAll-test.js b/test/addAll-test.js index dd2f75e..23d312f 100644 --- a/test/addAll-test.js +++ b/test/addAll-test.js @@ -32,3 +32,13 @@ it("quadtree.addAll(data) computes the extent of the data before adding", () => const q = quadtree().addAll([[0.4, 0.4], [0, 0], [0.9, 0.9]]); assert.deepStrictEqual(q.root(), [[{data: [0, 0]},,, {data: [0.4, 0.4]}],,, {data: [0.9, 0.9]}]); }); + +it("quadtree.addAll(iterable) adds points from an iterable", () => { + const q = quadtree().addAll(new Set([[0.4, 0.4], [0, 0], [0.9, 0.9]])); + assert.deepStrictEqual(q.root(), [[{data: [0, 0]},,, {data: [0.4, 0.4]}],,, {data: [0.9, 0.9]}]); +}); + +it("quadtree(iterable) adds points from an iterable", () => { + const q = quadtree(new Set([[0.4, 0.4], [0, 0], [0.9, 0.9]])); + assert.deepStrictEqual(q.root(), [[{data: [0, 0]},,, {data: [0.4, 0.4]}],,, {data: [0.9, 0.9]}]); +});