[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[ns] patch: fast, simple and bugfree scheduler



Hi,

I'm using ns 2.1b7 and found that the default Calendar scheduler is buggy (after some time it starts consuming megabytes of memory and the time stops) and that the simple and bugfree list scheduler is dog slow.
So I hacked a fast, simple and bugfree scheduler, here it is:

in scheduler.h:

#include <set>

struct EventComp
{
   bool operator()(Event* e1, Event *e2) const
   {
      return (e1->time_ < e2->time_);
   };
};

class SetScheduler : public Scheduler {
public:
	SetScheduler();
	virtual void cancel(Event*);
	virtual void insert(Event*);
	virtual Event* deque();
	virtual Event* lookup(int uid);
protected:
   std::multiset< Event* ,EventComp> mset;
};

and the implementation:

static class SetSchedulerClass : public TclClass {
public:
	SetSchedulerClass() : TclClass("Scheduler/Set") {}
	TclObject* create(int , const char*const* ) {
		return (new SetScheduler);
	}
} class_set_sched;

SetScheduler::SetScheduler()
{ };

//this one is slooooow !!!!!
void SetScheduler::cancel(Event* e)
{
   for (std::multiset<Event* , EventComp >::iterator it=mset.begin(); it!=mset.end(); it++)
   {
      if ((*it)==e)
      {
         mset.erase(it);
         e->uid_ = - e->uid_;
         return;
      };
   };
};

void SetScheduler::insert(Event* e)
{
   if (e==0)
      return;
   mset.insert(e);
};

Event* SetScheduler::deque()
{
   std::multiset<Event* , EventComp >::iterator it=mset.begin();
   if (it==mset.end())
   {
      return 0;
   };
   Event* e=*it;
   mset.erase(it);
   return e;
};

//this one is slooooow !!!!
Event* SetScheduler::lookup(int uid)
{
   for (std::multiset<Event* , EventComp >::iterator it=mset.begin(); it!=mset.end(); it++)
   {
      if ((*it)->uid_==uid)
      {
         return (*it);
      };
   };
   return 0;
};


Bye
Alex

--
Free Dmitry Sklyarov ! 
http://www.freesklyarov.org