BeginPackage["UVW`Timerep`"]
(* Version 2.0, 08/15/97 *)

Queue::usage= "Queue[interarrivals,services] represents on a graphics as a 
function of time, the evolution of the number of customers in a queue 
with one server.The times between consecutive arrivals are read
in the first list, the service times in the second one."
 

CumulatedTimes::usage= "CumulatedTimes[listoftimes] represents on a graphics
the function of time defined as follows. Starting from 0, it is  
incremented by one at dates separated by the times read in listoftimes."
	

Geiger::usage= "Geiger[listoftimes] plots on a line the dates separated by
the durations read in listoftimes."


	 
Begin["`Private`"]




Queue[interarrivals_List,services_List] :=

     Block[{m, arr={}, dep={}, abs={}, ord={}, events={}},
	

	  m=Min[Length[interarrivals],Length[services]];

	  arr=Rest[FoldList[Plus,0,N[interarrivals]]];
	  dep=Append[dep,First[arr]+First[services]];


	  For[k=1, k<m, k++,
		   If [(dep[[k]]<arr[[k+1]]),
			dep = Append[ dep, arr[[k+1]]+services[[k+1]] ],
			dep = Append[ dep, dep[[k]]+services[[k+1]] ]
		      ]
	     ]; 
   
	  arr    = Transpose[{arr,Table[1.,{Length[arr]}]}];
	  dep    = Transpose[{dep,Table[-1.,{m}]}];
	  events = Transpose[Sort[Join[arr,dep]]];
	  abs    = events[[1]];
	  ord    = FoldList[Plus,0,events[[2]]];
	  abs    = Flatten[Transpose[{abs,abs}]];
	  ord    = Flatten[Transpose[{Drop[ord,-1],Drop[ord,1]}]];
	  events = Transpose[{abs,ord}];  
	  events = Prepend[events,{0.,0.}];

	  ListPlot [events, PlotJoined -> True,
	  AxesLabel -> {"t","N(t)"}]
      ]




CumulatedTimes[listoftimes_List] :=

      Block[{cumul={},ordinates={},g},
       
	 cumul = FoldList[Plus,0,N[listoftimes]]; 
	 ordinates = Range[0.,N[Length[listoftimes]-1]];
	 g = Transpose[ { Transpose[{Drop[cumul,-1],ordinates}] , 
			       Transpose[{Drop[cumul,1],ordinates}] } ];

	 g = Table[Line[g[[i]]],{i,1,Length[g]}];
     
	 Show[ Graphics [g] , 
		    Prolog    -> Thickness[0.003],
		    Axes      -> True, 
		    AxesLabel -> {"t","N(t)"}]
      ]



Geiger[listoftimes_List]:=

	Block[{cumul,max,g},
	    
	     cumul = Rest[FoldList[Plus,0,N[listoftimes]]];
	     max   = Max[cumul];

	     g = Table[Line[{{cumul[[i]],0.},{cumul[[i]],0.01}}],
		       {i,Length[cumul]}]; 
	     
	     Show[ Graphics [g], 
			Prolog    -> Thickness[0.003],
			PlotRange -> {{0.,max},{0.,1.}},
			Axes ->{True,False},
			AxesLabel ->{"t","t"}]
	     ]
	

End[]


EndPackage[]

