Last active
December 9, 2018 05:06
-
-
Save plasmatic1/cae8065429a5ab1c8d3878d90b138b03 to your computer and use it in GitHub Desktop.
Priority Queue Implementation
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 | |
class PriorityQueue: | |
def __init__(self, key=None, heap=[]): | |
self.key = key | |
self.heap = heapq.heapify(heap) if heap else [] | |
def __bool__(self): return bool(self.heap) | |
def __len__(self): return len(self.heap) | |
def __iter__(self): | |
if self.key: return map(lambda x: x[1], self.heap) | |
else: return iter(self.heap) | |
def __str__(self): | |
if self.key: str(map(lambda x: str(x[1]), self.heap)) | |
else: return str(self.heap) | |
def push(self, x): | |
if self.key: heapq.heappush(self.heap, (self.key(x), x)) | |
else: heapq.heappush(self.heap, x) | |
def top(self): | |
if self.key: return self.heap[0][1] | |
else: return self.heap[0] | |
def pop(self): | |
if self.key: return heapq.heappop(self.heap)[1] | |
else: return heapq.heappop(self.heap) | |
def pushpop(self, x): | |
if self.key: return heapq.heappushpop(self.heap, (self.key(x), x))[1] | |
else: return heapq.heappop(self.heap) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
added ability for non natural sorting methods