@thi.ng/grid-iterators 4.0.34 → 4.0.36

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/random.js CHANGED
@@ -2,19 +2,13 @@ import { shuffle } from "@thi.ng/arrays/shuffle";
2
2
  import { SYSTEM } from "@thi.ng/random/system";
3
3
  import { range } from "@thi.ng/transducers/range";
4
4
  import { __opts } from "./utils.js";
5
- /**
6
- * Yields 2D grid coordinates in random order w/ support for optional
7
- * [`IRandom`](https://docs.thi.ng/umbrella/random/interfaces/IRandom.html)
8
- * implementation (default:
9
- * [`SYSTEM`](https://docs.thi.ng/umbrella/random/variables/SYSTEM.html) aka
10
- * `Math.random`).
11
- *
12
- * @param opts -
13
- */
14
- export function* random2d(opts) {
15
- const { cols, rows, tx } = __opts(opts);
16
- const rnd = opts.rnd || SYSTEM;
17
- for (let i of shuffle([...range(cols * rows)], undefined, rnd)) {
18
- yield tx(i % cols, (i / cols) | 0);
19
- }
5
+ function* random2d(opts) {
6
+ const { cols, rows, tx } = __opts(opts);
7
+ const rnd = opts.rnd || SYSTEM;
8
+ for (let i of shuffle([...range(cols * rows)], void 0, rnd)) {
9
+ yield tx(i % cols, i / cols | 0);
10
+ }
20
11
  }
12
+ export {
13
+ random2d
14
+ };
package/row-ends.js CHANGED
@@ -1,15 +1,12 @@
1
1
  import { __opts } from "./utils.js";
2
- /**
3
- * Filtered version of {@link rows2d}, only including end points of
4
- * each row.
5
- *
6
- * @param opts -
7
- */
8
- export function* rowEnds2d(opts) {
9
- let { cols, rows, tx } = __opts(opts);
10
- cols--;
11
- for (let y = 0; y < rows; y++) {
12
- yield tx(0, y);
13
- yield tx(cols, y);
14
- }
2
+ function* rowEnds2d(opts) {
3
+ let { cols, rows, tx } = __opts(opts);
4
+ cols--;
5
+ for (let y = 0; y < rows; y++) {
6
+ yield tx(0, y);
7
+ yield tx(cols, y);
8
+ }
15
9
  }
10
+ export {
11
+ rowEnds2d
12
+ };
package/rows.js CHANGED
@@ -1,13 +1,10 @@
1
1
  import { map } from "@thi.ng/transducers/map";
2
2
  import { range2d } from "@thi.ng/transducers/range2d";
3
3
  import { __opts } from "./utils.js";
4
- /**
5
- * Yields sequence of 2D grid coordinates in row-major order. Same as
6
- * [`range2d()`](https://docs.thi.ng/umbrella/transducers/functions/range2d.html).
7
- *
8
- * @param opts -
9
- */
10
- export const rows2d = (opts) => {
11
- const { cols, rows, tx } = __opts(opts);
12
- return map((p) => tx(p[0], p[1]), range2d(cols, rows));
4
+ const rows2d = (opts) => {
5
+ const { cols, rows, tx } = __opts(opts);
6
+ return map((p) => tx(p[0], p[1]), range2d(cols, rows));
7
+ };
8
+ export {
9
+ rows2d
13
10
  };
package/spiral.js CHANGED
@@ -1,48 +1,39 @@
1
1
  import { __opts } from "./utils.js";
2
- /**
3
- * Yields sequence of 2D grid coordinates in outward spiral order starting from
4
- * the center, given `cols` and `rows`.
5
- *
6
- * Ported & modified from original Java code by Christopher Kulla.
7
- * https://sourceforge.net/p/sunflow/code/HEAD/tree/trunk/src/org/sunflow/core/bucket/SpiralBucketOrder.java
8
- *
9
- * @param opts -
10
- */
11
- export function* spiral2d(opts) {
12
- const { cols, rows, tx } = __opts(opts);
13
- const num = cols * rows;
14
- const center = (Math.min(cols, rows) - 1) >> 1;
15
- for (let i = 0; i < num; i++) {
16
- let nx = cols;
17
- let ny = rows;
18
- while (i < nx * ny) {
19
- nx--;
20
- ny--;
21
- }
22
- const nxny = nx * ny;
23
- const minnxny = Math.min(nx, ny);
24
- const m2 = minnxny >> 1;
25
- let bx, by;
26
- if (minnxny & 1) {
27
- if (i <= nxny + ny) {
28
- bx = nx - m2;
29
- by = -m2 + i - nxny;
30
- }
31
- else {
32
- bx = nx - m2 - (i - (nxny + ny));
33
- by = ny - m2;
34
- }
35
- }
36
- else {
37
- if (i <= nxny + ny) {
38
- bx = -m2;
39
- by = ny - m2 - (i - nxny);
40
- }
41
- else {
42
- bx = -m2 + (i - (nxny + ny));
43
- by = -m2;
44
- }
45
- }
46
- yield tx(bx + center, by + center);
2
+ function* spiral2d(opts) {
3
+ const { cols, rows, tx } = __opts(opts);
4
+ const num = cols * rows;
5
+ const center = Math.min(cols, rows) - 1 >> 1;
6
+ for (let i = 0; i < num; i++) {
7
+ let nx = cols;
8
+ let ny = rows;
9
+ while (i < nx * ny) {
10
+ nx--;
11
+ ny--;
47
12
  }
13
+ const nxny = nx * ny;
14
+ const minnxny = Math.min(nx, ny);
15
+ const m2 = minnxny >> 1;
16
+ let bx, by;
17
+ if (minnxny & 1) {
18
+ if (i <= nxny + ny) {
19
+ bx = nx - m2;
20
+ by = -m2 + i - nxny;
21
+ } else {
22
+ bx = nx - m2 - (i - (nxny + ny));
23
+ by = ny - m2;
24
+ }
25
+ } else {
26
+ if (i <= nxny + ny) {
27
+ bx = -m2;
28
+ by = ny - m2 - (i - nxny);
29
+ } else {
30
+ bx = -m2 + (i - (nxny + ny));
31
+ by = -m2;
32
+ }
33
+ }
34
+ yield tx(bx + center, by + center);
35
+ }
48
36
  }
37
+ export {
38
+ spiral2d
39
+ };
package/transforms.js CHANGED
@@ -1,23 +1,18 @@
1
- /**
2
- * No-op / identity {@link PointTransform}.
3
- */
4
- export const ident = () => (x, y) => [x, y];
5
- export const flipX = (cols) => (x, y) => [cols - 1 - x, y];
6
- export const flipY = (_, rows) => (x, y) => [x, rows - 1 - y];
7
- export const flipXY = (cols, rows) => (x, y) => [cols - 1 - x, rows - 1 - y];
8
- /**
9
- * {@link PointTransform} to swaps X & Y coords.
10
- */
11
- export const swapXY = () => (x, y) => [y, x];
12
- /**
13
- * Higher order {@link PointTransform} to compose given transforms in
14
- * left-to-right order.
15
- *
16
- * @param a
17
- * @param b
18
- */
19
- export const compTransforms = (a, b) => (cols, rows) => {
20
- const $a = a(cols, rows);
21
- const $b = b(cols, rows);
22
- return (x, y) => $b(...$a(x, y));
1
+ const ident = () => (x, y) => [x, y];
2
+ const flipX = (cols) => (x, y) => [cols - 1 - x, y];
3
+ const flipY = (_, rows) => (x, y) => [x, rows - 1 - y];
4
+ const flipXY = (cols, rows) => (x, y) => [cols - 1 - x, rows - 1 - y];
5
+ const swapXY = () => (x, y) => [y, x];
6
+ const compTransforms = (a, b) => (cols, rows) => {
7
+ const $a = a(cols, rows);
8
+ const $b = b(cols, rows);
9
+ return (x, y) => $b(...$a(x, y));
10
+ };
11
+ export {
12
+ compTransforms,
13
+ flipX,
14
+ flipXY,
15
+ flipY,
16
+ ident,
17
+ swapXY
23
18
  };
package/utils.js CHANGED
@@ -1,8 +1,10 @@
1
- // thing:no-export
2
1
  import { asInt } from "@thi.ng/api/typedarray";
3
2
  import { ident } from "./transforms.js";
4
- export const __opts = (opts) => {
5
- let { cols, rows, tx } = { rows: opts.cols, tx: ident, ...opts };
6
- [cols, rows] = asInt(cols, rows);
7
- return { cols, rows, tx: tx(cols, rows) };
3
+ const __opts = (opts) => {
4
+ let { cols, rows, tx } = { rows: opts.cols, tx: ident, ...opts };
5
+ [cols, rows] = asInt(cols, rows);
6
+ return { cols, rows, tx: tx(cols, rows) };
7
+ };
8
+ export {
9
+ __opts
8
10
  };
package/zcurve.js CHANGED
@@ -1,21 +1,16 @@
1
1
  import { ceilPow2 } from "@thi.ng/binary/pow";
2
2
  import { demux2 } from "@thi.ng/morton/mux";
3
3
  import { __opts } from "./utils.js";
4
- /**
5
- * Yields 2D grid coordinates in Z-curve (Morton) order. A perfect
6
- * Z-curve is only generated if `cols` AND `rows` are equal and a power
7
- * of 2. Due to using 32bit morton codes, only supports grid sizes up to
8
- * 32767 (0x7fff) in either dimension.
9
- *
10
- * @param opts -
11
- */
12
- export function* zcurve2d(opts) {
13
- const { cols, rows, tx } = __opts(opts);
14
- const max = ceilPow2(Math.pow(Math.max(cols, rows), 2));
15
- for (let i = 0; i < max; i++) {
16
- const [x, y] = demux2(i);
17
- if (x < cols && y < rows) {
18
- yield tx(x, y);
19
- }
4
+ function* zcurve2d(opts) {
5
+ const { cols, rows, tx } = __opts(opts);
6
+ const max = ceilPow2(Math.pow(Math.max(cols, rows), 2));
7
+ for (let i = 0; i < max; i++) {
8
+ const [x, y] = demux2(i);
9
+ if (x < cols && y < rows) {
10
+ yield tx(x, y);
20
11
  }
12
+ }
21
13
  }
14
+ export {
15
+ zcurve2d
16
+ };
package/zigzag-columns.js CHANGED
@@ -1,20 +1,14 @@
1
1
  import { __opts } from "./utils.js";
2
- /**
3
- * Yields sequence of 2D grid coordinates in zigzag column order starting from
4
- * [0,0], given `cols` and `rows`.
5
- *
6
- * Ported & modified from original Java code by Christopher Kulla.
7
- * https://sourceforge.net/p/sunflow/code/HEAD/tree/trunk/src/org/sunflow/core/bucket/SpiralBucketOrder.java
8
- *
9
- * @param opts -
10
- */
11
- export function* zigzagColumns2d(opts) {
12
- const { cols, rows, tx } = __opts(opts);
13
- const num = cols * rows;
14
- for (let i = 0; i < num; i++) {
15
- const x = (i / rows) | 0;
16
- let y = i % rows;
17
- x & 1 && (y = rows - 1 - y);
18
- yield tx(x, y);
19
- }
2
+ function* zigzagColumns2d(opts) {
3
+ const { cols, rows, tx } = __opts(opts);
4
+ const num = cols * rows;
5
+ for (let i = 0; i < num; i++) {
6
+ const x = i / rows | 0;
7
+ let y = i % rows;
8
+ x & 1 && (y = rows - 1 - y);
9
+ yield tx(x, y);
10
+ }
20
11
  }
12
+ export {
13
+ zigzagColumns2d
14
+ };
@@ -1,36 +1,31 @@
1
1
  import { __opts } from "./utils.js";
2
- /**
3
- * Similar to {@link diagonal2d}, but yields 2D grid coordinates in zigzag
4
- * diagonal order starting at [0,0] and using given `cols` and `rows`.
5
- *
6
- * @param opts -
7
- */
8
- export function* zigzagDiagonal2d(opts) {
9
- const { cols, rows, tx } = __opts(opts);
10
- const num = cols * rows - 1;
11
- for (let x = 0, y = 0, ny = 0, dx = -1, dy = 1, d = 0, down = true, i = 0; i <= num; i++) {
12
- yield tx(x, y);
13
- if (i !== num) {
14
- do {
15
- if (y === ny) {
16
- if (down) {
17
- y++;
18
- d++;
19
- ny = 0;
20
- }
21
- else {
22
- x++;
23
- ny = ++d;
24
- }
25
- down = !down;
26
- dx *= -1;
27
- dy *= -1;
28
- }
29
- else {
30
- x += dx;
31
- y += dy;
32
- }
33
- } while (x >= cols || y >= rows);
2
+ function* zigzagDiagonal2d(opts) {
3
+ const { cols, rows, tx } = __opts(opts);
4
+ const num = cols * rows - 1;
5
+ for (let x = 0, y = 0, ny = 0, dx = -1, dy = 1, d = 0, down = true, i = 0; i <= num; i++) {
6
+ yield tx(x, y);
7
+ if (i !== num) {
8
+ do {
9
+ if (y === ny) {
10
+ if (down) {
11
+ y++;
12
+ d++;
13
+ ny = 0;
14
+ } else {
15
+ x++;
16
+ ny = ++d;
17
+ }
18
+ down = !down;
19
+ dx *= -1;
20
+ dy *= -1;
21
+ } else {
22
+ x += dx;
23
+ y += dy;
34
24
  }
25
+ } while (x >= cols || y >= rows);
35
26
  }
27
+ }
36
28
  }
29
+ export {
30
+ zigzagDiagonal2d
31
+ };
package/zigzag-rows.js CHANGED
@@ -1,20 +1,14 @@
1
1
  import { __opts } from "./utils.js";
2
- /**
3
- * Yields sequence of 2D grid coordinates in zigzag row order starting from
4
- * [0,0], given `cols` and `rows`.
5
- *
6
- * Ported & modified from original Java code by Christopher Kulla.
7
- * https://sourceforge.net/p/sunflow/code/HEAD/tree/trunk/src/org/sunflow/core/bucket/SpiralBucketOrder.java
8
- *
9
- * @param opts -
10
- */
11
- export function* zigzagRows2d(opts) {
12
- const { cols, rows, tx } = __opts(opts);
13
- const num = cols * rows;
14
- for (let i = 0; i < num; i++) {
15
- let x = i % cols;
16
- const y = (i / cols) | 0;
17
- y & 1 && (x = cols - 1 - x);
18
- yield tx(x, y);
19
- }
2
+ function* zigzagRows2d(opts) {
3
+ const { cols, rows, tx } = __opts(opts);
4
+ const num = cols * rows;
5
+ for (let i = 0; i < num; i++) {
6
+ let x = i % cols;
7
+ const y = i / cols | 0;
8
+ y & 1 && (x = cols - 1 - x);
9
+ yield tx(x, y);
10
+ }
20
11
  }
12
+ export {
13
+ zigzagRows2d
14
+ };