src/simulator/list-scheduler.cc
changeset 2913 66dd24c80d75
parent 2577 5b41cb5c3fcf
child 3805 902c5237743a
equal deleted inserted replaced
2912:843e6218834f 2913:66dd24c80d75
       
     1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
       
     2 /*
       
     3  * Copyright (c) 2005 INRIA
       
     4  *
       
     5  * This program is free software; you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License version 2 as
       
     7  * published by the Free Software Foundation;
       
     8  *
       
     9  * This program is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    12  * GNU General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU General Public License
       
    15  * along with this program; if not, write to the Free Software
       
    16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    17  *
       
    18  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
       
    19  */
       
    20 
       
    21 #include "list-scheduler.h"
       
    22 #include "event-impl.h"
       
    23 #include <utility>
       
    24 #include <string>
       
    25 #include "ns3/assert.h"
       
    26 
       
    27 namespace ns3 {
       
    28 
       
    29 
       
    30 ListScheduler::ListScheduler ()
       
    31 {}
       
    32 ListScheduler::~ListScheduler ()
       
    33 {}
       
    34 
       
    35 bool 
       
    36 ListScheduler::IsLower (Scheduler::EventKey const*a, Scheduler::EventKey const*b) const
       
    37 {
       
    38   if (a->m_ts < b->m_ts)
       
    39     {
       
    40       return true;
       
    41     }
       
    42   else if (a->m_ts == b->m_ts &&
       
    43            a->m_uid < b->m_uid)
       
    44     {
       
    45       return true;
       
    46     }
       
    47   else
       
    48     {
       
    49       return false;
       
    50     }
       
    51 }
       
    52 
       
    53 void
       
    54 ListScheduler::Insert (const EventId &id)
       
    55 {
       
    56   Scheduler::EventKey key;
       
    57   // acquire refcount on EventImpl
       
    58   EventImpl *event = id.PeekEventImpl ();
       
    59   event->Ref ();
       
    60   key.m_ts = id.GetTs ();
       
    61   key.m_uid = id.GetUid ();
       
    62   for (EventsI i = m_events.begin (); i != m_events.end (); i++) 
       
    63     {
       
    64       if (IsLower (&key, &i->second))
       
    65         {
       
    66           m_events.insert (i, std::make_pair (event, key));
       
    67           return;
       
    68         }
       
    69     }
       
    70   m_events.push_back (std::make_pair (event, key));
       
    71 }
       
    72 bool 
       
    73 ListScheduler::IsEmpty (void) const
       
    74 {
       
    75   return m_events.empty ();
       
    76 }
       
    77 EventId
       
    78 ListScheduler::PeekNext (void) const
       
    79 {
       
    80   std::pair<EventImpl *, EventKey> next = m_events.front ();
       
    81   return EventId (next.first, next.second.m_ts, next.second.m_uid);
       
    82 }
       
    83 
       
    84 EventId
       
    85 ListScheduler::RemoveNext (void)
       
    86 {
       
    87   std::pair<EventImpl *, EventKey> next = m_events.front ();
       
    88   m_events.pop_front ();
       
    89   return EventId (Ptr<EventImpl> (next.first,false), next.second.m_ts, next.second.m_uid);
       
    90 }
       
    91 
       
    92 bool
       
    93 ListScheduler::Remove (const EventId &id)
       
    94 {
       
    95   for (EventsI i = m_events.begin (); i != m_events.end (); i++) 
       
    96     {
       
    97       if (i->second.m_uid == id.GetUid ())
       
    98         {
       
    99           NS_ASSERT (id.PeekEventImpl () == i->first);
       
   100           // release single acquire ref.
       
   101           i->first->Unref ();
       
   102           m_events.erase (i);
       
   103           return true;
       
   104         }
       
   105     }
       
   106   NS_ASSERT (false);
       
   107   return false;
       
   108 }
       
   109 
       
   110 }; // namespace ns3