@mark1russell7/layout-contract 0.7.0 → 0.9.0

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.7.0",
4
+ "version": "0.9.0",
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",
@@ -59,7 +59,9 @@
59
59
  "vitest": "^5.0.3"
60
60
  },
61
61
  "dependencies": {
62
- "@mark1russell7/rigor-generators": "^0.1.0",
62
+ "@mark1russell7/rigor-generators": "^0.1.8",
63
+ "@mark1russell7/rigor-statistics": "^0.1.23",
64
+ "@mark1russell7/strategy-space": "^0.0.2",
63
65
  "@mark1russell7/typeset": "^0.2.0"
64
66
  },
65
67
  "scripts": {
package/src/hash.ts CHANGED
@@ -1,85 +1,24 @@
1
+ import { canonicalJson as homeCanonicalJson, sha256Hex as homeSha256Hex } from "@mark1russell7/rigor-generators";
1
2
  import { canonicalInput, type LayoutInput } from "./input.ts";
2
3
  import type { PackOutput } from "./output.ts";
3
4
  import { RECORD_EPS } from "./tolerance.ts";
4
- import { utf8Write } from "./utf8.ts";
5
-
6
- /** The round constants of SHA-256. */
7
- const K: readonly number[] = [
8
- 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe,
9
- 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
10
- 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
11
- 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
12
- 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
13
- ];
14
5
 
15
6
  /**
16
- * This function gives the SHA-256 digest of the UTF-8 bytes of a string, as 64 lowercase hex characters.
17
- * It uses no API of Node or of the browser.
7
+ * This function gives the SHA-256 digest of the bytes of a string, as 64 lowercase hex characters. A lone surrogate gives the 3 bytes of its code.
8
+ * Its home is `sha256Hex` of `@mark1russell7/rigor-generators`, with the option `input: "wtf8"`. This function keeps the name and the signature of the API.
18
9
  */
19
10
  export function sha256Hex(text: string): string {
20
- // The bytes go straight into the padded buffer, so the digest makes one array, not a list of the bytes and a copy.
21
- const length = utf8Write(text);
22
- const bits = length * 8;
23
- const total = Math.ceil((length + 9) / 64) * 64;
24
- const data = new Uint8Array(total);
25
- utf8Write(text, data);
26
- data[length] = 0x80;
27
- const view = new DataView(data.buffer);
28
- view.setUint32(total - 8, Math.floor(bits / 0x100000000));
29
- view.setUint32(total - 4, bits >>> 0);
30
- const h = [0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19];
31
- const w = new Uint32Array(64);
32
- const rotr = (x: number, n: number): number => (x >>> n) | (x << (32 - n));
33
- for (let off = 0; off < total; off += 64) {
34
- for (let i = 0; i < 16; i++) w[i] = view.getUint32(off + 4 * i);
35
- for (let i = 16; i < 64; i++) {
36
- const a = w[i - 15]!;
37
- const b = w[i - 2]!;
38
- const s0 = rotr(a, 7) ^ rotr(a, 18) ^ (a >>> 3);
39
- const s1 = rotr(b, 17) ^ rotr(b, 19) ^ (b >>> 10);
40
- w[i] = (w[i - 16]! + s0 + w[i - 7]! + s1) >>> 0;
41
- }
42
- let [a, b, c, d, e, f, g, k] = h as [number, number, number, number, number, number, number, number];
43
- for (let i = 0; i < 64; i++) {
44
- const s1 = rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25);
45
- const ch = (e & f) ^ (~e & g);
46
- const t1 = (k + s1 + ch + K[i]! + w[i]!) >>> 0;
47
- const s0 = rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22);
48
- const maj = (a & b) ^ (a & c) ^ (b & c);
49
- const t2 = (s0 + maj) >>> 0;
50
- k = g;
51
- g = f;
52
- f = e;
53
- e = (d + t1) >>> 0;
54
- d = c;
55
- c = b;
56
- b = a;
57
- a = (t1 + t2) >>> 0;
58
- }
59
- h[0] = (h[0]! + a) >>> 0;
60
- h[1] = (h[1]! + b) >>> 0;
61
- h[2] = (h[2]! + c) >>> 0;
62
- h[3] = (h[3]! + d) >>> 0;
63
- h[4] = (h[4]! + e) >>> 0;
64
- h[5] = (h[5]! + f) >>> 0;
65
- h[6] = (h[6]! + g) >>> 0;
66
- h[7] = (h[7]! + k) >>> 0;
67
- }
68
- return h.map((x) => x.toString(16).padStart(8, "0")).join("");
11
+ return homeSha256Hex(text, { input: "wtf8" });
69
12
  }
70
13
 
71
14
  /**
72
15
  * This function gives the canonical form of a value: sorted keys and no whitespace, with the numbers of `JSON.stringify`.
73
16
  * A key with the value `undefined` is not in the form. An `undefined` in an array becomes `null`.
17
+ * Its home is `canonicalJson` of `@mark1russell7/rigor-generators`, with the option `form: "json"`.
18
+ * So a value with a method `toJSON`, for example a `Date`, gives the form of the result of that method. A function, a symbol and a cycle cause a `TypeError`.
74
19
  */
75
20
  export function canonicalJson(value: unknown): string {
76
- if (value === null || typeof value !== "object") return JSON.stringify(value) ?? "null";
77
- if (Array.isArray(value)) return `[${value.map((v: unknown) => (v === undefined ? "null" : canonicalJson(v))).join(",")}]`;
78
- const o = value as Record<string, unknown>;
79
- const keys = Object.keys(o)
80
- .filter((k) => o[k] !== undefined)
81
- .sort();
82
- return `{${keys.map((k) => `${JSON.stringify(k)}:${canonicalJson(o[k])}`).join(",")}}`;
21
+ return homeCanonicalJson(value, { form: "json" });
83
22
  }
84
23
 
85
24
  /** The options of `outputHash` and `documentHash`. */
package/src/index.ts CHANGED
@@ -15,7 +15,6 @@ export * from "./hash.ts";
15
15
  export * from "./portfolio.ts";
16
16
  export * from "./corpus.ts";
17
17
  export * from "./strategy-space.ts";
18
- export * from "./strategy-options.ts";
19
18
  export * from "./strategy-catalog.ts";
20
19
  export * from "./verdict.ts";
21
20
  export * from "./families.ts";
package/src/random.ts CHANGED
@@ -1,5 +1,4 @@
1
- import { mulberry32 as homeMulberry32 } from "@mark1russell7/rigor-generators";
2
- import { utf8 } from "./utf8.ts";
1
+ import { fnv1a32 as homeFnv1a32, mulberry32 as homeMulberry32 } from "@mark1russell7/rigor-generators";
3
2
 
4
3
  /**
5
4
  * This function gives a generator of random numbers in [0, 1). It is `mulberry32`, as in `docs/FORMAT.md` of `packing`.
@@ -10,13 +9,11 @@ export function mulberry32(seed: number): () => number {
10
9
  }
11
10
 
12
11
  /**
13
- * This function gives the FNV-1a hash of 32 bits of the UTF-8 bytes of a string.
14
- * It stays here because `fnv1a32` of `@mark1russell7/rigor-generators` encodes a lone surrogate as U+FFFD. This function encodes the 3 bytes of its code.
12
+ * This function gives the FNV-1a hash of 32 bits of the UTF-8 bytes of a string. A lone surrogate gives the 3 bytes of its code.
13
+ * Its home is `fnv1a32` of `@mark1russell7/rigor-generators`, with the option `input: "wtf8"`. This function keeps the name and the signature of the API.
15
14
  */
16
15
  export function fnv1a32(text: string): number {
17
- let h = 0x811c9dc5;
18
- for (const b of utf8(text)) h = Math.imul(h ^ b, 0x01000193) >>> 0;
19
- return h;
16
+ return homeFnv1a32(text, { input: "wtf8" });
20
17
  }
21
18
 
22
19
  /** This function gives the generator of one step of a seeded generator: `mulberry32(fnv1a32("<seed>:<step>"))`. */
@@ -1,7 +1,7 @@
1
1
  // The strategy catalog: every strategy and every option of an engine, as JSON (`docs/STRATEGY-CATALOG.md`).
2
2
  import { canonicalJson } from "./hash.ts";
3
3
  import type { Supports } from "./record.ts";
4
- import { type AnyOption, declarationIssues, type ListItem, type NoDefaultReason, optionIssue, type OptionScale, type ValueOption } from "./strategy-options.ts";
4
+ import { type AnyOption, declarationIssues, type ListItem, type NoDefaultReason, optionIssue, type OptionScale, type ValueOption } from "./strategy-space.ts";
5
5
  import type { ParamValue, StrategySpace } from "./strategy-space.ts";
6
6
 
7
7
  /** The newest schema of a strategy catalog. A new field is a new version of the schema. */