summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorSkip Montanaro <skip@pobox.com>2002-06-26 05:07:28 (GMT)
committerSkip Montanaro <skip@pobox.com>2002-06-26 05:07:28 (GMT)
commit09d9f86c7767bbd394591bc3b1c7d5025fc1f91b (patch)
tree2600b494f04e9f880347b5911573a173d10478a2 /Doc
parentffd3a4217a557bad4984621c22f5ae312d708169 (diff)
downloadcpython-09d9f86c7767bbd394591bc3b1c7d5025fc1f91b.zip
cpython-09d9f86c7767bbd394591bc3b1c7d5025fc1f91b.tar.gz
cpython-09d9f86c7767bbd394591bc3b1c7d5025fc1f91b.tar.bz2
add /F's PriorityQueue example
Diffstat (limited to 'Doc')
-rw-r--r--Doc/lib/libbisect.tex20
1 files changed, 19 insertions, 1 deletions
diff --git a/Doc/lib/libbisect.tex b/Doc/lib/libbisect.tex
index 8c108a6..3241891 100644
--- a/Doc/lib/libbisect.tex
+++ b/Doc/lib/libbisect.tex
@@ -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}