-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmy_pqueue.c
More file actions
188 lines (137 loc) · 3.47 KB
/
my_pqueue.c
File metadata and controls
188 lines (137 loc) · 3.47 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
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
//
// Created by Xavier McNulty on 4/28/16.
//
#include "my_pqueue.h"
#include <stdlib.h>
#include <stdio.h>
/* initialize a priority queue; O(1) */
void my_pqueue_init(my_pqueue_t *q) {
q = malloc(sizeof(my_pqueue_t));
q->size = 0;
}
/* insert thread into priority queue; O(n) */
void my_pqueue_insert(my_pqueue_t *q, int prio, my_tcb t) {
if (q == NULL)
return;
t->priority = prio;
// case: q is empty
if (!q->q_head) {
q->q_head = t;
return;
}
my_tcb cur = q->q_head, prev = 0;
// find where the tcb should be inserted.
while (cur && cur->priority >= prio) {
prev = cur;
cur = cur->next;
}
// case: insert at head
if (!prev) {
cur->prev = t;
t->next = cur;
q->q_head = t;
}
// case: insert at tail
else if (!cur) {
prev->next = t;
t->prev = prev;
}
// case: insert between two tcb's
else {
prev->next = t;
cur->prev = t;
}
q->size ++;
}
/* remove thread with maximum priority from priority queue; O(1) */
my_tcb my_pqueue_delmax(my_pqueue_t *q) {
// return empty if no queue or queue is empty
if (!q || !q->q_head)
return NULL;
my_tcb head = q->q_head;
q->q_head = head->next;
q->size --;
return head;
}
/* remove thread from priority queue; O(n) */
void my_pqueue_delete(my_pqueue_t *q, my_tcb t) {
if (!q || !q->q_head)
return;
my_tcb cur = q->q_head;
while (cur && cur != t)
cur = cur->next;
if (!cur) // t not in queue
return;
if (cur->next)
cur->next->prev = cur->prev;
if (cur->prev)
cur->prev->next = cur->next;
q->size --;
}
/* move a thread inside a queue to the top; O(n) */
int my_pqueue_favorite(my_pqueue_t *q, my_tcb t) {
if (!q || !q->q_head)
return 0;
my_tcb cur = q->q_head;
while (cur && cur != t)
cur = cur->next;
if (!cur)
return 0;
// remove thread from it's current position
if (cur->next)
cur->next->prev = cur->prev;
if (cur->prev)
cur->prev->next = cur->next;
// put cur at the head of the queue
q->q_head->prev = cur;
cur->next = q->q_head;
cur->prev = NULL;
q->q_head = cur;
return 1;
}
/* increase priority of all(!) threads in queue; O(1) */
void my_queue_increase(my_pqueue_t *q) {
if (!q)
return;
my_tcb cur = q->q_head;
while (cur) {
if (cur->priority < MY_PRIO_MAX)
cur->priority ++;
cur = cur->next;
}
}
/* returns the my_tcb at the tail of the q */
my_tcb my_pqueue_tail(my_pqueue_t *q) {
if (!q || !q->q_head)
return NULL;
my_tcb cur = q->q_head;
while (cur->next)
cur = cur->next;
return cur;
}
/* walk to next or previous thread in queue; O(1) */
my_tcb my_pqueue_walk(my_pqueue_t *q, my_tcb t, int direction) {
if (!q || !q->q_head)
return NULL;
my_tcb cur = q->q_head;
while (cur) {
if (direction == PTH_WALK_PREV && cur->next == t)
return cur;
else if (direction == PTH_WALK_NEXT && cur == t)
return cur->next;
cur = cur->next;
}
return NULL;
}
/* check whether a thread is in a queue: O(n) */
int my_pqueue_contains(my_pqueue_t *q, my_tcb t) {
if (!q || !q->q_head)
return 0;
my_tcb cur = q->q_head;
while (cur) {
if (cur == t)
return 1;
cur = cur->next;
}
return 0;
}