-
Notifications
You must be signed in to change notification settings - Fork 0
/
Circuit.java
122 lines (105 loc) · 2.88 KB
/
Circuit.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
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Queue;
import java.util.PriorityQueue;
public class Circuit {
private String circuitId;
//Max Queue size (total number of jugglers / total number of circuits)
private int queueSize;
private ArrangeJugglers arrange;
private boolean isFull;
private Skills skill;
//Priority Queue to store Juggler in order of best fit (Min Heap)
private PriorityQueue<Juggler> inList;
public ArrangeJugglers getArrange() {
return arrange;
}
public Skills getSkill() {
return skill;
}
public void setSkill(Skills skill) {
this.skill = skill;
}
public void setArrange(ArrangeJugglers arrange) {
this.arrange = arrange;
}
public boolean isFull() {
if(inList.size()==queueSize)
return true;
else
return false;
}
//Comparator for min heap
public class MinJuggler implements Comparator<Juggler>
{
@Override
public int compare(Juggler x, Juggler y)
{
int sumx=skill.dotProduct(x.getSkill());
int sumy=skill.dotProduct(y.getSkill());
if(sumx>sumy)
return 1;
if(sumx<sumy)
return -1;
return 0;
}
}
Circuit(String circuitId,int size,Skills skill)
{
queueSize=size;
this.circuitId=circuitId;
this.skill=skill;
inList=new PriorityQueue<Juggler>(queueSize,new MinJuggler());
}
public String getCircuitId() {
return circuitId;
}
public void setCircuitId(String circuitId) {
this.circuitId = circuitId;
}
public PriorityQueue<Juggler> getInList() {
return inList;
}
public void setInList(PriorityQueue<Juggler> inList) {
this.inList = inList;
}
//Adds Juggler to the queue
public boolean addtoQueue(Juggler juggler)
{
/*If queue is full checks whether the juggler is best fit or not
a) if Juggler is best fit then removes head of the minHeap and inserts new Juggler
--- adds juggler to alloted List and removes head of the Queue to unalloted List
if Juggler is not best fit returns false
*/
if(isFull())
{
Juggler last=inList.peek();
int sumlast=skill.dotProduct(last.getSkill());
int sumJuggler=skill.dotProduct(juggler.getSkill());;
if(sumlast<sumJuggler)
{
//System.out.println(circuitId+"-"+juggler.getJugglerId()+":"+sumJuggler+"-"+last.getJugglerId()+":"+sumlast);
last=inList.poll();
last.setAssigned(false);
juggler.setAssigned(true);
arrange.addtounAllotedJuggler(last);
arrange.removeAllotedJuggler(last);
arrange.addtoAllotedJuggler(juggler);
arrange.removeUnAllotedJuggler(juggler);
inList.add(juggler);
return true;
}
else
{
return false;
}
}
//If Queue is not full adds Juggler to the Circuit
//System.out.println(circuitId+"-"+juggler.getJugglerId());
juggler.setAssigned(true);
inList.add(juggler);
arrange.addtoAllotedJuggler(juggler);
arrange.removeUnAllotedJuggler(juggler);
return true;
}
}