You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stdcxx.apache.org by fa...@apache.org on 2007/11/14 18:35:53 UTC
svn commit: r594967 - /incubator/stdcxx/branches/4.2.x/doc/stdlibug/11-3.html
Author: faridz
Date: Wed Nov 14 09:35:49 2007
New Revision: 594967
URL: http://svn.apache.org/viewvc?rev=594967&view=rev
Log:
2007-11-14 Farid Zaripov <fa...@epam.com>
* stdlibug/11-3.html: Updated example source according to
changes in r594966.
Modified:
incubator/stdcxx/branches/4.2.x/doc/stdlibug/11-3.html
Modified: incubator/stdcxx/branches/4.2.x/doc/stdlibug/11-3.html
URL: http://svn.apache.org/viewvc/incubator/stdcxx/branches/4.2.x/doc/stdlibug/11-3.html?rev=594967&r1=594966&r2=594967&view=diff
==============================================================================
--- incubator/stdcxx/branches/4.2.x/doc/stdlibug/11-3.html (original)
+++ incubator/stdcxx/branches/4.2.x/doc/stdlibug/11-3.html Wed Nov 14 09:35:49 2007
@@ -51,8 +51,8 @@
<P>Since comparison of pointers cannot be specialized on the basis of the pointer types, we must instead define a new comparison function for pointers to events. In the C++ Standard Library we do this by defining a new structure whose sole purpose is to define the function invocation <SAMP>operator()()</SAMP> in the appropriate fashion. Since in this particular example we want to use the <B><I><A HREF="../stdlibref/priority-queue.html">priority_queue</A></I></B> to return the smallest element each time, rather than the largest, the order of the comparison is reversed, as follows:</P>
<UL><PRE>
-struct eventComparison {
- bool operator () (const event * left, const event * right) {
+struct eventComparator {
+ bool operator() (const event * left, const event * right) {
return left->time > right->time;
}
};
@@ -74,9 +74,8 @@
unsigned int time;
protected:
std::priority_queue<event*,
- std::vector<event *,
- std::allocator<event*> >,
- eventComparison> eventQueue;
+ std::vector<event *, std::allocator<event*> >,
+ eventComparator> eventQueue;
};
</PRE></UL>
<P>Notice the declaration of the <B><I><A HREF="../stdlibref/priority-queue.html">priority_queue</A></I></B> used to hold the pending <B><I>event</I></B>s. In this case we are using a <B><I><A HREF="../stdlibref/vector.html">vector</A></I></B> as the underlying container, but we could just as easily have used a <B><I><A HREF="../stdlibref/deque.html">deque</A></I></B>. </P>
@@ -104,8 +103,8 @@
<UL><PRE>
class storeSimulation : public simulation {
public:
- storeSimulation () : simulation (),
- freeChairs (35), profit (0.0) { }
+ storeSimulation () : simulation (), freeChairs (35), profit (0.0)
+ { }
bool canSeat (unsigned int numberOfPeople);
void order (unsigned int numberOfScoops);
void leave (unsigned int numberOfPeople);
@@ -121,16 +120,15 @@
bool storeSimulation::canSeat (unsigned int numberOfPeople) {
std::cout << "Time: " << time;
- std::cout << " group of " << numberOfPeople
- << " customers arrives";
+ std::cout << " group of " << numberOfPeople << " customers arrives";
if (numberOfPeople < freeChairs) {
- std::cout << " is seated" << std::endl;
+ std::cout << " is seated\n";
freeChairs -= numberOfPeople;
return true;
}
else {
- std::cout << " no room, they leave" << std::endl;
+ std::cout << " no room, they leave\n";
return false;
}
}
@@ -138,17 +136,16 @@
void storeSimulation::order (unsigned int numberOfScoops) {
std::cout << "Time: " << time << " serviced order for "
- << numberOfScoops << std::endl;
+ << numberOfScoops << '\n';
profit += 0.35 * numberOfScoops;
}
void storeSimulation::leave (unsigned int numberOfPeople) {
std::cout << "Time: " << time << " group of size "
- << numberOfPeople << " leaves" << std::endl;
+ << numberOfPeople << " leaves\n";
freeChairs += numberOfPeople;
}
-
</PRE></UL>
<A NAME="idx217"><!></A>
<P>As we noted already, each activity is matched by a subclass of <B><I>event</I></B>. Each subclass of <B><I>event</I></B> includes an integer data member, which represents the size of a group of customers. The arrival event occurs when a group enters. When executed, the arrival event creates and installs a new instance of the order event. The function <SAMP>randomInteger()</SAMP> is used to compute a random integer between 1 and the argument value (see <A HREF="2-2.html#225">Section 2.2.5</A>).</P>
@@ -165,6 +162,7 @@
};
void arriveEvent::processEvent () {
+
if (theSimulation.canSeat (size))
theSimulation.scheduleEvent
(new orderEvent (time + 1 + irand (4), size));
@@ -210,6 +208,7 @@
};
void leaveEvent::processEvent () {
+
theSimulation.leave (size);
}
</PRE></UL>
@@ -218,24 +217,20 @@
<UL><PRE>
int main () {
- std::cout << "Ice Cream Store simulation from Chapter 9"
- << std::endl;
+ std::cout << "Ice Cream Store simulation from Chapter 9\n";
// Load queue with some number of initial events.
- unsigned int t = 0;
- while (t < 20) {
- t += irand (6);
- std::cout << "pumping queue with event " << t << std::endl;
- theSimulation.scheduleEvent
- (new arriveEvent (t, 1 + irand (4)));
+ for (unsigned t = 0; t < 20; t += irand (6)) {
+
+ std::cout << "pumping queue with event " << t << '\n';
+ theSimulation.scheduleEvent (new arriveEvent (t, 1 + irand (4)));
}
// Run the simulation.
theSimulation.run ();
- std::cout << "Total profits "
- << theSimulation.profit << std::endl
- << "End of ice cream store simulation" << std::endl;
+ std::cout << "Total profits " << theSimulation.profit
+ << "\nEnd of ice cream store simulation\n";
return 0;
}