-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbenchheap_insert.c
More file actions
232 lines (191 loc) · 5.76 KB
/
benchheap_insert.c
File metadata and controls
232 lines (191 loc) · 5.76 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <assert.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <limits.h>
#include <time.h>
#include <sys/time.h>
#include <sys/resource.h>
#define TRUE 1
#define FALSE 0
#define SAMPLES 20
// clock_t start, end;
// double cpu_time_used;
typedef int DATTYPE; //ptr stands for pointer
struct pq_element
{
DATTYPE data; // this can be any data type
double priority;
};
typedef struct pq_element pq_element;
struct pq
{
pq_element *pq_array; //array of pq_element-s
int n; // number of elements in PQ
int alloc; // number of elements memory was allocated for
};
typedef struct pq pq;
typedef struct pq * pq_ptr;
pq_ptr pq_new(int size)
{
pq_ptr new_pq;
new_pq = (pq_ptr *)malloc(sizeof(pq));
new_pq->pq_array = (pq_element *)malloc(sizeof(pq_element) * size);
new_pq->alloc = size;
new_pq->n = 0;
return new_pq;
}
void pq_push(pq_ptr q, DATTYPE data, double priority)
{
pq_element * pq_element_ptr;
int s, f; // indices to traverse tree (s son, f father)
// check if we need to reallocate memory
int myq=q->n;
int myalloc=q->alloc;
if (myq >= myalloc)
{
q->alloc *= 2;
q->pq_array = (pq_element *)realloc(q->pq_array, sizeof(pq_element) * q->alloc);
}
q->n++; // one more element in PQ
s = q->n - 1; // new element is placed at bottom of tree/array
f = (s-1)/2; //pq_array[f] is father of pq_array[s]
while ((s > 0) && (priority < q->pq_array[f].priority))
{
q->pq_array[s] = q->pq_array[f]; // shift father down
s = f; // son takes place of father
f = (s-1)/2; // father at this new position of son
}
//place element here
//printf("Inserting data %d and priority %d in heap at index %d\n", data, priority, s);
q->pq_array[s].data = data;
q->pq_array[s].priority = priority;
}
/* adjust tree that lost its root */
void pq_adjusttree(pq_ptr q)
{
int p; // index to parent
int s1; // index for son 1
int s2; // index for son 2
int i;
p = 0;
s1 = 1;
s2 = 2;
// readjusting tree since root (pq_array[0]) will be deleted
do
{
// if only one son, or son 1 lower priority
if ((s2 > q->n-1) )
{
q->pq_array[p]=q->pq_array[s1]; // son 1 takes place of parent
p = s1;
}
else if((q->pq_array[s1].priority < q->pq_array[s2].priority)){
q->pq_array[p]=q->pq_array[s1]; // son 1 takes place of parent
p = s1;
}
else
{
q->pq_array[p]=q->pq_array[s2]; // son 2 takes place of parent
p = s2;
}
s1 = p+p + 1;
s2 = p+p + 2;
} while (s1 <= q->n-1); // parent doesn't have any children. we are done
//printf("priority last parent: %d\n", p);
// rellocate nodes in array after last parent that was moved up
int size = q->n;
int current = p+1;
for(current; current !=size; current++)
{
q->n = current - 1;
pq_push(q, q->pq_array[current].data, q->pq_array[current].priority);
}
q->n = size-1; // one fewer element in heap
if (q->n < q->alloc/2 && q->n >= 4)
{
q->pq_array = realloc(q->pq_array, sizeof(pq_element) * (q->alloc/2));
q->alloc = q->alloc/2;
}
}
void pq_display(pq_ptr pq)
{
int i;
printf("Size of priority queue: %d\n", pq->n);
printf("Space allocated for queue: %d\n", pq->alloc);
printf("Data in priority queue:\n");
for (i = 0; i < pq->n; i++)
printf("%d ", pq->pq_array[i].data);
printf("\n");
printf("Priority of the data:\n");
for (i = 0; i < pq->n; i++)
printf("%lf ", pq->pq_array[i].priority);
printf("\n");
}
DATTYPE pq_pop(pq_ptr q)
{
// if (q->n == 0)
// {
// printf("Underflow error: trying to pop empty queue\n");
// return 0;
// }
DATTYPE out;
out = q->pq_array[0].data;
if (q->n == 1)
q->n = 0;
else
pq_adjusttree(q);
return out;
}
int main(int argc, char *argv[]) {
int numOperations = atoi(argv[1]);
int typeOfOperation = atoi(argv[2]);
int seed = atoi(argv[3]);
double *timestamps;
timestamps = (double *) malloc(sizeof(double) * numOperations);
srand(seed); // Assign the seed to the function rand()
for(int i = 0; i < numOperations; i++) {
timestamps[i] = (rand() % 1000000) / 1000000.;
}
int i;
int data;
pq_ptr my_pq;
my_pq = pq_new(numOperations);
// double priority[] = {54.5, 234, 34, 354, 76, 12, 79, 23, 8, 28};
// start=clock();
struct timespec t_start, t_stop; // Used for bench timings
// srand(time(NULL)); // Used for random (actual random)
long wall_sec;
long wall_nsec;
long wall_msec;
int sample = numOperations/SAMPLES;
for(int k = sample; k <= numOperations; k += sample) {
clock_gettime(CLOCK_MONOTONIC_COARSE, &t_start);
for(int i = 0; i < k; i++) {
// args are; pq, data, priority
pq_push(my_pq, i, timestamps[i]);
// pq_display(my_pq);
// printf("\n");
}
clock_gettime(CLOCK_MONOTONIC_COARSE, &t_stop);
wall_sec = t_stop.tv_sec - t_start.tv_sec;
wall_nsec = t_stop.tv_nsec - t_start.tv_nsec;
wall_msec = (wall_sec *1000) + (wall_nsec / 1000000);
// printf("%d %ld\n", k, wall_msec);
printf("%ld\n", wall_msec);
}
// cpu_time_used=((double) (end-start))/CLOCKS_PER_SEC;
// printf("cpu_time_used: %lf\n", cpu_time_used);
// for (i = 0; i < 10; i++)
// {
// printf("Popping element out of priority queue: ");
// data = pq_pop(my_pq);
// //printf("%d\n");
// pq_display(my_pq);
// printf("\n");
// }
return 0;
}