Skip to content

Commit

Permalink
feat(grid-iterators): add interleave fns
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Oct 25, 2019
1 parent da0ec3d commit c883ea0
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 7 deletions.
4 changes: 2 additions & 2 deletions packages/grid-iterators/src/columns.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Yields sequence of 2D grid coordinates in zigzag column order starting
* from [0,0], given `cols` and `rows`.
* Yields sequence of 2D grid coordinates in zigzag column order
* starting from [0,0], given `cols` and `rows`.
*
* Ported & modified from original Java code by Christopher Kulla.
* https://sourceforge.net/p/sunflow/code/HEAD/tree/trunk/src/org/sunflow/core/bucket/SpiralBucketOrder.java
Expand Down
3 changes: 2 additions & 1 deletion packages/grid-iterators/src/diagonal.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* Yields sequence of 2D grid coordinates in diagonal order starting at
* [0,0] and using given `cols` and `rows`.
* [0,0] and using given `cols` and `rows`. Each diagonal starts at y=0
* and progresses in -x,+y direction.
*
* Ported & modified from original Java code by Christopher Kulla.
* https://sourceforge.net/p/sunflow/code/HEAD/tree/trunk/src/org/sunflow/core/bucket/DiagonalBucketOrder.java
Expand Down
1 change: 1 addition & 0 deletions packages/grid-iterators/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from "./columns";
export * from "./diagonal";
export * from "./hilbert";
export * from "./interleave";
export * from "./random";
export * from "./rows";
export * from "./spiral";
Expand Down
43 changes: 43 additions & 0 deletions packages/grid-iterators/src/interleave.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { map, range2d } from "@thi.ng/transducers";

/**
* Yields 2D grid coordinates in the order of interleaved columns with
* configurable `step` size (default: 2). I.e. returns columns in this
* order:
*
* - 0, step, 2 * step, 3 * step...
* - 1, 2 * step + 1, 3 * step + 1...
* - etc.
*
* @see interleaveRows2d
*
* @param cols
* @param rows
* @param step
*/
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));
}
}

/**
* Similar to `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:
*
* - 0, step, 2 * step, 3 * step...
* - 1, 2 * step + 1, 3 * step + 1...
* - etc.
*
* @see interleaveColumns2d
*
* @param cols
* @param rows
* @param step
*/
export function* interleaveRows2d(cols: number, rows = cols, step = 2) {
for (let j = 0; j < step; j++) {
yield* range2d(0, cols, j, rows, 1, step);
}
}
8 changes: 4 additions & 4 deletions packages/grid-iterators/src/zcurve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { ceilPow2 } from "@thi.ng/binary";
import { demux2 } from "@thi.ng/morton";

/**
* Yields grid coordinates in Z-curve (Morton) order. A perfect Z-curve
* is only generated if `cols` AND `rows` are equal and a power of 2.
* Due to using 32bit morton codes, only supports grid sizes up to 32767
* (0x7fff) in either dimension.
* Yields 2D grid coordinates in Z-curve (Morton) order. A perfect
* Z-curve is only generated if `cols` AND `rows` are equal and a power
* of 2. Due to using 32bit morton codes, only supports grid sizes up to
* 32767 (0x7fff) in either dimension.
*
* @param cols
* @param rows
Expand Down

0 comments on commit c883ea0

Please sign in to comment.