page437.html

来自「wqeqwvrw rkjqhwrjwq jkhrjqwhrwq jkhrwq」· HTML 代码 · 共 87 行

HTML
87
字号
<HTML>
<HEAD>
<TITLE>Implementation</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="tex2html7316" HREF="page438.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page438.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="tex2html7314" HREF="page436.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page436.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="tex2html7310" HREF="page436.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page436.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="tex2html7318" 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="tex2html7319" 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="SECTION0014510000000000000000">Implementation</A></H2>
<P>
This section shows how write the simulation described above.
Program&nbsp;<A HREF="page437.html#progapp10ac" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page437.html#progapp10ac"><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> declares the class <tt>Event</tt>
which is used to keep track of the address of an area
and the time at which it is to be freed.
Therefore, there are two parts to an event,
a <tt>VoidPtr</tt> and a <tt>Time</tt>.
<P>
<P><A NAME="32235">&#160;</A><A NAME="progapp10ac">&#160;</A> <IMG WIDTH=575 HEIGHT=295 ALIGN=BOTTOM ALT="program32085" SRC="img1800.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1800.gif"  ><BR>
<STRONG>Program:</STRONG> <tt>Event</tt> Class Definition<BR>
<P>
<P>
Since events will be put into a priority queue,
the <tt>Event</tt> class is derived from the <tt>Association</tt> class
which is defined in Section&nbsp;<A HREF="page125.html#secadtsassociations" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page125.html#secadtsassociations"><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>.
An association is an ordered pair comprised of a key and a value.
In the case of the <tt>Event</tt> class,
the key is the time of the event
and the value is address of the area to be free.
The events in a priority queue are prioritized by their times.
<P>
The simulation program is embodied in the function <tt>StoragePoolTest</tt>
defined in Program&nbsp;<A HREF="page437.html#progapp10bc" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page437.html#progapp10bc"><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>.
This function takes two arguments,
a reference to the storage pool to be tested
and an integer which specifies the number of simulation cycles to execute.
<P>
<P><A NAME="32101">&#160;</A><A NAME="progapp10bc">&#160;</A> <IMG WIDTH=575 HEIGHT=525 ALIGN=BOTTOM ALT="program32098" SRC="img1801.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1801.gif"  ><BR>
<STRONG>Program:</STRONG> Simulation Procedure for Exercising a Storage Pool<BR>
<P>
<P>
The variables <tt>size</tt> and <tt>latency</tt> are <tt>UniformRV</tt>s.
The class <tt>UniformRV</tt> is a random number generator
defined in Section&nbsp;<A HREF="page472.html#secalgsrng" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page472.html#secalgsrng"><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>.
This class provides a member function called <tt>Sample</tt>
which is used to sample the random number generator.
I.e., every time <tt>Sample</tt> is called,
a different (random) result is returned.
The random values are uniformly distributed on the interval
which is specified in the constructor (lines&nbsp;3-4).
<P>
The <tt>StoragePoolTest</tt> routine uses
a <tt>LeftistHeap</tt> priority queue
(see Section&nbsp;<A HREF="page362.html#secpqueuesleftist" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page362.html#secpqueuesleftist"><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>)
to keep track of scheduled events (line&nbsp;5).
<P>
The body of the main loop directly implements the steps outlined above.
First, any events scheduled for the current time step are
withdrawn from the priority queue
and the associated storage areas are released (lines&nbsp;10-19).
<P>
Second, the random variable <tt>size</tt> is sampled to determine the size
of area to allocate
and the random variable <tt>latency</tt> is sampled to determine when
the allocated area should be deleted.
An area of the required size is acquired
and an event is scheduled in priority queue
that will cause the area to be freed
after the appropriate amount of time as elapsed.
<P>
The <tt>StoragePoolTest</tt> function given in Program&nbsp;<A HREF="page437.html#progapp10bc" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page437.html#progapp10bc"><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> 
was used by the author to create Figures&nbsp;<A HREF="page417.html#figpool1" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page417.html#figpool1"><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="page426.html#figpool2" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page426.html#figpool2"><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&nbsp;<A HREF="page431.html#figpool3" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page431.html#figpool3"><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>.
Figure&nbsp;<A HREF="page417.html#figpool1" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page417.html#figpool1"><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> shows the condition of a <tt>SinglyLinkedPool</tt>
after  <IMG WIDTH=33 HEIGHT=12 ALIGN=BOTTOM ALT="tex2html_wrap_inline68383" SRC="img1802.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1802.gif"  > simulation cycles.
Figure&nbsp;<A HREF="page426.html#figpool2" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page426.html#figpool2"><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> shows the condition of a <tt>DoublyLinkedPool</tt>
and
Figure&nbsp;<A HREF="page431.html#figpool3" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page431.html#figpool3"><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> shows the condition of a <tt>BuddyPool</tt>
after exactly the same  <IMG WIDTH=33 HEIGHT=12 ALIGN=BOTTOM ALT="tex2html_wrap_inline68383" SRC="img1802.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1802.gif"  > simulation cycles.
<P>
<HR><A NAME="tex2html7316" HREF="page438.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page438.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="tex2html7314" HREF="page436.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page436.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="tex2html7310" HREF="page436.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page436.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="tex2html7318" 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="tex2html7319" 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 &#169; 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 + -
显示快捷键?