@mark1russell7/packing 0.1.3 → 0.1.4

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/packing",
4
- "version": "0.1.3",
4
+ "version": "0.1.4",
5
5
  "description": "Rectangle packing: shelf, guillotine and skyline strategies, push cascades and measures of a layout.",
6
6
  "keywords": [
7
7
  "rectangle-packing",
package/src/corpus.ts ADDED
@@ -0,0 +1,65 @@
1
+ // The manifest of a corpus: `layout-corpus/1` of `docs/FORMAT.md`.
2
+
3
+ import { generator } from "./generators.ts";
4
+ import type { CorpusCell, GeneratorParams, LayoutInput } from "./layout-input.ts";
5
+
6
+ /** One spec of a corpus: a generator, its parameters and its seeds. */
7
+ export interface CorpusSpec {
8
+ /** The name of the generator. */
9
+ generator: string;
10
+ /** The version of the generator. Without it, the spec uses the version of the package. A different version is an error. */
11
+ version?: number;
12
+ /** The parameters over the defaults of the generator. */
13
+ params?: GeneratorParams;
14
+ seeds: readonly number[];
15
+ }
16
+
17
+ /** One input in the index of a manifest. */
18
+ export interface CorpusEntry {
19
+ id: string;
20
+ hash: string;
21
+ cell: CorpusCell;
22
+ }
23
+
24
+ /** The manifest `corpus.json` of a corpus. */
25
+ export interface CorpusManifest {
26
+ schema: "layout-corpus/1";
27
+ /** The name of the corpus. Each input has it in `set`. */
28
+ corpus: string;
29
+ /** The specs, each with its version and its full parameters. */
30
+ specs: { generator: string; version: number; params: GeneratorParams; seeds: number[] }[];
31
+ inputs: CorpusEntry[];
32
+ }
33
+
34
+ /** A corpus: the manifest and the input files, in the order of the specs and the seeds. */
35
+ export interface Corpus {
36
+ manifest: CorpusManifest;
37
+ inputs: LayoutInput[];
38
+ }
39
+
40
+ /**
41
+ * This function generates a corpus from specs. Each input gets the name of the corpus as its set. The function throws a
42
+ * `RangeError` for an unknown generator, a different version or 2 inputs with the same id.
43
+ */
44
+ export function corpus(name: string, specs: readonly CorpusSpec[]): Corpus {
45
+ const manifest: CorpusManifest = { schema: "layout-corpus/1", corpus: name, specs: [], inputs: [] };
46
+ const inputs: LayoutInput[] = [];
47
+ const ids = new Set<string>();
48
+ for (const spec of specs) {
49
+ const gen = generator(spec.generator);
50
+ if (spec.version !== undefined && spec.version !== gen.version) {
51
+ throw new RangeError(`The generator "${gen.name}" has the version ${gen.version}, not ${spec.version}.`);
52
+ }
53
+ const params = gen.withDefaults(spec.params);
54
+ const cell = gen.cell(params);
55
+ manifest.specs.push({ generator: gen.name, version: gen.version, params, seeds: [...spec.seeds] });
56
+ for (const seed of spec.seeds) {
57
+ const file = gen.generate(seed, params, name);
58
+ if (ids.has(file.id)) throw new RangeError(`The corpus has 2 inputs with the id "${file.id}".`);
59
+ ids.add(file.id);
60
+ inputs.push(file);
61
+ manifest.inputs.push({ id: file.id, hash: file.hash, cell });
62
+ }
63
+ }
64
+ return { manifest, inputs };
65
+ }