-
Notifications
You must be signed in to change notification settings - Fork 0
/
neighbours_cache.py
30 lines (24 loc) · 961 Bytes
/
neighbours_cache.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from client.message.extra_types import XY
from .constants import X, Y
from .map_utils import MapUtilsMixin
class NeighboursCache(MapUtilsMixin):
def __init__(self, size: XY):
self.size: XY = size
self.cache: dict[XY, list[XY]] = self._generate_neighbours()
def __contains__(self, pt: XY) -> bool:
return pt in self.cache
def __getitem__(self, pt: XY) -> list[XY]:
return self.cache[pt]
def _generate_neighbours(self) -> dict[XY, list[XY]]:
neighbours = dict()
for y in range(self.size[Y]):
for x in range(self.size[X]):
pt = (x, y)
if pt == (0, 0):
neighbours[pt] = [pt for pt in self.neighbours(pt)]
else:
neighbours[pt] = [
self.position_add(zero_pt, (x, y))
for zero_pt in neighbours[(0, 0)]
]
return neighbours