page395.html
来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 49 行
HTML
49 行
<HTML><HEAD><TITLE>Union, Intersection, and Difference</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="tex2html5733" HREF="page396.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html5731" HREF="page393.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html5727" HREF="page394.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html5735" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H3><A NAME="SECTION0012212000000000000000">Union, Intersection, and Difference</A></H3><P>The implementations of the union, intersection, and difference methodsfor operands of type <tt>SetAsBitVector</tt>are shown in Program <A HREF="page395.html#progsetAsBitVectorc"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.The code is quite similar to that for the <tt>SetAsArray</tt> classgiven in Program <A HREF="page391.html#progsetAsArrayc"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.<P><P><A NAME="28231"> </A><A NAME="progsetAsBitVectorc"> </A> <IMG WIDTH=575 HEIGHT=523 ALIGN=BOTTOM ALT="program27897" SRC="img1583.gif" ><BR><STRONG>Program:</STRONG> <tt>SetAsBitVector</tt> class <tt>__or__</tt>, <tt>__and__</tt> and <tt>__sub__</tt> methods.<BR><P><P>Instead of using the Boolean operators<tt>and</tt>, <tt>or</tt>, and <tt>no</tt>,we have used the bitwise operators<tt>&</tt>, <tt>|</tt>, and <tt></tt>.By using the bitwise operators, <IMG WIDTH=66 HEIGHT=10 ALIGN=BOTTOM ALT="tex2html_wrap_inline66793" SRC="img1578.gif" > bits of the result are computedin each iteration of the loop.Therefore, the number of iterationsrequired is <IMG WIDTH=43 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline66791" SRC="img1577.gif" > instead of <I>N</I>.The worst-case running time of each of these operationsis <IMG WIDTH=131 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline66795" SRC="img1579.gif" >.<P>Notice that the asymptotic performance of these <tt>SetAsBitVector</tt>class operations is the same as the asymptotic performance ofthe <tt>SetAsArray</tt> class operations.That is, both of them are <I>O</I>(<I>N</I>).Nevertheless, the <tt>SetAsBitVector</tt> class operations are faster.In fact, the bit-vector approach is asymptotically faster than the the array approach by the factor <I>w</I>.<P><HR><A NAME="tex2html5733" HREF="page396.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html5731" HREF="page393.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html5727" HREF="page394.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html5735" 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 + -
显示快捷键?