Created
May 5, 2013 20:32
-
-
Save st0le/5522078 to your computer and use it in GitHub Desktop.
min/max pqueue implementation with tie breaker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import heapq as hp | |
class pqueue: | |
def __init__(self, minheap = True): | |
self.heap = [] | |
self.mul = 1 if minheap else -1 | |
self.count = 0 | |
def size(self): | |
return len(self.heap) | |
def push(self,item): | |
if item and isinstance(item, tuple) and isinstance(item[0],int): | |
self.count = self.count + 1 | |
hp.heappush(self.heap,(item[0] * self.mul,self.count) + item[1:]) | |
else: | |
raise Exception("parameter has to be a tuple with the first entry as an integer.") | |
def pop(self): | |
if self.empty(): raise Exception("pqueue is empty.") | |
item = hp.heappop(self.heap) | |
return (self.mul * item[0],) + item[2:] | |
def __str__(self): | |
return str(list((self.mul * p, r) for p,q,r in self.heap)) | |
def __repr__(self): | |
return str(list((self.mul * p, r) for p,q,r in self.heap)) | |
def empty(self): | |
return len(self.heap) == 0 | |
def clear(self): | |
self.heap = [] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment