@thi.ng/grid-iterators 2.3.26 → 3.0.1

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/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2022-12-20T16:33:11Z
3
+ - **Last updated**: 2022-12-29T20:56:59Z
4
4
  - **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
5
5
 
6
6
  All notable changes to this project will be documented in this file.
@@ -9,6 +9,31 @@ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelin
9
9
  **Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
10
10
  and/or version bumps of transitive dependencies.
11
11
 
12
+ ### [3.0.1](https://github.com/thi-ng/umbrella/tree/@thi.ng/grid-iterators@3.0.1) (2022-12-29)
13
+
14
+ #### 🩹 Bug fixes
15
+
16
+ - add missing type exports ([879c11c](https://github.com/thi-ng/umbrella/commit/879c11c))
17
+
18
+ #### ♻️ Refactoring
19
+
20
+ - add GridIterator2D/3D type aliases ([610ad0e](https://github.com/thi-ng/umbrella/commit/610ad0e))
21
+
22
+ # [3.0.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/grid-iterators@3.0.0) (2022-12-22)
23
+
24
+ #### 🛑 Breaking changes
25
+
26
+ - add point transforms & global options ([1861154](https://github.com/thi-ng/umbrella/commit/1861154))
27
+ - BREAKING CHANGE: update function signatures, switch to using options object as arg
28
+ - add `GridIterOpts` interface
29
+ - add `PointTransform` and implementations:
30
+ - add flipX/Y/XY, swapXY transforms
31
+ - update most iterators to use new options
32
+
33
+ #### 🩹 Bug fixes
34
+
35
+ - fix imports ([d4cede6](https://github.com/thi-ng/umbrella/commit/d4cede6))
36
+
12
37
  ## [2.3.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/grid-iterators@2.3.0) (2022-04-07)
13
38
 
14
39
  #### 🚀 Features
package/README.md CHANGED
@@ -22,8 +22,9 @@ This project is part of the
22
22
  - [Zigzag columns](#zigzag-columns)
23
23
  - [Zigzag diagonal](#zigzag-diagonal)
24
24
  - [Zigzag rows](#zigzag-rows)
25
+ - [Mirror symmetries & arbitrary coordinate transformations](#mirror-symmetries--arbitrary-coordinate-transformations)
25
26
  - [Flood filling](#flood-filling)
26
- - [Miscellaneous](#miscellaneous)
27
+ - [Shape iterators](#shape-iterators)
27
28
  - [Status](#status)
28
29
  - [Related packages](#related-packages)
29
30
  - [Installation](#installation)
@@ -35,11 +36,11 @@ This project is part of the
35
36
 
36
37
  ## About
37
38
 
38
- 2D grid and shape iterators w/ multiple orderings
39
+ 2D grid and shape iterators w/ multiple orderings.
39
40
 
40
- Provides the altogether 25 following orderings to generate grid coordinates,
41
- including iterators for shape rasterization, drawing, clipping, filling,
42
- processing in general:
41
+ Provides the altogether 25 following orderings (excluding symmetries) to
42
+ generate grid coordinates, including iterators for shape rasterization, drawing,
43
+ clipping, filling, processing in general:
43
44
 
44
45
  ### Columns
45
46
 
@@ -140,6 +141,15 @@ For more basic 2D/3D grid iteration, also see `range2d()` & `range3d()`
140
141
  in
141
142
  [@thi.ng/transducers](https://github.com/thi-ng/umbrella/tree/develop/packages/transducers).
142
143
 
144
+ ### Mirror symmetries & arbitrary coordinate transformations
145
+
146
+ All of the above mentioned grid iterators support point (grid coordinate)
147
+ transformations to create variations of their base orderings. The package
148
+ provides the `flipX`, `flipY` and `flipXY` preset transforms to mirror one or
149
+ both axes, but custom transforms can be easily implemented via the same
150
+ underlying mechanism. These transforms can be specified via the `tx` option
151
+ passed to the iterators (see code example further below).
152
+
143
153
  ### Flood filling
144
154
 
145
155
  The `floodFill()` iterator can be used to iterate arbitrary 2D grids using an
@@ -180,7 +190,7 @@ img
180
190
  // ]
181
191
  ```
182
192
 
183
- ### Miscellaneous
193
+ ### Shape iterators
184
194
 
185
195
  Additionally, the following shape iterators are available, all also with
186
196
  optional clipping:
@@ -222,7 +232,7 @@ For Node.js REPL:
222
232
  const gridIterators = await import("@thi.ng/grid-iterators");
223
233
  ```
224
234
 
225
- Package sizes (brotli'd, pre-treeshake): ESM: 2.19 KB
235
+ Package sizes (brotli'd, pre-treeshake): ESM: 2.31 KB
226
236
 
227
237
  ## Dependencies
228
238
 
@@ -253,7 +263,7 @@ A selection:
253
263
  ```ts
254
264
  import * as gi from "@thi.ng/grid-iterators";
255
265
 
256
- [...gi.zigzagRows2d(4, 4)]
266
+ [...gi.zigzagRows2d({ cols: 4, rows: 4 })]
257
267
 
258
268
  // [
259
269
  // [ 0, 0 ], [ 1, 0 ], [ 2, 0 ], [ 3, 0 ],
@@ -261,6 +271,15 @@ import * as gi from "@thi.ng/grid-iterators";
261
271
  // [ 0, 2 ], [ 1, 2 ], [ 2, 2 ], [ 3, 2 ],
262
272
  // [ 3, 3 ], [ 2, 3 ], [ 1, 3 ], [ 0, 3 ]
263
273
  // ]
274
+
275
+ // with applied horizontal mirroring
276
+ [...gi.zigzagRows2d({ cols: 4, tx: gi.flipX })]
277
+ // [
278
+ // [ 3, 0 ], [ 2, 0 ], [ 1, 0 ], [ 0, 0 ],
279
+ // [ 0, 1 ], [ 1, 1 ], [ 2, 1 ], [ 3, 1 ],
280
+ // [ 3, 2 ], [ 2, 2 ], [ 1, 2 ], [ 0, 2 ],
281
+ // [ 0, 3 ], [ 1, 3 ], [ 2, 3 ], [ 3, 3 ]
282
+ // ]
264
283
  ```
265
284
 
266
285
  ## Authors
package/api.d.ts ADDED
@@ -0,0 +1,27 @@
1
+ import type { Fn, FnU2 } from "@thi.ng/api";
2
+ /**
3
+ * Higher order point coordinate transformation function. First is called with
4
+ * grid resolution (cols,rows), then returns a function which is applied to each
5
+ * generated grid coordinate.
6
+ */
7
+ export type PointTransform = FnU2<number, FnU2<number, [number, number]>>;
8
+ export interface GridIterOpts {
9
+ /**
10
+ * Number of grid columns
11
+ */
12
+ cols: number;
13
+ /**
14
+ * Number of grid rows, by default same as columns.
15
+ */
16
+ rows?: number;
17
+ /**
18
+ * Point coordinate transformation function, e.g. to mirror iteration order
19
+ * along X or Y. See {@link flipX}, {@link flipY}, {@link flipXY} etc.
20
+ *
21
+ * @defaultValue {@link ident}
22
+ */
23
+ tx?: PointTransform;
24
+ }
25
+ export type GridIterator2D = Fn<GridIterOpts, Iterable<[number, number]>>;
26
+ export type GridIterator3D = Fn<GridIterOpts, Iterable<[number, number, number]>>;
27
+ //# sourceMappingURL=api.d.ts.map
package/api.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/column-ends.d.ts CHANGED
@@ -1,9 +1,9 @@
1
+ import type { GridIterOpts } from "./api.js";
1
2
  /**
2
3
  * Filtered version of {@link columns2d}, only including end points of
3
4
  * each column.
4
5
  *
5
- * @param cols -
6
- * @param rows -
6
+ * @param opts -
7
7
  */
8
- export declare function columnEnds2d(cols: number, rows?: number): Generator<number[], void, unknown>;
8
+ export declare function columnEnds2d(opts: GridIterOpts): Generator<[number, number], void, unknown>;
9
9
  //# sourceMappingURL=column-ends.d.ts.map
package/column-ends.js CHANGED
@@ -1,16 +1,15 @@
1
- import { asInt } from "@thi.ng/api/typedarray";
1
+ import { __opts } from "./utils.js";
2
2
  /**
3
3
  * Filtered version of {@link columns2d}, only including end points of
4
4
  * each column.
5
5
  *
6
- * @param cols -
7
- * @param rows -
6
+ * @param opts -
8
7
  */
9
- export function* columnEnds2d(cols, rows = cols) {
10
- [cols, rows] = asInt(cols, rows);
8
+ export function* columnEnds2d(opts) {
9
+ let { cols, rows, tx } = __opts(opts);
11
10
  rows--;
12
11
  for (let x = 0; x < cols; x++) {
13
- yield [x, 0];
14
- yield [x, rows];
12
+ yield tx(x, 0);
13
+ yield tx(x, rows);
15
14
  }
16
15
  }
package/columns.d.ts CHANGED
@@ -1,8 +1,8 @@
1
+ import type { GridIterOpts } from "./api.js";
1
2
  /**
2
3
  * Yields sequence of 2D grid coordinates in column-major order.
3
4
  *
4
- * @param cols -
5
- * @param rows -
5
+ * @param opts -
6
6
  */
7
- export declare const columns2d: (cols: number, rows?: number) => IterableIterator<number[]>;
7
+ export declare const columns2d: (opts: GridIterOpts) => IterableIterator<[number, number]>;
8
8
  //# sourceMappingURL=columns.d.ts.map
package/columns.js CHANGED
@@ -1,10 +1,12 @@
1
- import { range2d } from "@thi.ng/transducers/range2d";
2
1
  import { map } from "@thi.ng/transducers/map";
3
- import { swapxy } from "./utils.js";
2
+ import { range2d } from "@thi.ng/transducers/range2d";
3
+ import { __opts } from "./utils.js";
4
4
  /**
5
5
  * Yields sequence of 2D grid coordinates in column-major order.
6
6
  *
7
- * @param cols -
8
- * @param rows -
7
+ * @param opts -
9
8
  */
10
- export const columns2d = (cols, rows = cols) => map(swapxy, range2d(rows | 0, cols | 0));
9
+ export const columns2d = (opts) => {
10
+ const { cols, rows, tx } = __opts(opts);
11
+ return map((p) => tx(p[1], p[0]), range2d(rows | 0, cols | 0));
12
+ };
@@ -1,3 +1,4 @@
1
+ import type { GridIterOpts } from "./api.js";
1
2
  /**
2
3
  * Filtered version of {@link diagonal2d}, only including end points of
3
4
  * the diagonals, apart from the very first and last points: `[0,0]` and
@@ -6,8 +7,7 @@
6
7
  * @remarks
7
8
  * `cols` and `rows` MUST be both >= 2.
8
9
  *
9
- * @param cols -
10
- * @param rows -
10
+ * @param opts -
11
11
  */
12
- export declare function diagonalEnds2d(cols: number, rows?: number): Generator<number[], void, unknown>;
12
+ export declare function diagonalEnds2d(opts: GridIterOpts): Generator<[number, number], void, unknown>;
13
13
  //# sourceMappingURL=diagonal-ends.d.ts.map
package/diagonal-ends.js CHANGED
@@ -1,5 +1,5 @@
1
- import { asInt } from "@thi.ng/api/typedarray";
2
1
  import { diagonal2d } from "./diagonal.js";
2
+ import { __opts } from "./utils.js";
3
3
  /**
4
4
  * Filtered version of {@link diagonal2d}, only including end points of
5
5
  * the diagonals, apart from the very first and last points: `[0,0]` and
@@ -8,20 +8,19 @@ import { diagonal2d } from "./diagonal.js";
8
8
  * @remarks
9
9
  * `cols` and `rows` MUST be both >= 2.
10
10
  *
11
- * @param cols -
12
- * @param rows -
11
+ * @param opts -
13
12
  */
14
- export function* diagonalEnds2d(cols, rows = cols) {
15
- [cols, rows] = asInt(cols, rows);
13
+ export function* diagonalEnds2d(opts) {
14
+ const { cols, rows, tx } = __opts(opts);
16
15
  const num = cols * rows - 1;
17
16
  const maxX = cols - 1;
18
17
  const maxY = rows - 1;
19
18
  let i = 0;
20
- for (let p of diagonal2d(cols, rows)) {
19
+ for (let p of diagonal2d({ cols, rows })) {
21
20
  if (i > 0 && i < num) {
22
21
  const [x, y] = p;
23
22
  if (x === 0 || x === maxX || y === 0 || y === maxY)
24
- yield p;
23
+ yield tx(x, y);
25
24
  }
26
25
  i++;
27
26
  }
package/diagonal.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import type { GridIterOpts } from "./api.js";
1
2
  /**
2
3
  * Yields sequence of 2D grid coordinates in diagonal order starting at [0,0]
3
4
  * and using given `cols` and `rows`. Each diagonal starts at y=0 and progresses
@@ -6,8 +7,7 @@
6
7
  * Ported & modified from original Java code by Christopher Kulla.
7
8
  * https://sourceforge.net/p/sunflow/code/HEAD/tree/trunk/src/org/sunflow/core/bucket/DiagonalBucketOrder.java
8
9
  *
9
- * @param cols -
10
- * @param rows -
10
+ * @param opts -
11
11
  */
12
- export declare function diagonal2d(cols: number, rows?: number): Generator<number[], void, unknown>;
12
+ export declare function diagonal2d(opts: GridIterOpts): Generator<[number, number], void, unknown>;
13
13
  //# sourceMappingURL=diagonal.d.ts.map
package/diagonal.js CHANGED
@@ -1,4 +1,4 @@
1
- import { asInt } from "@thi.ng/api/typedarray";
1
+ import { __opts } from "./utils.js";
2
2
  /**
3
3
  * Yields sequence of 2D grid coordinates in diagonal order starting at [0,0]
4
4
  * and using given `cols` and `rows`. Each diagonal starts at y=0 and progresses
@@ -7,14 +7,13 @@ import { asInt } from "@thi.ng/api/typedarray";
7
7
  * Ported & modified from original Java code by Christopher Kulla.
8
8
  * https://sourceforge.net/p/sunflow/code/HEAD/tree/trunk/src/org/sunflow/core/bucket/DiagonalBucketOrder.java
9
9
  *
10
- * @param cols -
11
- * @param rows -
10
+ * @param opts -
12
11
  */
13
- export function* diagonal2d(cols, rows = cols) {
14
- [cols, rows] = asInt(cols, rows);
12
+ export function* diagonal2d(opts) {
13
+ const { cols, rows, tx } = __opts(opts);
15
14
  const num = cols * rows - 1;
16
15
  for (let x = 0, y = 0, nx = 1, ny = 0, i = 0; i <= num; i++) {
17
- yield [x, y];
16
+ yield tx(x, y);
18
17
  if (i != num) {
19
18
  do {
20
19
  if (y === ny) {
package/hilbert.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import type { GridIterOpts } from "./api.js";
1
2
  /**
2
3
  * Yields sequence of 2D grid coordinates along 2D Hilbert curve using given
3
4
  * `cols` and `rows` (each max. 32768 (2^15)).
@@ -5,8 +6,7 @@
5
6
  * Ported & modified from original Java code by Christopher Kulla.
6
7
  * https://sourceforge.net/p/sunflow/code/HEAD/tree/trunk/src/org/sunflow/core/bucket/HilbertBucketOrder.java
7
8
  *
8
- * @param cols -
9
- * @param rows -
9
+ * @param opts -
10
10
  */
11
- export declare function hilbert2d(cols: number, rows?: number): Generator<number[], void, unknown>;
11
+ export declare function hilbert2d(opts: GridIterOpts): Generator<[number, number], void, unknown>;
12
12
  //# sourceMappingURL=hilbert.d.ts.map
package/hilbert.js CHANGED
@@ -1,4 +1,4 @@
1
- import { asInt } from "@thi.ng/api/typedarray";
1
+ import { __opts } from "./utils.js";
2
2
  /**
3
3
  * Yields sequence of 2D grid coordinates along 2D Hilbert curve using given
4
4
  * `cols` and `rows` (each max. 32768 (2^15)).
@@ -6,11 +6,10 @@ import { asInt } from "@thi.ng/api/typedarray";
6
6
  * Ported & modified from original Java code by Christopher Kulla.
7
7
  * https://sourceforge.net/p/sunflow/code/HEAD/tree/trunk/src/org/sunflow/core/bucket/HilbertBucketOrder.java
8
8
  *
9
- * @param cols -
10
- * @param rows -
9
+ * @param opts -
11
10
  */
12
- export function* hilbert2d(cols, rows = cols) {
13
- [cols, rows] = asInt(cols, rows);
11
+ export function* hilbert2d(opts) {
12
+ const { cols, rows, tx } = __opts(opts);
14
13
  let hIndex = 0; // hilbert curve index
15
14
  let hOrder = 0; // hilbert curve order
16
15
  // fit to number of buckets
@@ -61,6 +60,6 @@ export function* hilbert2d(cols, rows = cols) {
61
60
  // Dont't emit any outside cells
62
61
  (hx >= cols || hy >= rows || hx < 0 || hy < 0) &&
63
62
  hIndex < numBuckets);
64
- yield [hx, hy];
63
+ yield tx(hx, hy);
65
64
  }
66
65
  }
package/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ export * from "./api.js";
1
2
  export * from "./circle.js";
2
3
  export * from "./clipping.js";
3
4
  export * from "./column-ends.js";
@@ -14,6 +15,7 @@ export * from "./random.js";
14
15
  export * from "./row-ends.js";
15
16
  export * from "./rows.js";
16
17
  export * from "./spiral.js";
18
+ export * from "./transforms.js";
17
19
  export * from "./zcurve.js";
18
20
  export * from "./zigzag-columns.js";
19
21
  export * from "./zigzag-diagonal.js";
package/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ export * from "./api.js";
1
2
  export * from "./circle.js";
2
3
  export * from "./clipping.js";
3
4
  export * from "./column-ends.js";
@@ -14,6 +15,7 @@ export * from "./random.js";
14
15
  export * from "./row-ends.js";
15
16
  export * from "./rows.js";
16
17
  export * from "./spiral.js";
18
+ export * from "./transforms.js";
17
19
  export * from "./zcurve.js";
18
20
  export * from "./zigzag-columns.js";
19
21
  export * from "./zigzag-diagonal.js";
package/interleave.d.ts CHANGED
@@ -1,33 +1,42 @@
1
+ import type { GridIterOpts } from "./api.js";
2
+ interface InterleaveOpts extends GridIterOpts {
3
+ /**
4
+ * Row or column stride.
5
+ *
6
+ * @defaultValue 2
7
+ */
8
+ step?: number;
9
+ }
1
10
  /**
2
11
  * Yields 2D grid coordinates in the order of interleaved columns with
3
- * configurable `step` size (default: 2). I.e. returns columns in this
4
- * order:
12
+ * configurable `step` size (default: 2).
13
+ *
14
+ * @remarks
15
+ * Returns columns in this order:
5
16
  *
6
17
  * - 0, step, 2 * step, 3 * step...
7
18
  * - 1, 2 * step + 1, 3 * step + 1...
8
19
  * - etc.
9
20
  *
10
- * {@link interleaveRows2d}
21
+ * Also see {@link interleaveRows2d}.
11
22
  *
12
- * @param cols -
13
- * @param rows -
14
- * @param step - column stride
23
+ * @param opts -
15
24
  */
16
- export declare function interleaveColumns2d(cols: number, rows?: number, step?: number): Generator<number[], void, undefined>;
25
+ export declare function interleaveColumns2d(opts: InterleaveOpts): Generator<[number, number], void, undefined>;
17
26
  /**
18
27
  * Similar to {@link interleaveColumns2d}, but yields 2D grid coordinates in
19
28
  * the order of interleaved rows with configurable `step` size (default:
20
- * 2). I.e. returns rows in this order:
29
+ * 2).
30
+ *
31
+ * @remarks
32
+ * Returns rows in this order:
21
33
  *
22
34
  * - 0, step, 2 * step, 3 * step...
23
35
  * - 1, 2 * step + 1, 3 * step + 1...
24
36
  * - etc.
25
37
  *
26
- * {@link interleaveColumns2d}
27
- *
28
- * @param cols -
29
- * @param rows -
30
- * @param step - row stride
38
+ * @param opts -
31
39
  */
32
- export declare function interleaveRows2d(cols: number, rows?: number, step?: number): Generator<[number, number], void, undefined>;
40
+ export declare function interleaveRows2d(opts: InterleaveOpts): Generator<[number, number], void, undefined>;
41
+ export {};
33
42
  //# sourceMappingURL=interleave.d.ts.map
package/interleave.js CHANGED
@@ -1,43 +1,46 @@
1
- import { range2d } from "@thi.ng/transducers/range2d";
2
1
  import { map } from "@thi.ng/transducers/map";
3
- import { swapxy } from "./utils.js";
2
+ import { range2d } from "@thi.ng/transducers/range2d";
3
+ import { __opts } from "./utils.js";
4
4
  /**
5
5
  * Yields 2D grid coordinates in the order of interleaved columns with
6
- * configurable `step` size (default: 2). I.e. returns columns in this
7
- * order:
6
+ * configurable `step` size (default: 2).
7
+ *
8
+ * @remarks
9
+ * Returns columns in this order:
8
10
  *
9
11
  * - 0, step, 2 * step, 3 * step...
10
12
  * - 1, 2 * step + 1, 3 * step + 1...
11
13
  * - etc.
12
14
  *
13
- * {@link interleaveRows2d}
15
+ * Also see {@link interleaveRows2d}.
14
16
  *
15
- * @param cols -
16
- * @param rows -
17
- * @param step - column stride
17
+ * @param opts -
18
18
  */
19
- export function* interleaveColumns2d(cols, rows = cols, step = 2) {
19
+ export function* interleaveColumns2d(opts) {
20
+ const { cols, rows, tx } = __opts(opts);
21
+ const step = (opts.step != null ? opts.step : 2) | 0;
20
22
  for (let j = 0; j < step; j++) {
21
- yield* map(swapxy, range2d(0, rows | 0, j, cols | 0, 1, step | 0));
23
+ yield* map((p) => tx(p[1], p[0]), range2d(0, rows, j, cols, 1, step));
22
24
  }
23
25
  }
24
26
  /**
25
27
  * Similar to {@link interleaveColumns2d}, but yields 2D grid coordinates in
26
28
  * the order of interleaved rows with configurable `step` size (default:
27
- * 2). I.e. returns rows in this order:
29
+ * 2).
30
+ *
31
+ * @remarks
32
+ * Returns rows in this order:
28
33
  *
29
34
  * - 0, step, 2 * step, 3 * step...
30
35
  * - 1, 2 * step + 1, 3 * step + 1...
31
36
  * - etc.
32
37
  *
33
- * {@link interleaveColumns2d}
34
- *
35
- * @param cols -
36
- * @param rows -
37
- * @param step - row stride
38
+ * @param opts -
38
39
  */
39
- export function* interleaveRows2d(cols, rows = cols, step = 2) {
40
+ export function* interleaveRows2d(opts) {
41
+ const { cols, rows, tx } = __opts(opts);
42
+ const step = (opts.step != null ? opts.step : 2) | 0;
40
43
  for (let j = 0; j < step; j++) {
41
- yield* range2d(0, cols | 0, j, rows | 0, 1, step | 0);
44
+ yield* map((p) => tx(p[0], p[1]), range2d(0, cols, j, rows, 1, step));
42
45
  }
43
46
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/grid-iterators",
3
- "version": "2.3.26",
3
+ "version": "3.0.1",
4
4
  "description": "2D grid and shape iterators w/ multiple orderings",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -35,17 +35,17 @@
35
35
  "test": "testament test"
36
36
  },
37
37
  "dependencies": {
38
- "@thi.ng/api": "^8.6.1",
39
- "@thi.ng/arrays": "^2.4.6",
40
- "@thi.ng/binary": "^3.3.14",
41
- "@thi.ng/bitfield": "^2.2.16",
42
- "@thi.ng/morton": "^3.1.25",
43
- "@thi.ng/random": "^3.3.19",
44
- "@thi.ng/transducers": "^8.3.27"
38
+ "@thi.ng/api": "^8.6.2",
39
+ "@thi.ng/arrays": "^2.5.0",
40
+ "@thi.ng/binary": "^3.3.15",
41
+ "@thi.ng/bitfield": "^2.2.17",
42
+ "@thi.ng/morton": "^3.1.26",
43
+ "@thi.ng/random": "^3.3.20",
44
+ "@thi.ng/transducers": "^8.3.29"
45
45
  },
46
46
  "devDependencies": {
47
47
  "@microsoft/api-extractor": "^7.33.7",
48
- "@thi.ng/testament": "^0.3.7",
48
+ "@thi.ng/testament": "^0.3.8",
49
49
  "rimraf": "^3.0.2",
50
50
  "tools": "^0.0.1",
51
51
  "typedoc": "^0.23.22",
@@ -83,6 +83,9 @@
83
83
  ".": {
84
84
  "default": "./index.js"
85
85
  },
86
+ "./api": {
87
+ "default": "./api.js"
88
+ },
86
89
  "./circle": {
87
90
  "default": "./circle.js"
88
91
  },
@@ -131,6 +134,9 @@
131
134
  "./spiral": {
132
135
  "default": "./spiral.js"
133
136
  },
137
+ "./transforms": {
138
+ "default": "./transforms.js"
139
+ },
134
140
  "./zcurve": {
135
141
  "default": "./zcurve.js"
136
142
  },
@@ -152,5 +158,5 @@
152
158
  ],
153
159
  "year": 2019
154
160
  },
155
- "gitHead": "7b2af448da8a63fb21704a79cc4cdf1f3d7d7a64\n"
161
+ "gitHead": "28bb74c67217a352d673b6efdab234921d4a370e\n"
156
162
  }
package/random.d.ts CHANGED
@@ -1,4 +1,13 @@
1
1
  import type { IRandom } from "@thi.ng/random";
2
+ import type { GridIterOpts } from "./api.js";
3
+ interface Random2DOpts extends GridIterOpts {
4
+ /**
5
+ * PRNG instance to use
6
+ *
7
+ * @defaultValue `SYSTEM`
8
+ */
9
+ rnd?: IRandom;
10
+ }
2
11
  /**
3
12
  * Yields 2D grid coordinates in random order w/ support for optional
4
13
  * [`IRandom`](https://docs.thi.ng/umbrella/random/interfaces/IRandom.html)
@@ -6,9 +15,8 @@ import type { IRandom } from "@thi.ng/random";
6
15
  * [`SYSTEM`](https://docs.thi.ng/umbrella/random/variables/SYSTEM.html) aka
7
16
  * `Math.random`).
8
17
  *
9
- * @param cols -
10
- * @param rows -
11
- * @param rnd - PRNG
18
+ * @param opts -
12
19
  */
13
- export declare function random2d(cols: number, rows?: number, rnd?: IRandom): Generator<number[], void, unknown>;
20
+ export declare function random2d(opts: Random2DOpts): Generator<[number, number], void, unknown>;
21
+ export {};
14
22
  //# sourceMappingURL=random.d.ts.map
package/random.js CHANGED
@@ -1,7 +1,7 @@
1
- import { asInt } from "@thi.ng/api/typedarray";
2
1
  import { shuffle } from "@thi.ng/arrays/shuffle";
3
2
  import { SYSTEM } from "@thi.ng/random/system";
4
3
  import { range } from "@thi.ng/transducers/range";
4
+ import { __opts } from "./utils.js";
5
5
  /**
6
6
  * Yields 2D grid coordinates in random order w/ support for optional
7
7
  * [`IRandom`](https://docs.thi.ng/umbrella/random/interfaces/IRandom.html)
@@ -9,13 +9,12 @@ import { range } from "@thi.ng/transducers/range";
9
9
  * [`SYSTEM`](https://docs.thi.ng/umbrella/random/variables/SYSTEM.html) aka
10
10
  * `Math.random`).
11
11
  *
12
- * @param cols -
13
- * @param rows -
14
- * @param rnd - PRNG
12
+ * @param opts -
15
13
  */
16
- export function* random2d(cols, rows = cols, rnd = SYSTEM) {
17
- [cols, rows] = asInt(cols, rows);
14
+ export function* random2d(opts) {
15
+ const { cols, rows, tx } = __opts(opts);
16
+ const rnd = opts.rnd || SYSTEM;
18
17
  for (let i of shuffle([...range(cols * rows)], undefined, rnd)) {
19
- yield [i % cols, (i / cols) | 0];
18
+ yield tx(i % cols, (i / cols) | 0);
20
19
  }
21
20
  }
package/row-ends.d.ts CHANGED
@@ -1,9 +1,9 @@
1
+ import type { GridIterOpts } from "./api.js";
1
2
  /**
2
3
  * Filtered version of {@link rows2d}, only including end points of
3
4
  * each row.
4
5
  *
5
- * @param cols -
6
- * @param rows -
6
+ * @param opts -
7
7
  */
8
- export declare function rowEnds2d(cols: number, rows?: number): Generator<number[], void, unknown>;
8
+ export declare function rowEnds2d(opts: GridIterOpts): Generator<[number, number], void, unknown>;
9
9
  //# sourceMappingURL=row-ends.d.ts.map
package/row-ends.js CHANGED
@@ -1,16 +1,15 @@
1
- import { asInt } from "@thi.ng/api/typedarray";
1
+ import { __opts } from "./utils.js";
2
2
  /**
3
3
  * Filtered version of {@link rows2d}, only including end points of
4
4
  * each row.
5
5
  *
6
- * @param cols -
7
- * @param rows -
6
+ * @param opts -
8
7
  */
9
- export function* rowEnds2d(cols, rows = cols) {
10
- [cols, rows] = asInt(cols, rows);
8
+ export function* rowEnds2d(opts) {
9
+ let { cols, rows, tx } = __opts(opts);
11
10
  cols--;
12
11
  for (let y = 0; y < rows; y++) {
13
- yield [0, y];
14
- yield [cols, y];
12
+ yield tx(0, y);
13
+ yield tx(cols, y);
15
14
  }
16
15
  }
package/rows.d.ts CHANGED
@@ -1,9 +1,9 @@
1
+ import type { GridIterOpts } from "./api.js";
1
2
  /**
2
3
  * Yields sequence of 2D grid coordinates in row-major order. Same as
3
4
  * [`range2d()`](https://docs.thi.ng/umbrella/transducers/functions/range2d.html).
4
5
  *
5
- * @param cols -
6
- * @param rows -
6
+ * @param opts -
7
7
  */
8
- export declare const rows2d: (cols: number, rows?: number) => IterableIterator<[number, number]>;
8
+ export declare const rows2d: (opts: GridIterOpts) => IterableIterator<[number, number]>;
9
9
  //# sourceMappingURL=rows.d.ts.map
package/rows.js CHANGED
@@ -1,9 +1,13 @@
1
+ import { map } from "@thi.ng/transducers/map";
1
2
  import { range2d } from "@thi.ng/transducers/range2d";
3
+ import { __opts } from "./utils.js";
2
4
  /**
3
5
  * Yields sequence of 2D grid coordinates in row-major order. Same as
4
6
  * [`range2d()`](https://docs.thi.ng/umbrella/transducers/functions/range2d.html).
5
7
  *
6
- * @param cols -
7
- * @param rows -
8
+ * @param opts -
8
9
  */
9
- export const rows2d = (cols, rows = cols) => range2d(cols | 0, rows | 0);
10
+ export const rows2d = (opts) => {
11
+ const { cols, rows, tx } = __opts(opts);
12
+ return map((p) => tx(p[0], p[1]), range2d(cols, rows));
13
+ };
package/spiral.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import type { GridIterOpts } from "./api.js";
1
2
  /**
2
3
  * Yields sequence of 2D grid coordinates in outward spiral order starting from
3
4
  * the center, given `cols` and `rows`.
@@ -5,8 +6,7 @@
5
6
  * Ported & modified from original Java code by Christopher Kulla.
6
7
  * https://sourceforge.net/p/sunflow/code/HEAD/tree/trunk/src/org/sunflow/core/bucket/SpiralBucketOrder.java
7
8
  *
8
- * @param cols -
9
- * @param rows -
9
+ * @param opts -
10
10
  */
11
- export declare function spiral2d(cols: number, rows?: number): Generator<number[], void, unknown>;
11
+ export declare function spiral2d(opts: GridIterOpts): Generator<[number, number], void, unknown>;
12
12
  //# sourceMappingURL=spiral.d.ts.map
package/spiral.js CHANGED
@@ -1,4 +1,4 @@
1
- import { asInt } from "@thi.ng/api/typedarray";
1
+ import { __opts } from "./utils.js";
2
2
  /**
3
3
  * Yields sequence of 2D grid coordinates in outward spiral order starting from
4
4
  * the center, given `cols` and `rows`.
@@ -6,11 +6,10 @@ import { asInt } from "@thi.ng/api/typedarray";
6
6
  * Ported & modified from original Java code by Christopher Kulla.
7
7
  * https://sourceforge.net/p/sunflow/code/HEAD/tree/trunk/src/org/sunflow/core/bucket/SpiralBucketOrder.java
8
8
  *
9
- * @param cols -
10
- * @param rows -
9
+ * @param opts -
11
10
  */
12
- export function* spiral2d(cols, rows = cols) {
13
- [cols, rows] = asInt(cols, rows);
11
+ export function* spiral2d(opts) {
12
+ const { cols, rows, tx } = __opts(opts);
14
13
  const num = cols * rows;
15
14
  const center = (Math.min(cols, rows) - 1) >> 1;
16
15
  for (let i = 0; i < num; i++) {
@@ -44,6 +43,6 @@ export function* spiral2d(cols, rows = cols) {
44
43
  by = -m2;
45
44
  }
46
45
  }
47
- yield [bx + center, by + center];
46
+ yield tx(bx + center, by + center);
48
47
  }
49
48
  }
@@ -0,0 +1,21 @@
1
+ import type { PointTransform } from "./api.js";
2
+ /**
3
+ * No-op / identity {@link PointTransform}.
4
+ */
5
+ export declare const ident: PointTransform;
6
+ export declare const flipX: PointTransform;
7
+ export declare const flipY: PointTransform;
8
+ export declare const flipXY: PointTransform;
9
+ /**
10
+ * {@link PointTransform} to swaps X & Y coords.
11
+ */
12
+ export declare const swapXY: PointTransform;
13
+ /**
14
+ * Higher order {@link PointTransform} to compose given transforms in
15
+ * left-to-right order.
16
+ *
17
+ * @param a
18
+ * @param b
19
+ */
20
+ export declare const compTransforms: (a: PointTransform, b: PointTransform) => PointTransform;
21
+ //# sourceMappingURL=transforms.d.ts.map
package/transforms.js ADDED
@@ -0,0 +1,23 @@
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));
23
+ };
package/utils.d.ts CHANGED
@@ -1,9 +1,7 @@
1
- /**
2
- * Swaps XY in-place.
3
- *
4
- * @param p -
5
- *
6
- * @internal
7
- */
8
- export declare const swapxy: (p: number[]) => number[];
1
+ import type { GridIterOpts } from "./api.js";
2
+ export declare const __opts: (opts: GridIterOpts) => {
3
+ cols: number;
4
+ rows: number;
5
+ tx: import("@thi.ng/api").FnU2<number, [number, number]>;
6
+ };
9
7
  //# sourceMappingURL=utils.d.ts.map
package/utils.js CHANGED
@@ -1,14 +1,8 @@
1
1
  // thing:no-export
2
- /**
3
- * Swaps XY in-place.
4
- *
5
- * @param p -
6
- *
7
- * @internal
8
- */
9
- export const swapxy = (p) => {
10
- const t = p[0];
11
- p[0] = p[1];
12
- p[1] = t;
13
- return p;
2
+ import { asInt } from "@thi.ng/api/typedarray";
3
+ 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) };
14
8
  };
package/zcurve.d.ts CHANGED
@@ -1,11 +1,11 @@
1
+ import type { GridIterOpts } from "./api.js";
1
2
  /**
2
3
  * Yields 2D grid coordinates in Z-curve (Morton) order. A perfect
3
4
  * Z-curve is only generated if `cols` AND `rows` are equal and a power
4
5
  * of 2. Due to using 32bit morton codes, only supports grid sizes up to
5
6
  * 32767 (0x7fff) in either dimension.
6
7
  *
7
- * @param cols -
8
- * @param rows -
8
+ * @param opts -
9
9
  */
10
- export declare function zcurve2d(cols: number, rows?: number): Generator<number[], void, unknown>;
10
+ export declare function zcurve2d(opts: GridIterOpts): Generator<[number, number], void, unknown>;
11
11
  //# sourceMappingURL=zcurve.d.ts.map
package/zcurve.js CHANGED
@@ -1,22 +1,21 @@
1
- import { asInt } from "@thi.ng/api/typedarray";
2
1
  import { ceilPow2 } from "@thi.ng/binary/pow";
3
2
  import { demux2 } from "@thi.ng/morton/mux";
3
+ import { __opts } from "./utils.js";
4
4
  /**
5
5
  * Yields 2D grid coordinates in Z-curve (Morton) order. A perfect
6
6
  * Z-curve is only generated if `cols` AND `rows` are equal and a power
7
7
  * of 2. Due to using 32bit morton codes, only supports grid sizes up to
8
8
  * 32767 (0x7fff) in either dimension.
9
9
  *
10
- * @param cols -
11
- * @param rows -
10
+ * @param opts -
12
11
  */
13
- export function* zcurve2d(cols, rows = cols) {
14
- [cols, rows] = asInt(cols, rows);
12
+ export function* zcurve2d(opts) {
13
+ const { cols, rows, tx } = __opts(opts);
15
14
  const max = ceilPow2(Math.pow(Math.max(cols, rows), 2));
16
15
  for (let i = 0; i < max; i++) {
17
- const p = demux2(i);
18
- if (p[0] < cols && p[1] < rows) {
19
- yield p;
16
+ const [x, y] = demux2(i);
17
+ if (x < cols && y < rows) {
18
+ yield tx(x, y);
20
19
  }
21
20
  }
22
21
  }
@@ -1,3 +1,4 @@
1
+ import type { GridIterOpts } from "./api.js";
1
2
  /**
2
3
  * Yields sequence of 2D grid coordinates in zigzag column order starting from
3
4
  * [0,0], given `cols` and `rows`.
@@ -5,9 +6,7 @@
5
6
  * Ported & modified from original Java code by Christopher Kulla.
6
7
  * https://sourceforge.net/p/sunflow/code/HEAD/tree/trunk/src/org/sunflow/core/bucket/SpiralBucketOrder.java
7
8
  *
8
- * @param cols -
9
- * @param rows -
10
- *
9
+ * @param opts -
11
10
  */
12
- export declare function zigzagColumns2d(cols: number, rows?: number): Generator<number[], void, unknown>;
11
+ export declare function zigzagColumns2d(opts: GridIterOpts): Generator<[number, number], void, unknown>;
13
12
  //# sourceMappingURL=zigzag-columns.d.ts.map
package/zigzag-columns.js CHANGED
@@ -1,4 +1,4 @@
1
- import { asInt } from "@thi.ng/api/typedarray";
1
+ import { __opts } from "./utils.js";
2
2
  /**
3
3
  * Yields sequence of 2D grid coordinates in zigzag column order starting from
4
4
  * [0,0], given `cols` and `rows`.
@@ -6,17 +6,15 @@ import { asInt } from "@thi.ng/api/typedarray";
6
6
  * Ported & modified from original Java code by Christopher Kulla.
7
7
  * https://sourceforge.net/p/sunflow/code/HEAD/tree/trunk/src/org/sunflow/core/bucket/SpiralBucketOrder.java
8
8
  *
9
- * @param cols -
10
- * @param rows -
11
- *
9
+ * @param opts -
12
10
  */
13
- export function* zigzagColumns2d(cols, rows = cols) {
14
- [cols, rows] = asInt(cols, rows);
11
+ export function* zigzagColumns2d(opts) {
12
+ const { cols, rows, tx } = __opts(opts);
15
13
  const num = cols * rows;
16
14
  for (let i = 0; i < num; i++) {
17
15
  const x = (i / rows) | 0;
18
16
  let y = i % rows;
19
17
  x & 1 && (y = rows - 1 - y);
20
- yield [x, y];
18
+ yield tx(x, y);
21
19
  }
22
20
  }
@@ -1,9 +1,9 @@
1
+ import type { GridIterOpts } from "./api.js";
1
2
  /**
2
3
  * Similar to {@link diagonal2d}, but yields 2D grid coordinates in zigzag
3
4
  * diagonal order starting at [0,0] and using given `cols` and `rows`.
4
5
  *
5
- * @param cols -
6
- * @param rows -
6
+ * @param opts -
7
7
  */
8
- export declare function zigzagDiagonal2d(cols: number, rows?: number): Generator<number[], void, unknown>;
8
+ export declare function zigzagDiagonal2d(opts: GridIterOpts): Generator<[number, number], void, unknown>;
9
9
  //# sourceMappingURL=zigzag-diagonal.d.ts.map
@@ -1,16 +1,15 @@
1
- import { asInt } from "@thi.ng/api/typedarray";
1
+ import { __opts } from "./utils.js";
2
2
  /**
3
3
  * Similar to {@link diagonal2d}, but yields 2D grid coordinates in zigzag
4
4
  * diagonal order starting at [0,0] and using given `cols` and `rows`.
5
5
  *
6
- * @param cols -
7
- * @param rows -
6
+ * @param opts -
8
7
  */
9
- export function* zigzagDiagonal2d(cols, rows = cols) {
10
- [cols, rows] = asInt(cols, rows);
8
+ export function* zigzagDiagonal2d(opts) {
9
+ const { cols, rows, tx } = __opts(opts);
11
10
  const num = cols * rows - 1;
12
11
  for (let x = 0, y = 0, ny = 0, dx = -1, dy = 1, d = 0, down = true, i = 0; i <= num; i++) {
13
- yield [x, y];
12
+ yield tx(x, y);
14
13
  if (i !== num) {
15
14
  do {
16
15
  if (y === ny) {
package/zigzag-rows.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import type { GridIterOpts } from "./api.js";
1
2
  /**
2
3
  * Yields sequence of 2D grid coordinates in zigzag row order starting from
3
4
  * [0,0], given `cols` and `rows`.
@@ -5,9 +6,7 @@
5
6
  * Ported & modified from original Java code by Christopher Kulla.
6
7
  * https://sourceforge.net/p/sunflow/code/HEAD/tree/trunk/src/org/sunflow/core/bucket/SpiralBucketOrder.java
7
8
  *
8
- * @param cols -
9
- * @param rows -
10
- *
9
+ * @param opts -
11
10
  */
12
- export declare function zigzagRows2d(cols: number, rows?: number): Generator<number[], void, unknown>;
11
+ export declare function zigzagRows2d(opts: GridIterOpts): Generator<[number, number], void, unknown>;
13
12
  //# sourceMappingURL=zigzag-rows.d.ts.map
package/zigzag-rows.js CHANGED
@@ -1,4 +1,4 @@
1
- import { asInt } from "@thi.ng/api/typedarray";
1
+ import { __opts } from "./utils.js";
2
2
  /**
3
3
  * Yields sequence of 2D grid coordinates in zigzag row order starting from
4
4
  * [0,0], given `cols` and `rows`.
@@ -6,17 +6,15 @@ import { asInt } from "@thi.ng/api/typedarray";
6
6
  * Ported & modified from original Java code by Christopher Kulla.
7
7
  * https://sourceforge.net/p/sunflow/code/HEAD/tree/trunk/src/org/sunflow/core/bucket/SpiralBucketOrder.java
8
8
  *
9
- * @param cols -
10
- * @param rows -
11
- *
9
+ * @param opts -
12
10
  */
13
- export function* zigzagRows2d(cols, rows = cols) {
14
- [cols, rows] = asInt(cols, rows);
11
+ export function* zigzagRows2d(opts) {
12
+ const { cols, rows, tx } = __opts(opts);
15
13
  const num = cols * rows;
16
14
  for (let i = 0; i < num; i++) {
17
15
  let x = i % cols;
18
16
  const y = (i / cols) | 0;
19
17
  y & 1 && (x = cols - 1 - x);
20
- yield [x, y];
18
+ yield tx(x, y);
21
19
  }
22
20
  }