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-&gt;time &gt; right-&gt;time;
   }
 };
@@ -74,9 +74,8 @@
   unsigned int time;
 protected:
   std::priority_queue&lt;event*,
-                      std::vector&lt;event *,
-                                  std::allocator&lt;event*&gt; &gt;,
-                      eventComparison&gt; eventQueue;
+                      std::vector&lt;event *, std::allocator&lt;event*&gt; 
&gt;,
+                      eventComparator&gt; 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 &lt;&lt; "Time: " &lt;&lt; time;
-  std::cout &lt;&lt; " group of " &lt;&lt; numberOfPeople
-            &lt;&lt; " customers arrives";
+  std::cout &lt;&lt; " group of " &lt;&lt; numberOfPeople &lt;&lt; " customers 
arrives";
 
   if (numberOfPeople &lt; freeChairs) {
-    std::cout &lt;&lt; " is seated" &lt;&lt; std::endl;
+    std::cout &lt;&lt; " is seated\n";
     freeChairs -= numberOfPeople;
     return true;
   }
   else {
-    std::cout &lt;&lt; " no room, they leave" &lt;&lt; std::endl;
+    std::cout &lt;&lt; " no room, they leave\n";
     return false;
   }
 }
@@ -138,17 +136,16 @@
 void storeSimulation::order (unsigned int numberOfScoops) {
     
   std::cout &lt;&lt; "Time: " &lt;&lt; time &lt;&lt; " serviced order for "
-            &lt;&lt; numberOfScoops &lt;&lt; std::endl;
+            &lt;&lt; numberOfScoops &lt;&lt; '\n';
   profit += 0.35 * numberOfScoops;
 }
 
 void storeSimulation::leave (unsigned int numberOfPeople) {
     
   std::cout &lt;&lt; "Time: " &lt;&lt; time &lt;&lt; " group of size "
-            &lt;&lt; numberOfPeople &lt;&lt; " leaves" &lt;&lt; std::endl;
+            &lt;&lt; numberOfPeople &lt;&lt; " 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&nbsp;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 &lt;&lt; "Ice Cream Store simulation from Chapter 9"
-            &lt;&lt; std::endl;
+  std::cout &lt;&lt; "Ice Cream Store simulation from Chapter 9\n";
 
   // Load queue with some number of initial events.
-  unsigned int t = 0;
-  while (t &lt; 20) {
-    t += irand (6);
-    std::cout &lt;&lt; "pumping queue with event " &lt;&lt; t &lt;&lt; 
std::endl;
-    theSimulation.scheduleEvent
-      (new arriveEvent (t, 1 + irand (4)));
+  for (unsigned t = 0; t &lt; 20; t += irand (6)) {
+
+    std::cout &lt;&lt; "pumping queue with event " &lt;&lt; t &lt;&lt; '\n';
+    theSimulation.scheduleEvent (new arriveEvent (t, 1 + irand (4)));
   }
 
   // Run the simulation.
   theSimulation.run ();
 
-  std::cout &lt;&lt; "Total profits "
-            &lt;&lt; theSimulation.profit &lt;&lt; std::endl
-            &lt;&lt; "End of ice cream store simulation" &lt;&lt; std::endl;
+  std::cout &lt;&lt; "Total profits " &lt;&lt; theSimulation.profit
+            &lt;&lt; "\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


Reply via email to