-
Notifications
You must be signed in to change notification settings - Fork 0
/
priority_queue.c
174 lines (159 loc) · 4.81 KB
/
priority_queue.c
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
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <pthread.h>
#define QUEUE_SIZE 100
int getParent(int index) {
assert(index > 0);
return (index - 1) / 2;
}
int getLeftChild(int index) {
assert(index >= 0);
return (index * 2) + 1;
}
int getRightChild(int index) {
assert(index >= 0);
return (index * 2) + 2;
}
typedef struct {
int priority;
void *data;
} Priority_node;
typedef struct {
Priority_node* queue;
int size;
int MAX_SIZE;
pthread_mutex_t lock_on_data;
pthread_mutex_t lock_on_enqueue;
pthread_mutex_t lock_on_deque;
} Heap;
Heap *init_queue(int size) {
Heap *new_heap = malloc(sizeof(Heap));
new_heap->queue = malloc(sizeof(Priority_node)*size);
new_heap->MAX_SIZE = size;
new_heap->size = 0;
pthread_mutex_lock(&new_heap->lock_on_enqueue);
pthread_mutex_lock(&new_heap->lock_on_deque);
return new_heap;
}
void close_queue(Heap *heap) {
free(heap->queue);
free(heap);
}
int enqueue(Heap *heap, int priority, void *data) {
if (heap->size == heap->MAX_SIZE) {
pthread_mutex_lock(&heap->lock_on_enqueue);
}
pthread_mutex_lock(&heap->lock_on_data);
int index = heap->size++;
Priority_node *queue = heap->queue;
queue[index].priority = priority;
queue[index].data = data;
while (index > 0) {
int parent = getParent(index);
if (queue[index].priority > queue[parent].priority) {
Priority_node temp_node = queue[index];
queue[index] = queue[parent];
queue[parent] = temp_node;
index = parent;
} else {
pthread_mutex_unlock(&heap->lock_on_deque);
pthread_mutex_unlock(&heap->lock_on_data);
return 0;
}
}
pthread_mutex_unlock(&heap->lock_on_deque);
pthread_mutex_unlock(&heap->lock_on_data);
return 0;
}
int delete_max(Heap *heap) {
// if(heap->size == 0){
// pthread_mutex_lock(&heap->lock_on_deque);
// }
// if(heap->size == 1){
// heap->size = 0;
// pthread_mutex_unlock(&heap->lock_on_data);
// return 0;
// }
Priority_node *queue = heap->queue;
int index = 0, size = heap->size;
queue[index] = queue[--size];
while (index < size) {
int right_child = getRightChild(index);
int left_child = getLeftChild(index);
if (right_child > size) {
right_child = -1;
}
if (left_child > size) {
left_child = -1;
}
int max_child;
if ((right_child != -1) && (left_child != -1)) {
if (queue[right_child].priority > queue[left_child].priority) {
max_child = right_child;
} else {
max_child = left_child;
}
} else {
if (right_child == left_child) {
heap->size = size;
// pthread_mutex_unlock(&heap->lock_on_enqueue);
// pthread_mutex_unlock(&heap->lock_on_data);
return 0;
} else {
if (right_child == -1) {
max_child = left_child;
} else {
max_child = right_child;
}
}
}
if (queue[index].priority < queue[max_child].priority) {
// print_heap(heap);
// //// printf("\nindex is %d, max_child is %d, size is %d, left_child is %d, right_child is %d\n", index, max_child, heap->size, left_child, right_child );
Priority_node temp_node = queue[index];
queue[index] = queue[max_child];
queue[max_child] = temp_node;
index = max_child;
} else {
heap->size = size;
// pthread_mutex_unlock(&heap->lock_on_enqueue);
// pthread_mutex_unlock(&heap->lock_on_data);
return 0;
}
}
// pthread_mutex_unlock(&heap->lock_on_enqueue);
// pthread_mutex_unlock(&heap->lock_on_data);
heap->size = size;
return 0;
}
void *getMax(Heap *heap) {
assert(heap->size != 0);
void *result = heap->queue[0].data;
return result;
}
void *deque(Heap *heap) {
if (heap->size == 0) {
//// printf("Blocked deque\n");
pthread_mutex_lock(&heap->lock_on_deque);
}
pthread_mutex_lock(&heap->lock_on_data);
void *result = getMax(heap);
delete_max(heap);
pthread_mutex_unlock(&heap->lock_on_enqueue);
pthread_mutex_unlock(&heap->lock_on_data);
return result;
}
void print_heap(Heap *heap) {
int i;
pthread_mutex_lock(&heap->lock_on_data);
//// printf("\nIT IS A HEAP\n");
for (i = 0; i < heap->size; i++) {
//// printf("%6d", heap->queue[i].priority);
}
//// printf("\n");
for (i = 0; i < heap->size; i++) {
//// printf("%6d", heap->queue[i].data);
}
pthread_mutex_unlock(&heap->lock_on_data);
}