-
Notifications
You must be signed in to change notification settings - Fork 0
/
ps.c
115 lines (97 loc) · 1.78 KB
/
ps.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// --- G. Rubino, oct. 2014
/* PICOSIMULATOR, simplified version --- ps.c */
/* gcc -c -o ps.o ps.c */
#include <stdio.h>
#include <stdlib.h>
#include "ps.h"
/* --- this will point to the first event in the list */
EVENT *First;
/* --- event insertion */
void InsertEv(EVENT *ev)
{
EVENT *x, *y;
double time;
time = ev->Time;
if (time < First->Time)
{
/* --- ev will become the first one */
ev->Next = First;
First = ev;
return;
}
/* here, ev goes at least at 2nd position */
x = First;
y = x->Next;
while (y != NULL)
{
if (time < y->Time)
{ /* --- ev goes after x end before y */
ev->Next = y;
x->Next = ev;
return;
}
x = y;
y = y->Next;
}
/* ev goes after the whole list */
x->Next = ev;
ev->Next = NULL;
}
/* --- event destruction */
void DeleteEv(EVENT *ev)
{
EVENT *x, *y;
x = First;
if (ev == First)
{
First = First->Next;
free(x);
return;
}
/* ev is not the first element */
y = x->Next;
while (y != NULL)
{
if (y == ev)
{
x->Next = y->Next;
free(y);
return;
}
x = y;
y = y->Next;
}
}
/* accessing the first event */
void FirstEv(int *c, double *t)
{
EVENT *x;
x = First;
First = First->Next;
*t = x->Time;
*c = x->Class;
free(x);
}
/* initializing the event-list */
void InitEvList(double t)
{
EVENT *ev;
CREATE_EV(ev, END_SIM, t);
First = ev;
}
/* print event list */
void PrintEventList()
{
EVENT *x;
int n = 0;
printf(" --- Event list:\n");
x = First;
while(x != NULL)
{
n++;
printf(" --- event %d: class %d, time %lf\n",n,x->Class,x->Time);
x = x->Next;
}
if (n == 0) printf(" --- event list empty.\n");
free(x);
}