-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpqueue.c
75 lines (62 loc) · 1.63 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
/*
* Priority queue.
*
* Bob Baldwin, February, 1985.
*/
#include <stdio.h>
#include "pqueue.h"
/* Initialize a pqueue header with the given parameters.
*/
void pque_init(pque_hdr, max_score, pque_tab, pque_size)
pqueue_hdr *pque_hdr;
float max_score;
pqueue_ent *pque_tab;
int pque_size;
{
pque_hdr->next_index = 0;
pque_hdr->pque_size = pque_size;
pque_hdr->max_score = max_score;
pque_hdr->pque_tab = pque_tab;
}
/* Return TRUE if the pqueue cannot hold another entry.
*/
int pque_full(pque_hdr)
pqueue_hdr *pque_hdr;
{
return (pque_hdr->next_index >= pque_hdr->pque_size);
}
/* Add an entry to the priority queue. Sorted lowest score first.
* The queue header indicates the next free slot, the maximum
* score (all scores in queue < max), and the size of the table.
* If the pqueue is full the lowest scoring entry will be
* thrown out.
*
* Implementation: Find the first slot before sizelast+1 that
* has a size less than the size arg. Shuffle down the list
* to create a hole and insert the new entry.
*/
void pque_add(pque_hdr, score, value1, value2)
pqueue_hdr *pque_hdr;
float score;
int value1;
int value2;
{
int k; /* Slot where new entry will go. */
int i;
pqueue_ent *pque;
pqueue_ent new_ent;
if (score >= pque_hdr->max_score) return;
new_ent.score = score;
new_ent.value1 = value1;
new_ent.value2 = value2;
pque = pque_hdr->pque_tab;
for (k = 0 ; k < pque_hdr->next_index ; k++) {
if (pque[k].score > score) break;
}
for (i = pque_hdr->next_index ; i > k ; i--) {
pque[i] = pque[i-1];
}
if (pque_hdr->next_index < pque_hdr->pque_size)
pque_hdr->next_index++;
pque[k] = new_ent;
}