@mark1russell7/layout-contract 0.3.11 → 0.3.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package",
3
3
  "name": "@mark1russell7/layout-contract",
4
- "version": "0.3.11",
4
+ "version": "0.3.12",
5
5
  "description": "The shared contract of the packing and highways layout engines: the input and record formats, the turn rule, margins, tolerances and hard rules.",
6
6
  "keywords": [
7
7
  "layout",
package/src/edges.ts CHANGED
@@ -20,12 +20,28 @@ export interface EdgeStepParams {
20
20
  readonly hubBy?: "area" | "random";
21
21
  }
22
22
 
23
+ /**
24
+ * The key of the edges of an input, in `data.collection`.
25
+ * - `none`: the field is absent, so a router with the rule `collection` has no ways.
26
+ * - `random`: a random name for each target box, so the key has no meaning.
27
+ * - `cluster`: a name for each cluster of boxes, so the key follows the structure.
28
+ */
29
+ export type EdgeKey = "none" | "random" | "cluster";
30
+
31
+ /** The parameters of `edgeKeyStep`. */
32
+ export interface EdgeKeyParams {
33
+ readonly edgeKey: EdgeKey;
34
+ }
35
+
23
36
  /** The mean count of the edges of each box for each value of `edgeDensity`. */
24
37
  export const EDGE_DENSITY: Readonly<Record<EdgeStepParams["edgeDensity"], number>> = /* @__PURE__ */ Object.freeze({ sparse: 0.5, medium: 1.5, dense: 4 });
25
38
 
26
39
  /** The version of `edgeStep`. A change of its output gives a new version. */
27
40
  export const EDGE_STEP_VERSION = 1;
28
41
 
42
+ /** The version of `edgeKeyStep`. A change of its output gives a new version. */
43
+ export const EDGE_KEY_STEP_VERSION = 1;
44
+
29
45
  const CLUSTER = 8;
30
46
  const CHAIN = 32;
31
47
  const INSIDE = 0.85;
@@ -35,6 +51,26 @@ const COLLECTIONS = ["users", "orders", "items", "events"] as const;
35
51
 
36
52
  const round3 = (x: number): number => Math.round(x * 1000) / 1000;
37
53
 
54
+ /** This function gives the shuffled order of `n` boxes, from the first numbers of the stream `edges`. */
55
+ function shuffledBoxes(n: number, r: () => number): number[] {
56
+ const perm = Array.from({ length: n }, (_, i) => i);
57
+ for (let i = n - 1; i > 0; i--) {
58
+ const j = randomInt(r, 0, i);
59
+ [perm[i], perm[j]] = [perm[j]!, perm[i]!];
60
+ }
61
+ return perm;
62
+ }
63
+
64
+ /** This function gives the cluster of each box from the shuffled order: each cluster has `CLUSTER` boxes. */
65
+ function clustersOf(perm: readonly number[]): number[] {
66
+ const clusterOf: number[] = new Array<number>(perm.length);
67
+ perm.forEach((box, p) => (clusterOf[box] = Math.floor(p / CLUSTER)));
68
+ return clusterOf;
69
+ }
70
+
71
+ /** This function gives the key of the boxes of one cluster. */
72
+ const clusterKey = (c: number): string => `${COLLECTIONS[c % COLLECTIONS.length]!}-${c}`;
73
+
38
74
  /**
39
75
  * This function gives the input with edges, groups and ports from its topology alone: a joint input has no positions before the pack.
40
76
  * The random numbers come from the steps `edges` and `ports` of `seed`. The function appends its stamp to `steps`, and removes the old `hash`.
@@ -46,18 +82,13 @@ export function edgeStep(input: LayoutInput, params: EdgeStepParams, seed: numbe
46
82
  const n = input.boxes.length;
47
83
  const r = stepRandom(seed, "edges");
48
84
 
49
- const perm = input.boxes.map((_, i) => i);
50
- for (let i = n - 1; i > 0; i--) {
51
- const j = randomInt(r, 0, i);
52
- [perm[i], perm[j]] = [perm[j]!, perm[i]!];
53
- }
54
- const clusterOf: number[] = new Array<number>(n);
55
- perm.forEach((box, p) => (clusterOf[box] = Math.floor(p / CLUSTER)));
85
+ const perm = shuffledBoxes(n, r);
86
+ const clusterOf = clustersOf(perm);
56
87
  const clusters = Math.ceil(n / CLUSTER);
57
88
  const members = (c: number): readonly number[] => perm.slice(c * CLUSTER, (c + 1) * CLUSTER);
58
89
  const collectionOf =
59
90
  full.edgeShape === "bundled"
60
- ? clusterOf.map((c) => `${COLLECTIONS[c % COLLECTIONS.length]!}-${c}`)
91
+ ? clusterOf.map(clusterKey)
61
92
  : input.boxes.map(() => COLLECTIONS[randomInt(r, 0, COLLECTIONS.length - 1)]!);
62
93
  const area = (i: number): number => input.boxes[i]!.w * input.boxes[i]!.h;
63
94
  const rank = full.hubBy === "area" ? input.boxes.map((_, i) => i).sort((a, b) => area(b) - area(a) || a - b) : perm;
@@ -182,3 +213,32 @@ export function edgeStep(input: LayoutInput, params: EdgeStepParams, seed: numbe
182
213
  const { hash: _old, ...rest } = input;
183
214
  return { ...rest, boxes, edges, steps: [...(input.steps ?? []), stamp] };
184
215
  }
216
+
217
+ /**
218
+ * This function gives the input with a stated key on each edge. The edges stay the same in all but `data.collection`, so the 3 values of `edgeKey` read one factor.
219
+ * The key of an edge depends on its target box, as in `edgeStep`.
220
+ * The value `cluster` uses the clusters of `edgeStep` with the same seed and box order, so this step comes directly after `edgeStep`. On the shape `bundled` it gives the keys of `edgeStep`.
221
+ * The value `random` uses the stream `edge-key`, so it does not change the numbers of `edgeStep`. The function appends its stamp to `steps`, and removes the old `hash`.
222
+ * It throws a `RangeError` for an edge with an unknown target.
223
+ */
224
+ export function edgeKeyStep(input: LayoutInput, params: EdgeKeyParams, seed: number = input.seed ?? 0): LayoutInput {
225
+ const n = input.boxes.length;
226
+ const keys = (): readonly string[] => {
227
+ if (params.edgeKey === "cluster") return clustersOf(shuffledBoxes(n, stepRandom(seed, "edges"))).map(clusterKey);
228
+ const r = stepRandom(seed, "edge-key");
229
+ return input.boxes.map(() => COLLECTIONS[randomInt(r, 0, COLLECTIONS.length - 1)]!);
230
+ };
231
+ const keyOf = params.edgeKey === "none" ? [] : keys();
232
+ const indexOf = new Map(input.boxes.map((box, i) => [box.id, i]));
233
+ const edges = input.edges?.map((edge): LayoutEdge => {
234
+ const { data: oldData, ...plain } = edge;
235
+ const { collection: _old, ...data } = oldData ?? {};
236
+ if (params.edgeKey === "none") return Object.keys(data).length === 0 ? plain : { ...plain, data };
237
+ const target = indexOf.get(edge.target);
238
+ if (target === undefined) throw new RangeError(`edgeKeyStep: the edge "${edge.id}" has the unknown target "${edge.target}".`);
239
+ return { ...plain, data: { ...data, collection: keyOf[target]! } };
240
+ });
241
+ const stamp: LayoutGenerator = { name: "edge-key", version: EDGE_KEY_STEP_VERSION, params: { edgeKey: params.edgeKey }, seed };
242
+ const { hash: _oldHash, edges: _oldEdges, ...rest } = input;
243
+ return { ...rest, ...(edges === undefined ? {} : { edges }), steps: [...(input.steps ?? []), stamp] };
244
+ }
package/src/families.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  // The shared corpus generator (`docs/CORPUS.md`): the dimension schema, the family registry and the seeded random numbers.
2
2
  // An engine owns its families and gives them to a registry. This file imports no engine and no API of Node, so a browser can run it.
3
3
  import type { FamilyBy } from "./corpus.ts";
4
- import { edgeStep, type EdgeStepParams } from "./edges.ts";
4
+ import { EDGE_KEY_STEP_VERSION, edgeKeyStep, edgeStep, type EdgeKey, type EdgeStepParams } from "./edges.ts";
5
5
  import { documentHash } from "./hash.ts";
6
6
  import type { LayoutInput, Obstacle } from "./input.ts";
7
7
  import { mulberry32, randomInt, stepRandom } from "./random.ts";
@@ -227,6 +227,7 @@ export const CORPUS_DIMENSIONS: Readonly<Record<string, CorpusDimension>> = /* @
227
227
  edgeDensity: /* @__PURE__ */ choice("edgeDensity", ["sparse", "medium", "dense"], "medium", "The mean count of the edges of each item: 0.5, 1.5 or 4.", [H, J]),
228
228
  edgeShape: /* @__PURE__ */ choice("edgeShape", ["random", "clustered", "hubChain", "hubs", "longRange", "tree", "bundled"], "random", "The topology of the edges.", [H, J]),
229
229
  ports: /* @__PURE__ */ choice("ports", ["free", "sides", "fixed"], "free", "The ports of the edges: free, a side for each end, or fixed ports.", [H, J]),
230
+ edgeKey: /* @__PURE__ */ choice("edgeKey", ["none", "random", "cluster"], "none", "The key of each edge in `data.collection`: absent, a random name for each target box, or a name for each cluster of boxes.", [H, J]),
230
231
  hubBy: /* @__PURE__ */ withWhen(
231
232
  /* @__PURE__ */ choice("hubBy", ["area", "random"], "area", "The order of the hubs of the shapes hubs and tree: the largest item first, or random.", [J]),
232
233
  /* @__PURE__ */ Object.freeze({ param: "edgeShape", in: /* @__PURE__ */ Object.freeze(["hubs", "tree"]) }),
@@ -805,6 +806,32 @@ export const EDGES_STEP: CorpusStep = /* @__PURE__ */ Object.freeze({
805
806
  apply: (input: LayoutInput, seed: number, d: DimensionValues) => edgeStep(input, edgeParams(d), seed),
806
807
  });
807
808
 
809
+ /** The dimension of `EDGE_KEY_STEP`, from the shared table. */
810
+ const EDGE_KEY_DIMENSIONS: readonly CorpusDimension[] = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ ["edgeKey"].map((n) => CORPUS_DIMENSIONS[n]!));
811
+
812
+ /** This function gives the value of `edgeKey`. It throws a `RangeError` for a missing or a bad value. */
813
+ function edgeKeyOf(d: DimensionValues): EdgeKey {
814
+ const value = Object.hasOwn(d, "edgeKey") ? d["edgeKey"] : undefined;
815
+ const bad = value === undefined ? "the value is missing" : paramProblem(EDGE_KEY_DIMENSIONS[0]!, value);
816
+ if (bad !== undefined) throw new RangeError(`EDGE_KEY_STEP: the dimension "edgeKey" is bad: ${bad}.`);
817
+ return value as EdgeKey;
818
+ }
819
+
820
+ /**
821
+ * The step `edge-key` of `edgeKeyStep` (version 1), as a corpus step. It comes directly after `EDGES_STEP`. Its family part is `key-<edgeKey>`.
822
+ */
823
+ export const EDGE_KEY_STEP: CorpusStep = /* @__PURE__ */ Object.freeze({
824
+ name: "edge-key",
825
+ version: EDGE_KEY_STEP_VERSION,
826
+ owner: "layout-contract",
827
+ meaning: "The key of each edge: absent, with no meaning, or with the meaning of a cluster of boxes.",
828
+ dimensions: EDGE_KEY_DIMENSIONS,
829
+ seeding: "stepRandom(seed, step) for the value random. The value cluster reads the stream edges again, as edgeStep does.",
830
+ crossEngine: "exact",
831
+ familyPart: (d: DimensionValues) => `key-${d["edgeKey"]}`,
832
+ apply: (input: LayoutInput, seed: number, d: DimensionValues) => edgeKeyStep(input, { edgeKey: edgeKeyOf(d) }, seed),
833
+ });
834
+
808
835
  /** The set of `RECT_SCATTERED` when the caller gives no set. */
809
836
  const XCHECK_SET = "xcheck-v1";
810
837