PriorityQueue.py

Go to the documentation of this file.
00001 ##
00002 # 
00003 # This module provides three types of queues, with these constructors:
00004 #   Stack([items])  -- Create a Last In First Out queue, implemented as a list
00005 #   Queue([items])  -- Create a First In First Out queue
00006 #   PriorityQueue([items]) -- Create a queue where minimum item (by <) is first
00007 # Here [items] is an optional list of initial items; if omitted, queue is empty.
00008 # Each type supports the following methods and functions:
00009 #   len(q)          -- number of items in q (also q.__len__())
00010 #   q.append(item)  -- add an item to the queue
00011 #   q.extend(items) -- add each of the items to the queue
00012 #   q.pop()         -- remove and return the "first" item from the queue
00013 # 
00014 
00015 def Stack(items=None):
00016     "A stack, or last-in-first-out queue, is implemented as a list."
00017     return items or []
00018 
00019 class Queue:
00020     "A first-in-first-out queue."
00021     def __init__(self, items=None): self.start = 0; self.A = items or []
00022     def __len__(self):                return len(self.A) - self.start
00023     def append(self, item):           self.A.append(item)
00024     def extend(self, items):          self.A.extend(items)
00025 
00026     def pop(self):
00027         A = self.A
00028         item = A[self.start]
00029         self.start += 1
00030         if self.start > 100 and self.start > len(A)/2:
00031             del A[:self.start]
00032             self.start = 0
00033         return item
00034 
00035 class PriorityQueue:
00036     "A queue in which the minimum element (as determined by cmp) is first."
00037     def __init__(self, items=None, cmp=operator.lt):
00038         self.A = []; self.cmp = cmp;
00039         if items: self.extend(items)
00040       
00041     def __len__(self): return len(self.A)
00042 
00043     def append(self, item):
00044         A, cmp = self.A, self.cmp
00045         A.append(item)
00046         i = len(A) - 1
00047         while i > 0 and cmp(item, A[i//2]):
00048             A[i], i = A[i//2], i//2
00049         A[i] = item
00050 
00051     def extend(self, items):
00052         for item in items: self.append(item)
00053 
00054     def pop(self):
00055         A = self.A
00056         if len(A) == 1: return A.pop()
00057         e = A[0]
00058         A[0] = A.pop()
00059         self.heapify(0)
00060         return e
00061 
00062     def heapify(self, i):
00063         "Assumes A is an array whose left and right children are heaps,"
00064         "move A[i] into the correct position.  See CLR&S p. 130"
00065         A, cmp = self.A, self.cmp
00066         left, right, N = 2*i + 1, 2*i + 2, len(A)-1
00067         if left <= N and cmp(A[left], A[i]):
00068             smallest = left
00069         else:
00070             smallest = i
00071         if right <= N and cmp(A[right], A[smallest]):
00072             smallest = right
00073         if smallest != i:
00074             A[i], A[smallest] = A[smallest], A[i]
00075             self.heapify(smallest)
00076 
00077 if (__name__ == '__main__'):
00078     pass
00079 
00080 

© Copyright 2008-2009 Vyper Logix Corp., All Right Reserved; If you reference this document or any part of this document you must use the citation verbatim (including the link) "© Copyright 2008-2009 Vyper Logix Corp., All Right Reserved."

Notice: This source code contained in this document is NOT open source and is NOT being distributed as open source.

122,241 lines of code and growing...