@mark1russell7/layout-contract 0.9.8 → 0.10.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/README.md +12 -7
- package/build/index.d.ts +314 -11
- package/build/index.d.ts.map +1 -1
- package/build/index.js +784 -24
- package/build/index.js.map +1 -1
- package/build/{measures-CJZw-Y3H.d.ts → measures-J0yY_JvV.d.ts} +32 -2
- package/build/measures-J0yY_JvV.d.ts.map +1 -0
- package/build/text.d.ts +28 -2
- package/build/text.d.ts.map +1 -1
- package/build/text.js +93 -24
- package/build/text.js.map +1 -1
- package/package.json +2 -2
- package/src/balance.ts +11 -4
- package/src/cell.ts +104 -0
- package/src/curves.ts +200 -0
- package/src/geometry.ts +136 -0
- package/src/hash.ts +2 -2
- package/src/index.ts +4 -0
- package/src/input.ts +5 -0
- package/src/measure-registry.ts +192 -0
- package/src/measure-survey.ts +149 -0
- package/src/measures.ts +4 -4
- package/src/output.ts +15 -3
- package/src/resize.ts +13 -1
- package/src/rules.ts +82 -18
- package/src/text.ts +100 -13
- package/build/measures-CJZw-Y3H.d.ts.map +0 -1
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { JOINT_MEASURES, type JointMeasureId, type MeasureSpec } from "./measures.ts";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The noise of a measure: the cause of a change of its value when the layout does not change.
|
|
5
|
+
*
|
|
6
|
+
* - `exact`: the value is a function of the layout. Two reads of one layout give the same value.
|
|
7
|
+
* - `sampled`: the value is an estimate from a seeded sample. Another seed gives another value.
|
|
8
|
+
* - `timed`: the value is a time of the machine. Each read gives another value.
|
|
9
|
+
*/
|
|
10
|
+
export type NoiseKind = "exact" | "sampled" | "timed";
|
|
11
|
+
|
|
12
|
+
/** The ids of the noise kinds, in the order of `NoiseKind`. */
|
|
13
|
+
export const NOISE_KINDS: readonly NoiseKind[] = /* @__PURE__ */ Object.freeze(["exact", "sampled", "timed"] as const);
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The rule that maps a value of a measure to a loss from 0 to 1. The loss 0 is the best value. `docs/MEASURE-REGISTRY.md` gives the formulas.
|
|
17
|
+
*
|
|
18
|
+
* - `range`: the share of the range `min` to `max`. Only a measure with a finite `max` has this rule.
|
|
19
|
+
* - `scale`: `d / (d + half)` for the distance `d` from `min`. The loss is 0.5 at `min + half`. A measure with no upper limit uses it.
|
|
20
|
+
* - `none`: no agreed scale. An objective compares two values with `compareMeasure`, or ranks the values of a set. The reason tells why the measure has no scale.
|
|
21
|
+
*
|
|
22
|
+
* The rule `range` needs a finite `min` and `max`. The rule `scale` needs a finite `min`. A measure with no finite range gets the rule `none`, not a guess.
|
|
23
|
+
*/
|
|
24
|
+
export type MeasureNormalize =
|
|
25
|
+
| { readonly kind: "range" }
|
|
26
|
+
| { readonly kind: "scale"; readonly half: number; readonly reason: string }
|
|
27
|
+
| { readonly kind: "none"; readonly reason: string };
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The one unit word of a value from 0 to 1: a share, or a flag of 0 or 1 that a record averages.
|
|
31
|
+
* `checkRegistry` refuses a unit text that names a range, for example `0 to 1`, `0 or 1` or `-1 to 1`. The range goes in `min` and `max`.
|
|
32
|
+
*/
|
|
33
|
+
export const SHARE_UNIT = "share";
|
|
34
|
+
|
|
35
|
+
/** The unit word of a measure with no other unit and a range that is not 0 to 1. An example is a correlation from -1 to 1. */
|
|
36
|
+
export const NO_UNIT = "no unit";
|
|
37
|
+
|
|
38
|
+
/** A unit text that names a range, for example `0 to 1` or `-0.5 to 1`. */
|
|
39
|
+
const RANGE_UNIT = /^-?\d+(?:\.\d+)? (?:to|or) -?\d+(?:\.\d+)?$/;
|
|
40
|
+
/** The other words of a share. */
|
|
41
|
+
const SHARE_WORDS: ReadonlySet<string> = /* @__PURE__ */ new Set(["boolean", "fraction", "proportion"]);
|
|
42
|
+
|
|
43
|
+
/** This function gives the fault of a unit text, or `undefined` for a good unit. */
|
|
44
|
+
export function unitFault(spec: Pick<MeasureSpec, "unit" | "min" | "max">): string | undefined {
|
|
45
|
+
const u = spec.unit;
|
|
46
|
+
if (typeof u !== "string" || u.length === 0) return "the unit is empty";
|
|
47
|
+
if (RANGE_UNIT.test(u)) return `the unit ${JSON.stringify(u)} names a range: use ${JSON.stringify(SHARE_UNIT)} for the range 0 to 1, else ${JSON.stringify(NO_UNIT)}, with the range in min and max`;
|
|
48
|
+
if (SHARE_WORDS.has(u)) return `the unit ${JSON.stringify(u)} is a share: use ${JSON.stringify(SHARE_UNIT)}`;
|
|
49
|
+
if (u === SHARE_UNIT && !(spec.min === 0 && spec.max === 1)) return `the unit ${JSON.stringify(SHARE_UNIT)} needs the range 0 to 1`;
|
|
50
|
+
return undefined;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The cost of one read of a measure, as an order of growth. The text is a sum of terms, for example `n + e^2` or `e log e`.
|
|
55
|
+
* The variables are `n` (the boxes), `e` (the edges) and `p` (the points of the routes). The value `1` is a constant cost.
|
|
56
|
+
*/
|
|
57
|
+
export type CostOrder = string;
|
|
58
|
+
|
|
59
|
+
/** The pattern of a `CostOrder`: terms of `1`, `n`, `e` or `p`, each with an optional `^2`, `^3` or `log`, joined by ` + `. */
|
|
60
|
+
const COST_TERM = "(?:1|[nep](?:\\^[23])?(?: log [nep])?)";
|
|
61
|
+
const COST_ORDER = /* @__PURE__ */ new RegExp(`^${COST_TERM}(?: \\+ ${COST_TERM})*$`);
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* One entry of the measure registry: the spec of the measure, its source package, its noise, its normalization and its cost.
|
|
65
|
+
* The registry extends `MeasureSpec`. Thus a spec of an engine becomes an entry with `measureEntry`.
|
|
66
|
+
*/
|
|
67
|
+
export interface MeasureEntry<Id extends string = string> extends MeasureSpec<Id> {
|
|
68
|
+
/** The npm name of the package that computes the measure. */
|
|
69
|
+
readonly source: string;
|
|
70
|
+
readonly noise: NoiseKind;
|
|
71
|
+
readonly normalize: MeasureNormalize;
|
|
72
|
+
/** The cost of one read of the measure. Without it, the cost is not known. */
|
|
73
|
+
readonly cost?: CostOrder;
|
|
74
|
+
/**
|
|
75
|
+
* The reason that no code computes the measure yet. A planned measure has a spec and no value.
|
|
76
|
+
* An objective refuses a planned measure, and `normalizeMeasure` throws an error for it.
|
|
77
|
+
*/
|
|
78
|
+
readonly planned?: string;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** The facts that `measureEntry` adds to a spec. Without `normalize`, the rule is `range` for a finite range and `none` for the other ranges. */
|
|
82
|
+
export interface EntryFacts {
|
|
83
|
+
readonly source: string;
|
|
84
|
+
readonly noise?: NoiseKind;
|
|
85
|
+
readonly normalize?: MeasureNormalize;
|
|
86
|
+
readonly cost?: CostOrder;
|
|
87
|
+
readonly planned?: string;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** The reason of the default rule `none`. */
|
|
91
|
+
export const NO_FINITE_RANGE = "The range is not finite, and the registry has no agreed scale for the measure.";
|
|
92
|
+
|
|
93
|
+
/** This function gives the default normalization of a spec: `range` when `min` and `max` are finite, else `none` with the reason `NO_FINITE_RANGE`. */
|
|
94
|
+
export function defaultNormalize(spec: Pick<MeasureSpec, "min" | "max">): MeasureNormalize {
|
|
95
|
+
return Number.isFinite(spec.min) && Number.isFinite(spec.max) ? { kind: "range" } : { kind: "none", reason: NO_FINITE_RANGE };
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** This function makes a registry entry from a spec and the added facts. The noise is `exact` by default. */
|
|
99
|
+
export function measureEntry<Id extends string>(spec: MeasureSpec<Id>, facts: EntryFacts): MeasureEntry<Id> {
|
|
100
|
+
return Object.freeze({
|
|
101
|
+
...spec,
|
|
102
|
+
source: facts.source,
|
|
103
|
+
noise: facts.noise ?? "exact",
|
|
104
|
+
normalize: facts.normalize ?? defaultNormalize(spec),
|
|
105
|
+
...(facts.cost === undefined ? {} : { cost: facts.cost }),
|
|
106
|
+
...(facts.planned === undefined ? {} : { planned: facts.planned }),
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* This function gives the loss of the value `value` of a measure: a number from 0 to 1, where 0 is the best value.
|
|
112
|
+
* A value outside the range of the spec gives a `RangeError`. A value `NaN` also gives a `RangeError`.
|
|
113
|
+
* The rule `none` gives `undefined`: the measure has no agreed scale.
|
|
114
|
+
* A planned measure gives an `Error`: no code computes it, so it has no value.
|
|
115
|
+
*/
|
|
116
|
+
export function normalizeMeasure(value: number, entry: Pick<MeasureEntry, "id" | "direction" | "min" | "max" | "normalize" | "planned">): number | undefined {
|
|
117
|
+
if (entry.planned !== undefined) throw new Error(`normalizeMeasure: ${entry.id} is planned, and no code computes it: ${entry.planned}`);
|
|
118
|
+
if (Number.isNaN(value) || value < entry.min || value > entry.max) throw new RangeError(`normalizeMeasure: the value ${value} of ${entry.id} is not in [${entry.min}, ${entry.max}].`);
|
|
119
|
+
const rule = entry.normalize;
|
|
120
|
+
if (rule.kind === "none") return undefined;
|
|
121
|
+
let worse: number;
|
|
122
|
+
if (rule.kind === "range") {
|
|
123
|
+
const t = entry.max === entry.min ? 0 : (value - entry.min) / (entry.max - entry.min);
|
|
124
|
+
worse = entry.direction === "lower" ? t : 1 - t;
|
|
125
|
+
} else {
|
|
126
|
+
const d = value - entry.min;
|
|
127
|
+
if (d === Infinity) worse = entry.direction === "lower" ? 1 : 0;
|
|
128
|
+
else worse = entry.direction === "lower" ? d / (d + rule.half) : rule.half / (d + rule.half);
|
|
129
|
+
}
|
|
130
|
+
return worse === 0 ? 0 : worse;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* This function gives the faults of a list of registry entries, as plain text. An empty list tells that the entries are good.
|
|
135
|
+
* It examines the unique ids, the unit, the range, the floor, the noise, the normalization, the cost, the source and the planned reason.
|
|
136
|
+
*/
|
|
137
|
+
export function checkRegistry(entries: readonly MeasureEntry[]): string[] {
|
|
138
|
+
const out: string[] = [];
|
|
139
|
+
const seen = new Set<string>();
|
|
140
|
+
for (const e of entries) {
|
|
141
|
+
const at = `measure ${JSON.stringify(e.id)}`;
|
|
142
|
+
if (typeof e.id !== "string" || e.id.length === 0) out.push(`${at}: the id is empty`);
|
|
143
|
+
if (seen.has(e.id)) out.push(`${at}: the id is not unique`);
|
|
144
|
+
seen.add(e.id);
|
|
145
|
+
const unit = unitFault(e);
|
|
146
|
+
if (unit !== undefined) out.push(`${at}: ${unit}`);
|
|
147
|
+
if (e.direction !== "lower" && e.direction !== "higher") out.push(`${at}: the direction is not lower or higher`);
|
|
148
|
+
if (typeof e.min !== "number" || Number.isNaN(e.min) || e.min === Infinity) out.push(`${at}: min is not a number below Infinity`);
|
|
149
|
+
if (!(e.max > e.min)) out.push(`${at}: max is not above min`);
|
|
150
|
+
if (e.floor !== undefined && !(e.floor >= e.min && e.floor <= e.max)) out.push(`${at}: the floor is not in the range`);
|
|
151
|
+
if (typeof e.source !== "string" || e.source.length === 0) out.push(`${at}: the source is empty`);
|
|
152
|
+
if (!NOISE_KINDS.includes(e.noise)) out.push(`${at}: the noise is not one of ${NOISE_KINDS.join(", ")}`);
|
|
153
|
+
if (e.cost !== undefined && !COST_ORDER.test(e.cost)) out.push(`${at}: the cost ${JSON.stringify(e.cost)} is not an order of growth`);
|
|
154
|
+
if (e.planned !== undefined && !(typeof e.planned === "string" && e.planned.length > 0)) out.push(`${at}: the planned reason is empty`);
|
|
155
|
+
if (e.planned !== undefined && e.cost !== undefined) out.push(`${at}: a planned measure has no code, so it has no cost`);
|
|
156
|
+
const n = e.normalize;
|
|
157
|
+
if (n === undefined || (n.kind !== "range" && n.kind !== "scale" && n.kind !== "none")) out.push(`${at}: the normalization is not range, scale or none`);
|
|
158
|
+
else if (n.kind === "range" && !(Number.isFinite(e.min) && Number.isFinite(e.max))) out.push(`${at}: the normalization range needs a finite min and max`);
|
|
159
|
+
else if (n.kind === "scale" && !Number.isFinite(e.min)) out.push(`${at}: the normalization scale needs a finite min`);
|
|
160
|
+
else if (n.kind === "scale" && !(Number.isFinite(n.half) && n.half > 0)) out.push(`${at}: the half of the normalization scale is not above 0`);
|
|
161
|
+
else if (n.kind === "scale" && !(typeof n.reason === "string" && n.reason.length > 0)) out.push(`${at}: the normalization scale has no reason`);
|
|
162
|
+
else if (n.kind === "none" && !(typeof n.reason === "string" && n.reason.length > 0)) out.push(`${at}: the normalization none has no reason`);
|
|
163
|
+
}
|
|
164
|
+
return out;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** The package of the joint bench. It is private, so the joint measures have no npm version. */
|
|
168
|
+
export const JOINT_SOURCE = "@layout-contract/joint";
|
|
169
|
+
|
|
170
|
+
/** The planned reason of the two joint stability measures. */
|
|
171
|
+
const NO_CHANGE_PAIRS = "The joint bench has no pairs of a base input and a changed input, so no code computes the measure.";
|
|
172
|
+
|
|
173
|
+
const JOINT_FACTS: Readonly<Record<JointMeasureId, Omit<EntryFacts, "source">>> = {
|
|
174
|
+
"joint.fill": { cost: "n + p" },
|
|
175
|
+
"joint.aspectError": { cost: "n + p", normalize: { kind: "scale", half: Math.LN2, reason: "An aspect at a factor of 2 from the target has the loss 0.5." } },
|
|
176
|
+
"joint.spill": { cost: "n + p" },
|
|
177
|
+
"joint.straight.crossings": { cost: "n + e^2", normalize: { kind: "none", reason: "A count of crossings has no agreed scale." } },
|
|
178
|
+
"joint.straight.length": { cost: "n + e", normalize: { kind: "scale", half: 1, reason: "An edge as long as the side S of the square of the box area has the loss 0.5." } },
|
|
179
|
+
"joint.stability.mean": { planned: NO_CHANGE_PAIRS },
|
|
180
|
+
"joint.stability.moved": { planned: NO_CHANGE_PAIRS },
|
|
181
|
+
"joint.proxy.rank": { cost: "n + p + e log e" },
|
|
182
|
+
"joint.route.length": { cost: "p", normalize: { kind: "none", reason: "A length in layout units has no agreed scale." } },
|
|
183
|
+
"joint.ms": { noise: "timed", normalize: { kind: "none", reason: "A time of the machine has no agreed scale, and each read gives another value." } },
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* The registry entries of the joint measures, in the order of `JOINT_MEASURES`. `docs/MEASURE-REGISTRY.md` gives the reason of each cost and scale.
|
|
188
|
+
* The engines keep the specs of their own measures. `docs/measure-survey.json` lists them, and an engine adopts the registry with `measureEntry`.
|
|
189
|
+
*/
|
|
190
|
+
export const MEASURE_REGISTRY: readonly MeasureEntry<JointMeasureId>[] = /* @__PURE__ */ Object.freeze(
|
|
191
|
+
JOINT_MEASURES.map((spec) => measureEntry(spec, { source: JOINT_SOURCE, ...JOINT_FACTS[spec.id] })),
|
|
192
|
+
);
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import type { Direction } from "./measures.ts";
|
|
2
|
+
|
|
3
|
+
/** The schema of a measure survey. */
|
|
4
|
+
export const MEASURE_SURVEY_SCHEMA = "measure-survey/1";
|
|
5
|
+
|
|
6
|
+
/** One measure of a source, as the source declares it. A source without a range has no `min` and no `max`. */
|
|
7
|
+
export interface SurveyMeasure {
|
|
8
|
+
readonly id: string;
|
|
9
|
+
readonly unit: string;
|
|
10
|
+
readonly direction: Direction;
|
|
11
|
+
readonly family?: string;
|
|
12
|
+
readonly min?: number;
|
|
13
|
+
/** The highest value. JSON has no `Infinity`, so `null` tells that the measure has no upper limit. */
|
|
14
|
+
readonly max?: number | null;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** The measures of one package at one version. */
|
|
18
|
+
export interface SurveySource {
|
|
19
|
+
readonly package: string;
|
|
20
|
+
readonly version: string;
|
|
21
|
+
readonly measures: readonly SurveyMeasure[];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** A list of the measures of each package, kept as data in `docs/measure-survey.json`. */
|
|
25
|
+
export interface MeasureSurvey {
|
|
26
|
+
readonly schema: typeof MEASURE_SURVEY_SCHEMA;
|
|
27
|
+
/** The day of the survey, as `YYYY-MM-DD`. */
|
|
28
|
+
readonly date: string;
|
|
29
|
+
readonly sources: readonly SurveySource[];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Two measures of two sources with one name after the stage, for example `pack.fill` and `joint.fill`, and another unit or direction.
|
|
34
|
+
* The name after the stage is the id without its first part.
|
|
35
|
+
*/
|
|
36
|
+
export interface TwinConflict {
|
|
37
|
+
readonly name: string;
|
|
38
|
+
readonly field: "unit" | "direction";
|
|
39
|
+
/** The id and the value of each measure of the twins. */
|
|
40
|
+
readonly values: readonly { readonly id: string; readonly source: string; readonly value: string }[];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** The facts of a survey that feed the idea queue. */
|
|
44
|
+
export interface SurveyFindings {
|
|
45
|
+
/** The count of the distinct ids over all sources. */
|
|
46
|
+
readonly ids: number;
|
|
47
|
+
/** The count of the measures of each source. */
|
|
48
|
+
readonly bySource: Readonly<Record<string, number>>;
|
|
49
|
+
/** The ids that two sources declare, with another unit or direction. */
|
|
50
|
+
readonly conflicts: readonly { readonly id: string; readonly field: "unit" | "direction"; readonly sources: readonly string[] }[];
|
|
51
|
+
/** The twins of two sources with another unit or another direction. */
|
|
52
|
+
readonly twins: readonly TwinConflict[];
|
|
53
|
+
/** The ids without a range. An objective cannot normalize them with the rule `range` or `scale`. */
|
|
54
|
+
readonly noRange: readonly string[];
|
|
55
|
+
/** The distinct units, with the count of the measures of each. */
|
|
56
|
+
readonly units: Readonly<Record<string, number>>;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const DATE = /^\d{4}-\d{2}-\d{2}$/;
|
|
60
|
+
const isObj = (v: unknown): v is Record<string, unknown> => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
61
|
+
|
|
62
|
+
/** This function gives the faults of a survey, as plain text. An empty list tells that the survey is good. */
|
|
63
|
+
export function checkSurvey(v: unknown): string[] {
|
|
64
|
+
const out: string[] = [];
|
|
65
|
+
if (!isObj(v)) return ["the survey is not an object"];
|
|
66
|
+
if (v["schema"] !== MEASURE_SURVEY_SCHEMA) out.push(`the schema is not ${MEASURE_SURVEY_SCHEMA}`);
|
|
67
|
+
if (typeof v["date"] !== "string" || !DATE.test(v["date"])) out.push("the date is not YYYY-MM-DD");
|
|
68
|
+
const sources = v["sources"];
|
|
69
|
+
if (!Array.isArray(sources) || sources.length === 0) return [...out, "the survey has no sources"];
|
|
70
|
+
const names = new Set<string>();
|
|
71
|
+
for (const [i, s] of sources.entries()) {
|
|
72
|
+
const at = `sources[${i}]`;
|
|
73
|
+
if (!isObj(s)) {
|
|
74
|
+
out.push(`${at} is not an object`);
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
if (typeof s["package"] !== "string" || s["package"].length === 0) out.push(`${at}.package is empty`);
|
|
78
|
+
else if (names.has(s["package"])) out.push(`${at}.package ${s["package"]} is not unique`);
|
|
79
|
+
else names.add(s["package"]);
|
|
80
|
+
if (typeof s["version"] !== "string" || s["version"].length === 0) out.push(`${at}.version is empty`);
|
|
81
|
+
const measures = s["measures"];
|
|
82
|
+
if (!Array.isArray(measures)) {
|
|
83
|
+
out.push(`${at}.measures is not a list`);
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
const ids = new Set<string>();
|
|
87
|
+
for (const [j, m] of measures.entries()) {
|
|
88
|
+
const mat = `${at}.measures[${j}]`;
|
|
89
|
+
if (!isObj(m)) {
|
|
90
|
+
out.push(`${mat} is not an object`);
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
if (typeof m["id"] !== "string" || m["id"].length === 0) out.push(`${mat}.id is empty`);
|
|
94
|
+
else if (ids.has(m["id"])) out.push(`${mat}.id ${m["id"]} is not unique in its source`);
|
|
95
|
+
else ids.add(m["id"]);
|
|
96
|
+
if (typeof m["unit"] !== "string" || m["unit"].length === 0) out.push(`${mat}.unit is empty`);
|
|
97
|
+
if (m["direction"] !== "lower" && m["direction"] !== "higher") out.push(`${mat}.direction is not lower or higher`);
|
|
98
|
+
const min = m["min"];
|
|
99
|
+
const max = m["max"];
|
|
100
|
+
if ((min === undefined) !== (max === undefined)) out.push(`${mat} has min or max, not both`);
|
|
101
|
+
if (min !== undefined && (typeof min !== "number" || !Number.isFinite(min))) out.push(`${mat}.min is not a finite number`);
|
|
102
|
+
if (max !== undefined && max !== null && (typeof max !== "number" || !Number.isFinite(max) || !(typeof min === "number" && max > min))) out.push(`${mat}.max is not a number above min, or null`);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return out;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** This function gives the name of an id after its stage: the id without its first part. An id without a dot is its own name. */
|
|
109
|
+
export function nameAfterStage(id: string): string {
|
|
110
|
+
const dot = id.indexOf(".");
|
|
111
|
+
return dot < 0 ? id : id.slice(dot + 1);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** This function gives the findings of a good survey. `checkSurvey` examines the survey first. */
|
|
115
|
+
export function surveyFindings(survey: MeasureSurvey): SurveyFindings {
|
|
116
|
+
const bySource: Record<string, number> = {};
|
|
117
|
+
const byId = new Map<string, { source: string; m: SurveyMeasure }[]>();
|
|
118
|
+
const units: Record<string, number> = {};
|
|
119
|
+
const noRange = new Set<string>();
|
|
120
|
+
for (const s of survey.sources) {
|
|
121
|
+
bySource[s.package] = s.measures.length;
|
|
122
|
+
for (const m of s.measures) {
|
|
123
|
+
const list = byId.get(m.id) ?? [];
|
|
124
|
+
list.push({ source: s.package, m });
|
|
125
|
+
byId.set(m.id, list);
|
|
126
|
+
units[m.unit] = (units[m.unit] ?? 0) + 1;
|
|
127
|
+
if (m.min === undefined) noRange.add(m.id);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
const conflicts: { id: string; field: "unit" | "direction"; sources: string[] }[] = [];
|
|
131
|
+
for (const [id, list] of byId)
|
|
132
|
+
for (const field of ["unit", "direction"] as const)
|
|
133
|
+
if (new Set(list.map((e) => e.m[field])).size > 1) conflicts.push({ id, field, sources: list.map((e) => e.source) });
|
|
134
|
+
|
|
135
|
+
const byName = new Map<string, { id: string; source: string; m: SurveyMeasure }[]>();
|
|
136
|
+
for (const [id, list] of byId) {
|
|
137
|
+
const name = nameAfterStage(id);
|
|
138
|
+
const group = byName.get(name) ?? [];
|
|
139
|
+
for (const e of list) group.push({ id, ...e });
|
|
140
|
+
byName.set(name, group);
|
|
141
|
+
}
|
|
142
|
+
const twins: TwinConflict[] = [];
|
|
143
|
+
for (const [name, group] of [...byName].sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))) {
|
|
144
|
+
if (new Set(group.map((e) => e.source)).size < 2) continue;
|
|
145
|
+
for (const field of ["unit", "direction"] as const)
|
|
146
|
+
if (new Set(group.map((e) => e.m[field])).size > 1) twins.push({ name, field, values: group.map((e) => ({ id: e.id, source: e.source, value: e.m[field] })) });
|
|
147
|
+
}
|
|
148
|
+
return { ids: byId.size, bySource, conflicts, twins, noRange: [...noRange].sort(), units };
|
|
149
|
+
}
|
package/src/measures.ts
CHANGED
|
@@ -62,14 +62,14 @@ const AREA: readonly ShapeKind[] = ["rect"];
|
|
|
62
62
|
* `joint.fill` and `joint.straight.length` read the area of each box, so they support only `rect`. `docs/SHAPES.md` tells the reasons.
|
|
63
63
|
*/
|
|
64
64
|
export const JOINT_MEASURES: readonly MeasureSpec<JointMeasureId>[] = /* @__PURE__ */ Object.freeze([
|
|
65
|
-
{ id: "joint.fill", minEffect: 0.001, unit: "
|
|
65
|
+
{ id: "joint.fill", minEffect: 0.001, unit: "share", direction: "higher", min: 0, max: 1, floor: 1, role: "guard", shapes: AREA },
|
|
66
66
|
{ id: "joint.aspectError", minEffect: 0.01, unit: "natural log", direction: "lower", min: 0, max: Infinity, floor: 0, tol: 0.01, role: "guard", shapes: BOUNDS_ONLY },
|
|
67
|
-
{ id: "joint.spill", minEffect: 0.001, unit: "
|
|
67
|
+
{ id: "joint.spill", minEffect: 0.001, unit: "share", direction: "lower", min: 0, max: 1, floor: 0, tol: 0.001, role: "guard", shapes: BOUNDS_ONLY },
|
|
68
68
|
{ id: "joint.straight.crossings", minEffect: 0, unit: "count", direction: "lower", min: 0, max: Infinity, floor: 0, tol: 0, role: "guard", shapes: BOUNDS_ONLY },
|
|
69
69
|
{ id: "joint.straight.length", minEffect: 0.01, unit: "S for each edge", direction: "lower", min: 0, max: Infinity, floor: 0, tol: 0.01, role: "guard", shapes: AREA },
|
|
70
70
|
{ id: "joint.stability.mean", minEffect: 0.005, unit: "W", direction: "lower", min: 0, max: Infinity, floor: 0, tol: 0.005, role: "change-guard", shapes: BOUNDS_ONLY },
|
|
71
|
-
{ id: "joint.stability.moved", minEffect: 0.02, unit: "
|
|
72
|
-
{ id: "joint.proxy.rank", unit: "
|
|
71
|
+
{ id: "joint.stability.moved", minEffect: 0.02, unit: "share", direction: "lower", min: 0, max: 1, floor: 0, tol: 0.02, role: "change-guard", shapes: BOUNDS_ONLY },
|
|
72
|
+
{ id: "joint.proxy.rank", unit: "no unit", direction: "higher", min: -1, max: 1, role: "diagnostic", shapes: BOUNDS_ONLY },
|
|
73
73
|
{ id: "joint.route.length", unit: "layout units for each route", direction: "lower", min: 0, max: Infinity, role: "diagnostic", shapes: BOUNDS_ONLY },
|
|
74
74
|
{ id: "joint.ms", unit: "milliseconds", direction: "lower", min: 0, max: Infinity, role: "time", shapes: BOUNDS_ONLY },
|
|
75
75
|
] as const);
|
package/src/output.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { boundsOf, translate } from "./geometry.ts";
|
|
2
|
-
import type { LayoutInput } from "./input.ts";
|
|
1
|
+
import { boundsOf, translate, type Region } from "./geometry.ts";
|
|
2
|
+
import type { LayoutBox, LayoutInput } from "./input.ts";
|
|
3
3
|
import { resizeBox } from "./resize.ts";
|
|
4
4
|
import { turnBox } from "./turn.ts";
|
|
5
5
|
|
|
@@ -14,6 +14,11 @@ export interface Placement {
|
|
|
14
14
|
readonly turn?: 0 | 90;
|
|
15
15
|
readonly w?: number;
|
|
16
16
|
readonly h?: number;
|
|
17
|
+
/**
|
|
18
|
+
* The shape of the placed box, relative to its origin, as `LayoutBox.shape`. Only a box with `resize.reshape` can get it (`size-kept`).
|
|
19
|
+
* The shape stays inside the box: `w` by `h` of the placement, or of the box without them.
|
|
20
|
+
*/
|
|
21
|
+
readonly shape?: Region;
|
|
17
22
|
}
|
|
18
23
|
|
|
19
24
|
/** The facts of one stage of a strategy, for the records and the site. */
|
|
@@ -35,12 +40,19 @@ export interface PackOutput {
|
|
|
35
40
|
readonly trace?: readonly StageTrace[];
|
|
36
41
|
}
|
|
37
42
|
|
|
43
|
+
/** This function gives a box with the shape `shape` and without its `shapeKind`. */
|
|
44
|
+
function reshaped(box: LayoutBox, shape: Region): LayoutBox {
|
|
45
|
+
const { shapeKind: _kind, ...rest } = box;
|
|
46
|
+
return { ...rest, shape };
|
|
47
|
+
}
|
|
48
|
+
|
|
38
49
|
/**
|
|
39
50
|
* This function gives the input with the output of a packer applied. Each placed box gets its position.
|
|
40
51
|
* A turned box gets its turned size, ports, margin and shape. Each moved obstacle moves.
|
|
41
52
|
* The container gets the size of the output. The router reads this result. It does not read `turn`.
|
|
42
53
|
*
|
|
43
54
|
* A placement with `w` or `h` gives the box that size after the turn (`resizeBox`).
|
|
55
|
+
* A placement with `shape` gives the box that shape. The box loses its `shapeKind`, so `shapeKindOf` reads the kind of the new shape.
|
|
44
56
|
*/
|
|
45
57
|
export function applyPack(input: LayoutInput, output: PackOutput): LayoutInput {
|
|
46
58
|
const at = new Map(output.placements.map((p) => [p.id, p]));
|
|
@@ -50,7 +62,7 @@ export function applyPack(input: LayoutInput, output: PackOutput): LayoutInput {
|
|
|
50
62
|
if (p === undefined) return box;
|
|
51
63
|
const turned = p.turn === 90 ? turnBox(box) : box;
|
|
52
64
|
const sized = p.w === undefined && p.h === undefined ? turned : resizeBox(turned, p.w ?? turned.w, p.h ?? turned.h);
|
|
53
|
-
return { ...sized, x: p.x, y: p.y };
|
|
65
|
+
return { ...(p.shape === undefined ? sized : reshaped(sized, p.shape)), x: p.x, y: p.y };
|
|
54
66
|
});
|
|
55
67
|
const obstacles = input.obstacles?.map((o) => {
|
|
56
68
|
const m = moved.get(o.id);
|
package/src/resize.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
// The resize of a box (`LayoutBox.resize`): the sizes that a packer can give a box, and the box at a new size.
|
|
2
|
+
import { boundsOf, type Region } from "./geometry.ts";
|
|
2
3
|
import type { LayoutBox, Port } from "./input.ts";
|
|
3
4
|
import { shapeKindOf } from "./shapes.ts";
|
|
4
5
|
import { RECORD_EPS } from "./tolerance.ts";
|
|
@@ -44,6 +45,16 @@ export function sizeFits(box: LayoutBox, w: number, h: number): boolean {
|
|
|
44
45
|
return b.aspect === "free" || Math.abs(w * box.h - h * box.w) <= RECORD_EPS * (box.w + box.h);
|
|
45
46
|
}
|
|
46
47
|
|
|
48
|
+
/**
|
|
49
|
+
* This function tells if a placement can give a box the shape `shape` at the size `w` by `h`.
|
|
50
|
+
* The box needs `resize.reshape`, and the bounding box of the shape stays inside the rectangle from 0, 0 to `w`, `h`, with the tolerance `RECORD_EPS`.
|
|
51
|
+
*/
|
|
52
|
+
export function shapeFits(box: LayoutBox, shape: Region, w: number, h: number): boolean {
|
|
53
|
+
if (box.resize?.reshape !== true) return false;
|
|
54
|
+
const b = boundsOf(shape);
|
|
55
|
+
return b.x >= -RECORD_EPS && b.y >= -RECORD_EPS && b.x + b.w <= w + RECORD_EPS && b.y + b.h <= h + RECORD_EPS;
|
|
56
|
+
}
|
|
57
|
+
|
|
47
58
|
/** This function gives a port of a box of the size `w` by `h` after a resize to `toW` by `toH`. A port with `along` keeps its value. */
|
|
48
59
|
function resizePort(port: Port, w: number, h: number, toW: number, toH: number): Port {
|
|
49
60
|
return {
|
|
@@ -71,7 +82,7 @@ export function resizeBox(box: LayoutBox, w: number, h: number): LayoutBox {
|
|
|
71
82
|
/**
|
|
72
83
|
* This function gives the problems of the `resize` of a box. A box without `resize` has no problem.
|
|
73
84
|
* Only a `rect` box without a shape can have a resize. Each bound is a positive finite number.
|
|
74
|
-
* The size of the box is in its bounds,
|
|
85
|
+
* The size of the box is in its bounds, `aspect` is `keep` or `free`, and `reshape` is `true` or `false`.
|
|
75
86
|
*/
|
|
76
87
|
export function resizeProblems(box: LayoutBox): string[] {
|
|
77
88
|
const r = box.resize;
|
|
@@ -85,6 +96,7 @@ export function resizeProblems(box: LayoutBox): string[] {
|
|
|
85
96
|
if (v !== undefined && !(typeof v === "number" && v > 0 && Number.isFinite(v))) problems.push(`${at}: the resize bound ${k} ${String(v)} is not positive and finite`);
|
|
86
97
|
}
|
|
87
98
|
if (r.aspect !== undefined && r.aspect !== "keep" && r.aspect !== "free") problems.push(`${at}: the resize aspect ${String(r.aspect)} is not keep or free`);
|
|
99
|
+
if (r.reshape !== undefined && typeof r.reshape !== "boolean") problems.push(`${at}: the resize reshape ${String(r.reshape)} is not true or false`);
|
|
88
100
|
const b = sizeBoundsOf(box);
|
|
89
101
|
if (!(b.minW <= box.w && box.w <= b.maxW)) problems.push(`${at}: the width ${box.w} is not in the resize bounds ${b.minW} to ${b.maxW}`);
|
|
90
102
|
if (!(b.minH <= box.h && box.h <= b.maxH)) problems.push(`${at}: the height ${box.h} is not in the resize bounds ${b.minH} to ${b.maxH}`);
|