Skip to content

Commit

Permalink
feat(grid-iterators): add new iterators
Browse files Browse the repository at this point in the history
- add columns, rows
- add columnEnds, diagonalEnds, rowEnds
  • Loading branch information
postspectacular committed Jun 19, 2020
1 parent 7baa3ba commit e08985e
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 1 deletion.
14 changes: 14 additions & 0 deletions packages/grid-iterators/src/column-ends.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Filtered version of {@link columns2d}, only including end points of
* each column.
*
* @param cols -
* @param rows -
*/
export function* columnEnds2d(cols: number, rows = cols) {
rows--;
for (let x = 0; x < cols; x++) {
yield [x, 0];
yield [x, rows];
}
}
11 changes: 11 additions & 0 deletions packages/grid-iterators/src/columns.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { map, range2d } from "@thi.ng/transducers";
import { swapxy } from "./swap";

/**
* Yields sequence of 2D grid coordinates in column-major order.
*
* @param cols
* @param rows
*/
export const columns2d = (cols: number, rows = cols) =>
map(swapxy, range2d(rows, cols));
26 changes: 26 additions & 0 deletions packages/grid-iterators/src/diagonal-ends.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { diagonal2d } from "./diagonal";

/**
* Filtered version of {@link diagonal2d}, only including end points of
* the diagonals, apart from the very first and last points: `[0,0]` and
* `[cols-1, rows-1]`.
*
* @remarks
* `cols` and `rows` MUST be both >= 2.
*
* @param cols -
* @param rows -
*/
export function* diagonalEnds2d(cols: number, rows = cols) {
const num = cols * rows - 1;
const maxX = cols - 1;
const maxY = rows - 1;
let i = 0;
for (let p of diagonal2d(cols, rows)) {
if (i > 0 && i < num) {
const [x, y] = p;
if (x === 0 || x === maxX || y === 0 || y === maxY) yield p;
}
i++;
}
}
5 changes: 5 additions & 0 deletions packages/grid-iterators/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
export * from "./circle";
export * from "./column-ends";
export * from "./columns";
export * from "./diagonal";
export * from "./diagonal-ends";
export * from "./hilbert";
export * from "./hvline";
export * from "./interleave";
export * from "./line";
export * from "./random";
export * from "./row-ends";
export * from "./rows";
export * from "./spiral";
export * from "./zcurve";
export * from "./zigzag-columns";
Expand Down
3 changes: 2 additions & 1 deletion packages/grid-iterators/src/interleave.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { map, range2d } from "@thi.ng/transducers";
import { swapxy } from "./swap";

/**
* Yields 2D grid coordinates in the order of interleaved columns with
Expand All @@ -17,7 +18,7 @@ import { map, range2d } from "@thi.ng/transducers";
*/
export function* interleaveColumns2d(cols: number, rows = cols, step = 2) {
for (let j = 0; j < step; j++) {
yield* map((p) => [p[1], p[0]], range2d(0, rows, j, cols, 1, step));
yield* map(swapxy, range2d(0, rows, j, cols, 1, step));
}
}

Expand Down
14 changes: 14 additions & 0 deletions packages/grid-iterators/src/row-ends.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Filtered version of {@link rows2d}, only including end points of
* each row.
*
* @param cols -
* @param rows -
*/
export function* rowEnds2d(cols: number, rows = cols) {
cols--;
for (let y = 0; y < rows; y++) {
yield [0, y];
yield [cols, y];
}
}
10 changes: 10 additions & 0 deletions packages/grid-iterators/src/rows.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { range2d } from "@thi.ng/transducers";

/**
* Yields sequence of 2D grid coordinates in row-major order. Same as
* {@link @thi.ng/transducers#range2d}.
*
* @param cols
* @param rows
*/
export const rows2d = (cols: number, rows = cols) => range2d(cols, rows);
13 changes: 13 additions & 0 deletions packages/grid-iterators/src/swap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Swaps XY in-place.
*
* @param p
*
* @internal
*/
export const swapxy = (p: number[]) => {
const t = p[0];
p[0] = p[1];
p[1] = t;
return p;
};

0 comments on commit e08985e

Please sign in to comment.