Skip to content

Commit

Permalink
refactor(geom-accel): dedupe into() impls
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Sep 19, 2020
1 parent 45156b2 commit 83b8cbd
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 18 deletions.
7 changes: 2 additions & 5 deletions packages/geom-accel/src/aspatial-grid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
subN,
VecOpRoVV,
} from "@thi.ng/vectors";
import { into } from "./utils";

/**
* Common base class for {@link SpatialGrid2} and {@link SpatialGrid3}.
Expand Down Expand Up @@ -95,11 +96,7 @@ export abstract class ASpatialGrid<K extends ReadonlyVec, V>
}

into(pairs: Iterable<Pair<K, V>>, eps = EPS) {
let ok = true;
for (let [k, v] of pairs) {
ok = this.set(k, v, eps) && ok;
}
return ok;
return into(this, pairs, eps);
}

remove(k: K) {
Expand Down
8 changes: 2 additions & 6 deletions packages/geom-accel/src/kd-tree-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Heap } from "@thi.ng/heaps";
import { EPS } from "@thi.ng/math";
import { map } from "@thi.ng/transducers";
import { distSq, ReadonlyVec, Vec } from "@thi.ng/vectors";
import { addResults, CMP } from "./utils";
import { addResults, CMP, into } from "./utils";

type MaybeKdNode<K extends ReadonlyVec, V> = KdNode<K, V> | undefined;

Expand Down Expand Up @@ -139,11 +139,7 @@ export class KdTreeMap<K extends ReadonlyVec, V>
}

into(pairs: Iterable<Pair<K, V>>, eps = EPS) {
let ok = true;
for (let [k, v] of pairs) {
ok = this.set(k, v, eps) && ok;
}
return ok;
return into(this, pairs, eps);
}

remove(key: K) {
Expand Down
8 changes: 2 additions & 6 deletions packages/geom-accel/src/nd-quadtree-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
submN,
vop,
} from "@thi.ng/vectors";
import { addResults, CMP } from "./utils";
import { addResults, CMP, into } from "./utils";

export class NdQtNode<K extends ReadonlyVec, V> {
pos: ReadonlyVec;
Expand Down Expand Up @@ -273,11 +273,7 @@ export class NdQuadtreeMap<K extends ReadonlyVec, V>
}

into(pairs: Iterable<Pair<K, V>>, eps = EPS) {
let ok = true;
for (let [k, v] of pairs) {
ok = this.set(k, v, eps) && ok;
}
return ok;
return into(this, pairs, eps);
}

remove(p: K) {
Expand Down
23 changes: 22 additions & 1 deletion packages/geom-accel/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Fn, Nullable } from "@thi.ng/api";
import type { Fn, Fn3, Nullable, Pair } from "@thi.ng/api";

/** @internal */
export const CMP = (a: [number, any?], b: [number, any?]) => b[0] - a[0];
Expand All @@ -15,3 +15,24 @@ export const addResults = <A, B>(
}
return acc;
};

/**
* Shared `into()` impl for spatial map types.
*
* @param map
* @param pairs
* @param eps
*
* @internal
*/
export const into = <K, V>(
map: { set: Fn3<K, V, number, boolean> },
pairs: Iterable<Pair<K, V>>,
eps: number
) => {
let ok = true;
for (let p of pairs) {
ok = map.set(p[0], p[1], eps) && ok;
}
return ok;
};

0 comments on commit 83b8cbd

Please sign in to comment.