From b27187e99a5ec0cba2f86be338b085192893e1ee Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Wed, 27 Apr 2011 14:50:07 +0300 Subject: [PATCH] Rewrite _HeapDict.pop() a bit --- pokedex/util/movesets.py | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/pokedex/util/movesets.py b/pokedex/util/movesets.py index d2a191f..bd1144f 100755 --- a/pokedex/util/movesets.py +++ b/pokedex/util/movesets.py @@ -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 - x, (f, g, h) = opened.pop() + 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]: - del self.dict[key] - return key, value - except KeyError: - # deleted from dict = not here - pass - except IndexError: - # nothing more to pop - raise StopIteration + value, key = heapq.heappop(self.heap) # raises IndexError for us + if key in self.dict: + del self.dict[key] + return key, value ### ### Result objects