Welcome, guest | Sign In | My Account | Store | Cart
import heapq

class PriorityQueue(dict):
    def __init__(self, _, f):
        self.f = f
        self.heap = []
    
    def __delitem__(self, item):
        super(PriorityQueue, self).__delitem__(item)
        del self.heap[self.heap.index(item)]
        heapq.heapify(self.heap)
    
    def pop(self):
        _, smallest = heapq.heappop(self.heap)
        super(PriorityQueue, self).__delitem__(smallest)
        return smallest

    def append(self, item):
        self[item]=item
        heapq.heappush(self.heap, (self.f(item), item))

History