Author: faridz
Date: Wed Nov 14 09:43:17 2007
New Revision: 594970
URL: http://svn.apache.org/viewvc?rev=594970&view=rev
Log:
2007-11-14 Farid Zaripov <[EMAIL PROTECTED]>
Merged r594966 from branches/4.2.x with a fix for STDCXX-661
* examples/tutorial/icecream.cpp (class event): Removed unused
operator>().
(eventComparator): Defined as separate struct instead of typedef
std::greater<>.
* examples/tutorial/out/icecream.out: Updated .out file to reflect the
changes above.
Merged r594967 from branches/4.2.x
* doc/stdlibug/11-3.html: Updated example source according to
changes in r594966.
Modified:
incubator/stdcxx/trunk/doc/stdlibug/11-3.html
incubator/stdcxx/trunk/examples/tutorial/icecream.cpp
incubator/stdcxx/trunk/examples/tutorial/out/icecream.out
Modified: incubator/stdcxx/trunk/doc/stdlibug/11-3.html
URL:
http://svn.apache.org/viewvc/incubator/stdcxx/trunk/doc/stdlibug/11-3.html?rev=594970&r1=594969&r2=594970&view=diff
==============================================================================
--- incubator/stdcxx/trunk/doc/stdlibug/11-3.html (original)
+++ incubator/stdcxx/trunk/doc/stdlibug/11-3.html Wed Nov 14 09:43:17 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;
}
Modified: incubator/stdcxx/trunk/examples/tutorial/icecream.cpp
URL:
http://svn.apache.org/viewvc/incubator/stdcxx/trunk/examples/tutorial/icecream.cpp?rev=594970&r1=594969&r2=594970&view=diff
==============================================================================
--- incubator/stdcxx/trunk/examples/tutorial/icecream.cpp (original)
+++ incubator/stdcxx/trunk/examples/tutorial/icecream.cpp Wed Nov 14 09:43:17
2007
@@ -41,16 +41,17 @@
event (unsigned int t) : time (t)
{ }
- // Execute event my invoking this method.
+ // Execute event by invoking this method.
virtual void processEvent () = 0;
- bool operator> ( const event* evt_ ) {
- return time > evt_->time;
- }
const unsigned int time;
};
-typedef std::greater<event*> eventComparator;
+struct eventComparator {
+ bool operator() (const event * left, const event * right) {
+ return left->time > right->time;
+ }
+};
// Framework for discrete event-driven simulations.
@@ -66,14 +67,14 @@
protected:
std::priority_queue<event*,
std::vector<event *, std::allocator<event*> >,
- eventComparator > eventQueue;
+ eventComparator> eventQueue;
};
void simulation::run () {
-
+
while (! eventQueue.empty ()) {
-
+
event * nextEvent = eventQueue.top ();
eventQueue.pop ();
time = nextEvent->time;
@@ -147,7 +148,7 @@
void arriveEvent::processEvent () {
-
+
if (theSimulation.canSeat (size))
theSimulation.scheduleEvent
(new orderEvent (time + 1 + irand (4), size));
@@ -155,7 +156,7 @@
void orderEvent::processEvent () {
-
+
// Each person orders some number of scoops.
for (unsigned int i = 0; i < size; i++)
theSimulation.order (1 + irand (4));
@@ -174,7 +175,7 @@
// If sufficient room then seat customers.
bool storeSimulation::canSeat (unsigned int numberOfPeople) {
-
+
std::cout << "Time: " << time;
std::cout << " group of " << numberOfPeople << " customers arrives";
@@ -192,7 +193,7 @@
// Service icecream, compute profits.
void storeSimulation::order (unsigned int numberOfScoops) {
-
+
std::cout << "Time: " << time << " serviced order for "
<< numberOfScoops << '\n';
profit += 0.35 * numberOfScoops;
@@ -201,7 +202,7 @@
// People leave, free up chairs.
void storeSimulation::leave (unsigned int numberOfPeople) {
-
+
std::cout << "Time: " << time << " group of size "
<< numberOfPeople << " leaves\n";
freeChairs += numberOfPeople;
@@ -209,7 +210,7 @@
int main () {
-
+
std::cout << "Ice Cream Store simulation from Chapter 9\n";
// Load queue with some number of initial events.
Modified: incubator/stdcxx/trunk/examples/tutorial/out/icecream.out
URL:
http://svn.apache.org/viewvc/incubator/stdcxx/trunk/examples/tutorial/out/icecream.out?rev=594970&r1=594969&r2=594970&view=diff
==============================================================================
--- incubator/stdcxx/trunk/examples/tutorial/out/icecream.out (original)
+++ incubator/stdcxx/trunk/examples/tutorial/out/icecream.out Wed Nov 14
09:43:17 2007
@@ -11,46 +11,46 @@
pumping queue with event 17
Time: 0 group of 2 customers arrives is seated
Time: 0 group of 4 customers arrives is seated
-Time: 3 serviced order for 3
+Time: 2 group of 3 customers arrives is seated
Time: 3 serviced order for 4
Time: 3 serviced order for 1
Time: 3 serviced order for 1
-Time: 10 group of size 4 leaves
-Time: 2 group of 3 customers arrives is seated
-Time: 5 serviced order for 4
+Time: 3 serviced order for 3
+Time: 4 serviced order for 4
+Time: 4 serviced order for 1
+Time: 4 group of size 4 leaves
+Time: 5 serviced order for 2
Time: 5 serviced order for 1
Time: 5 serviced order for 3
-Time: 13 group of size 3 leaves
Time: 7 group of 1 customers arrives is seated
-Time: 8 serviced order for 3
-Time: 10 group of size 1 leaves
+Time: 7 group of size 2 leaves
+Time: 7 group of size 3 leaves
Time: 8 group of 3 customers arrives is seated
-Time: 11 serviced order for 4
-Time: 11 serviced order for 3
-Time: 11 serviced order for 1
-Time: 19 group of size 3 leaves
-Time: 12 group of 3 customers arrives is seated
-Time: 16 serviced order for 2
-Time: 16 serviced order for 2
-Time: 16 serviced order for 4
-Time: 25 group of size 3 leaves
+Time: 10 serviced order for 3
Time: 12 group of 2 customers arrives is seated
-Time: 15 serviced order for 3
-Time: 15 serviced order for 4
-Time: 22 group of size 2 leaves
+Time: 12 group of 3 customers arrives is seated
+Time: 12 serviced order for 2
+Time: 12 serviced order for 2
+Time: 12 serviced order for 4
Time: 12 group of 1 customers arrives is seated
-Time: 15 serviced order for 4
-Time: 18 group of size 1 leaves
+Time: 14 serviced order for 3
+Time: 14 serviced order for 4
Time: 14 group of 2 customers arrives is seated
-Time: 15 serviced order for 3
-Time: 15 serviced order for 2
-Time: 20 group of size 2 leaves
+Time: 15 serviced order for 4
+Time: 16 serviced order for 1
+Time: 16 serviced order for 3
+Time: 16 serviced order for 2
+Time: 17 serviced order for 2
+Time: 17 serviced order for 2
Time: 17 group of 2 customers arrives is seated
-Time: 19 serviced order for 2
-Time: 19 serviced order for 3
-Time: 29 group of size 2 leaves
-Time: 4 serviced order for 3
-Time: 4 serviced order for 4
-Time: 10 group of size 2 leaves
-Total profits 22.4
+Time: 18 group of size 1 leaves
+Time: 19 group of size 1 leaves
+Time: 21 group of size 2 leaves
+Time: 21 group of size 3 leaves
+Time: 21 group of size 3 leaves
+Time: 21 serviced order for 3
+Time: 21 serviced order for 4
+Time: 24 group of size 2 leaves
+Time: 27 group of size 2 leaves
+Total profits 20.65
End of ice cream store simulation