Merge pull request #1 from cpacia/nodeheapfix

Bug fix in NodeHeap - no duplicates
This commit is contained in:
Brian Muller 2015-06-04 16:16:03 -04:00
commit 42b6c949ac

View File

@ -94,8 +94,9 @@ class NodeHeap(object):
nodes = [nodes] nodes = [nodes]
for node in nodes: for node in nodes:
distance = self.node.distanceTo(node) if node not in self:
heapq.heappush(self.heap, (distance, node)) distance = self.node.distanceTo(node)
heapq.heappush(self.heap, (distance, node))
def __len__(self): def __len__(self):
return min(len(self.heap), self.maxsize) return min(len(self.heap), self.maxsize)
@ -104,5 +105,11 @@ class NodeHeap(object):
nodes = heapq.nsmallest(self.maxsize, self.heap) nodes = heapq.nsmallest(self.maxsize, self.heap)
return iter(map(itemgetter(1), nodes)) return iter(map(itemgetter(1), nodes))
def __contains__(self, node):
for distance, n in self.heap:
if node.id == n.id:
return True
return False
def getUncontacted(self): def getUncontacted(self):
return [n for n in self if n.id not in self.contacted] return [n for n in self if n.id not in self.contacted]