📄 page396.html
字号:
<HTML>
<HEAD>
<TITLE>Union, Intersection and Difference</TITLE>
</HEAD>
<BODY bgcolor="#FFFFFF">
<img src="cover75.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cover75.gif" alt="Logo" align=right>
<b>Data Structures and Algorithms
with Object-Oriented Design Patterns in C++</b><br>
<A NAME="tex2html6819" HREF="page397.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page397.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="next_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/next_motif.gif"></A> <A NAME="tex2html6817" HREF="page394.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page394.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="up_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/up_motif.gif"></A> <A NAME="tex2html6813" HREF="page395.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page395.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/previous_motif.gif"></A> <A NAME="tex2html6821" HREF="page9.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page9.html"><IMG WIDTH=65 HEIGHT=24 ALIGN=BOTTOM ALT="contents" SRC="contents_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/contents_motif.gif"></A> <A NAME="tex2html6822" HREF="page620.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page620.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="index_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/index_motif.gif"></A> <BR><HR>
<H3><A NAME="SECTION0013212000000000000000">Union, Intersection and Difference</A></H3>
<P>
The implementations of the union, intersection, and difference operators
(<tt>+</tt>, <tt>*</tt>, and <tt>-</tt>, respectively)
for operands of type <tt>SetAsBitVector</tt> are shown in Program <A HREF="page396.html#progbitset5c" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page396.html#progbitset5c"><IMG ALIGN=BOTTOM ALT="gif" SRC="cross_ref_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cross_ref_motif.gif"></A>.
The code is quite similar to that for the <tt>SetAsArray</tt> class
given in Program <A HREF="page392.html#progbitset2c" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page392.html#progbitset2c"><IMG ALIGN=BOTTOM ALT="gif" SRC="cross_ref_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cross_ref_motif.gif"></A>.
<P>
<P><A NAME="28758"> </A><A NAME="progbitset5c"> </A> <IMG WIDTH=575 HEIGHT=620 ALIGN=BOTTOM ALT="program28441" SRC="img1630.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1630.gif" ><BR>
<STRONG>Program:</STRONG> <tt>SetAsBitVector</tt> Class Union, Intersection and Difference Operator Definitions<BR>
<P>
<P>
Instead of using the Boolean operators <tt>&&</tt>, <tt>||</tt> and <tt>!</tt>,
we have used the bitwise operators <tt>&</tt>, <tt>|</tt> and <tt></tt>.
By using the bitwise operators,
<IMG WIDTH=99 HEIGHT=10 ALIGN=BOTTOM ALT="tex2html_wrap_inline67410" SRC="img1625.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1625.gif" > bits of the result are computed
in each iteration of the loop.
Therefore, the number of iterations
required is <IMG WIDTH=43 HEIGHT=26 ALIGN=MIDDLE ALT="tex2html_wrap_inline67408" SRC="img1624.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1624.gif" > instead of <I>N</I>.
The worst-case running time of each of these operations
is <IMG WIDTH=130 HEIGHT=26 ALIGN=MIDDLE ALT="tex2html_wrap_inline67412" SRC="img1626.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1626.gif" >.
<P>
Notice that the asymptotic performance of these <tt>SetAsBitVector</tt>
class operations is the same as the asymptotic performance of
the <tt>SetAsArray</tt> class operations.
I.e., 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="tex2html6819" HREF="page397.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page397.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="next_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/next_motif.gif"></A> <A NAME="tex2html6817" HREF="page394.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page394.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="up_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/up_motif.gif"></A> <A NAME="tex2html6813" HREF="page395.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page395.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/previous_motif.gif"></A> <A NAME="tex2html6821" HREF="page9.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page9.html"><IMG WIDTH=65 HEIGHT=24 ALIGN=BOTTOM ALT="contents" SRC="contents_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/contents_motif.gif"></A> <A NAME="tex2html6822" HREF="page620.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page620.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="index_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/index_motif.gif"></A> <P><ADDRESS>
<img src="bruno.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/bruno.gif" alt="Bruno" align=right>
<a href="javascript:if(confirm('http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/copyright.html \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/copyright.html'" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/copyright.html">Copyright © 1997</a> by <a href="javascript:if(confirm('http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/signature.html \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/signature.html'" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/signature.html">Bruno R. Preiss, P.Eng.</a> All rights reserved.
</ADDRESS>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -