mirror of
https://github.com/veekun/pokedex.git
synced 2024-08-20 18:16:34 +00:00
Rewrite _HeapDict.pop() a bit
This commit is contained in:
parent
e4c0d0b16b
commit
b27187e99a
1 changed files with 10 additions and 13 deletions
|
@ -775,7 +775,11 @@ def a_star(initial, expand, is_goal, estimate=lambda x: 0, notify=None,
|
|||
opened[initial] = (est, 0, est)
|
||||
came_from = {initial: None} # node -> (prev_node, came_from[prev_node])
|
||||
while True: # _HeapDict will raise StopIteration for us
|
||||
try:
|
||||
x, (f, g, h) = opened.pop()
|
||||
except IndexError:
|
||||
raise StopIteration
|
||||
|
||||
closed.add(x)
|
||||
|
||||
if notify is not None:
|
||||
|
@ -837,20 +841,13 @@ class _HeapDict(object):
|
|||
def pop(self):
|
||||
"""Return (key, value) with the smallest value.
|
||||
|
||||
Raise StopIteration (!!) if empty
|
||||
Raises IndexError if empty
|
||||
"""
|
||||
while True:
|
||||
try:
|
||||
value, key = heapq.heappop(self.heap)
|
||||
if value is self.dict[key]:
|
||||
value, key = heapq.heappop(self.heap) # raises IndexError for us
|
||||
if key in self.dict:
|
||||
del self.dict[key]
|
||||
return key, value
|
||||
except KeyError:
|
||||
# deleted from dict = not here
|
||||
pass
|
||||
except IndexError:
|
||||
# nothing more to pop
|
||||
raise StopIteration
|
||||
|
||||
###
|
||||
### Result objects
|
||||
|
|
Loading…
Reference in a new issue