@mark1russell7/packing 0.2.10 → 0.2.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/README.md +13 -0
- package/build/index.d.ts +81 -9
- package/build/index.d.ts.map +1 -1
- package/build/index.js +555 -200
- package/build/index.js.map +1 -1
- package/package.json +1 -1
- package/src/aspect-fit.ts +133 -0
- package/src/corpus.ts +2 -5
- package/src/frame.ts +24 -8
- package/src/generators.ts +58 -15
- package/src/index.ts +4 -1
- package/src/maxrects.ts +67 -17
- package/src/portfolio-strategy.ts +13 -6
- package/src/portfolio.ts +13 -30
- package/src/registry.ts +46 -3
- package/src/select-key.ts +48 -0
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// The key of a pick over the outputs of one input: the hard breaks, then some measures of `MEASURE_SPECS`, then the hash.
|
|
2
|
+
// `packPortfolio` and `aspectFit` use it. The module is internal: the package root exports only `hardBreaks`.
|
|
3
|
+
|
|
4
|
+
import type { PortfolioKey, SelectLevel } from "@mark1russell7/layout-contract";
|
|
5
|
+
import { checkHard } from "./check.ts";
|
|
6
|
+
import type { PackInput, PackOutput } from "./contract.ts";
|
|
7
|
+
import { outputHash } from "./layout-input.ts";
|
|
8
|
+
import { MEASURE_SPECS, measure, roundMeasures, type LayoutMeasures } from "./measures.ts";
|
|
9
|
+
|
|
10
|
+
/** This function gives the count of the hard breaks of an output: the sum of `checkHard` with `exactContainer`. */
|
|
11
|
+
export function hardBreaks(input: PackInput, output: PackOutput): number {
|
|
12
|
+
return Object.values(checkHard(input, output, undefined, { exactContainer: true })).reduce((sum, n) => sum + n, 0);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** This function gives the level of a measure id with its spec of `MEASURE_SPECS`. It throws a `RangeError` for an unknown id. */
|
|
16
|
+
export function measureLevel(id: string): SelectLevel {
|
|
17
|
+
const spec = (MEASURE_SPECS as Readonly<Record<string, SelectLevel["spec"] | undefined>>)[id];
|
|
18
|
+
if (spec === undefined || id === "hard") throw new RangeError(`A level of a key is a measure of MEASURE_SPECS, not "${id}".`);
|
|
19
|
+
return { id, spec };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* This function makes the key of a pick: `hardBreaks`, then the measures of `levels` in order, then `outputHash`.
|
|
24
|
+
*
|
|
25
|
+
* - The levels read the measures after `roundMeasures`, as a record keeps them. A replay of the records gives the same pick.
|
|
26
|
+
* - The measures of one output are computed one time.
|
|
27
|
+
* - `view` gives the input of the measures. The default is the input. The hard breaks always read the input.
|
|
28
|
+
*/
|
|
29
|
+
export function measureKey(levels: readonly SelectLevel[], view: (input: PackInput) => PackInput = (input) => input): PortfolioKey<PackInput, PackOutput> {
|
|
30
|
+
const cache = new WeakMap<PackOutput, LayoutMeasures>();
|
|
31
|
+
const measured = (input: PackInput, output: PackOutput): LayoutMeasures => {
|
|
32
|
+
let m = cache.get(output);
|
|
33
|
+
if (m === undefined) {
|
|
34
|
+
m = roundMeasures(measure(view(input), output));
|
|
35
|
+
cache.set(output, m);
|
|
36
|
+
}
|
|
37
|
+
return m;
|
|
38
|
+
};
|
|
39
|
+
return {
|
|
40
|
+
hard: hardBreaks,
|
|
41
|
+
levels: levels.map((level) => ({
|
|
42
|
+
id: level.id,
|
|
43
|
+
spec: level.spec,
|
|
44
|
+
value: (input: PackInput, output: PackOutput): number => (measured(input, output) as unknown as Readonly<Record<string, number>>)[level.id]!,
|
|
45
|
+
})),
|
|
46
|
+
hash: outputHash,
|
|
47
|
+
};
|
|
48
|
+
}
|