page394.html
来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 58 行
HTML
58 行
<HTML><HEAD><TITLE>Basic Operations</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="tex2html5724" HREF="page395.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html5722" HREF="page393.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html5716" HREF="page393.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html5726" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H3><A NAME="SECTION0012211000000000000000">Basic Operations</A></H3><P>Program <A HREF="page394.html#progsetAsBitVectorb"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> defines the three basic operations<tt>insert</tt>, <tt>__contains__</tt>, and <tt>withdraw</tt>for the <tt>SetAsBitVector</tt> class.<P><P><A NAME="28225"> </A><A NAME="progsetAsBitVectorb"> </A> <IMG WIDTH=575 HEIGHT=275 ALIGN=BOTTOM ALT="program27863" SRC="img1580.gif" ><BR><STRONG>Program:</STRONG> <tt>SetAsBitVector</tt> <tt>insert</tt>, <tt>__contains__</tt> and <tt>withdraw</tt> methods.<BR><P><P>To insert an item into the set,we need to change the appropriate bit in the array of bits to one.The <IMG WIDTH=17 HEIGHT=14 ALIGN=BOTTOM ALT="tex2html_wrap_inline57847" SRC="img77.gif" > bit of the bit arrayis bit <IMG WIDTH=56 HEIGHT=11 ALIGN=BOTTOM ALT="tex2html_wrap_inline66799" SRC="img1581.gif" > of word <IMG WIDTH=34 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline66801" SRC="img1582.gif" >.Thus, the <tt>insert</tt> method is implemented usinga <em>bitwise or</em> operation to change the <IMG WIDTH=17 HEIGHT=14 ALIGN=BOTTOM ALT="tex2html_wrap_inline57847" SRC="img77.gif" > bit to oneas shown in Program <A HREF="page394.html#progsetAsBitVectorb"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.Even though it is slightly more complicated than the corresponding operationfor the <tt>SetAsArray</tt> class,the running time for this operation is still <I>O</I>(1).Since <IMG WIDTH=66 HEIGHT=10 ALIGN=BOTTOM ALT="tex2html_wrap_inline66793" SRC="img1578.gif" > is a power of two,it is possible to replace the division and modulo operations,<tt>/</tt> and <tt>%</tt>,with shifts and masks like this:<PRE>SHIFT = 5MASK = (1 << SHIFT) - 1vector[item >> SHIFT] |= 1 << (item & MASK)</PRE>Depending on the Python interpreter and machine architecture,doing so may improve the performance of the <tt>insert</tt> operation bya constant factor.Of course, its asymptotic performance is still <I>O</I>(1).<P>To withdraw an item from the set,we need to clear the appropriate bit in the array of bitsand to test if an item is a member of the set,we test the corresponding bit.The <tt>__contains__</tt> and <tt>withdraw</tt> methods in Program <A HREF="page394.html#progsetAsBitVectorb"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>show how this can be done.Like <tt>insert</tt>,both these methods have constant worst-case running times.<P><HR><A NAME="tex2html5724" HREF="page395.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html5722" HREF="page393.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html5716" HREF="page393.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html5726" 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 + -
显示快捷键?