-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector.java
177 lines (115 loc) · 2.83 KB
/
Vector.java
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import java.util.*; import java.lang.Math;
class vector{
public static void main(String[] args)
{
System.out.println("Enter No.of Processes:");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
// System.out.print(n);
int ne [] = new int[n];
int status[][] = new int[n][10];
int prevp[][] = new int[n][10];
int preve[][] = new int[n][10]; for ( int i = 0; i<n;i++)
{
System.out.print("Enter no. of events of Process "+(i+1)+": "); ne[i] = sc.nextInt();
System.out.println("Enter 0 for Internal, 1 for Send, 2 for Receive"); for (int j = 0; j < ne[i]; j++)
{
if((status[i][j] != 1) && (status[i][j] !=2))
{
System.out.print("Event "+(j+1)+": "); status[i][j]= sc.nextInt();
if(status[i][j] == 1)
{
System.out.print("Enter Receiving Process: "); int a = sc.nextInt();
System.out.print("Enter Receiving Event: "); int b = sc.nextInt();
status[(a-1)][(b-1)] = 2;
prevp[i][j] = 0;preve[i][j] = -1;
prevp[(a-1)][(b-1)] = i;
preve[(a-1)][(b-1)] = j;
}
else if(status[i][j] == 2)
{
System.out.print("Enter Sending Process: "); int a = sc.nextInt();
System.out.print("Enter Sending Event: "); int b = sc.nextInt();
status[(a-1)][(b-1)] = 1;
prevp[i][j] = (a-1);preve[i][j] = (b-1);
prevp[(a-1)][(b-1)] = -1;
preve[(a-1)][(b-1)] = -1;
}
else
{
prevp[i][j] = 0;preve[i][j] = 0; continue;
}
}
else {
continue;
}
}
}
System.out.println();
int time[][] = new int[n][10];
for(int x = n-1; x>-1;x--)
{
for(int z=0;z<ne[x];z++)
{
time[x][z] = 0;
int p = (int)Math.pow(10,(n-(x+1))); if(z == 0)
{
time[x][z] = p;
}
else{
time[x][z] = p + time[x][(z-1)];
}
if(status[x][z] == 2)
{
if ( x == n-1)
{
int sum = ((time[(prevp[x][z])][(preve[x][z])])/10); sum = sum*10;
time[x][z] = sum + time[x][z];
}
else if( x == 0 ) {
int sum = ((time[(prevp[x][z])][(preve[x][z])])%p); time[x][z] = sum + time[x][z];
}
else
time[x][z] = (time[(prevp[x][z])][(preve[x][z])] + time[x][z]);
}
}
}
for(int x1 =n-1; x1 > -1; x1--)
{
for(int z1 =0;z1 <ne[x1]; z1 ++)
{
int p = (int)Math.pow(10,(n-(x1+1)));
if(z1 != 0)
{
time[x1][z1] = p + time[x1][(z1-1)];
}
else{
time[x1][z1] = p;
}
if(status[x1][z1] == 2)
{
if ( x1 == n-1)
{
int sum = ((time[(prevp[x1][z1])][(preve[x1][z1])])/10); sum = sum*10;
time[x1][z1] = sum + time[x1][z1];
}
else if( x1 == 0 ) {
int sum = ((time[(prevp[x1][z1])][(preve[x1][z1])])%p); time[x1][z1] = sum + time[x1][z1];
}
else
time[x1][z1] = (time[(prevp[x1][z1])][(preve[x1][z1])] + time[x1][z1]);
}
}
}
System.out.println("Vector Clocks");
for(int q =0; q<n;q++)
{
System.out.print("Process "+(q+1)+"\t"); for(int p=0;p<ne[q];p++)
{
String format = ("%0"+n+"d");
System.out.print((String.format(format, time[q][p]))+"\t");
}
System.out.println();
}
}
}