-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArrangeJugglers.java~
228 lines (191 loc) · 5.31 KB
/
ArrangeJugglers.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.Stack;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CopyOnWriteArrayList;
public class ArrangeJugglers {
/*Holding a Map for Circuits and Jugglers for fast access*/
private HashMap<String, Juggler> Jugglers;
private HashMap<String, Circuit> circuits;
//Queue to store alloted Jugglers
private Queue<Juggler> alloted;
/*Queues to Store all Circuits and all Jugglers*/
private Queue<Juggler> unAlloted;
private Queue <Circuit> unOccupiedCircuits;
//Queue to store Jugglers with no preference List
private Queue<Juggler> unDefined;
ArrangeJugglers()
{
unOccupiedCircuits=new ConcurrentLinkedQueue<Circuit>();
alloted=new ConcurrentLinkedQueue<Juggler>();
unAlloted=new ConcurrentLinkedQueue<Juggler>();
unDefined=new ConcurrentLinkedQueue<Juggler>();
}
public Queue<Circuit> getUnOccupiedCircuits() {
return unOccupiedCircuits;
}
public void setUnOccupiedCircuits(Queue<Circuit> unOccupiedCircuits) {
this.unOccupiedCircuits = unOccupiedCircuits;
}
public HashMap<String, Juggler> getJugglers() {
return Jugglers;
}
public void setJugglers(HashMap<String, Juggler> jugglers) {
Jugglers = jugglers;
}
public Queue<Juggler> getAlloted() {
return alloted;
}
public void setAlloted(Queue<Juggler> alloted) {
this.alloted = alloted;
}
public Queue<Juggler> getUnAlloted() {
return unAlloted;
}
public void setUnAlloted(Queue<Juggler> unAlloted) {
this.unAlloted = unAlloted;
}
public HashMap<String, Circuit> getCircuits() {
return circuits;
}
public void setCircuits(HashMap<String, Circuit> circuits) {
this.circuits = circuits;
}
public void addtounAllotedJuggler(Juggler juggler)
{
//System.out.println("kicked out"+juggler.getJugglerId());
unAlloted.add(juggler);
}
public void removeAllotedJuggler(Juggler juggler)
{
alloted.remove(juggler);
}
public void addtoAllotedJuggler(Juggler juggler)
{
alloted.add(juggler);
}
public void removeUnAllotedJuggler(Juggler juggler)
{
unAlloted.remove(juggler);
}
//Actual Logic
public void addtoCircuit()
{
//Loops un-till there are no un-alloted jugglers
while (unAlloted.size() > 0)
{
for(Juggler juggler:unAlloted)
{
//For each juggler once a circuit is visited it is removed
if(juggler.getCircuitPreference().size()>0)
{
Circuit circuit=circuits.get(juggler.getCircuitPreference().peek());
circuit.addtoQueue(juggler);
juggler.getCircuitPreference().poll();
}
else
{
//Juggler whose circuits are exhausted and still not assigned to any group
//System.out.println("undefined"+juggler.getJugglerId());
unAlloted.remove(juggler);
unDefined.add(juggler);
}
}
//Look for all non empty circuits
for(Map.Entry<String, Circuit> entry : circuits.entrySet())
{
String circuitId=entry.getKey();
Circuit circuit=entry.getValue();
if(circuit.isFull())
{
unOccupiedCircuits.remove(circuit);
}
}
}
//Assign Each undefined Juggler to each non Empty Circuit in order
for(Circuit circuit:unOccupiedCircuits)
{
for(Juggler juggler:unDefined)
{
if(!circuit.isFull())
{
circuit.addtoQueue(juggler);
unDefined.remove(juggler);
}
else
{
unOccupiedCircuits.remove(circuit);
break;
}
}
}
}
//Sanity check to verify whether the program is correct or not
public boolean check()
{
return (alloted.size()==Jugglers.size());
}
class circuitCompare implements Comparator<Circuit>{
public int compare(Circuit x, Circuit y)
{
int cxNumber=Integer.parseInt(x.getCircuitId().substring(1));
int cyNumber=Integer.parseInt(y.getCircuitId().substring(1));
if(cxNumber>cyNumber)
return 1;
if(cxNumber<cyNumber)
return -1;
return 0;
}
}
//Writes Output to a file
public void print() throws IOException
{
System.out.println("Printing.........");
BufferedWriter output = new BufferedWriter(new FileWriter("output.txt"));
//To print in sorted order of circuits according to circuit numbers
Set<Circuit> circuitList=new TreeSet<Circuit>(new circuitCompare());
for(Map.Entry<String, Circuit> entry : circuits.entrySet())
{
circuitList.add(entry.getValue());
}
//Printing to output.txt
for(Circuit circuit:circuitList)
{
Stack<Juggler> jug=new Stack<Juggler>();
output.write(circuit.getCircuitId() + " ");
for(Juggler juggler:circuit.getInList())
{
jug.push(juggler);
}
while(!jug.isEmpty())
{
Juggler juggler=jug.pop();
output.write(juggler.getJugglerId() + " ");
for(String ckt:juggler.getCircuits())
{
Circuit ckts=circuits.get(ckt);
output.write(ckt + ":");
output.write(juggler.getSkill().dotProduct(ckts.getSkill()) + " ");
}
if(jug.size()!=0)
output.write(",");
}
output.write("\n");
}
output.close();
System.out.println("Done Printing.........Check Output.txt");
}
}