-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest_heap.c
50 lines (45 loc) · 1.25 KB
/
request_heap.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
/*
* Implements Min Heap for keeping http requests during SJF
*/
#include <stdio.h>
#include <stdlib.h>
#include "request.h"
#include "request_heap.h"
int heapsize = 0;
struct heap_node *heap = NULL;
int insert_into_heap(struct http_request *req){
if(heapsize%BUF_LEN == 0)
heap = (struct heap_node *)realloc(heap, sizeof(struct heap_node)*(heapsize+BUF_LEN));
int i = heapsize;
heapsize = heapsize + 1;
while(i>0 && heap[PARENT(i)].req->priority > req->priority){
heap[i].req = heap[PARENT(i)].req;
i = PARENT(i);
}
heap[i].req = req;
return 0;
}
int min_heapify(int index){
int smallest;
struct http_request *temp;
if(LEFT_CHILD(index) < heapsize && heap[LEFT_CHILD(index)].req->priority < heap[index].req->priority)
smallest = LEFT_CHILD(index);
else
smallest = index;
if(RIGHT_CHILD(index) < heapsize && heap[RIGHT_CHILD(index)].req->priority < heap[smallest].req->priority)
smallest = RIGHT_CHILD(index);
if(smallest != index){
temp = heap[index].req;
heap[index].req = heap[smallest].req;
heap[smallest].req = temp;
min_heapify(smallest);
}
return 0;
}
struct http_request * extract_shortest(){
struct http_request *shortest = heap[0].req;
heapsize = heapsize - 1;
heap[0]=heap[heapsize];
min_heapify(0);
return shortest;
}