precomputed rotated matrix in Matrix class

This commit is contained in:
Christoph Stahl 2023-12-15 10:13:37 +01:00
parent bb4eac6e1b
commit 3688946a24

View file

@ -40,9 +40,13 @@ class Matrix:
self.height = len(lines) self.height = len(lines)
self.width = len(lines[0]) self.width = len(lines[0])
self.array = array("u") self.array = array("u")
self.array_rot = array("u")
for line in lines: for line in lines:
self.array.fromlist(list(line)) self.array.fromlist(list(line))
for line in zip(*lines):
self.array_rot.fromlist(list(line))
def __getitem__(self, pos: Position | tuple[int, int]) -> str: def __getitem__(self, pos: Position | tuple[int, int]) -> str:
return self.array[self.width * pos[1] + pos[0]] return self.array[self.width * pos[1] + pos[0]]
@ -56,7 +60,8 @@ class Matrix:
return (self.get_row(y) for y in range(self.height)) return (self.get_row(y) for y in range(self.height))
def get_col(self, col_no: int) -> array[str]: def get_col(self, col_no: int) -> array[str]:
return array("u", [self[col_no, y] for y in range(self.height)]) return self.array_rot[self.height * col_no : self.width * (col_no + 1)]
# return array("u", [self[col_no, y] for y in range(self.height)])
def iter_cols(self) -> Iterable[array[str]]: def iter_cols(self) -> Iterable[array[str]]:
return (self.get_col(x) for x in range(self.width)) return (self.get_col(x) for x in range(self.width))