Skip to content

Commit b6accc6

Browse files
Tortarrht
authored andcommitted
HexGrid: exchange iter_neighborhood with get_neighborhood
1 parent de03089 commit b6accc6

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

mesa/space.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -598,10 +598,10 @@ class HexGrid(Grid):
598598
def torus_adj_2d(self, pos: Coordinate) -> Coordinate:
599599
return pos[0] % self.width, pos[1] % self.height
600600

601-
def iter_neighborhood(
601+
def get_neighborhood(
602602
self, pos: Coordinate, include_center: bool = False, radius: int = 1
603-
) -> Iterator[Coordinate]:
604-
"""Return an iterator over cell coordinates that are in the
603+
) -> list[Coordinate]:
604+
"""Return a list of coordinates that are in the
605605
neighborhood of a certain point. To calculate the neighborhood
606606
for a HexGrid the parity of the x coordinate of the point is
607607
important, the neighborhood can be sketched as:
@@ -617,7 +617,7 @@ def iter_neighborhood(
617617
radius: radius, in cells, of neighborhood to get.
618618
619619
Returns:
620-
An iterator of coordinate tuples representing the neighborhood. For
620+
A list of coordinate tuples representing the neighborhood. For
621621
example with radius 1, it will return list with number of elements
622622
equals at most 9 (8) if Moore, 5 (4) if Von Neumann (if not
623623
including the center).
@@ -626,8 +626,7 @@ def iter_neighborhood(
626626
neighborhood = self._neighborhood_cache.get(cache_key, None)
627627

628628
if neighborhood is not None:
629-
yield from neighborhood
630-
return
629+
return neighborhood
631630

632631
queue = collections.deque()
633632
queue.append(pos)
@@ -687,7 +686,7 @@ def iter_neighborhood(
687686
neighborhood = sorted(coordinates)
688687
self._neighborhood_cache[cache_key] = neighborhood
689688

690-
yield from neighborhood
689+
return neighborhood
691690

692691
def neighbor_iter(self, pos: Coordinate) -> Iterator[Agent]:
693692
"""Iterate over position neighbors.
@@ -702,11 +701,11 @@ def neighbor_iter(self, pos: Coordinate) -> Iterator[Agent]:
702701
)
703702
return self.iter_neighbors(pos)
704703

705-
def get_neighborhood(
704+
def iter_neighborhood(
706705
self, pos: Coordinate, include_center: bool = False, radius: int = 1
707-
) -> list[Coordinate]:
708-
"""Return a list of cells that are in the neighborhood of a
709-
certain point.
706+
) -> Iterator[Coordinate]:
707+
"""Return an iterator over cell coordinates that are in the
708+
neighborhood of a certain point.
710709
711710
Args:
712711
pos: Coordinate tuple for the neighborhood to get.
@@ -715,10 +714,9 @@ def get_neighborhood(
715714
radius: radius, in cells, of neighborhood to get.
716715
717716
Returns:
718-
A list of coordinate tuples representing the neighborhood;
719-
With radius 1
717+
An iterator of coordinate tuples representing the neighborhood.
720718
"""
721-
return list(self.iter_neighborhood(pos, include_center, radius))
719+
yield from self.get_neighborhood(pos, include_center, radius)
722720

723721
def iter_neighbors(
724722
self, pos: Coordinate, include_center: bool = False, radius: int = 1

0 commit comments

Comments
 (0)