make Heap scheduler remove operation first perform a linear search to find the target location and then perform a correct remove through a top-down heapification
authorMathieu Lacage <mathieu.lacage@sophia.inria.fr>
Mon, 18 Dec 2006 11:39:48 +0100
changeset 206 525ffa5b4d24
parent 205 681e44f1cf58
child 207 3732a5c036b3
child 223 80f1c6b76999
make Heap scheduler remove operation first perform a linear search to find the target location and then perform a correct remove through a top-down heapification
src/simulator/scheduler-heap.cc
src/simulator/scheduler-heap.h
--- a/src/simulator/scheduler-heap.cc	Fri Dec 15 13:51:56 2006 +0100
+++ b/src/simulator/scheduler-heap.cc	Mon Dec 18 11:39:48 2006 +0100
@@ -120,7 +120,7 @@
 }
 
 bool
-SchedulerHeap::IsLower (Scheduler::EventKey const*a, Scheduler::EventKey const*b) const
+SchedulerHeap::IsLowerStrictly (Scheduler::EventKey const*a, Scheduler::EventKey const*b) const
 {
   if (a->m_ns < b->m_ns)
     {
@@ -141,15 +141,15 @@
 }
 
 bool
-SchedulerHeap::IsLess (uint32_t a, uint32_t b) const
+SchedulerHeap::IsLessStrictly (uint32_t a, uint32_t b) const
 {
-  return IsLower (&m_heap[a].second, &m_heap[b].second);
+  return IsLowerStrictly (&m_heap[a].second, &m_heap[b].second);
 }
 
 uint32_t 
 SchedulerHeap::Smallest (uint32_t a, uint32_t b) const
 {
-  return IsLess (a,b)?a:b;
+  return IsLessStrictly (a,b)?a:b;
 }
 
 bool
@@ -163,7 +163,7 @@
 {
   uint32_t index = Last ();
   while (!IsRoot (index) && 
-         IsLess (index, Parent (index))) 
+         IsLessStrictly (index, Parent (index))) 
     { 
       Exch(index, Parent (index)); 
       index = Parent (index); 
@@ -171,15 +171,15 @@
 }
 
 void
-SchedulerHeap::TopDown (void)
+SchedulerHeap::TopDown (uint32_t start)
 {
-  uint32_t index = Root ();
+  uint32_t index = start;
   uint32_t right = RightChild (index);
   while (!IsBottom (right)) 
     {
       uint32_t left = LeftChild (index);
       uint32_t tmp = Smallest (left, right);
-      if (IsLess (index, tmp)) 
+      if (IsLessStrictly (index, tmp)) 
         {
           return;
         }
@@ -197,7 +197,7 @@
     {
       return;
     }
-  if (IsLess (index, left)) 
+  if (IsLessStrictly (index, left)) 
     {
       return;
     }
@@ -228,38 +228,29 @@
 {
   Exch (Root (), Last ());
   m_heap.pop_back ();
-  TopDown ();
+  TopDown (Root ());
 }
 
 
 EventImpl *
 SchedulerHeap::RealRemove (EventId id, Scheduler::EventKey *key)
 {
-  uint32_t top;
-  uint32_t bottom;
-  bottom = 1;
-  top = Last ();
   key->m_ns = id.GetNs ();
   key->m_uid = id.GetUid ();
-  while (top != bottom)
+  for (uint32_t i = 1; i < m_heap.size (); i++)
     {
-      uint32_t i = bottom + (top - bottom) / 2;
-      if (IsLower (key, &m_heap[i].second))
+      if (key->m_uid == m_heap[i].second.m_uid)
         {
-          top = i;
-        }
-      else
-        {
-          bottom = i;
+          EventImpl *retval = m_heap[i].first;
+          Exch (i, Last ());
+          m_heap.pop_back ();
+          TopDown (i);
+          return retval;
         }
     }
-  assert (top == bottom);
-  assert (m_heap[top].second.m_uid == id.GetUid ());
-  EventImpl *retval = m_heap[top].first;
-  Exch (top, Last ());
-  m_heap.pop_back ();
-  TopDown ();
-  return retval;
+  assert (false);
+  // quiet compiler
+  return 0;
 }
 
 bool 
--- a/src/simulator/scheduler-heap.h	Fri Dec 15 13:51:56 2006 +0100
+++ b/src/simulator/scheduler-heap.h	Mon Dec 18 11:39:48 2006 +0100
@@ -51,16 +51,17 @@
   inline uint32_t LeftChild (uint32_t id) const;
   inline uint32_t RightChild (uint32_t id) const;
   inline uint32_t Root (void) const;
+  /* Return the position in the array of the last element included in it. */
   uint32_t Last (void) const;
   inline bool IsRoot (uint32_t id) const;
   inline bool IsBottom (uint32_t id) const;
-  inline bool IsLower (Scheduler::EventKey const*a, Scheduler::EventKey const*b) const;
-  inline bool IsLess (uint32_t a, uint32_t b) const;
+  inline bool IsLowerStrictly (Scheduler::EventKey const*a, Scheduler::EventKey const*b) const;
+  inline bool IsLessStrictly (uint32_t a, uint32_t b) const;
   inline uint32_t Smallest (uint32_t a, uint32_t b) const;
 
   inline void Exch (uint32_t a, uint32_t b);
   void BottomUp (void);
-  void TopDown (void);
+  void TopDown (uint32_t start);
 
   BinaryHeap m_heap;
 };