@mark1russell7/packing 0.2.11 → 0.2.13
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 +524 -236
- 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 +44 -56
- 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
package/src/registry.ts
CHANGED
|
@@ -1,16 +1,59 @@
|
|
|
1
1
|
import { maxrects, push, rows, shelf, skyline } from "./adapters.ts";
|
|
2
|
+
import { aspectFit } from "./aspect-fit.ts";
|
|
2
3
|
import type { PackInput, PackOutput } from "./contract.ts";
|
|
3
4
|
import { portfolioStrategy } from "./portfolio-strategy.ts";
|
|
4
|
-
import { createContext, type Strategy } from "./strategy.ts";
|
|
5
|
+
import { createContext, type PackContext, type Strategy, type Supports } from "./strategy.ts";
|
|
5
6
|
|
|
6
7
|
/** Each single strategy of the package. The benchmark starts each one on each input of the corpus. */
|
|
7
8
|
export const strategies: readonly Strategy[] = Object.freeze([rows, skyline, shelf, push, maxrects] as Strategy[]);
|
|
8
9
|
|
|
10
|
+
/** The options of the strategy `aspect-fit` */
|
|
11
|
+
export interface AspectFitStrategyOptions {
|
|
12
|
+
/** The id of the wrapped strategy of the registry. The option is necessary. */
|
|
13
|
+
strategy?: string;
|
|
14
|
+
/** The options of the wrapped strategy over its defaults */
|
|
15
|
+
options?: unknown;
|
|
16
|
+
/** The target aspect of `aspectFit`. The default is `aspect` of the container, else 1. */
|
|
17
|
+
targetAspect?: number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** This function gives the union of the supports of some strategies. */
|
|
21
|
+
function unionOf(list: readonly Strategy[]): Supports {
|
|
22
|
+
const out: Record<string, string[]> = {};
|
|
23
|
+
for (const s of list) {
|
|
24
|
+
for (const [dim, values] of Object.entries(s.supports) as [string, string[]][]) {
|
|
25
|
+
const own = (out[dim] ??= []);
|
|
26
|
+
for (const v of values) if (!own.includes(v)) own.push(v);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return out as unknown as Supports;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The strategy `aspect-fit`: `aspectFit` of roadmap item k62 around the strategy of the option `strategy`. Its supports are
|
|
34
|
+
* the union of the supports of the single strategies. A cell that the wrapped strategy does not support throws its error.
|
|
35
|
+
* The strategy throws a `RangeError` without the option `strategy`.
|
|
36
|
+
*/
|
|
37
|
+
export const aspectFitStrategy: Strategy<AspectFitStrategyOptions> = {
|
|
38
|
+
id: "aspect-fit",
|
|
39
|
+
family: "aspect-fit",
|
|
40
|
+
catalog: [],
|
|
41
|
+
supports: unionOf(strategies),
|
|
42
|
+
defaults: {},
|
|
43
|
+
deterministic: true,
|
|
44
|
+
pack(input: PackInput, options: AspectFitStrategyOptions, context: PackContext): PackOutput {
|
|
45
|
+
if (options.strategy === undefined) throw new RangeError("The strategy aspect-fit needs the option strategy.");
|
|
46
|
+
const inner = strategy(options.strategy);
|
|
47
|
+
const merged: unknown = { ...(inner.defaults as object), ...(options.options as object | undefined) };
|
|
48
|
+
return aspectFit(inner, options.targetAspect === undefined ? {} : { targetAspect: options.targetAspect }).pack(input, merged, context);
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
|
|
9
52
|
/**
|
|
10
53
|
* The composite strategies of the registry: each one packs an input with other strategies. They are not in `strategies`.
|
|
11
|
-
* Since 0.2.7, the list has `portfolio` of roadmap item k20.
|
|
54
|
+
* Since 0.2.7, the list has `portfolio` of roadmap item k20. Roadmap item k62 adds `aspect-fit`.
|
|
12
55
|
*/
|
|
13
|
-
export const composites: readonly Strategy[] = Object.freeze([portfolioStrategy] as Strategy[]);
|
|
56
|
+
export const composites: readonly Strategy[] = Object.freeze([portfolioStrategy, aspectFitStrategy] as Strategy[]);
|
|
14
57
|
|
|
15
58
|
/** This function gives the strategy with an id, also a composite strategy. It throws a `RangeError` for an id that the registry does not have. */
|
|
16
59
|
export function strategy(id: string): Strategy {
|
|
@@ -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
|
+
}
|