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