-
-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(grid-iterators): add point transforms & global options
BREAKING CHANGE: update function signatures, switch to using options object as arg - add `GridIterOpts` interface - add `PointTransform` and implementations: - add flipX/Y/XY, swapXY transforms - update most iterators to use new options
- Loading branch information
1 parent
9b2361d
commit 1861154
Showing
19 changed files
with
202 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import type { FnU2 } from "@thi.ng/api"; | ||
|
||
/** | ||
* Higher order point coordinate transformation function. First is called with | ||
* grid resolution (cols,rows), then returns a function which is applied to each | ||
* generated grid coordinate. | ||
*/ | ||
export type PointTransform = FnU2<number, FnU2<number, [number, number]>>; | ||
|
||
export interface GridIterOpts { | ||
/** | ||
* Number of grid columns | ||
*/ | ||
cols: number; | ||
/** | ||
* Number of grid rows, by default same as columns. | ||
*/ | ||
rows?: number; | ||
/** | ||
* Point coordinate transformation function, e.g. to mirror iteration order | ||
* along X or Y. See {@link flipX}, {@link flipY}, {@link flipXY} etc. | ||
* | ||
* @defaultValue {@link ident} | ||
*/ | ||
tx?: PointTransform; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,17 @@ | ||
import { asInt } from "@thi.ng/api/typedarray"; | ||
import type { GridIterOpts } from "./api.js"; | ||
import { __opts } from "./utils.js"; | ||
|
||
/** | ||
* Filtered version of {@link columns2d}, only including end points of | ||
* each column. | ||
* | ||
* @param cols - | ||
* @param rows - | ||
* @param opts - | ||
*/ | ||
export function* columnEnds2d(cols: number, rows = cols) { | ||
[cols, rows] = asInt(cols, rows); | ||
export function* columnEnds2d(opts: GridIterOpts) { | ||
let { cols, rows, tx } = __opts(opts); | ||
rows--; | ||
for (let x = 0; x < cols; x++) { | ||
yield [x, 0]; | ||
yield [x, rows]; | ||
yield tx(x, 0); | ||
yield tx(x, rows); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,14 @@ | ||
import { range2d } from "@thi.ng/transducers/range2d"; | ||
import { map } from "@thi.ng/transducers/map"; | ||
import { swapxy } from "./utils.js"; | ||
import { range2d } from "@thi.ng/transducers/range2d"; | ||
import type { GridIterOpts } from "./api.js"; | ||
import { __opts } from "./utils.js"; | ||
|
||
/** | ||
* Yields sequence of 2D grid coordinates in column-major order. | ||
* | ||
* @param cols - | ||
* @param rows - | ||
* @param opts - | ||
*/ | ||
export const columns2d = (cols: number, rows = cols) => | ||
map(swapxy, range2d(rows | 0, cols | 0)); | ||
export const columns2d = (opts: GridIterOpts) => { | ||
const { cols, rows, tx } = __opts(opts); | ||
return map((p) => tx(p[1], p[0]), range2d(rows | 0, cols | 0)); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,58 @@ | ||
import { range2d } from "@thi.ng/transducers/range2d"; | ||
import { map } from "@thi.ng/transducers/map"; | ||
import { swapxy } from "./utils.js"; | ||
import { range2d } from "@thi.ng/transducers/range2d"; | ||
import type { GridIterOpts } from "./api.js"; | ||
import { __opts } from "./utils.js"; | ||
|
||
interface InterleaveOpts extends GridIterOpts { | ||
/** | ||
* Row or column stride. | ||
* | ||
* @defaultValue 2 | ||
*/ | ||
step?: number; | ||
} | ||
|
||
/** | ||
* Yields 2D grid coordinates in the order of interleaved columns with | ||
* configurable `step` size (default: 2). I.e. returns columns in this | ||
* order: | ||
* configurable `step` size (default: 2). | ||
* | ||
* @remarks | ||
* Returns columns in this order: | ||
* | ||
* - 0, step, 2 * step, 3 * step... | ||
* - 1, 2 * step + 1, 3 * step + 1... | ||
* - etc. | ||
* | ||
* {@link interleaveRows2d} | ||
* Also see {@link interleaveRows2d}. | ||
* | ||
* @param cols - | ||
* @param rows - | ||
* @param step - column stride | ||
* @param opts - | ||
*/ | ||
export function* interleaveColumns2d(cols: number, rows = cols, step = 2) { | ||
export function* interleaveColumns2d(opts: InterleaveOpts) { | ||
const { cols, rows, tx } = __opts(opts); | ||
const step = (opts.step != null ? opts.step : 2) | 0; | ||
for (let j = 0; j < step; j++) { | ||
yield* map(swapxy, range2d(0, rows | 0, j, cols | 0, 1, step | 0)); | ||
yield* map((p) => tx(p[1], p[0]), range2d(0, rows, j, cols, 1, step)); | ||
} | ||
} | ||
|
||
/** | ||
* Similar to {@link interleaveColumns2d}, but yields 2D grid coordinates in | ||
* the order of interleaved rows with configurable `step` size (default: | ||
* 2). I.e. returns rows in this order: | ||
* 2). | ||
* | ||
* @remarks | ||
* Returns rows in this order: | ||
* | ||
* - 0, step, 2 * step, 3 * step... | ||
* - 1, 2 * step + 1, 3 * step + 1... | ||
* - etc. | ||
* | ||
* {@link interleaveColumns2d} | ||
* | ||
* @param cols - | ||
* @param rows - | ||
* @param step - row stride | ||
* @param opts - | ||
*/ | ||
export function* interleaveRows2d(cols: number, rows = cols, step = 2) { | ||
export function* interleaveRows2d(opts: InterleaveOpts) { | ||
const { cols, rows, tx } = __opts(opts); | ||
const step = (opts.step != null ? opts.step : 2) | 0; | ||
for (let j = 0; j < step; j++) { | ||
yield* range2d(0, cols | 0, j, rows | 0, 1, step | 0); | ||
yield* map((p) => tx(p[0], p[1]), range2d(0, cols, j, rows, 1, step)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,17 @@ | ||
import { asInt } from "@thi.ng/api/typedarray"; | ||
import type { GridIterOpts } from "./api.js"; | ||
import { __opts } from "./utils.js"; | ||
|
||
/** | ||
* Filtered version of {@link rows2d}, only including end points of | ||
* each row. | ||
* | ||
* @param cols - | ||
* @param rows - | ||
* @param opts - | ||
*/ | ||
export function* rowEnds2d(cols: number, rows = cols) { | ||
[cols, rows] = asInt(cols, rows); | ||
export function* rowEnds2d(opts: GridIterOpts) { | ||
let { cols, rows, tx } = __opts(opts); | ||
cols--; | ||
for (let y = 0; y < rows; y++) { | ||
yield [0, y]; | ||
yield [cols, y]; | ||
yield tx(0, y); | ||
yield tx(cols, y); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,15 @@ | ||
import { map } from "@thi.ng/transducers/map"; | ||
import { range2d } from "@thi.ng/transducers/range2d"; | ||
import type { GridIterOpts } from "./api.js"; | ||
import { __opts } from "./utils.js"; | ||
|
||
/** | ||
* Yields sequence of 2D grid coordinates in row-major order. Same as | ||
* [`range2d()`](https://docs.thi.ng/umbrella/transducers/functions/range2d.html). | ||
* | ||
* @param cols - | ||
* @param rows - | ||
* @param opts - | ||
*/ | ||
export const rows2d = (cols: number, rows = cols) => | ||
range2d(cols | 0, rows | 0); | ||
export const rows2d = (opts: GridIterOpts) => { | ||
const { cols, rows, tx } = __opts(opts); | ||
return map((p) => tx(p[0], p[1]), range2d(cols, rows)); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.