📄 page222.html
字号:
<HTML><HEAD><TITLE>Using Associations</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="tex2html3758" HREF="page223.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html3756" HREF="page217.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html3752" HREF="page221.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html3760" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H2><A NAME="SECTION008350000000000000000">Using Associations</A></H2><P>Hashing provides a way to determine the position of a given objectdirectly from that object itself.Given an object <I>x</I> we determine its position byevaluating the appropriate hash function, <I>h</I>(<I>x</I>).We find the location of object <I>x</I> in exactly the same way.But of what use is this ability to find an object if,in order to compute the hash function <I>h</I>(<I>x</I>),we must be able to access the object <I>x</I> in the first place?<P>In practice, when using hashingwe are dealing with <em>keyed data</em><A NAME=10974> </A>.Mathematically, keyed data consists of ordered pairs<P> <IMG WIDTH=346 HEIGHT=16 ALIGN=BOTTOM ALT="displaymath62155" SRC="img920.gif" ><P>where <I>K</I> is a set of keys,and <I>V</I> is a set of values.The idea is that we will access elements of the set <I>A</I> using the key.That is, the hash function for elements of the set <I>A</I> is given by<P> <IMG WIDTH=316 HEIGHT=16 ALIGN=BOTTOM ALT="displaymath62156" SRC="img921.gif" ><P>where <IMG WIDTH=18 HEIGHT=25 ALIGN=MIDDLE ALT="tex2html_wrap_inline62177" SRC="img922.gif" > is the hash function associated with the set <I>K</I>.<P>For example, suppose we wish to use hashing to implementa database which contains driver's license records.Each record contains information about a driver,such as his name, address, and perhaps a summary of traffic violations.Furthermore, each record has a unique driver's license number.The driver's license number is the keyand the other information is the value associated with that key.<P>In Section <A HREF="page127.html#secadtsassociations"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> the <tt>Association</tt> classwas declared which comprises two instance attributes, a key and a value.Given this declaration,the definition of the hash method for <tt>Association</tt>s is trivial.As shown in Program <A HREF="page222.html#progassociationd"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>,it simply calls the built-in <tt>hash</tt> functionon the <tt>key</tt> instance attribute.<P><P><A NAME="11102"> </A><A NAME="progassociationd"> </A> <IMG WIDTH=575 HEIGHT=122 ALIGN=BOTTOM ALT="program10981" SRC="img923.gif" ><BR><STRONG>Program:</STRONG> <tt>Association</tt> class <tt>__hash__</tt> method.<BR><P><HR><A NAME="tex2html3758" HREF="page223.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html3756" HREF="page217.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html3752" HREF="page221.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html3760" 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 + -