]> granicus.if.org Git - python/commitdiff
add /F's PriorityQueue example
authorSkip Montanaro <skip@pobox.com>
Wed, 26 Jun 2002 05:07:28 +0000 (05:07 +0000)
committerSkip Montanaro <skip@pobox.com>
Wed, 26 Jun 2002 05:07:28 +0000 (05:07 +0000)
Doc/lib/libbisect.tex

index 8c108a6a5e463e69e1c15809691f4e6c0c6fe525..32418914b8560d5f399013ec0a66e7ee34147187 100644 (file)
@@ -61,7 +61,7 @@ The following functions are provided:
 \end{funcdesc}
 
 
-\subsection{Example}
+\subsection{Examples}
 \nodename{bisect-example}
 
 The \function{bisect()} function is generally useful for categorizing
@@ -81,3 +81,21 @@ breakpoints: 85 and up is an `A', 75..84 is a `B', etc.
 >>> map(grade, [33, 99, 77, 44, 12, 88])
 ['E', 'A', 'B', 'D', 'F', 'A']
 \end{verbatim}
+
+The bisect module can be used with the Queue module to implement a priority
+queue (example courtesy of Fredrik Lundh): \index{Priority Queue}
+
+\begin{verbatim}
+import Queue, bisect
+
+class PriorityQueue(Queue.Queue):
+    def _put(self, item):
+        bisect.insort(self.queue, item)
+
+# usage
+queue = PriorityQueue(0)
+queue.put((2, "second"))
+queue.put((1, "first"))
+queue.put((3, "third"))
+priority, value = queue.get()
+\end{verbatim}