@thi.ng/grid-iterators 3.0.11 → 3.1.0
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 +17 -1
- package/README.md +14 -4
- package/diagonal-ends.d.ts +6 -4
- package/diagonal-ends.js +5 -4
- package/diagonal-slope.d.ts +44 -0
- package/diagonal-slope.js +112 -0
- package/diagonal.js +1 -1
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/package.json +9 -4
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
-
- **Last updated**: 2023-03-
|
|
3
|
+
- **Last updated**: 2023-03-25T17:04:52Z
|
|
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,22 @@ 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.1.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/grid-iterators@3.1.0) (2023-03-25)
|
|
13
|
+
|
|
14
|
+
#### 🚀 Features
|
|
15
|
+
|
|
16
|
+
- add diagonalSlopeX/Y() ([f63dc6e](https://github.com/thi-ng/umbrella/commit/f63dc6e))
|
|
17
|
+
- add diagonal iterators with configurable slope (X & Y versions)
|
|
18
|
+
- update pkg deps
|
|
19
|
+
|
|
20
|
+
#### ♻️ Refactoring
|
|
21
|
+
|
|
22
|
+
- update diagonalEnds2d() ([e20bf7b](https://github.com/thi-ng/umbrella/commit/e20bf7b))
|
|
23
|
+
- add `all` option to include first & last points
|
|
24
|
+
- extract DiagonalSlopeOpts ([e08857d](https://github.com/thi-ng/umbrella/commit/e08857d))
|
|
25
|
+
- update diagonalEnds2d() opts ([d14458a](https://github.com/thi-ng/umbrella/commit/d14458a))
|
|
26
|
+
- make `all` optional
|
|
27
|
+
|
|
12
28
|
### [3.0.1](https://github.com/thi-ng/umbrella/tree/@thi.ng/grid-iterators@3.0.1) (2022-12-29)
|
|
13
29
|
|
|
14
30
|
#### 🩹 Bug fixes
|
package/README.md
CHANGED
|
@@ -11,7 +11,8 @@ This project is part of the
|
|
|
11
11
|
|
|
12
12
|
- [About](#about)
|
|
13
13
|
- [Columns](#columns)
|
|
14
|
-
- [Diagonal](#diagonal)
|
|
14
|
+
- [Diagonal (45 degrees)](#diagonal-45-degrees)
|
|
15
|
+
- [Diagonal with configurable slope](#diagonal-with-configurable-slope)
|
|
15
16
|
- [Hilbert curve](#hilbert-curve)
|
|
16
17
|
- [Interleave columns](#interleave-columns)
|
|
17
18
|
- [Interleave rows](#interleave-rows)
|
|
@@ -52,7 +53,7 @@ Also see the filtered version
|
|
|
52
53
|
[`columnEnds2d()`](https://github.com/thi-ng/umbrella/tree/develop/packages/grid-iterators/src/column-ends.ts),
|
|
53
54
|
which only includes the end points of each column.
|
|
54
55
|
|
|
55
|
-
### Diagonal
|
|
56
|
+
### Diagonal (45 degrees)
|
|
56
57
|
|
|
57
58
|

|
|
58
59
|
|
|
@@ -62,6 +63,13 @@ Also see the filtered version
|
|
|
62
63
|
[`diagonalEnds2d()`](https://github.com/thi-ng/umbrella/tree/develop/packages/grid-iterators/src/diagonal-ends.ts),
|
|
63
64
|
which only includes the end points of the diagonals.
|
|
64
65
|
|
|
66
|
+
### Diagonal with configurable slope
|
|
67
|
+
|
|
68
|
+

|
|
69
|
+

|
|
70
|
+
|
|
71
|
+
[Source](https://github.com/thi-ng/umbrella/tree/develop/packages/grid-iterators/src/diagonal-slope.ts)
|
|
72
|
+
|
|
65
73
|
### Hilbert curve
|
|
66
74
|
|
|
67
75
|

|
|
@@ -123,7 +131,7 @@ which only includes the end points of each row.
|
|
|
123
131
|
|
|
124
132
|
### Zigzag diagonal
|
|
125
133
|
|
|
126
|
-

|
|
127
135
|
|
|
128
136
|
[Source](https://github.com/thi-ng/umbrella/tree/develop/packages/grid-iterators/src/zigzag-diagonal.ts)
|
|
129
137
|
|
|
@@ -232,7 +240,7 @@ For Node.js REPL:
|
|
|
232
240
|
const gridIterators = await import("@thi.ng/grid-iterators");
|
|
233
241
|
```
|
|
234
242
|
|
|
235
|
-
Package sizes (brotli'd, pre-treeshake): ESM: 2.
|
|
243
|
+
Package sizes (brotli'd, pre-treeshake): ESM: 2.52 KB
|
|
236
244
|
|
|
237
245
|
## Dependencies
|
|
238
246
|
|
|
@@ -240,6 +248,7 @@ Package sizes (brotli'd, pre-treeshake): ESM: 2.31 KB
|
|
|
240
248
|
- [@thi.ng/arrays](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays)
|
|
241
249
|
- [@thi.ng/binary](https://github.com/thi-ng/umbrella/tree/develop/packages/binary)
|
|
242
250
|
- [@thi.ng/bitfield](https://github.com/thi-ng/umbrella/tree/develop/packages/bitfield)
|
|
251
|
+
- [@thi.ng/errors](https://github.com/thi-ng/umbrella/tree/develop/packages/errors)
|
|
243
252
|
- [@thi.ng/morton](https://github.com/thi-ng/umbrella/tree/develop/packages/morton)
|
|
244
253
|
- [@thi.ng/random](https://github.com/thi-ng/umbrella/tree/develop/packages/random)
|
|
245
254
|
- [@thi.ng/transducers](https://github.com/thi-ng/umbrella/tree/develop/packages/transducers)
|
|
@@ -273,6 +282,7 @@ import * as gi from "@thi.ng/grid-iterators";
|
|
|
273
282
|
// ]
|
|
274
283
|
|
|
275
284
|
// with applied horizontal mirroring
|
|
285
|
+
// also, if `rows` is missing, it defaults to same value as `cols`
|
|
276
286
|
[...gi.zigzagRows2d({ cols: 4, tx: gi.flipX })]
|
|
277
287
|
// [
|
|
278
288
|
// [ 3, 0 ], [ 2, 0 ], [ 1, 0 ], [ 0, 0 ],
|
package/diagonal-ends.d.ts
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
import type { GridIterOpts } from "./api.js";
|
|
2
2
|
/**
|
|
3
|
-
* Filtered version of {@link diagonal2d}, only including end points of
|
|
4
|
-
*
|
|
5
|
-
* `[cols-1, rows-1]`.
|
|
3
|
+
* Filtered version of {@link diagonal2d}, only including end points of the
|
|
4
|
+
* diagonals. Unless `all` option is enabled (default: false), the very first
|
|
5
|
+
* and last points are skipped, i.e. `[0,0]` and `[cols-1, rows-1]`.
|
|
6
6
|
*
|
|
7
7
|
* @remarks
|
|
8
8
|
* `cols` and `rows` MUST be both >= 2.
|
|
9
9
|
*
|
|
10
10
|
* @param opts -
|
|
11
11
|
*/
|
|
12
|
-
export declare function diagonalEnds2d(opts: GridIterOpts
|
|
12
|
+
export declare function diagonalEnds2d(opts: GridIterOpts & {
|
|
13
|
+
all?: boolean;
|
|
14
|
+
}): Generator<[number, number], void, unknown>;
|
|
13
15
|
//# sourceMappingURL=diagonal-ends.d.ts.map
|
package/diagonal-ends.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { diagonal2d } from "./diagonal.js";
|
|
2
2
|
import { __opts } from "./utils.js";
|
|
3
3
|
/**
|
|
4
|
-
* Filtered version of {@link diagonal2d}, only including end points of
|
|
5
|
-
*
|
|
6
|
-
* `[cols-1, rows-1]`.
|
|
4
|
+
* Filtered version of {@link diagonal2d}, only including end points of the
|
|
5
|
+
* diagonals. Unless `all` option is enabled (default: false), the very first
|
|
6
|
+
* and last points are skipped, i.e. `[0,0]` and `[cols-1, rows-1]`.
|
|
7
7
|
*
|
|
8
8
|
* @remarks
|
|
9
9
|
* `cols` and `rows` MUST be both >= 2.
|
|
@@ -15,9 +15,10 @@ export function* diagonalEnds2d(opts) {
|
|
|
15
15
|
const num = cols * rows - 1;
|
|
16
16
|
const maxX = cols - 1;
|
|
17
17
|
const maxY = rows - 1;
|
|
18
|
+
const check = opts.all ? (i) => i > 0 && i < num : () => true;
|
|
18
19
|
let i = 0;
|
|
19
20
|
for (let p of diagonal2d({ cols, rows })) {
|
|
20
|
-
if (i
|
|
21
|
+
if (check(i)) {
|
|
21
22
|
const [x, y] = p;
|
|
22
23
|
if (x === 0 || x === maxX || y === 0 || y === maxY)
|
|
23
24
|
yield tx(x, y);
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { GridIterOpts } from "./api.js";
|
|
2
|
+
interface DiagonalSlopeOpts extends GridIterOpts {
|
|
3
|
+
/**
|
|
4
|
+
* Diagonal slope / step size
|
|
5
|
+
*/
|
|
6
|
+
slope: number;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Similar to {@link diagonalSlopeX}. Yields sequence of 2D grid coordinates in
|
|
10
|
+
* diagonal order with configurable slope, starting at [0,0]. Each diagonal
|
|
11
|
+
* starts at y=0 and progresses in +y direction and every `step` steps, one
|
|
12
|
+
* step in -x direction.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* // iterate grid in diagonals of 1:3 ratio (x:y)
|
|
17
|
+
* [...diagonalSlopeY({ cols: 5, step: 3 })]
|
|
18
|
+
* // [
|
|
19
|
+
* // [0, 0], [0, 1 ], [0, 2 ],
|
|
20
|
+
* // [1, 0], [1, 1 ], [1, 2 ],
|
|
21
|
+
* // [0, 3], [0, 4 ], [2, 0 ],
|
|
22
|
+
* // [2, 1], [2, 2 ], [1, 3 ],
|
|
23
|
+
* // [1, 4], [3, 0 ], [3, 1 ],
|
|
24
|
+
* // [3, 2], [2, 3 ], [2, 4 ],
|
|
25
|
+
* // [4, 0], [4, 1 ], [4, 2 ],
|
|
26
|
+
* // [3, 3], [3, 4 ], [4, 3 ],
|
|
27
|
+
* // [4, 4]
|
|
28
|
+
* // ]
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* @param opts -
|
|
32
|
+
*/
|
|
33
|
+
export declare function diagonalSlopeY(opts: DiagonalSlopeOpts): Generator<[number, number], void, unknown>;
|
|
34
|
+
/**
|
|
35
|
+
* Similar to {@link diagonalSlopeY}. Yields sequence of 2D grid coordinates in
|
|
36
|
+
* diagonal order with configurable slope, starting at [step-1,0]. Each
|
|
37
|
+
* diagonal starts at y=0 and progresses in -x direction and every `step`
|
|
38
|
+
* steps, one step in +y direction.
|
|
39
|
+
*
|
|
40
|
+
* @param opts -
|
|
41
|
+
*/
|
|
42
|
+
export declare function diagonalSlopeX(opts: DiagonalSlopeOpts): Generator<[number, number], void, unknown>;
|
|
43
|
+
export {};
|
|
44
|
+
//# sourceMappingURL=diagonal-slope.d.ts.map
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { assert } from "@thi.ng/errors/assert";
|
|
2
|
+
import { __opts } from "./utils.js";
|
|
3
|
+
/**
|
|
4
|
+
* Similar to {@link diagonalSlopeX}. Yields sequence of 2D grid coordinates in
|
|
5
|
+
* diagonal order with configurable slope, starting at [0,0]. Each diagonal
|
|
6
|
+
* starts at y=0 and progresses in +y direction and every `step` steps, one
|
|
7
|
+
* step in -x direction.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* // iterate grid in diagonals of 1:3 ratio (x:y)
|
|
12
|
+
* [...diagonalSlopeY({ cols: 5, step: 3 })]
|
|
13
|
+
* // [
|
|
14
|
+
* // [0, 0], [0, 1 ], [0, 2 ],
|
|
15
|
+
* // [1, 0], [1, 1 ], [1, 2 ],
|
|
16
|
+
* // [0, 3], [0, 4 ], [2, 0 ],
|
|
17
|
+
* // [2, 1], [2, 2 ], [1, 3 ],
|
|
18
|
+
* // [1, 4], [3, 0 ], [3, 1 ],
|
|
19
|
+
* // [3, 2], [2, 3 ], [2, 4 ],
|
|
20
|
+
* // [4, 0], [4, 1 ], [4, 2 ],
|
|
21
|
+
* // [3, 3], [3, 4 ], [4, 3 ],
|
|
22
|
+
* // [4, 4]
|
|
23
|
+
* // ]
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* @param opts -
|
|
27
|
+
*/
|
|
28
|
+
export function* diagonalSlopeY(opts) {
|
|
29
|
+
const { cols, rows, tx } = __opts(opts);
|
|
30
|
+
const maxX = cols - 1;
|
|
31
|
+
const slope = opts.slope | 0;
|
|
32
|
+
assert(slope > 0, "slope must be > 0");
|
|
33
|
+
const num = cols * rows - 1;
|
|
34
|
+
let x = 0;
|
|
35
|
+
let y = 0;
|
|
36
|
+
let nx = Math.min(1, maxX);
|
|
37
|
+
let ny = nx > 0 ? 0 : slope;
|
|
38
|
+
let n = slope;
|
|
39
|
+
const reset = () => {
|
|
40
|
+
n = slope;
|
|
41
|
+
x = nx;
|
|
42
|
+
y = ny;
|
|
43
|
+
if (nx < maxX)
|
|
44
|
+
nx++;
|
|
45
|
+
else
|
|
46
|
+
ny += slope;
|
|
47
|
+
};
|
|
48
|
+
for (let i = 0; i <= num; i++) {
|
|
49
|
+
yield tx(x, y);
|
|
50
|
+
if (--n > 0) {
|
|
51
|
+
y++;
|
|
52
|
+
if (y >= rows)
|
|
53
|
+
reset();
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
x--;
|
|
57
|
+
y++;
|
|
58
|
+
if (x < 0 || y >= rows)
|
|
59
|
+
reset();
|
|
60
|
+
else
|
|
61
|
+
n = slope;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Similar to {@link diagonalSlopeY}. Yields sequence of 2D grid coordinates in
|
|
67
|
+
* diagonal order with configurable slope, starting at [step-1,0]. Each
|
|
68
|
+
* diagonal starts at y=0 and progresses in -x direction and every `step`
|
|
69
|
+
* steps, one step in +y direction.
|
|
70
|
+
*
|
|
71
|
+
* @param opts -
|
|
72
|
+
*/
|
|
73
|
+
export function* diagonalSlopeX(opts) {
|
|
74
|
+
const { cols, rows, tx } = __opts(opts);
|
|
75
|
+
const maxX = cols - 1;
|
|
76
|
+
const slope = opts.slope | 0;
|
|
77
|
+
assert(slope > 0, "slope must be > 0");
|
|
78
|
+
const num = cols * rows - 1;
|
|
79
|
+
let x = Math.min(slope - 1, maxX);
|
|
80
|
+
let y = 0;
|
|
81
|
+
let n = x + 1;
|
|
82
|
+
let nx = Math.min(x + slope, maxX);
|
|
83
|
+
let ny = nx > 0 ? 0 : slope;
|
|
84
|
+
const reset = () => {
|
|
85
|
+
x = nx;
|
|
86
|
+
y = ny;
|
|
87
|
+
if (nx < maxX) {
|
|
88
|
+
nx = Math.min(nx + slope, maxX);
|
|
89
|
+
n = slope;
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
ny++;
|
|
93
|
+
n = (x % slope) + 1;
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
for (let i = 0; i <= num; i++) {
|
|
97
|
+
yield tx(x, y);
|
|
98
|
+
if (--n > 0) {
|
|
99
|
+
x--;
|
|
100
|
+
if (x < 0)
|
|
101
|
+
reset();
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
x--;
|
|
105
|
+
y++;
|
|
106
|
+
if (x < 0 || y >= rows)
|
|
107
|
+
reset();
|
|
108
|
+
else
|
|
109
|
+
n = slope;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
package/diagonal.js
CHANGED
package/index.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ export * from "./column-ends.js";
|
|
|
5
5
|
export * from "./columns.js";
|
|
6
6
|
export * from "./diagonal.js";
|
|
7
7
|
export * from "./diagonal-ends.js";
|
|
8
|
+
export * from "./diagonal-slope.js";
|
|
8
9
|
export * from "./diamond-square.js";
|
|
9
10
|
export * from "./flood-fill.js";
|
|
10
11
|
export * from "./hilbert.js";
|
package/index.js
CHANGED
|
@@ -5,6 +5,7 @@ export * from "./column-ends.js";
|
|
|
5
5
|
export * from "./columns.js";
|
|
6
6
|
export * from "./diagonal.js";
|
|
7
7
|
export * from "./diagonal-ends.js";
|
|
8
|
+
export * from "./diagonal-slope.js";
|
|
8
9
|
export * from "./diamond-square.js";
|
|
9
10
|
export * from "./flood-fill.js";
|
|
10
11
|
export * from "./hilbert.js";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thi.ng/grid-iterators",
|
|
3
|
-
"version": "3.0
|
|
3
|
+
"version": "3.1.0",
|
|
4
4
|
"description": "2D grid and shape iterators w/ multiple orderings",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -32,16 +32,18 @@
|
|
|
32
32
|
"doc:readme": "yarn doc:stats && tools:readme",
|
|
33
33
|
"doc:stats": "tools:module-stats",
|
|
34
34
|
"pub": "yarn npm publish --access public",
|
|
35
|
-
"test": "testament test"
|
|
35
|
+
"test": "testament test",
|
|
36
|
+
"tool:assets": "tools:node-esm tools/build-assets.ts"
|
|
36
37
|
},
|
|
37
38
|
"dependencies": {
|
|
38
39
|
"@thi.ng/api": "^8.7.4",
|
|
39
40
|
"@thi.ng/arrays": "^2.5.8",
|
|
40
41
|
"@thi.ng/binary": "^3.3.21",
|
|
41
42
|
"@thi.ng/bitfield": "^2.2.24",
|
|
43
|
+
"@thi.ng/errors": "^2.2.13",
|
|
42
44
|
"@thi.ng/morton": "^3.1.32",
|
|
43
45
|
"@thi.ng/random": "^3.3.27",
|
|
44
|
-
"@thi.ng/transducers": "^8.
|
|
46
|
+
"@thi.ng/transducers": "^8.4.0"
|
|
45
47
|
},
|
|
46
48
|
"devDependencies": {
|
|
47
49
|
"@microsoft/api-extractor": "^7.34.4",
|
|
@@ -101,6 +103,9 @@
|
|
|
101
103
|
"./diagonal-ends": {
|
|
102
104
|
"default": "./diagonal-ends.js"
|
|
103
105
|
},
|
|
106
|
+
"./diagonal-slope": {
|
|
107
|
+
"default": "./diagonal-slope.js"
|
|
108
|
+
},
|
|
104
109
|
"./diagonal": {
|
|
105
110
|
"default": "./diagonal.js"
|
|
106
111
|
},
|
|
@@ -158,5 +163,5 @@
|
|
|
158
163
|
],
|
|
159
164
|
"year": 2019
|
|
160
165
|
},
|
|
161
|
-
"gitHead": "
|
|
166
|
+
"gitHead": "f5c562f74466d36a6df27ddd6957eeb280831fa9\n"
|
|
162
167
|
}
|