-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskAllocation.java
More file actions
124 lines (109 loc) · 3.53 KB
/
TaskAllocation.java
File metadata and controls
124 lines (109 loc) · 3.53 KB
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
/**
* A task allocation queue which holds the tasks in two parallel queues.
* One according to priority and one according to order of creation.
* At any given time, a task at the front of either line may be removed from the data structure.
*
*
*
*
*/
/*
Assignment number : 8
File Name : TaskAllocation.java
Name: Ran Zaaroor
Student ID : 209374040
Email : Ran.zaaroor@gmail.com
*/
public class TaskAllocation{
/*
* The heap and queue which compose the data structure.
* The most important thing to observe is:
* A TaskElement exists in the queue if and only if it also exists in the heap.
*/
TaskHeap heap;
TaskQueue q;
/**
* Creates an empty task allocation queue
*/
public TaskAllocation(){
heap = new TaskHeap();
q = new TaskQueue();
}
/**
* Creates a task allocation queue from an a array of tasks, ordered according to the creation time
* @param arr a given array of TaskElements. The heapIndex field of the elements in arr might be incorrect
*/
public TaskAllocation(TaskElement [] arr){
this.q=new TaskQueue();
for(int i=0; i<arr.length;i++) {
q.enqueue(arr[i]);
}
this.heap= new TaskHeap(arr);
}
/**
* Adds a new Task to the data structure.
* The Task is entered (wrapped by a TaskElement) to the back of the queue
* and into the heap, according to its priority.
*
* @param c
*/
public void addTask(Task t){
TaskElement e = new TaskElement(t);
heap.insert(e);
q.enqueue(e);
}
/**
* Removes the task with the highest priority from the data structure.
* The task must be removed both from the heap and the queue.
*
* @return the task with the highest priority.
*/
public Task allocatePriorityTask(){
TaskElement e = heap.extractMax();
if(q.peek() == e){
q.dequeue();
return e.t;
}
if (q.last == e){
q.last = e.next;
}
e.disconnect();
return e.t;
}
/**
* Removes the task which was created first to the data structure.
* The task must be removed both from the heap and the queue.
*
* @return the task which arrived first to the data structure
*/
public Task allocateRegularTask(){
TaskElement e = q.dequeue();
heap.remove(e.heapIndex);
return e.t;
}
public static void main (String[] args){
/*
* A basic test to check your class.
* Expected outcome:
* task: Solve a problem in production, priority: 100
* task: Add a new feature, priority: 10
* task: Code Review, priority: 3
* task: Analyze performance, priority: 20
* task: Move to the new Kafka server, priority: 2
*/
Task a = new Task(10, "Add a new feature");
Task b = new Task(3, "Code Review");
Task c = new Task(2, "Move to the new Kafka server");
Task d = new Task(100, "Solve a problem in production");
Task e = new Task(20, "Analyze performance");
TaskElement [] arr = {new TaskElement(a), new TaskElement(b), new TaskElement(c)};
TaskAllocation q = new TaskAllocation(arr);
q.addTask(d);
System.out.println(q.allocatePriorityTask());
System.out.println(q.allocatePriorityTask());
q.addTask(e);
System.out.println(q.allocateRegularTask());
System.out.println(q.allocatePriorityTask());
System.out.println(q.allocateRegularTask());
}
}