44from .grid import Grid
55from .world import World
66
7+
78class SimpleHeap :
8- """Simple wrapper around open_list that keeps track of order and removed nodes automatically."""
9+ """Simple wrapper around open_list that keeps track of order and removed
10+ nodes automatically."""
911
1012 def __init__ (self , node , grid ):
1113 self .grid = grid
1214 self .open_list = [self ._get_node_tuple (node , 0 )]
1315 self .removed_node_tuples = set ()
1416 self .heap_order = {}
1517 self .number_pushed = 0
16-
18+
1719 def _get_node_tuple (self , node , heap_order ):
1820 if isinstance (self .grid , Graph ):
1921 return (node .f , heap_order , node .node_id )
@@ -23,7 +25,7 @@ def _get_node_tuple(self, node, heap_order):
2325 return (node .f , heap_order , node .x , node .y , node .grid_id )
2426 else :
2527 assert False , "unsupported heap node node=%s" % node
26-
28+
2729 def _get_node_id (self , node ):
2830 if isinstance (self .grid , Graph ):
2931 return node .node_id
@@ -32,13 +34,13 @@ def _get_node_id(self, node):
3234 elif isinstance (self .grid , World ):
3335 return (node .x , node .y , node .grid_id )
3436
35-
3637 def pop_node (self ):
3738 """
3839 Pops node off the heap. i.e. returns the one with the lowest f.
3940
4041 Notes:
41- 1. Checks if that values is in removed_node_tuples first, if not tries again.
42+ 1. Checks if that values is in removed_node_tuples first, if not tries
43+ again.
4244 2. We use this approach to avoid invalidating the heap structure.
4345 """
4446 node_tuple = heapq .heappop (self .open_list )
@@ -50,10 +52,11 @@ def pop_node(self):
5052 elif isinstance (self .grid , Grid ):
5153 node = self .grid .node (node_tuple [2 ], node_tuple [3 ])
5254 elif isinstance (self .grid , World ):
53- node = self .grid .grids [node_tuple [4 ]].node (node_tuple [2 ], node_tuple [3 ])
55+ node = self .grid .grids [
56+ node_tuple [4 ]].node (node_tuple [2 ], node_tuple [3 ])
5457
5558 return node
56-
59+
5760 def push_node (self , node ):
5861 """
5962 Push node into heap.
@@ -67,12 +70,13 @@ def push_node(self, node):
6770 self .heap_order [node_id ] = self .number_pushed
6871
6972 heapq .heappush (self .open_list , node_tuple )
70-
73+
7174 def remove_node (self , node , f ):
7275 """
7376 Remove the node from the heap.
7477
75- This just stores it in a set and we just ignore the node if it does get popped from the heap.
78+ This just stores it in a set and we just ignore the node if it does
79+ get popped from the heap.
7680
7781 :param node: The node to remove.
7882 :param f: The old f value of the node.
@@ -81,7 +85,7 @@ def remove_node(self, node, f):
8185 heap_order = self .heap_order [node_id ]
8286 node_tuple = self ._get_node_tuple (node , heap_order )
8387 self .removed_node_tuples .add (node_tuple )
84-
88+
8589 def __len__ (self ):
8690 """Returns the length of the open_list."""
87- return len (self .open_list )
91+ return len (self .open_list )
0 commit comments