@mark1russell7/layout-contract 0.3.7 → 0.3.8

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.7",
4
+ "version": "0.3.8",
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",
@@ -49,7 +49,7 @@
49
49
  "node": ">=22.12"
50
50
  },
51
51
  "devDependencies": {
52
- "@mark1russell7/cue": "^1.0.2",
52
+ "@mark1russell7/cue": "^2.1.1",
53
53
  "@types/node": "^26.6.4",
54
54
  "esbuild": "^0.28.2",
55
55
  "fast-check": "^4.10.2",
@@ -690,6 +690,20 @@ interface Combo {
690
690
  const optionIds = (choices: Choices): Map<string, string | null> => new Map([...choices].map(([k, o]) => [k, o?.id ?? null]));
691
691
  const keeps = (space: StrategySpace, choices: Choices): boolean => (space.constraints ?? []).every((c) => holds(c, optionIds(choices)));
692
692
 
693
+ /**
694
+ * This function gives the constraints of a space for each stage: a constraint goes to the last stage that it names.
695
+ * The last list holds each constraint that names a stage outside the space, so a walk examines it at the end.
696
+ */
697
+ function constraintsByStage(space: StrategySpace): readonly (readonly SpaceConstraint[])[] {
698
+ const place = new Map(space.stages.map((s, i) => [s.name, i]));
699
+ const lists: SpaceConstraint[][] = Array.from({ length: space.stages.length + 1 }, () => []);
700
+ for (const c of space.constraints ?? []) {
701
+ const named = [c.if.stage, "not" in c.then ? c.then.not.stage : c.then.stage];
702
+ lists[Math.max(...named.map((name) => place.get(name) ?? space.stages.length))]!.push(c);
703
+ }
704
+ return lists;
705
+ }
706
+
693
707
  /** This function gives the count of the member lists of a meta option without a pool from `base` base configs, or `null` above `limit`. */
694
708
  function memberLists(base: bigint, rule: MemberRule, limit: bigint): bigint | null {
695
709
  let sum = 0n;
@@ -738,9 +752,12 @@ function combos(space: StrategySpace, levels: number, meta: boolean, limit: bigi
738
752
  };
739
753
  const out: Combo[] = [];
740
754
  const stages = space.stages;
755
+ // A space can have hundreds of constraints. Each one is examined at its last stage, so the walk leaves a branch at the first break.
756
+ const checks = constraintsByStage(space);
757
+ const ids = new Map<string, string | null>();
741
758
  const walk = (i: number, choices: Map<string, StageOption | null>, weight: bigint): void => {
742
759
  if (i === stages.length) {
743
- if (keeps(space, choices)) out.push({ choices: new Map(choices), base: true, weight });
760
+ if (checks[i]!.every((c) => holds(c, ids))) out.push({ choices: new Map(choices), base: true, weight });
744
761
  return;
745
762
  }
746
763
  const s = stages[i]!;
@@ -748,9 +765,11 @@ function combos(space: StrategySpace, levels: number, meta: boolean, limit: bigi
748
765
  if (s.optional) list.push(null);
749
766
  for (const o of list) {
750
767
  choices.set(s.name, o);
751
- walk(i + 1, choices, o === null ? weight : weight * countOf(o));
768
+ ids.set(s.name, o?.id ?? null);
769
+ if (checks[i]!.every((c) => holds(c, ids))) walk(i + 1, choices, o === null ? weight : weight * countOf(o));
752
770
  }
753
771
  choices.delete(s.name);
772
+ ids.delete(s.name);
754
773
  };
755
774
  walk(0, new Map(), 1n);
756
775
  if (!meta) return out;