@ac-kit/math-grid 0.1.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.
Files changed (52) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/LICENSE +28 -0
  3. package/README.md +55 -0
  4. package/dist/bounded-grid2.d.ts +25 -0
  5. package/dist/bounded-grid2.d.ts.map +1 -0
  6. package/dist/bounded-grid2.js +67 -0
  7. package/dist/bounded-grid2.js.map +1 -0
  8. package/dist/frontier-tiling.d.ts +36 -0
  9. package/dist/frontier-tiling.d.ts.map +1 -0
  10. package/dist/frontier-tiling.js +94 -0
  11. package/dist/frontier-tiling.js.map +1 -0
  12. package/dist/grid-coord2.d.ts +11 -0
  13. package/dist/grid-coord2.d.ts.map +1 -0
  14. package/dist/grid-coord2.js +25 -0
  15. package/dist/grid-coord2.js.map +1 -0
  16. package/dist/grid-pack-dfs.d.ts +58 -0
  17. package/dist/grid-pack-dfs.d.ts.map +1 -0
  18. package/dist/grid-pack-dfs.js +95 -0
  19. package/dist/grid-pack-dfs.js.map +1 -0
  20. package/dist/grid-rect2.d.ts +17 -0
  21. package/dist/grid-rect2.d.ts.map +1 -0
  22. package/dist/grid-rect2.js +45 -0
  23. package/dist/grid-rect2.js.map +1 -0
  24. package/dist/grid-size2.d.ts +8 -0
  25. package/dist/grid-size2.d.ts.map +1 -0
  26. package/dist/grid-size2.js +10 -0
  27. package/dist/grid-size2.js.map +1 -0
  28. package/dist/grid2-pixel.d.ts +18 -0
  29. package/dist/grid2-pixel.d.ts.map +1 -0
  30. package/dist/grid2-pixel.js +63 -0
  31. package/dist/grid2-pixel.js.map +1 -0
  32. package/dist/grid2.d.ts +16 -0
  33. package/dist/grid2.d.ts.map +1 -0
  34. package/dist/grid2.js +57 -0
  35. package/dist/grid2.js.map +1 -0
  36. package/dist/index.d.ts +12 -0
  37. package/dist/index.d.ts.map +1 -0
  38. package/dist/index.js +12 -0
  39. package/dist/index.js.map +1 -0
  40. package/dist/infinite-grid2.d.ts +17 -0
  41. package/dist/infinite-grid2.d.ts.map +1 -0
  42. package/dist/infinite-grid2.js +45 -0
  43. package/dist/infinite-grid2.js.map +1 -0
  44. package/dist/row-grid2.d.ts +23 -0
  45. package/dist/row-grid2.d.ts.map +1 -0
  46. package/dist/row-grid2.js +65 -0
  47. package/dist/row-grid2.js.map +1 -0
  48. package/dist/strip-grid2.d.ts +36 -0
  49. package/dist/strip-grid2.d.ts.map +1 -0
  50. package/dist/strip-grid2.js +90 -0
  51. package/dist/strip-grid2.js.map +1 -0
  52. package/package.json +65 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ # Changelog
2
+
3
+ ## [0.1.0] - 2026-08-26
4
+
5
+ ### Added
6
+
7
+ - Initial release.
package/LICENSE ADDED
@@ -0,0 +1,28 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2024-2026 Anthony Champagne and contributors
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are met:
7
+
8
+ 1. Redistributions of source code must retain the above copyright notice, this
9
+ list of conditions and the following disclaimer.
10
+
11
+ 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ this list of conditions and the following disclaimer in the documentation
13
+ and/or other materials provided with the distribution.
14
+
15
+ 3. Neither the name of the copyright holder nor the names of its
16
+ contributors may be used to endorse or promote products derived from
17
+ this software without specific prior written permission.
18
+
19
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # @ac-kit/math-grid
2
+
3
+ Discrete two-dimensional integer lattices: grid coordinates, several storage
4
+ strategies, and the packing and tiling algorithms over them.
5
+
6
+ ```ts
7
+ import { BoundedGrid2 } from "@ac-kit/math-grid";
8
+
9
+ const grid = new BoundedGrid2<string>({ rows: 4, cols: 8 });
10
+
11
+ const area = { start: { row: 0, col: 0 }, span: { rows: 2, cols: 3 } };
12
+ if (grid.canPlaceRect(area)) {
13
+ grid.placeRect(area, "widget");
14
+ }
15
+
16
+ grid.getCell({ row: 1, col: 2 }); // "widget"
17
+ ```
18
+
19
+ ## Scope
20
+
21
+ Geometry where positions are integers and cells are the unit of measurement —
22
+ tile maps, bin packing, layout solvers, cellular automata.
23
+
24
+ Continuous geometry lives in `@ac-kit/math-geometry`; the conversion functions
25
+ between the two are here.
26
+
27
+ ## Contents
28
+
29
+ | Type | Storage | Bounds |
30
+ | --------------- | ------------- | ----------------------------- |
31
+ | `BoundedGrid2` | Flat 1D array | Fixed rows × columns |
32
+ | `InfiniteGrid2` | Sparse `Map` | Unbounded in both axes |
33
+ | `RowGrid2` | Row-major | Fixed rows, unbounded columns |
34
+ | `StripGrid2` | Row-major | Fixed columns, unbounded rows |
35
+
36
+ Plus `GridCoord2`, `GridSize2`, `GridRect2`, the pixel conversions
37
+ (`gridCoord2ToPoint2`, `gridRect2ToPixelRect2`, …), and the tiling algorithms
38
+ `findFrontierTilings` and `dfsPackGrid2`.
39
+
40
+ ## Choosing a storage strategy
41
+
42
+ The four grid types exist because the access pattern, not the abstraction,
43
+ determines the cost. `BoundedGrid2` is a single contiguous array with O(1)
44
+ indexed access and no per-cell overhead — the right choice whenever the extent
45
+ is known. `InfiniteGrid2` pays a hash lookup per access but only stores occupied
46
+ cells, which wins as soon as the grid is mostly empty. `RowGrid2` and
47
+ `StripGrid2` cover the asymmetric cases: a fixed number of tracks growing in one
48
+ direction, as in a timeline or a masonry layout.
49
+
50
+ All four implement the same `Grid2` interface, so an algorithm written against
51
+ it works with any of them.
52
+
53
+ See the
54
+ [generated API docs](https://anthochamp.github.io/node-essentials/api/math-grid/)
55
+ for the full reference.
@@ -0,0 +1,25 @@
1
+ import { GridCoord2 } from "./grid-coord2.js";
2
+ import { GridSize2 } from "./grid-size2.js";
3
+ import { Grid2 } from "./grid2.js";
4
+ /**
5
+ * Bounded grid: fixed row and column counts.
6
+ *
7
+ * Flat 1D storage: cells[row * cols + col].
8
+ */
9
+ export declare class BoundedGrid2<T> extends Grid2<T> {
10
+ readonly rows: number;
11
+ readonly cols: number;
12
+ readonly cells: (T | undefined)[];
13
+ constructor(size: GridSize2);
14
+ clone(): BoundedGrid2<T>;
15
+ contains(coord: GridCoord2): boolean;
16
+ getCell(coord: GridCoord2): T | undefined;
17
+ getCells(): IterableIterator<[GridCoord2, T]>;
18
+ /**
19
+ * Returns the first empty cell in reading order (row-major), or undefined if
20
+ * the grid is full.
21
+ */
22
+ getFirstEmpty(): GridCoord2 | undefined;
23
+ setCell(coord: GridCoord2, value: T): void;
24
+ }
25
+ //# sourceMappingURL=bounded-grid2.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bounded-grid2.d.ts","sourceRoot":"","sources":["../src/bounded-grid2.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAEnC;;;;GAIG;AACH,qBAAa,YAAY,CAAC,CAAC,CAAE,SAAQ,KAAK,CAAC,CAAC,CAAC;IAC5C,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAC,GAAG,SAAS,CAAC,EAAE,CAAC;IAElC,YAAY,IAAI,EAAE,SAAS,EAK1B;IAED,KAAK,IAAI,YAAY,CAAC,CAAC,CAAC,CAMvB;IAED,QAAQ,CAAC,KAAK,EAAE,UAAU,GAAG,OAAO,CAOnC;IAED,OAAO,CAAC,KAAK,EAAE,UAAU,GAAG,CAAC,GAAG,SAAS,CAKxC;IAEA,QAAQ,IAAI,gBAAgB,CAAC,CAAC,UAAU,EAAE,CAAC,CAAC,CAAC,CAS7C;IAED;;;OAGG;IACH,aAAa,IAAI,UAAU,GAAG,SAAS,CAStC;IAED,OAAO,CAAC,KAAK,EAAE,UAAU,EAAE,KAAK,EAAE,CAAC,GAAG,IAAI,CAOzC;CACD"}
@@ -0,0 +1,67 @@
1
+ import { Grid2 } from "./grid2.js";
2
+ /**
3
+ * Bounded grid: fixed row and column counts.
4
+ *
5
+ * Flat 1D storage: cells[row * cols + col].
6
+ */
7
+ export class BoundedGrid2 extends Grid2 {
8
+ rows;
9
+ cols;
10
+ cells;
11
+ constructor(size) {
12
+ super();
13
+ this.rows = size.rows;
14
+ this.cols = size.cols;
15
+ this.cells = Array.from({ length: size.rows * size.cols });
16
+ }
17
+ clone() {
18
+ const grid = new BoundedGrid2({ rows: this.rows, cols: this.cols });
19
+ for (let i = 0; i < this.cells.length; i++) {
20
+ grid.cells[i] = this.cells[i];
21
+ }
22
+ return grid;
23
+ }
24
+ contains(coord) {
25
+ return (coord.row >= 0 &&
26
+ coord.row < this.rows &&
27
+ coord.col >= 0 &&
28
+ coord.col < this.cols);
29
+ }
30
+ getCell(coord) {
31
+ if (!this.contains(coord)) {
32
+ return undefined;
33
+ }
34
+ return this.cells[coord.row * this.cols + coord.col];
35
+ }
36
+ *getCells() {
37
+ for (let row = 0; row < this.rows; row++) {
38
+ for (let col = 0; col < this.cols; col++) {
39
+ const value = this.cells[row * this.cols + col];
40
+ if (value !== undefined) {
41
+ yield [{ row, col }, value];
42
+ }
43
+ }
44
+ }
45
+ }
46
+ /**
47
+ * Returns the first empty cell in reading order (row-major), or undefined if
48
+ * the grid is full.
49
+ */
50
+ getFirstEmpty() {
51
+ for (let row = 0; row < this.rows; row++) {
52
+ for (let col = 0; col < this.cols; col++) {
53
+ if (this.cells[row * this.cols + col] === undefined) {
54
+ return { row, col };
55
+ }
56
+ }
57
+ }
58
+ return undefined;
59
+ }
60
+ setCell(coord, value) {
61
+ if (!this.contains(coord)) {
62
+ throw new Error(`Coordinate (row=${coord.row}, col=${coord.col}) out of bounds for bounded grid of size ${this.rows}×${this.cols}.`);
63
+ }
64
+ this.cells[coord.row * this.cols + coord.col] = value;
65
+ }
66
+ }
67
+ //# sourceMappingURL=bounded-grid2.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bounded-grid2.js","sourceRoot":"","sources":["../src/bounded-grid2.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAEnC;;;;GAIG;AACH,MAAM,OAAO,YAAgB,SAAQ,KAAQ;IACnC,IAAI,CAAS;IACb,IAAI,CAAS;IACb,KAAK,CAAoB;IAElC,YAAY,IAAe;QAC1B,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC;QACtB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC;QACtB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC,IAAI,CAAgB,EAAE,MAAM,EAAE,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC;IAC3E,CAAC;I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EAAE,CAAC;oBACzB,MAAM,CAAC,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE,KAAK,CAAC,CAAC;gBAC7B,CAAC;YACF,CAAC;QACF,CAAC;IACF,CAAC;IAED;;;OAGG;IACH,aAAa;QACZ,KAAK,IAAI,GAAG,GAAG,CAAC,EAAE,GAAG,GAAG,IAAI,CAAC,IAAI,EAAE,GAAG,EAAE,EAAE,CAAC;YAC1C,KAAK,IAAI,GAAG,GAAG,CAAC,EAAE,GAAG,GAAG,IAAI,CAAC,IAAI,EAAE,GAAG,EAAE,EAAE,CAAC;gBAC1C,IAAI,IAAI,CAAC,KAAK,CAAC,GAAG,GAAG,IAAI,CAAC,IAAI,GAAG,GAAG,CAAC,KAAK,SAAS,EAAE,CAAC;oBACrD,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,CAAC;gBACrB,CAAC;YACF,CAAC;QACF,CAAC;QACD,OAAO,SAAS,CAAC;IAClB,CAAC;IAED,OAAO,CAAC,KAAiB,EAAE,KAAQ;QAClC,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;YAC3B,MAAM,IAAI,KAAK,CACd,mBAAmB,KAAK,CAAC,GAAG,SAAS,KAAK,CAAC,GAAG,4CAA4C,IAAI,CAAC,IAAI,IAAI,IAAI,CAAC,IAAI,GAAG,CACnH,CAAC;QACH,CAAC;QACD,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,GAAG,GAAG,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;IACvD,CAAC;CACD"}
@@ -0,0 +1,36 @@
1
+ import { GridRect2 } from "./grid-rect2.js";
2
+ import { GridSize2 } from "./grid-size2.js";
3
+ import { StripGrid2 } from "./strip-grid2.js";
4
+ export type FrontierMove<T> = {
5
+ rect: GridRect2;
6
+ item: T;
7
+ index: number;
8
+ };
9
+ export type FrontierTiling<T> = {
10
+ moves: FrontierMove<T>[];
11
+ usedIndices: Set<number>;
12
+ };
13
+ export type FrontierTilingOptions = {
14
+ lookahead?: number;
15
+ maxComponentRows?: number;
16
+ maxNodes?: number;
17
+ maxSolutions?: number;
18
+ /**
19
+ * Called after the search finishes. Receives the number of nodes explored and
20
+ * solutions found.
21
+ */
22
+ onComplete?: ((stats: {
23
+ nodes: number;
24
+ solutions: number;
25
+ }) => void) | null;
26
+ };
27
+ /**
28
+ * Finds all possible tilings of the given items in the given grid, starting
29
+ * from the first empty cell.
30
+ *
31
+ * The search is depth-first and will stop either: - when the maximum number of
32
+ * component rows is reached, - when the maximum number of solutions is reached,
33
+ * or - when the maximum number of nodes is reached.
34
+ */
35
+ export declare function findFrontierTilings<T>(grid: StripGrid2<T>, items: readonly T[], getSize: (item: T) => GridSize2, getDedupeKey: (item: T) => string, getMoveScore: (move: FrontierMove<T>) => number, options?: FrontierTilingOptions): FrontierTiling<T>[];
36
+ //# sourceMappingURL=frontier-tiling.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"frontier-tiling.d.ts","sourceRoot":"","sources":["../src/frontier-tiling.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAE9C,MAAM,MAAM,YAAY,CAAC,CAAC,IAAI;IAC7B,IAAI,EAAE,SAAS,CAAC;IAChB,IAAI,EAAE,CAAC,CAAC;IACR,KAAK,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,MAAM,MAAM,cAAc,CAAC,CAAC,IAAI;IAC/B,KAAK,EAAE,YAAY,CAAC,CAAC,CAAC,EAAE,CAAC;IACzB,WAAW,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;CACzB,CAAC;AAEF,MAAM,MAAM,qBAAqB,GAAG;IACnC,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB;;;OAGG;IACH,UAAU,CAAC,EAAE,CAAC,CAAC,KAAK,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,MAAM,CAAA;KAAE,KAAK,IAAI,CAAC,GAAG,IAAI,CAAC;CAC5E,CAAC;AAUF;;;;;;;GAOG;AACH,wBAAgB,mBAAmB,CAAC,CAAC,EACpC,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC,EACnB,KAAK,EAAE,SAAS,CAAC,EAAE,EACnB,OAAO,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,SAAS,EAC/B,YAAY,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,MAAM,EACjC,YAAY,EAAE,CAAC,IAAI,EAAE,YAAY,CAAC,CAAC,CAAC,KAAK,MAAM,EAC/C,OAAO,CAAC,EAAE,qBAAqB,GAC7B,cAAc,CAAC,CAAC,CAAC,EAAE,CAkFrB"}
@@ -0,0 +1,94 @@
1
+ const DEFAULT_OPTIONS_ = {
2
+ lookahead: 10,
3
+ maxComponentRows: 10,
4
+ maxNodes: 10000,
5
+ maxSolutions: 1,
6
+ onComplete: null,
7
+ };
8
+ /**
9
+ * Finds all possible tilings of the given items in the given grid, starting
10
+ * from the first empty cell.
11
+ *
12
+ * The search is depth-first and will stop either: - when the maximum number of
13
+ * component rows is reached, - when the maximum number of solutions is reached,
14
+ * or - when the maximum number of nodes is reached.
15
+ */
16
+ export function findFrontierTilings(grid, items, getSize, getDedupeKey, getMoveScore, options) {
17
+ const opts = {
18
+ lookahead: options?.lookahead ?? DEFAULT_OPTIONS_.lookahead,
19
+ maxComponentRows: options?.maxComponentRows ?? DEFAULT_OPTIONS_.maxComponentRows,
20
+ maxNodes: options?.maxNodes ?? DEFAULT_OPTIONS_.maxNodes,
21
+ maxSolutions: options?.maxSolutions ?? DEFAULT_OPTIONS_.maxSolutions,
22
+ onComplete: options?.onComplete ?? DEFAULT_OPTIONS_.onComplete,
23
+ };
24
+ const used = Array.from({ length: items.length }).fill(false);
25
+ const moves = [];
26
+ const solutions = [];
27
+ let nodes = 0;
28
+ const start = grid.getFirstEmpty();
29
+ // The end row of the component is the maximum row of any placed item in the current component.
30
+ let componentEndRow = start.row;
31
+ function search() {
32
+ if (solutions.length >= opts.maxSolutions) {
33
+ return;
34
+ }
35
+ const next = grid.getFirstEmpty();
36
+ // If the next empty cell is after the end of the component, we have a valid solution.
37
+ if (moves.length > 0 && next.row >= componentEndRow) {
38
+ solutions.push({
39
+ moves: moves.map((move) => ({ ...move })),
40
+ usedIndices: new Set(moves.map((move) => move.index)),
41
+ });
42
+ return;
43
+ }
44
+ // If the next empty cell is too far down, or if we have explored too many nodes, stop searching.
45
+ if (next.row - start.row >= opts.maxComponentRows ||
46
+ ++nodes > opts.maxNodes) {
47
+ return;
48
+ }
49
+ // Explore all candidate moves from the current frontier, sorted by score.
50
+ for (const move of getCandidates_(grid, items, used, next, getSize, getDedupeKey, getMoveScore, opts.lookahead)) {
51
+ used[move.index] = true;
52
+ moves.push(move);
53
+ grid.placeRect(move.rect, move.item);
54
+ const previousEndRow = componentEndRow;
55
+ // Update the end row of the component to include the newly placed item.
56
+ componentEndRow = Math.max(componentEndRow, move.rect.start.row + move.rect.span.rows);
57
+ search();
58
+ componentEndRow = previousEndRow;
59
+ grid.clearRect(move.rect);
60
+ moves.pop();
61
+ used[move.index] = false;
62
+ if (solutions.length >= opts.maxSolutions) {
63
+ return;
64
+ }
65
+ }
66
+ }
67
+ search();
68
+ opts.onComplete?.({ nodes, solutions: solutions.length });
69
+ return solutions;
70
+ }
71
+ function* getCandidates_(grid, items, used, coord, getSize, getDedupeKey, getMoveScore, lookahead) {
72
+ const candidates = [];
73
+ const tried = new Set();
74
+ for (let index = 0; index < Math.min(items.length, lookahead); index++) {
75
+ if (used[index]) {
76
+ continue;
77
+ }
78
+ const item = items[index];
79
+ const key = getDedupeKey(item);
80
+ if (tried.has(key)) {
81
+ continue;
82
+ }
83
+ tried.add(key);
84
+ const rect = {
85
+ start: coord,
86
+ span: getSize(item),
87
+ };
88
+ if (grid.canPlaceRect(rect)) {
89
+ candidates.push({ item, index, rect });
90
+ }
91
+ }
92
+ yield* candidates.sort((a, b) => getMoveScore(a) - getMoveScore(b));
93
+ }
94
+ //# sourceMappingURL=frontier-tiling.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"frontier-tiling.js","sourceRoot":"","sources":["../src/frontier-tiling.ts"],"names":[],"mappings":"AA4BA,MAAM,gBAAgB,GAAoC;IACzD,SAAS,EAAE,EAAE;IACb,gBAAgB,EAAE,EAAE;IACpB,QAAQ,EAAE,KAAK;IACf,YAAY,EAAE,CAAC;IACf,UAAU,EAAE,IAAI;CAChB,CAAC;AAEF;;;;;;;GAOG;AACH,MAAM,UAAU,mBAAmB,CAClC,IAAmB,EACnB,KAAmB,EACnB,OAA+B,EAC/B,YAAiC,EACjC,YAA+C,EAC/C,OAA+B;IAE/B,MAAM,IAAI,GAAoC;QAC7C,SAAS,EAAE,OAAO,EAAE,SAAS,IAAI,gBAAgB,CAAC,SAAS;QAC3D,gBAAgB,EACf,OAAO,EAAE,gBAAgB,IAAI,gBAAgB,CAAC,gBAAgB;QAC/D,QAAQ,EAAE,OAAO,EAAE,QAAQ,IAAI,gBAAgB,CAAC,QAAQ;QACxD,YAAY,EAAE,OAAO,EAAE,YAAY,IAAI,gBAAgB,CAAC,YAAY;QACpE,UAAU,EAAE,OAAO,EAAE,UAAU,IAAI,gBAAgB,CAAC,UAAU;KAC9D,CAAC;IAEF,MAAM,IAAI,GAAG,KAAK,CAAC,IAAI,CAAU,EAAE,MAAM,EAAE,KAAK,CAAC,MAAM,EAAE,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACvE,MAAM,KAAK,GAAsB,EAAE,CAAC;IACpC,MAAM,SAAS,GAAwB,EAAE,CAAC;IAC1C,IAA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,YAAY,CAAC,IAAI,CAAC,CAAC;QAC/B,IAAI,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC;YACpB,SAAS;QACV,CAAC;QACD,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QAEf,MAAM,IAAI,GAAc;YACvB,KAAK,EAAE,KAAK;YACZ,IAAI,EAAE,OAAO,CAAC,IAAI,CAAC;SACnB,CAAC;QAEF,IAAI,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,EAAE,CAAC;YAC7B,UAAU,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;QACxC,CAAC;IACF,CAAC;IAED,KAAK,CAAC,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,YAAY,CAAC,CAAC,CAAC,GAAG,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC;AACrE,CAAC"}
@@ -0,0 +1,11 @@
1
+ import type { DistanceType } from "@ac-kit/math-linear";
2
+ export type GridCoord2 = {
3
+ row: number;
4
+ col: number;
5
+ };
6
+ export declare function gridCoord2Add(a: GridCoord2, b: GridCoord2): GridCoord2;
7
+ export declare function gridCoord2Distance(a: GridCoord2, b: GridCoord2, type?: DistanceType): number;
8
+ export declare function gridCoord2Equals(a: GridCoord2, b: GridCoord2): boolean;
9
+ export declare function gridCoord2Scale(coord: GridCoord2, factor: number): GridCoord2;
10
+ export declare function gridCoord2Sub(a: GridCoord2, b: GridCoord2): GridCoord2;
11
+ //# sourceMappingURL=grid-coord2.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"grid-coord2.d.ts","sourceRoot":"","sources":["../src/grid-coord2.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAExD,MAAM,MAAM,UAAU,GAAG;IACxB,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;CACZ,CAAC;AAEF,wBAAgB,aAAa,CAAC,CAAC,EAAE,UAAU,EAAE,CAAC,EAAE,UAAU,GAAG,UAAU,CAEtE;AAED,wBAAgB,kBAAkB,CACjC,CAAC,EAAE,UAAU,EACb,CAAC,EAAE,UAAU,EACb,IAAI,GAAE,YAA0B,GAC9B,MAAM,CAWR;AAED,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,UAAU,EAAE,CAAC,EAAE,UAAU,GAAG,OAAO,CAEtE;AAED,wBAAgB,eAAe,CAAC,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,GAAG,UAAU,CAE7E;AAED,wBAAgB,aAAa,CAAC,CAAC,EAAE,UAAU,EAAE,CAAC,EAAE,UAAU,GAAG,UAAU,CAEtE"}
@@ -0,0 +1,25 @@
1
+ export function gridCoord2Add(a, b) {
2
+ return { row: a.row + b.row, col: a.col + b.col };
3
+ }
4
+ export function gridCoord2Distance(a, b, type = "euclidean") {
5
+ const dr = Math.abs(a.row - b.row);
6
+ const dc = Math.abs(a.col - b.col);
7
+ switch (type) {
8
+ case "manhattan":
9
+ return dr + dc;
10
+ case "chebyshev":
11
+ return Math.max(dr, dc);
12
+ case "euclidean":
13
+ return Math.sqrt(dr * dr + dc * dc);
14
+ }
15
+ }
16
+ export function gridCoord2Equals(a, b) {
17
+ return a.row === b.row && a.col === b.col;
18
+ }
19
+ export function gridCoord2Scale(coord, factor) {
20
+ return { row: coord.row * factor, col: coord.col * factor };
21
+ }
22
+ export function gridCoord2Sub(a, b) {
23
+ return { row: a.row - b.row, col: a.col - b.col };
24
+ }
25
+ //# sourceMappingURL=grid-coord2.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"grid-coord2.js","sourceRoot":"","sources":["../src/grid-coord2.ts"],"names":[],"mappings":"AAOA,MAAM,UAAU,aAAa,CAAC,CAAa,EAAE,CAAa;IACzD,OAAO,EAAE,GAAG,EAAE,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,GA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1,58 @@
1
+ import { GridCoord2 } from "./grid-coord2.js";
2
+ import { GridSize2 } from "./grid-size2.js";
3
+ import { StripGrid2 } from "./strip-grid2.js";
4
+ /** Minimum shape that a placeable item must expose. */
5
+ export type GridPackItem = {
6
+ span: GridSize2;
7
+ };
8
+ export type GridPackOptions<T extends GridPackItem> = {
9
+ /**
10
+ * Assigns a numeric priority weight to a candidate at the current grid
11
+ * position. Lower weights are tried first. Candidates with equal weight are
12
+ * tried in the order they appear in `items`.
13
+ *
14
+ * @param candidate - The item under consideration.
15
+ * @param lastPlaced - The most recently placed item, or `null` at the root.
16
+ * @param remaining - All items not yet placed at this DFS node.
17
+ */
18
+ classify: (candidate: T, lastPlaced: T | null, remaining: readonly T[]) => number;
19
+ /**
20
+ * Returns a deduplication key for shape-based DFS pruning: only one item per
21
+ * unique key is expanded at each grid position. Defaults to
22
+ * `"${cols}x${rows}"`.
23
+ */
24
+ shapeKey?: (item: T) => string;
25
+ /**
26
+ * Maximum DFS node expansions per frontier row before drilling a hole. The
27
+ * counter resets each time the DFS reaches a row it has never visited before,
28
+ * so this is a per-row limit, not a per-full-search limit.
29
+ */
30
+ rowBudget?: number;
31
+ };
32
+ export type GridPackResult<T> = {
33
+ /**
34
+ * Items successfully placed by the DFS, mapped to their `[row, col]` position
35
+ * (0-based).
36
+ */
37
+ place: Map<T, GridCoord2>;
38
+ /**
39
+ * The occupancy grid after the DFS run. Callers may reuse it to place
40
+ * remaining (tail) items without overlap.
41
+ */
42
+ grid: StripGrid2<boolean>;
43
+ };
44
+ /**
45
+ * Packs items into a `W`-column grid using depth-first search with
46
+ * backtracking.
47
+ *
48
+ * Items are placed in reading order (left-to-right, top-to-bottom) starting at
49
+ * the first empty cell. The caller controls candidate ordering through the
50
+ * `classify` weight function. When the node budget is exceeded, the algorithm
51
+ * preserves state at the deepest reached point, drills a hole to fill the
52
+ * remainder of the frontier row, and restarts the DFS from the next row.
53
+ *
54
+ * Items not placed by the DFS are absent from `result.place`. The caller is
55
+ * responsible for placing them (tail placement) using `result.grid`.
56
+ */
57
+ export declare function dfsPackGrid2<T extends GridPackItem>(items: T[], columnCount: number, options: GridPackOptions<T>): GridPackResult<T>;
58
+ //# sourceMappingURL=grid-pack-dfs.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"grid-pack-dfs.d.ts","sourceRoot":"","sources":["../src/grid-pack-dfs.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAE9C,uDAAuD;AACvD,MAAM,MAAM,YAAY,GAAG;IAC1B,IAAI,EAAE,SAAS,CAAC;CAChB,CAAC;AAEF,MAAM,MAAM,eAAe,CAAC,CAAC,SAAS,YAAY,IAAI;IACrD;;;;;;;;OAQG;IACH,QAAQ,EAAE,CACT,SAAS,EAAE,CAAC,EACZ,UAAU,EAAE,CAAC,GAAG,IAAI,EACpB,SAAS,EAAE,SAAS,CAAC,EAAE,KACnB,MAAM,CAAC;IACZ;;;;OAIG;IACH,QAAQ,CAAC,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,MAAM,CAAC;IAC/B;;;;OAIG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF,MAAM,MAAM,cAAc,CAAC,CAAC,IAAI;IAC/B;;;OAGG;IACH,KAAK,EAAE,GAAG,CAAC,CAAC,EAAE,UAAU,CAAC,CAAC;IAC1B;;;OAGG;IACH,IAAI,EAAE,UAAU,CAAC,OAAO,CAAC,CAAC;CAC1B,CAAC;AAQF;;;;;;;;;;;;GAYG;AACH,wBAAgB,YAAY,CAAC,CAAC,SAAS,YAAY,EAClD,KAAK,EAAE,CAAC,EAAE,EACV,WAAW,EAAE,MAAM,EACnB,OAAO,EAAE,eAAe,CAAC,CAAC,CAAC,GACzB,cAAc,CAAC,CAAC,CAAC,CAkGnB"}
@@ -0,0 +1,95 @@
1
+ import { backtrack, BacktrackExhausted } from "@ac-kit/algo";
2
+ import { StripGrid2 } from "./strip-grid2.js";
3
+ /**
4
+ * Packs items into a `W`-column grid using depth-first search with
5
+ * backtracking.
6
+ *
7
+ * Items are placed in reading order (left-to-right, top-to-bottom) starting at
8
+ * the first empty cell. The caller controls candidate ordering through the
9
+ * `classify` weight function. When the node budget is exceeded, the algorithm
10
+ * preserves state at the deepest reached point, drills a hole to fill the
11
+ * remainder of the frontier row, and restarts the DFS from the next row.
12
+ *
13
+ * Items not placed by the DFS are absent from `result.place`. The caller is
14
+ * responsible for placing them (tail placement) using `result.grid`.
15
+ */
16
+ export function dfsPackGrid2(items, columnCount, options) {
17
+ const { classify, shapeKey = (it) => `${it.span.cols}x${it.span.rows}`, rowBudget = 5000, } = options;
18
+ const grid = new StripGrid2(columnCount);
19
+ const used = Array.from({ length: items.length }).fill(false);
20
+ const place = new Map();
21
+ let nodes = 0;
22
+ let placedCount = 0;
23
+ let frontierRow = -1;
24
+ const candidatesOf = (move) => {
25
+ const coord = grid.getFirstEmpty();
26
+ // Reset the node counter each time the DFS reaches a row it has never
27
+ // visited before. This makes rowBudget a per-row limit: the DFS gets a
28
+ // fresh budget for each new row rather than one budget for the whole search.
29
+ if (coord.row > frontierRow) {
30
+ frontierRow = coord.row;
31
+ nodes = 0;
32
+ }
33
+ const remaining = items.filter((_, i) => !used[i]);
34
+ const triedShapes = new Set();
35
+ const weighted = [];
36
+ for (let i = 0; i < items.length; i++) {
37
+ if (used[i])
38
+ continue;
39
+ const it = items[i];
40
+ const key = shapeKey(it);
41
+ if (triedShapes.has(key))
42
+ continue;
43
+ if (!grid.canPlaceRect({ start: coord, span: it.span })) {
44
+ continue;
45
+ }
46
+ triedShapes.add(key);
47
+ const weight = classify(it, move?.item ?? null, remaining);
48
+ weighted.push({ move: { item: it, index: i, coord }, weight });
49
+ }
50
+ weighted.sort((a, b) => a.weight - b.weight);
51
+ return weighted.map((w) => w.move);
52
+ };
53
+ const apply = (move) => {
54
+ used[move.index] = true;
55
+ placedCount++;
56
+ place.set(move.item, move.coord);
57
+ grid.placeRect({ start: move.coord, span: move.item.span }, true);
58
+ };
59
+ const undo = (move) => {
60
+ grid.placeRect({ start: move.coord, span: move.item.span }, false);
61
+ place.delete(move.item);
62
+ placedCount--;
63
+ used[move.index] = false;
64
+ };
65
+ const isGoal = () => placedCount === items.length;
66
+ const isExhausted = () => {
67
+ if (++nodes > rowBudget) {
68
+ return true;
69
+ }
70
+ return false;
71
+ };
72
+ // Outer loop: run DFS; on BUDGET_EXCEEDED, place/grid/used hold the deepest-
73
+ // reached state (no backtracking). Fill the rest of the frontier row with a
74
+ // hole, reset the budget, and continue from the next row.
75
+ while (placedCount < items.length) {
76
+ nodes = 0;
77
+ const result = backtrack(undefined, {
78
+ candidatesOf,
79
+ apply,
80
+ undo,
81
+ isGoal,
82
+ isExhausted,
83
+ });
84
+ if (result !== BacktrackExhausted) {
85
+ break;
86
+ }
87
+ const firstCoord = grid.getFirstEmpty();
88
+ grid.placeRect({
89
+ start: firstCoord,
90
+ span: { cols: columnCount - firstCoord.col, rows: 1 },
91
+ }, true);
92
+ }
93
+ return { place, grid };
94
+ }
95
+ //# sourceMappingURL=grid-pack-dfs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"grid-pack-dfs.js","sourceRoot":"","sources":["../src/grid-pack-dfs.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAI7D,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAuD9C;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,YAAY,CAC3B,KAAU,EACV,WAAmB,EACnB,OAA2B;IAE3B,MAAM,EACL,QAAQ,EACR,QAAQ,GAAG,CAAC,EAAK,EAAE,EAAE,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,IAAI,IAAI,EAAE,CAAC,IAAI,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AAE,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,EAAE,KAAK,CAAC,CAAC;QACnE,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACxB,WAAW,EAAE,CAAC;QACd,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC;IAC1B,CAAC,CAAC;IAEF,MAAM,MAAM,GAAG,GAAY,EAAE,CAAC,WAAW,KAAK,KAAK,CAAC,MAAM,CAAC;IAE3D,MAAM,WAAW,GAAG,GAAY,EAAE;QACjC,IAAI,EAAE,KAAK,GAAG,SAAS,EAAE,CAAC;YACzB,OAAO,IAAI,CAAC;QACb,CAAC;QACD,OAAO,KAAK,CAAC;IACd,CAAC,CAAC;IAEF,6EAA6E;IAC7E,4EAA4E;IAC5E,0DAA0D;IAC1D,OAAO,WAAW,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC;QACnC,KAAK,GAAG,CAAC,CAAC;QACV,MAAM,MAAM,GAAG,SAAS,CAAC,SAAS,EAAE;YACnC,YAAY;YACZ,KAAK;YACL,IAAI;YACJ,MAAM;YACN,WAAW;SACX,CAAC,CAAC;QAEH,IAAI,MAAM,KAAK,kBAAkB,EAAE,CAAC;YACnC,MAAM;QACP,CAAC;QAED,MAAM,UAAU,GAAG,IAAI,CAAC,aAAa,EAAE,CAAC;QACxC,IAAI,CAAC,SAAS,CACb;YACC,KAAK,EAAE,UAAU;YACjB,IAAI,EAAE,EAAE,IAAI,EAAE,WAAW,GAAG,UAAU,CAAC,GAAG,EAAE,IAAI,EAAE,CAAC,EAAE;SACrD,EACD,IAAI,CACJ,CAAC;IACH,CAAC;IAED,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC;AACxB,CAAC"}
@@ -0,0 +1,17 @@
1
+ import { GridCoord2 } from "./grid-coord2.js";
2
+ import { GridSize2 } from "./grid-size2.js";
3
+ export type GridRect2 = {
4
+ start: GridCoord2;
5
+ span: GridSize2;
6
+ };
7
+ export declare function gridRect2ContainsCoord(rect: GridRect2, coord: GridCoord2): boolean;
8
+ export declare function gridRect2ContainsRect(container: GridRect2, contained: GridRect2): boolean;
9
+ /**
10
+ * Returns the exclusive end coordinate (one past the last cell in each
11
+ * dimension).
12
+ */
13
+ export declare function gridRect2End(rect: GridRect2): GridCoord2;
14
+ export declare function gridRect2Equals(a: GridRect2, b: GridRect2): boolean;
15
+ export declare function gridRect2Intersects(a: GridRect2, b: GridRect2): boolean;
16
+ export declare function gridRect2Translate(rect: GridRect2, offset: GridCoord2): GridRect2;
17
+ //# sourceMappingURL=grid-rect2.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"grid-rect2.d.ts","sourceRoot":"","sources":["../src/grid-rect2.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAoB,MAAM,kBAAkB,CAAC;AAChE,OAAO,EAAE,SAAS,EAAmB,MAAM,iBAAiB,CAAC;AAE7D,MAAM,MAAM,SAAS,GAAG;IACvB,KAAK,EAAE,UAAU,CAAC;IAClB,IAAI,EAAE,SAAS,CAAC;CAChB,CAAC;AAEF,wBAAgB,sBAAsB,CACrC,IAAI,EAAE,SAAS,EACf,KAAK,EAAE,UAAU,GACf,OAAO,CAOT;AAED,wBAAgB,qBAAqB,CACpC,SAAS,EAAE,SAAS,EACpB,SAAS,EAAE,SAAS,GAClB,OAAO,CAST;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,SAAS,GAAG,UAAU,CAKxD;AAED,wBAAgB,eAAe,CAAC,CAAC,EAAE,SAAS,EAAE,CAAC,EAAE,SAAS,GAAG,OAAO,CAEnE;AAED,wBAAgB,mBAAmB,CAAC,CAAC,EAAE,SAAS,EAAE,CAAC,EAAE,SAAS,GAAG,OAAO,CAOvE;AAED,wBAAgB,kBAAkB,CACjC,IAAI,EAAE,SAAS,EACf,MAAM,EAAE,UAAU,GAChB,SAAS,CAQX"}
@@ -0,0 +1,45 @@
1
+ import { gridCoord2Equals } from "./grid-coord2.js";
2
+ import { gridSize2Equals } from "./grid-size2.js";
3
+ export function gridRect2ContainsCoord(rect, coord) {
4
+ return (coord.row >= rect.start.row &&
5
+ coord.row < rect.start.row + rect.span.rows &&
6
+ coord.col >= rect.start.col &&
7
+ coord.col < rect.start.col + rect.span.cols);
8
+ }
9
+ export function gridRect2ContainsRect(container, contained) {
10
+ return (contained.start.row >= container.start.row &&
11
+ contained.start.col >= container.start.col &&
12
+ contained.start.row + contained.span.rows <=
13
+ container.start.row + container.span.rows &&
14
+ contained.start.col + contained.span.cols <=
15
+ container.start.col + container.span.cols);
16
+ }
17
+ /**
18
+ * Returns the exclusive end coordinate (one past the last cell in each
19
+ * dimension).
20
+ */
21
+ export function gridRect2End(rect) {
22
+ return {
23
+ row: rect.start.row + rect.span.rows,
24
+ col: rect.start.col + rect.span.cols,
25
+ };
26
+ }
27
+ export function gridRect2Equals(a, b) {
28
+ return gridCoord2Equals(a.start, b.start) && gridSize2Equals(a.span, b.span);
29
+ }
30
+ export function gridRect2Intersects(a, b) {
31
+ return !(a.start.row + a.span.rows <= b.start.row ||
32
+ b.start.row + b.span.rows <= a.start.row ||
33
+ a.start.col + a.span.cols <= b.start.col ||
34
+ b.start.col + b.span.cols <= a.start.col);
35
+ }
36
+ export function gridRect2Translate(rect, offset) {
37
+ return {
38
+ start: {
39
+ row: rect.start.row + offset.row,
40
+ col: rect.start.col + offset.col,
41
+ },
42
+ span: rect.span,
43
+ };
44
+ }
45
+ //# sourceMappingURL=grid-rect2.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"grid-rect2.js","sourceRoot":"","sources":["../src/grid-rect2.ts"],"names":[],"mappings":"AAAA,OAAO,EAAc,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAChE,OAAO,EAAa,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAO7D,MAAM,UAAU,sBAAsB,CACrC,IAAe,EACf,KAAiB;IAEjB,OAAO,CACN,KAAK,CAAC,GAAG,IAAI,IA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AC,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC;AAC9E,CAAC;AAED,MAAM,UAAU,mBAAmB,CAAC,CAAY,EAAE,CAAY;IAC7D,OAAO,CAAC,CACP,CAAC,CAAC,KAAK,CAAC,GAAG,GAAG,CAAC,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,CAAC,KAAK,CAAC,GAAG;QACxC,CAAC,CAAC,KAAK,CAAC,GAAG,GAAG,CAAC,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,CAAC,KAAK,CAAC,GAAG;QACxC,CAAC,CAAC,KAAK,CAAC,GAAG,GAAG,CAAC,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,CAAC,KAAK,CAAC,GAAG;QACxC,CAAC,CAAC,KAAK,CAAC,GAAG,GAAG,CAAC,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,CAAC,KAAK,CAAC,GAAG,CACxC,CAAC;AACH,CAAC;AAED,MAAM,UAAU,kBAAkB,CACjC,IAAe,EACf,MAAkB;IAElB,OAAO;QACN,KAAK,EAAE;YACN,GAAG,EAAE,IAAI,CAAC,KAAK,CAAC,GAAG,GAAG,MAAM,CAAC,GAAG;YAChC,GAAG,EAAE,IAAI,CAAC,KAAK,CAAC,GAAG,GAAG,MAAM,CAAC,GAAG;SAChC;QACD,IAAI,EAAE,IAAI,CAAC,IAAI;KACf,CAAC;AACH,CAAC"}
@@ -0,0 +1,8 @@
1
+ export type GridSize2 = {
2
+ rows: number;
3
+ cols: number;
4
+ };
5
+ export declare function gridSize2Area(size: GridSize2): number;
6
+ export declare function gridSize2Equals(a: GridSize2, b: GridSize2): boolean;
7
+ export declare function gridSize2Scale(size: GridSize2, factor: number): GridSize2;
8
+ //# sourceMappingURL=grid-size2.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"grid-size2.d.ts","sourceRoot":"","sources":["../src/grid-size2.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,SAAS,GAAG;IACvB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;CACb,CAAC;AAEF,wBAAgB,aAAa,CAAC,IAAI,EAAE,SAAS,GAAG,MAAM,CAErD;AAED,wBAAgB,eAAe,CAAC,CAAC,EAAE,SAAS,EAAE,CAAC,EAAE,SAAS,GAAG,OAAO,CAEnE;AAED,wBAAgB,cAAc,CAAC,IAAI,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,GAAG,SAAS,CAEzE"}