📄 page394.html
字号:
<HTML>
<HEAD>
<TITLE>Bit-Vector Sets</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="tex2html6795" HREF="page395.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page395.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="tex2html6793" HREF="page390.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page390.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="tex2html6789" HREF="page393.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page393.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="tex2html6797" 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="tex2html6798" 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>
<H2><A NAME="SECTION0013210000000000000000">Bit-Vector Sets</A></H2>
<P>
In the typical C++ implementation,
a <tt>bool</tt> occupies between one and four bytes.
However, since there are only the two values <tt>true</tt> and <tt>false</tt>,
a single bit is sufficient to hold a Boolean value.
Therefore, we can realize a significant reduction in the memory
space required to represent a set if we use an array of bits.
Furthermore, by using bitwise operations to implement the basic set operations
such 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>IsMember</tt> and <tt>Withdraw</tt>,
all slow down by a constant factor.
<P>
Since C++ does not directly support arrays of bits,
we must simulate an array of bits using an array of machine words.
Program <A HREF="page394.html#progbitset2h" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page394.html#progbitset2h"><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> illustrates how this can be done.
The class <tt>SetAsBitVector</tt> represents the elements of a the set
using the bits in an array of unsigned integers
(i.e., type <tt>Word</tt>).
The enumerated constant <tt>wordBits</tt> is defined as the number
of bits in a single <tt>Word</tt>.
<P>
<P><A NAME="28748"> </A><A NAME="progbitset2h"> </A> <IMG WIDTH=575 HEIGHT=200 ALIGN=BOTTOM ALT="program28382" SRC="img1623.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1623.gif" ><BR>
<STRONG>Program:</STRONG> <tt>SetAsBitVector</tt> Class Definition<BR>
<P><BR> <HR>
<UL>
<LI> <A NAME="tex2html6799" HREF="page395.html#SECTION0013211000000000000000" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page395.html#SECTION0013211000000000000000">Basic Operations</A>
<LI> <A NAME="tex2html6800" HREF="page396.html#SECTION0013212000000000000000" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page396.html#SECTION0013212000000000000000">Union, Intersection and Difference</A>
</UL>
<HR><A NAME="tex2html6795" HREF="page395.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page395.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="tex2html6793" HREF="page390.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page390.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="tex2html6789" HREF="page393.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page393.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="tex2html6797" 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="tex2html6798" 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 + -