Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import typing
- class BBMatr:
- """Разреженная бинарная матрица с возможностью итерирования по столбцам и по строкам"""
- def __init__(self):
- self.rows_by_col : typing.Dict[int, set] = dict()
- self.cols_by_row: typing.Dict[int, set] = dict()
- def __sizeof__(self):
- return sum(map(sys.getsizeof, [self.rows_by_col, self.cols_by_row,]))
- def __setitem__(self, key, value: bool):
- r, c = key
- rows_by_col = self.rows_by_col
- cols_by_row = self.cols_by_row
- rows = rows_by_col.setdefault(c, set())
- cols = cols_by_row.setdefault(r, set())
- if value:
- rows.add(r)
- cols.add(c)
- else:
- rows.discard(r)
- cols.discard(c)
- if not rows:
- rows_by_col.pop(c)
- if not cols:
- cols_by_row.pop(r)
- def __getitem__(self, key):
- r, c = key
- return c in self.cols_by_row.get(r, set())
- def __iter__(self):
- for r, cols in self.cols_by_row.items():
- for c in cols:
- yield r, c
- def iter_cols_by_row(self, r):
- yield from self.cols_by_row.get(r, [])
- def iter_rows_by_col(self, c):
- yield from self.rows_by_col.get(c, [])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement