📄 page218.html
字号:
<HTML><HEAD><TITLE>Integer Keys</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="tex2html3716" HREF="page219.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html3714" HREF="page217.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html3708" HREF="page217.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html3718" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H2><A NAME="SECTION008310000000000000000">Integer Keys</A></H2><P>Of all the Python types,integers (<tt>int</tt>s and <tt>long</tt>s)are the simplest to hash.A suitable function <I>f</I> is:<P> <IMG WIDTH=312 HEIGHT=19 ALIGN=BOTTOM ALT="displaymath61951" SRC="img873.gif" ><P><P>Program <A HREF="page218.html#progintegera"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> introduces the class <tt>Integer</tt>which extends the built-in <tt>int</tt> classas well as the abstract <tt>Object</tt> classintroduced in Program <A HREF="page116.html#progobjecta"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.In this case,the <tt>__hash__</tt> method simply returns the bitwise andof <tt>self</tt> and <tt>sys.maxint</tt>.Because <tt>sys.maxint</tt> has the value <IMG WIDTH=47 HEIGHT=26 ALIGN=MIDDLE ALT="tex2html_wrap_inline61955" SRC="img874.gif" >,this has the effect of discards all but the least significant 31 bits,that is, division module <IMG WIDTH=19 HEIGHT=14 ALIGN=BOTTOM ALT="tex2html_wrap_inline61957" SRC="img875.gif" >.<P>The running time of of the <tt>__hash__</tt> method is <I>O</I>(1)if the integer is a plain integer (a 32-bit integer).If the integer is a <tt>long</tt> integer,the running time of the <tt>__hash__</tt> methoddepends on the number of digits in that <tt>long</tt>.If the <tt>long</tt> value is <I>n</I>,the running time is <IMG WIDTH=56 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline59347" SRC="img400.gif" >.<P><P><A NAME="11093"> </A><A NAME="progintegera"> </A> <IMG WIDTH=575 HEIGHT=122 ALIGN=BOTTOM ALT="program10763" SRC="img876.gif" ><BR><STRONG>Program:</STRONG> <tt>Integer</tt> class <tt>__hash__</tt> method.<BR><P><HR><A NAME="tex2html3716" HREF="page219.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html3714" HREF="page217.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html3708" HREF="page217.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html3718" 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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -