⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 router_5.cpp

📁 router simulation,eveant drivven simulation
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//router eveant driven simulation

#include <stdafx.h>
#include <conio.h>
#include <iostream>
#include <string.h>
#include <fstream>
#include <stdlib.h>
using namespace std;
using namespace System;

struct  frame
//Represent Income Message
{
	  int path;//Destantion in the router(Link number)
	  int size;//Size of Message in bit
	  int time;//Store the clock at the evant of income message,used later for calculate the delay
	  void in (int p,int s,int t)  {path=p;size=s;time=t;}//Change the data in the current frame
};

ostream& operator<<(ostream& os, const frame& d)
// to enable cout for variable type frame
{
		
	cout<<"path = "<<d.path<<"\t"<<"size = "<<d.size<<"\t"<<"time = "<<d.time;
    return os;
}
//-------------------------------------------------------------------------------------
template <class T>
class Queue {
//a Queue class Template

protected:
	T* a_start;    // Pointer to queue
	int r_start;   // Refernce start in the Queue
	int len;       //length of cells in the queue
	int num;       // number of elments store in the queue

public:
	Queue (int=10);                   //Default Constructor            
	Queue (const Queue<T> &);         //Copy constrauctor
	~Queue (void);                    //Destructor
	int enqueue(const T& );           //Store element in queue 
	int dequeue(T&) ;                 //Pull element from queue 
    Queue<T> merge (const Queue<T>& );//Merge 2 Queue  into 1 and return it
	Queue<T> split (void);            //Spilt 1 queue into 2 and return the second queue	
	void print();                     //Print queue
	Queue<T> operator = (const Queue<T> & a );//Assign function overload '=' 
    int size_q() {return (num);}//return number elments in the queue
	
};
//----------------------------------------------------------------------------------

template <class T>
Queue<T>::Queue (int s)
//Default Constructor
{
	len=s > 0 && s < 1000 ? s : 10 ;//range of Length bounded by [1....1000]
	num=0;
	a_start=new T[len];// Dynamic allocate of memory 
	r_start=0;
}

template <class T>
Queue<T>::Queue (const Queue<T> & a )
//Copy Constructor
{
	len=a.len;
	num=a.num;
	r_start=0;//reference start
	a_start=new T[len];
	for (int i=0;i<a.num;i++) *(a_start+i)=*(a.a_start+(a.r_start+i)%a.len);
    //copy a Queue to current Queue,and position the elemnts
	//in the current queue from start (meanning referng start=0)
	//this done by help of modolue(%)
}

template <class T>
Queue<T> Queue<T>::operator = (const Queue<T> & a )
//Assign function overload operator =
{
    if (this==&a )return *this;//in case of a=a;
	delete a_start;
    len=a.len;
	num=a.num;
	r_start=a.r_start;
	a_start=new T[len];
	for (int i=0;i<a.num;i++) *(a_start+i)=*(a.a_start+i);
	//copy elments from one queue to anther
	return *this;
}
    
template <class T>
Queue<T>::~Queue (void)
//Destructor
{
 delete a_start;
}

template <class T>
int Queue<T>:: enqueue(const T& data )
//Store element in queue
{
	if (num==len) return 0;//in case the queue is full return 0
	
	*(a_start+(r_start+num)%len)=data;//calculate the i postion for the object (i)
	                                  //that needed to be store in the queue using %
	
	num++;//update number of elements in the queue
	
	return 1;//return 1 in case store element was sucsess	
}

template <class T>
int Queue<T>:: dequeue(T& ret)
//pull element from queue
{
if (num==0) return 0;//if queue is empty return 0
ret=*(a_start+r_start);//pull the first elment from the queue
r_start=(((++r_start)*(num-1!=0))%len);//update the refernce start(r_start)
num--;//update number of elments in the queue
return 1;// in case pull elment was sucsess return 1 
}

template <class T>
void Queue<T>::print()
//print queue
{
	for (int i=0;i<num;i++) cout<<endl<<*(a_start+(r_start+i)%len);
	cout<<endl<<"num = "<<num<<endl<<"length = "<<len;
    // print the elments in queue using % to calculate the postion for i object
	//in case they dont start from the beging of the queue meanning (r_strat !=0)
}


template <class T>
Queue<T> Queue<T>::merge (const Queue<T>& b)
//merge 2 queue  into 1 and return it
{
 Queue<T>  mer(len+b.len);//bulid a queue object with size of 2 object queue
 
 //copy elments of first queue,after that copy elments of second queue
 for (int i=0;i<num;i++) *(mer.a_start+i)=*(a_start+(r_start+i)%len);
 for (int i=0;i<b.num;i++) *(mer.a_start+num+i)=*(b.a_start+(b.r_start+i)%b.len);//
 
 mer.num=num+b.num;//update number of elments in the new queue 
 return (mer);//return the new queue (merge queue)
}

template <class T>
Queue<T> Queue<T>::split (void)
//spilt 1 queue into 2 and return the second queue
{
  Queue<T>  spl(len/2);//build a queue half size of current queue
  T* arr;//temp array
  spl.num=num/2;
  
  //copy first half of current queue to new queue
  for (int i=0;i<(num/2);i++) *(spl.a_start+i)=*(a_start+(r_start+i)%len);
  
  //copy second half of current queue to array
  arr=new T[len-len/2];
  for (int i=num/2;i<num;i++) *(arr+i-num/2)=*(a_start+(r_start+i)%len);
  
  //move the temp array to cureent queue,return the new queue
  delete a_start;
  num=num-num/2;
  len=len-len/2;
  r_start=0;
  a_start=arr;
  arr=NULL;//to aviod attachment between pointer a_start ,arr
  return spl;
}

//-----------------------------------------------------------------------------------------------

class Queue_frame:public Queue<frame>
//inherited from Queue<t> ,this is a queue class for variable type frame
{
public:
	static int  Queue_len;//needed for d.c when bulding queue array,it is update in Router constructor
    Queue_frame(int a=Queue_len):Queue<frame>(a) { }//default construactor,using static int as parametr for default,that can be changed
    frame first(void)  { return (*(a_start+r_start));} //return first frame from queue
	int len_q() {return (len);}//return the length of queue
};

int Queue_frame::Queue_len=10;//initial static int,it is also update in Router constructor

//---------------------------------------------------------------------------------------------

class Router {
//Create a Router that forward messages to N output links from signal input 
	
	int voluem;  //size in bit of link(queue)
	int speed; // speed of link
	int link; //numbers of link(channel)
	Queue_frame * router_p;//point to array of queue(links)
	int         * size_links;//point to array that contain the remain size (in bit) of each link
	int         * time_links;//point to array that contain all service time  of the 1st message in each link	

public:

	Router (int=1000,int=100,int=10,int=10);//Default constructor
	Router(const Router &);//copy constructor
	~Router (void); //destructor
    int push_msg( frame & ); //push frame to link
	int pop_msg (frame &,int);//pop frame from link
    int min_time (int &); //find min time to event and the number(path)  of the link;
    void time_update (int);//subtract  min time  to evant (update the min time)
};

//----------------------------------------------------------------------------------------------
	Router::Router(int v,int s,int p,int q)
	//Default constructor
	{
		voluem=v;//set size of link in bit
		speed=s;//speed of link  bit/(unit time) (bit/sec)
		link=p;//numberes of links
		Queue_frame::Queue_len=q;//change the value of  default constructor in class Queue_frmae(needed to bulid array of queue)
		router_p = new  Queue_frame[link];//array of queue,each queue has same size (update by the static int )
		time_links=new int [link];//array that store service time of all 1st mesage in each link
		size_links= new int [link];//array that store the remain size in each link
		for (int i=0;i<link;i++) {*(size_links+i)=voluem;*(time_links+i)=-1;} //initialization the array of remain size, service time 
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -