-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpriQ.h
163 lines (137 loc) · 3.68 KB
/
priQ.h
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
#pragma once
#include "headers.h"
typedef struct priQ {
int capacity;
int size;
process **queue;
} priQ;
/***** UTILS *****/
int parent(int i) {
return (i-1)/2;
}
int left(int i) {
return 2*i + 1;
}
int right(int i) {
return 2*i + 2;
}
bool comparisonSJF(process *x, process* y) {
if(x->runtime < y->runtime) {
return true;
}
return false;
}
bool comparison(process *x, process* y) {
//printf("I am flag %d\n",SJFflag);
if(SJFflag == 1) {
if(x->remainingtime < y->remainingtime) {
return true;
}
return false;
}
if(x->priority < y->priority) {
return true;
}
return false;
}
void swap(process** x, process** y) {
process* temp = *x;
*x = *y;
*y = temp;
}
//TODO - print
void priQprint(priQ *all_process) {
int size = all_process->size;
for(int i=0;i<size;i++) {
printf("%d ",all_process->queue[i]->id);
fflush(stdout);
}
printf("\n");
fflush(stdout);
}
// 1 for success, 0 for failed (when no queue is passed)
bool priQcreate(priQ *all_process,int capacity) {
if(!all_process) return 0;
if(!capacity) capacity=50;
all_process->size=0;
all_process->capacity=capacity;
all_process->queue = (process **)calloc(all_process->capacity, sizeof(process *)); //initialize to zero
return 1;
}
process* priQpeek(priQ *all_process) {
if(all_process->size==0) return NULL;
return all_process->queue[0];
}
void reheapDown(priQ* all_process, int i) {
int *size = &all_process->size;
bool left_ok = left(i) < *size && comparison(all_process->queue[left(i)],all_process->queue[i]);
bool right_ok = right(i) < *size && comparison(all_process->queue[right(i)],all_process->queue[i]);
while(i<*size && (left_ok || right_ok)) {
if (left_ok && right_ok) {
if (comparison(all_process->queue[left(i)],all_process->queue[right(i)])) {
swap(&all_process->queue[i], &all_process->queue[left(i)]);
i = left(i);
}
else {
swap(&all_process->queue[i], &all_process->queue[right(i)]);
i = right(i);
}
}
else if (left_ok) {
swap(&all_process->queue[i], &all_process->queue[left(i)]);
i = left(i);
}
else {
swap(&all_process->queue[i], &all_process->queue[right(i)]);
i = right(i);
}
left_ok = left(i) < *size && comparison(all_process->queue[left(i)],all_process->queue[i]);
right_ok = right(i) < *size && comparison(all_process->queue[right(i)],all_process->queue[i]);
}
}
void reheapUp(priQ* all_process) {
int *size = &all_process->size;
int i = *size - 1;
while (i != 0 && comparison(all_process->queue[i],all_process->queue[parent(i)])) {
swap(&all_process->queue[i], &all_process->queue[parent(i)]);
i = parent(i);
}
}
process* priQdequeue(priQ *all_process) {
if(!all_process || !all_process->queue) return NULL;
int *size = &all_process->size;
(*size--);
process* removed = all_process->queue[0];
all_process->queue[0] = all_process->queue[*size];
// reheap down
reheapDown(all_process,0);
return removed;
}
bool priQenqueue(priQ* all_process, process* p) {
if(all_process->capacity==all_process->size) return 0;
int *size = &all_process->size;
all_process->queue[*size] = p;
(*size)++;
// reheap up
reheapUp(all_process);
return 1;
}
bool priQremove(priQ* all_process, process* p) {
int i;
int *size = &all_process->size;
for (i = 0; i < *size; i++) {
if (all_process->queue[i] == p) {
swap(&all_process->queue[i], &all_process->queue[*size - 1]);
(*size)--;
reheapDown(all_process, i);
//break;
return 1;
}
}
return 0;
}
bool priQfree(priQ* all_process) {
if(!all_process || !all_process->queue) return 0;
free(all_process->queue);
return 1;
}