page388.html
来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 63 行
HTML
63 行
<HTML><HEAD><TITLE>Implementing 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="tex2html5656" HREF="page389.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html5654" HREF="page387.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html5650" HREF="page387.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html5658" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H2><A NAME="SECTION0012110000000000000000">Implementing Sets</A></H2><A NAME="secsetssets"> </A><P>As discussed above,this chapter addresses the implementation of sets of integers.A set is a collection of elements.Naturally, we want to insert and withdraw objects from the collectionand to test whether a given object is a member of the collection.Therefore, we consider sets as being derivedfrom the <tt>SearchableContainer</tt> class defined in Chapter <A HREF="page111.html#chapadts"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.(See Figure <A HREF="page388.html#figclasses8"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>).In general, a searchable container can hold arbitrary objects.However, in this chapter we will assume that the elementsof a set are integers.<P><P><A NAME="27629"> </A><A NAME="figclasses8"> </A> <IMG WIDTH=575 HEIGHT=349 ALIGN=BOTTOM ALT="figure27625" SRC="img1550.gif" ><BR><STRONG>Figure:</STRONG> Object class hierarchy<BR><P><P>Program <A HREF="page388.html#progseta"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> defines the <tt>Set</tt> class.The abstract <tt>Set</tt> class extendsthe abstract <tt>SearchableContainer</tt> classdefined in Program <A HREF="page126.html#progsearchableContainera"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.Five abstract methods are declared--<tt>__or__</tt>, <tt>__and__</tt>, <tt>__sub__</tt>,<tt>__eq__</tt>, and <tt>__lt__</tt>.These methods correspond to the various set operations discussed above.That is,<tt>__or__</tt> computes the <em>union</em>,<tt>__and__</tt> computes the <em>intersection</em>,<tt>__sub__</tt> computes the set <em>difference</em>,<tt>__eq__</tt> tests for set <em>equality</em>, and<tt>__lt__</tt> tests whether one set is a <em>subset</em> of the other.<P><P><A NAME="28197"> </A><A NAME="progseta"> </A> <IMG WIDTH=575 HEIGHT=582 ALIGN=BOTTOM ALT="program27652" SRC="img1551.gif" ><BR><STRONG>Program:</STRONG> Abstract <tt>Set</tt> class.<BR><P><P>The <tt>Set</tt> class alsodefines an instance attribute called <tt>_universeSize</tt>.This instance attribute is used to record the size of the universal set.The <tt>__init__</tt> method for the <tt>Set</tt> classis given in Program <A HREF="page388.html#progseta"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.In addition to <tt>self</tt>,it takes a single argument, <tt>n</tt>,which specifies that the universal set shall be <IMG WIDTH=108 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline66641" SRC="img1552.gif" >.<P><HR><A NAME="tex2html5656" HREF="page389.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html5654" HREF="page387.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html5650" HREF="page387.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html5658" 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 + -
显示快捷键?