|
| 1 | +from dataclasses import dataclass, field |
| 2 | +from functools import cached_property |
| 3 | +from itertools import product |
| 4 | +from typing import Any, Iterable, Sequence, TypeVar |
| 5 | + |
| 6 | +from kirin import types |
| 7 | +from kirin.dialects import ilist |
| 8 | + |
| 9 | +from bloqade.geometry.dialects import grid |
| 10 | + |
| 11 | +NumX = TypeVar("NumX") |
| 12 | +NumY = TypeVar("NumY") |
| 13 | + |
| 14 | + |
| 15 | +@dataclass(eq=False) |
| 16 | +class FilledGrid(grid.Grid[NumX, NumY]): |
| 17 | + x_spacing: tuple[float, ...] = field(init=False) |
| 18 | + y_spacing: tuple[float, ...] = field(init=False) |
| 19 | + x_init: float | None = field(init=False) |
| 20 | + y_init: float | None = field(init=False) |
| 21 | + |
| 22 | + parent: grid.Grid[NumX, NumY] |
| 23 | + vacancies: frozenset[tuple[int, int]] |
| 24 | + |
| 25 | + def __post_init__(self): |
| 26 | + self.x_spacing = self.parent.x_spacing |
| 27 | + self.y_spacing = self.parent.y_spacing |
| 28 | + self.x_init = self.parent.x_init |
| 29 | + self.y_init = self.parent.y_init |
| 30 | + |
| 31 | + self.type = types.Generic( |
| 32 | + FilledGrid, |
| 33 | + types.Literal(len(self.x_spacing) + 1), |
| 34 | + types.Literal(len(self.y_spacing) + 1), |
| 35 | + ) |
| 36 | + |
| 37 | + def __hash__(self): |
| 38 | + return hash((self.parent, self.vacancies)) |
| 39 | + |
| 40 | + def __eq__(self, other: Any) -> bool: |
| 41 | + return ( |
| 42 | + isinstance(other, FilledGrid) |
| 43 | + and self.parent == other.parent |
| 44 | + and self.vacancies == other.vacancies |
| 45 | + ) |
| 46 | + |
| 47 | + def is_equal(self, other: Any) -> bool: |
| 48 | + return self == other |
| 49 | + |
| 50 | + @cached_property |
| 51 | + def positions(self) -> ilist.IList[tuple[float, float], Any]: |
| 52 | + positions = tuple( |
| 53 | + (x, y) |
| 54 | + for (ix, x), (iy, y) in product( |
| 55 | + enumerate(self.x_positions), enumerate(self.y_positions) |
| 56 | + ) |
| 57 | + if (ix, iy) not in self.vacancies |
| 58 | + ) |
| 59 | + |
| 60 | + return ilist.IList(positions) |
| 61 | + |
| 62 | + @classmethod |
| 63 | + def fill( |
| 64 | + cls, grid_obj: grid.Grid[NumX, NumY], filled: Sequence[tuple[int, int]] |
| 65 | + ) -> "FilledGrid[NumX, NumY]": |
| 66 | + num_x, num_y = grid_obj.shape |
| 67 | + |
| 68 | + if isinstance(grid_obj, FilledGrid): |
| 69 | + vacancies = grid_obj.vacancies |
| 70 | + parent = grid_obj.parent |
| 71 | + else: |
| 72 | + vacancies = frozenset(product(range(num_x), range(num_y))) |
| 73 | + parent = grid_obj |
| 74 | + |
| 75 | + vacancies = vacancies - frozenset(filled) |
| 76 | + |
| 77 | + return cls(parent=parent, vacancies=vacancies) |
| 78 | + |
| 79 | + @classmethod |
| 80 | + def vacate( |
| 81 | + cls, grid_obj: grid.Grid[NumX, NumY], vacancies: Iterable[tuple[int, int]] |
| 82 | + ) -> "FilledGrid[NumX, NumY]": |
| 83 | + |
| 84 | + if isinstance(grid_obj, FilledGrid): |
| 85 | + input_vacancies = grid_obj.vacancies |
| 86 | + parent = grid_obj.parent |
| 87 | + else: |
| 88 | + input_vacancies = frozenset() |
| 89 | + parent = grid_obj |
| 90 | + |
| 91 | + input_vacancies = input_vacancies.union(vacancies) |
| 92 | + |
| 93 | + return cls(parent=parent, vacancies=input_vacancies) |
| 94 | + |
| 95 | + def get_view( # type: ignore |
| 96 | + self, x_indices: ilist.IList[int, Any], y_indices: ilist.IList[int, Any] |
| 97 | + ): |
| 98 | + remapping_x = {ix: i for i, ix in enumerate(x_indices)} |
| 99 | + remapping_y = {iy: i for i, iy in enumerate(y_indices)} |
| 100 | + return FilledGrid( |
| 101 | + parent=self.parent.get_view(x_indices, y_indices), |
| 102 | + vacancies=frozenset( |
| 103 | + (remapping_x[x], remapping_y[y]) |
| 104 | + for x, y in self.vacancies |
| 105 | + if x in remapping_x and y in remapping_y |
| 106 | + ), |
| 107 | + ) |
| 108 | + |
| 109 | + def shift(self, x_shift: float, y_shift: float): |
| 110 | + return FilledGrid( |
| 111 | + parent=self.parent.shift(x_shift, y_shift), |
| 112 | + vacancies=self.vacancies, |
| 113 | + ) |
| 114 | + |
| 115 | + def scale(self, x_scale: float, y_scale: float): |
| 116 | + return FilledGrid( |
| 117 | + parent=self.parent.scale(x_scale, y_scale), |
| 118 | + vacancies=self.vacancies, |
| 119 | + ) |
| 120 | + |
| 121 | + def repeat(self, x_times: int, y_times: int, x_gap: float, y_gap: float): |
| 122 | + new_parent = self.parent.repeat(x_times, y_times, x_gap, y_gap) |
| 123 | + x_dim, y_dim = self.shape |
| 124 | + vacancies = frozenset( |
| 125 | + (x + x_dim * i, y + y_dim * j) |
| 126 | + for i, j, (x, y) in product(range(x_times), range(y_times), self.vacancies) |
| 127 | + ) |
| 128 | + return FilledGrid.vacate(new_parent, vacancies) |
| 129 | + |
| 130 | + def row_x_pos(self, row_index: int): |
| 131 | + x_vacancies = {x for x, y in self.vacancies if y == row_index} |
| 132 | + return ilist.IList( |
| 133 | + [x for i, x in enumerate(self.parent.x_positions) if i not in x_vacancies] |
| 134 | + ) |
| 135 | + |
| 136 | + def col_y_pos(self, column_index: int): |
| 137 | + y_vacancies = {y for x, y in self.vacancies if x == column_index} |
| 138 | + return ilist.IList( |
| 139 | + [y for i, y in enumerate(self.y_positions) if i not in y_vacancies] |
| 140 | + ) |
| 141 | + |
| 142 | + |
| 143 | +FilledGridType = types.Generic(FilledGrid, types.TypeVar("NumX"), types.TypeVar("NumY")) |
0 commit comments