page219.html
来自「wqeqwvrw rkjqhwrjwq jkhrjqwhrwq jkhrwq」· HTML 代码 · 共 68 行
HTML
68 行
<HTML>
<HEAD>
<TITLE>Hashing Objects</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="tex2html4623" HREF="page220.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page220.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="tex2html4621" HREF="page215.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page215.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="tex2html4615" HREF="page218.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page218.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="tex2html4625" 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="tex2html4626" 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="SECTION009340000000000000000">Hashing <tt>Object</tt>s</A></H2>
<P>
In this section we consider hashing in the context
of the object hierarchy defined in Chapter <A HREF="page107.html#chapadts" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page107.html#chapadts"><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 abstract base class called <tt>Object</tt>
which was defined in Section <A HREF="page111.html#secadtsobjects" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page111.html#secadtsobjects"><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>
has a <tt>const</tt> virtual member function called <tt>Hash</tt>
which was declared as follows:
<PRE>class Object
{
...
virtual HashValue Hash () const = 0;
...
};</PRE>
The idea is that every object instance of a class which is derived
from the class <tt>Object</tt> has an associated member function
called <tt>Hash</tt> which hashes that object
to produce an integer <tt>HashValue</tt>.
For example, given a reference to an object, <tt>Object& obj</tt>,
the following computation
<PRE>HashValue x = obj.Hash ();</PRE>
sets the variable <tt>x</tt> to the value returned by
the <tt>Hash</tt> function associated with
the object to which <tt>obj</tt> refers.
<P>
A <tt>Wrapper</tt> class template was declared
in Section <A HREF="page115.html#secadtswrappers" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page115.html#secadtswrappers"><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>
which is used to wrap instances of the C++ built-in data types
within an <tt>Object</tt> abstract interface.
Using the <tt>Wrapper</tt> template,
the four classes <tt>Char</tt>, <tt>Int</tt>, <tt>Double</tt>, and <tt>String</tt>
were declared as follows:
<PRE>typedef Wrapper<char> Char;
typedef Wrapper<int> Int;
typedef Wrapper<double> Double;
typedef Wrapper<string> String;</PRE>
Since these classes are meant to be concrete classes,
they must provide implementations for all of the member functions
including the <tt>Hash</tt> function.
<P>
<P><A NAME="11722"> </A><A NAME="progwrapper2c"> </A> <IMG WIDTH=575 HEIGHT=66 ALIGN=BOTTOM ALT="program11556" SRC="img957.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img957.gif" ><BR>
<STRONG>Program:</STRONG> <tt>Wrapper<T></tt> Class <tt>Hash</tt> Member Function Definition<BR>
<P>
<P>
Program <A HREF="page219.html#progwrapper2c" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page219.html#progwrapper2c"><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> gives the definition of the
<tt>Hash</tt> member function of the <tt>Wrapper<T></tt> class.
The implementation simply calls the appropriate hashing function
from those given in Programs <A HREF="page216.html#proghash1c" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page216.html#proghash1c"><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>, <A HREF="page217.html#proghash2c" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page217.html#proghash2c"><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> and <A HREF="page218.html#proghash3c" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page218.html#proghash3c"><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>.
For example the <tt>Wrapper<int>::Hash</tt> function calls <tt>Hash(int)</tt> and
the <tt>Wrapper<string>::Hash</tt> function calls <tt>Hash(string)</tt>.
<P>
<HR><A NAME="tex2html4623" HREF="page220.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page220.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="tex2html4621" HREF="page215.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page215.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="tex2html4615" HREF="page218.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page218.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="tex2html4625" 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="tex2html4626" 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 + =
减小字号Ctrl + -
显示快捷键?