Skip to content

Commit

Permalink
accept iterables (#43)
Browse files Browse the repository at this point in the history
Co-authored-by: Mike Bostock <mbostock@gmail.com>
  • Loading branch information
Fil and mbostock committed Jul 16, 2021
1 parent 24d7ea8 commit 7224f73
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
"sourceType": "module",
"ecmaVersion": 8
},
"env": {
"es6": true
},
"rules": {
"no-cond-assign": 0,
"no-constant-condition": 0
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ const tree = d3.quadtree();

<a name="quadtree" href="#quadtree">#</a> d3.<b>quadtree</b>([<i>data</i>[, <i>x</i>, <i>y</i>]]) [<>](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()
Expand Down Expand Up @@ -88,7 +88,7 @@ Adds the specified *datum* to the quadtree, deriving its coordinates ⟨*x*,*y*

<a name="quadtree_addAll" href="#quadtree_addAll">#</a> <i>quadtree</i>.<b>addAll</b>(<i>data</i>) [<>](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) {
Expand Down
18 changes: 7 additions & 11 deletions src/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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]);
}

Expand Down
10 changes: 10 additions & 0 deletions test/addAll-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]}]);
});

0 comments on commit 7224f73

Please sign in to comment.