Skip to content

Commit

Permalink
feat(arrays): add blitPred1d() predicate version of blit1d()
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Feb 19, 2024
1 parent 11231de commit c13c4f9
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion packages/arrays/src/blit.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { TypedArray } from "@thi.ng/api";
import type { Fn3, TypedArray } from "@thi.ng/api";

/**
* Selectively copies all non-`mask` values from `src` into `dest` starting from
Expand Down Expand Up @@ -51,6 +51,44 @@ export function blit1d(dest: any[], x: number, src: ArrayLike<any>, mask: any) {
return dest;
}

/**
* Similar to {@link blit1d}, but uses a predicate function to
* determine/transform copied values. The predicate is called with the src, and
* dest item values and src index. The result of that function is written to the
* `dest` array. If the predicate returns `undefined`, no value will be written.
*
* @param dest
* @param dx
* @param src
* @param pred
*/
export function blitPred1d<T extends TypedArray>(
dest: T,
dx: number,
src: ArrayLike<number>,
pred: Fn3<number, number, number, number | undefined>
): T;
export function blitPred1d<T>(
dest: T[],
dx: number,
src: ArrayLike<T>,
pred: Fn3<T, T, number, T | undefined>
): T[];
export function blitPred1d(
dest: any[],
x: number,
src: ArrayLike<any>,
pred: any
) {
const [sx, sw, dx, dw] = __clip(0, src.length, x, dest.length);
if (sw < 1 || dx >= dw) return dest;
for (let i = 0; i < sw; i++) {
const val = pred(src[sx + i], dest[dx + i], sx + i);
val !== undefined && (dest[dx + i] = val);
}
return dest;
}

/**
* 2D version of {@link blit1d} (also with region clipping). Positions and sizes
* are given as 2D vectors.
Expand Down

0 comments on commit c13c4f9

Please sign in to comment.