src/simulator/scheduler-map.cc
changeset 2972 d76553495b91
parent 2971 75780f899be3
parent 2921 8a616a8b723a
child 2973 e8d8a0650927
equal deleted inserted replaced
2971:75780f899be3 2972:d76553495b91
     1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
       
     2 /*
       
     3  * Copyright (c) 2006 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  * The idea to use a std c++ map came from GTNetS
       
    20  */
       
    21 
       
    22 #include "scheduler-map.h"
       
    23 #include "event-impl.h"
       
    24 #include "ns3/assert.h"
       
    25 #include <string>
       
    26 
       
    27 #define noTRACE_MAP 1
       
    28 
       
    29 #ifdef TRACE_MAP
       
    30 #include <iostream>
       
    31 # define TRACE(x) \
       
    32 std::cout << "MAP TRACE " << x << std::endl;
       
    33 #else /* TRACE_MAP */
       
    34 # define TRACE(format,...)
       
    35 #endif /* TRACE_MAP */
       
    36 
       
    37 
       
    38 namespace ns3 {
       
    39 
       
    40 SchedulerMap::SchedulerMap ()
       
    41 {}
       
    42 SchedulerMap::~SchedulerMap ()
       
    43 {}
       
    44 
       
    45 /* Note the invariants which this function must provide:
       
    46  * - irreflexibility: f (x,x) is false)
       
    47  * - antisymmetry: f(x,y) = !f(y,x)
       
    48  * - transitivity: f(x,y) and f(y,z) => f(x,z)
       
    49  */
       
    50 bool
       
    51 SchedulerMap::EventKeyCompare::operator () (struct EventKey const&a, struct EventKey const&b)
       
    52 {
       
    53   if (a.m_ts < b.m_ts) 
       
    54     {
       
    55       return true;
       
    56     } 
       
    57   else if (a.m_ts > b.m_ts)
       
    58     {
       
    59       return false;
       
    60     } 
       
    61   else if (a.m_uid < b.m_uid)
       
    62     {
       
    63       return true;
       
    64     }
       
    65   else 
       
    66     {
       
    67       return false;
       
    68     }
       
    69 }
       
    70 
       
    71 
       
    72 
       
    73 void
       
    74 SchedulerMap::Insert (const EventId &id)
       
    75 {
       
    76   // acquire a single ref
       
    77   EventImpl *event = id.PeekEventImpl ();
       
    78   event->Ref ();
       
    79   Scheduler::EventKey key;
       
    80   key.m_ts = id.GetTs ();
       
    81   key.m_uid = id.GetUid ();
       
    82   std::pair<EventMapI,bool> result;
       
    83   result = m_list.insert (std::make_pair (key, event));
       
    84   NS_ASSERT (result.second);
       
    85 }
       
    86 
       
    87 bool
       
    88 SchedulerMap::IsEmpty (void) const
       
    89 {
       
    90   return m_list.empty ();
       
    91 }
       
    92 
       
    93 EventId
       
    94 SchedulerMap::PeekNext (void) const
       
    95 {
       
    96   EventMapCI i = m_list.begin ();
       
    97   NS_ASSERT (i != m_list.end ());
       
    98   
       
    99   return EventId (i->second, i->first.m_ts, i->first.m_uid);
       
   100 }
       
   101 EventId
       
   102 SchedulerMap::RemoveNext (void)
       
   103 {
       
   104   EventMapI i = m_list.begin ();
       
   105   std::pair<Scheduler::EventKey, EventImpl*> next = *i;
       
   106   m_list.erase (i);
       
   107   return EventId (Ptr<EventImpl> (next.second, false), next.first.m_ts, next.first.m_uid);
       
   108 }
       
   109 
       
   110 bool
       
   111 SchedulerMap::Remove (const EventId &id)
       
   112 {
       
   113   Scheduler::EventKey key;
       
   114   key.m_ts = id.GetTs ();
       
   115   key.m_uid = id.GetUid ();
       
   116   EventMapI i = m_list.find (key);
       
   117   NS_ASSERT (i->second == id.PeekEventImpl ());
       
   118   // release single ref.
       
   119   i->second->Unref ();
       
   120   m_list.erase (i);
       
   121   return true;
       
   122 }
       
   123 
       
   124 }; // namespace ns3