Skip to content

Commit

Permalink
added additional helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
harisankar95 committed Jan 19, 2024
1 parent 1fa33a9 commit 122ec20
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions pathfinding3d/core/heap.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def __init__(self, node: GridNode, grid: Union[Grid, World]):

self.grid = grid
self._get_node_tuple = self._determine_node_retrieval_function()
self._get_node = self._determine_node_function()
self.open_list = [self._get_node_tuple(node, 0)]
self.removed_node_tuples = set()
self.heap_order = {}
Expand Down Expand Up @@ -57,6 +58,29 @@ def _determine_node_retrieval_function(self) -> Callable:

raise ValueError("Unsupported grid type")

def _determine_node_function(self) -> Callable:
"""
Determines the node function based on the type of grid.
Returns
-------
function
A function that takes a node tuple and returns the corresponding node.
Raises
------
ValueError
If the grid is not of type Grid or World.
"""

if isinstance(self.grid, Grid):
return lambda node_tuple: self.grid.node(*node_tuple[2:])

if isinstance(self.grid, World):
return lambda node_tuple: self.grid.grids[node_tuple[5]].node(*node_tuple[2:5])

raise ValueError("Unsupported grid type")

def pop_node(self) -> GridNode:
"""
Pops the node with the lowest cost from the heap.
Expand All @@ -70,12 +94,7 @@ def pop_node(self) -> GridNode:
while node_tuple in self.removed_node_tuples:
node_tuple = heapq.heappop(self.open_list)

if isinstance(self.grid, Grid):
node = self.grid.node(*node_tuple[2:])
elif isinstance(self.grid, World):
node = self.grid.grids[node_tuple[5]].node(*node_tuple[2:5])

return node
return self._get_node(node_tuple)

def push_node(self, node: GridNode):
"""
Expand Down

0 comments on commit 122ec20

Please sign in to comment.