page393.html
来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 57 行
HTML
57 行
<HTML><HEAD><TITLE>Bit-Vector Sets</TITLE></HEAD><BODY bgcolor="#FFFFFF"> <a href="../index.html" target="_top"><img src="../icons/usins.gif" alt="Logo" align=right></a><b>Data Structures and Algorithms with Object-Oriented Design Patterns in Python</b><br><A NAME="tex2html5711" HREF="page394.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html5709" HREF="page389.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html5705" HREF="page392.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html5713" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H2><A NAME="SECTION0012210000000000000000">Bit-Vector Sets</A></H2><P>The Python virtual machine represents <tt>bool</tt> values<tt>False</tt> and <tt>True</tt>using the <tt>int</tt> values <tt>0</tt> and <tt>1</tt>, respectively.In effect, it uses a 32-bit number to represent one bit of information.Therefore, we can realize a significant reduction in the memoryspace required to represent a set if we use an array of bitsinstead of an array of <tt>bool</tt>s.Furthermore, by using bitwise operations to implement the basic set operationssuch as union and intersection,we can achieve a commensurate reduction in execution time.Unfortunately, these improvements are not free--the operations <tt>insert</tt>, <tt>__contains__</tt>, and <tt>withdraw</tt>,all slow down by a constant factor.<P>Since Python does not directly support arrays of bits,we will simulate an array of bits using an array of <tt>int</tt>s.Program <A HREF="page393.html#progsetAsBitVectora"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> illustrates how this can be done.The constant <tt>BITS</tt> is defined as the numberof bits in a single <tt>int</tt>.<P><P><A NAME="28222"> </A><A NAME="progsetAsBitVectora"> </A> <IMG WIDTH=575 HEIGHT=218 ALIGN=BOTTOM ALT="program27839" SRC="img1576.gif" ><BR><STRONG>Program:</STRONG> <tt>SetAsBitVector</tt> class <tt>__init__</tt> method.<BR><P><P>In addition to <tt>self</tt>,the <tt>__init__</tt> method takes a single argument <IMG WIDTH=44 HEIGHT=11 ALIGN=BOTTOM ALT="tex2html_wrap_inline66691" SRC="img1559.gif" >,which specifies the universe and, consequently,the number of bits needed in the bit array.The <tt>__init__</tt> method creates an array of <tt>int</tt>sof length <IMG WIDTH=43 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline66791" SRC="img1577.gif" >,where <IMG WIDTH=66 HEIGHT=10 ALIGN=BOTTOM ALT="tex2html_wrap_inline66793" SRC="img1578.gif" > is the number of bits in an <tt>int</tt>,and sets the elements of the array to zero.The running time of the <tt>__init__</tt> method is <IMG WIDTH=131 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline66795" SRC="img1579.gif" >.<P><BR> <HR><UL> <LI> <A NAME="tex2html5714" HREF="page394.html#SECTION0012211000000000000000">Basic Operations</A><LI> <A NAME="tex2html5715" HREF="page395.html#SECTION0012212000000000000000">Union, Intersection, and Difference</A></UL><HR><A NAME="tex2html5711" HREF="page394.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html5709" HREF="page389.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html5705" HREF="page392.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html5713" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <P><ADDRESS><img src="../icons/bruno.gif" alt="Bruno" align=right><a href="../copyright.html">Copyright © 2003</a> by <a href="../signature.html">Bruno R. Preiss, P.Eng.</a> All rights reserved.</ADDRESS></BODY></HTML>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?