-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpqueue.c
211 lines (157 loc) · 5.46 KB
/
pqueue.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
The MIT License (MIT)
Copyright (c) 2015 Mike Taghavi (mitghi) <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <assert.h>
#include "pqueue.h"
#define GAP 2
#define MPANIC(x) ; assert(x != NULL)
static void insert_node(Priqueue *heap, Node* node);
static Node* pop_node(Priqueue *heap);
static void swap_node(Priqueue *heap, unsigned int a, unsigned int b);
MHEAP_API Priqueue* priqueue_initialize(int initial_length){
unsigned int mutex_status;
Priqueue *heap = (Priqueue *) malloc(sizeof(Priqueue)) MPANIC(heap);
const size_t hsize = initial_length * sizeof(*heap->array);
mutex_status = pthread_mutex_init(&(heap->lock), NULL);
if (mutex_status != 0) goto error;
heap->head = NULL;
heap->heap_size = initial_length;
heap->occupied = 0;
heap->current = 1;
heap->array = malloc(hsize) MPANIC(heap->array);
memset(heap->array, 0x00, hsize);
return heap;
error:
free(heap);
return NULL;
}
static MHEAP_API MHEAPSTATUS realloc_heap(Priqueue *heap){
if (heap->occupied >= heap->heap_size){
const size_t arrsize = sizeof(*heap->array);
void **resized_heap;
resized_heap = realloc(heap->array, (2 * heap->heap_size) * arrsize);
if (resized_heap != NULL){
heap->heap_size *= 2;
heap->array = (Node**) resized_heap;
memset( (heap->array + heap->occupied + 1) , 0x00, (heap->heap_size / GAP) * arrsize );
return MHEAP_OK;
} else return MHEAP_REALLOCERROR;
}
return MHEAP_NOREALLOC;
}
MHEAP_API void priqueue_insert(Priqueue *heap, Data *data, int priority){
Node *node = (Node *) malloc(sizeof(Node)) MPANIC(node);
node->priority = priority;
node->data = data;
pthread_mutex_lock(&(heap->lock));
insert_node(heap,node);
pthread_mutex_unlock(&(heap->lock));
}
static void insert_node(Priqueue *heap, Node* node){
if (heap->current == 1 && heap->array[1] == NULL){
heap->head = node;
heap->array[1] = node;
heap->array[1]->index = heap->current;
heap->occupied++;
heap->current++;
return;
}
if(heap->occupied >= heap->heap_size) {
unsigned int realloc_status = realloc_heap(heap);
assert(realloc_status == MHEAP_OK);
}
if(heap->occupied <= heap->heap_size){
node->index = heap->current;
heap->array[heap->current] = node;
int parent = (heap->current / GAP);
if (heap->array[parent]->priority < node->priority){
heap->occupied++;
heap->current++;
int depth = heap->current / GAP;
int traverse = node->index;
while(depth >= 1){
if (traverse == 1) break;
unsigned int parent = (traverse / GAP);
if(heap->array[parent]->priority < heap->array[traverse]->priority){
swap_node(heap, parent , traverse);
traverse = heap->array[parent]->index;
}
depth --;
}
heap->head = heap->array[1];
} else {
heap->occupied++;
heap->current++;
}
}
}
void swap_node(Priqueue *heap, unsigned int parent, unsigned int child){
Node *tmp = heap->array[parent];
heap->array[parent] = heap->array[child];
heap->array[parent]->index = tmp->index;
heap->array[child] = tmp;
heap->array[child]->index = child;
}
MHEAP_API Node *priqueue_pop(Priqueue *heap){
Node *node = NULL;
pthread_mutex_lock(&(heap->lock));
node = pop_node(heap);
pthread_mutex_unlock(&(heap->lock));
return node;
}
static Node *pop_node(Priqueue *heap){
Node *node = NULL;
unsigned int i;
unsigned int depth;
if (heap->current == 1) return node;
else if (heap->current >= 2 ){
node = heap->array[1];
heap->array[1] = heap->array[heap->current - 1];
heap->current -= 1;
heap->occupied -= 1;
depth = (heap->current -1) / 2;
for(i = 1; i<=depth; i++){
if (heap->array[i]->priority < heap->array[i * GAP]->priority ||
heap->array[i]->priority < heap->array[(i * GAP)+1]->priority){
unsigned int biggest = heap->array[i * GAP]->priority > heap->array[(i * GAP)+1]->priority ?
heap->array[(i * GAP)]->index :
heap->array[(i * GAP)+1]->index;
swap_node(heap,i,biggest);
}
}
}
return node;
}
MHEAP_API void priqueue_free(Priqueue *heap){
if (heap->current >= 2 ) {
unsigned int i;
for (i = 1; i <= heap->current; i++) priqueue_node_free(heap,heap->array[i]);
}
free(heap->head);
free(*heap->array);
free(heap->array);
free(heap);
}
MHEAP_API void priqueue_node_free(Priqueue *heap,Node *node){
if (node != NULL) free(node->data->data);
free(node);
}