optimize resizing
authorMathieu Lacage <mathieu.lacage@sophia.inria.fr>
Thu, 08 Jan 2009 11:43:15 +0100
changeset 4050 ee134e4f44c5
parent 4049 3d2b3b35b4fb
child 4051 3df44c5a1ec1
optimize resizing
src/simulator/calendar-scheduler.cc
--- a/src/simulator/calendar-scheduler.cc	Thu Jan 08 11:21:25 2009 +0100
+++ b/src/simulator/calendar-scheduler.cc	Thu Jan 08 11:43:15 2009 +0100
@@ -37,7 +37,9 @@
             uint64_t newWidth);
   ~Calendar ();
 
+  void InsertAll (const Calendar *calendar);
   void Insert (const Scheduler::Event &ev);
+  void InsertMin (const Scheduler::Event &ev);
   Scheduler::Event PeekNext (void) const;
   Scheduler::Event RemoveNext (void);
   void Remove (const Scheduler::Event &ev);
@@ -80,6 +82,26 @@
 {
   return m_nBuckets;
 }
+void 
+Calendar::InsertAll (const Calendar *calendar)
+{
+  for (uint32_t i = 0; i < calendar->m_nBuckets; i++)
+    {
+      Bucket::iterator end = calendar->m_buckets[i].end ();
+      for (Bucket::iterator j = calendar->m_buckets[i].begin (); j != end; ++j) 
+        {
+          Insert (*j);
+        }
+    }
+}
+void 
+Calendar::InsertMin (const Scheduler::Event &ev)
+{
+  uint32_t bucket = (ev.key.m_ts / m_width) % m_nBuckets;
+  m_lastBucket = bucket;
+  m_bucketTop = (m_lastBucket + 1) * m_width;
+  Insert (ev);
+}
 void
 Calendar::Insert (const Scheduler::Event &ev)
 {
@@ -297,7 +319,6 @@
     {
       dequeued->push_back (m_calendar->RemoveNext ());
     }
-  m_qSize -= nSamples;
   
   uint64_t totalSeparation = 0;
   std::list<Scheduler::Event>::const_iterator end = dequeued->end ();
@@ -341,18 +362,20 @@
   
   Calendar *calendar = new Calendar (newSize, newWidth);
 
-  for (uint32_t i = 0; i < m_qSize; i++)
-    {
-      Scheduler::Event ev = m_calendar->RemoveNext ();
-      calendar->Insert (ev);
-    }
+  calendar->InsertAll (m_calendar);
   std::list<Scheduler::Event>::const_iterator end = dequeued.end ();
   for (std::list<Scheduler::Event>::const_iterator i = dequeued.begin ();
        i != end; ++i)
     {
-      calendar->Insert (*i);
+      if (i == dequeued.begin ())
+        {
+          calendar->InsertMin (*i);
+        }
+      else
+        {
+          calendar->Insert (*i);
+        }
     }
-  m_qSize += dequeued.size ();
 
   delete m_calendar;
   m_calendar = calendar;