This repository was archived by the owner on Feb 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathpqueue.c
177 lines (141 loc) · 3.79 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
/*
* pqueue.c
*
* Skeltrack - A Free Software skeleton tracking library
* Copyright (C) 2012 Igalia S.L.
*
* Based on the implementation at:
* http://algs4.cs.princeton.edu/44sp/IndexMinPQ.java.html
*
* Authors:
* Iago López Galeiras <iaguis@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License at http://www.gnu.org/licenses/lgpl-3.0.txt
* for more details.
*/
#include "pqueue.h"
PQueue *
pqueue_new (guint max_size, guint width, guint height)
{
guint i;
PQueue *queue = g_slice_new (PQueue);
queue->elements = g_slice_alloc0 ((max_size + 1) * sizeof (PQelement));
queue->map = g_slice_alloc (width * height * sizeof(guint));
for (i=0; i<width*height; i++)
queue->map[i] = -1;
queue->size = 0;
queue->max_size = max_size;
queue->width = width;
queue->height = height;
return queue;
}
static void
swap (PQueue *queue, guint a, guint b)
{
guint index_a, index_b;
index_a = queue->elements[a].data->j * queue->width +
queue->elements[a].data->i;
index_b = queue->elements[b].data->j * queue->width +
queue->elements[b].data->i;
guint temp = queue->map[index_a];
queue->map[index_a] = queue->map[index_b];
queue->map[index_b] = temp;
PQelement element_temp = queue->elements[b];
queue->elements[b] = queue->elements[a];
queue->elements[a] = element_temp;
}
static gboolean
greater (PQueue *queue, guint a, guint b)
{
return queue->elements[a].priority > queue->elements[b].priority;
}
static void
swim (PQueue *queue, guint index)
{
while (index > 1 && greater (queue, index/2, index))
{
swap (queue, index, index/2);
index = index/2;
}
}
static void
sink (PQueue *queue, guint index)
{
guint j, size;
size = queue->size;
while (2 * index <= size)
{
j = 2 * index;
if ((j < size) && (greater(queue, j, j+1)))
j++;
if (!greater (queue, index, j))
break;
swap (queue, index, j);
index = j;
}
}
void
pqueue_insert (PQueue *pqueue,
Node *data,
guint priority)
{
guint index;
pqueue->elements[++(pqueue->size)].data = data;
pqueue->elements[pqueue->size].priority = priority;
index = data->j * pqueue->width + data->i;
pqueue->map[index] = pqueue->size;
swim (pqueue, pqueue->size);
}
Node *
pqueue_pop_minimum (PQueue *pqueue)
{
if (pqueue_is_empty (pqueue))
return NULL;
Node *minimum = pqueue->elements[1].data;
guint index;
swap (pqueue, 1, pqueue->size);
pqueue->size--;
index = minimum->j * pqueue->width + minimum->i;
pqueue->map[index] = -1;
sink (pqueue, 1);
return minimum;
}
void
pqueue_delete (PQueue *pqueue,
Node *data)
{
guint index, pos;
index = data->j * pqueue->width + data->i;
pos = pqueue->map[index];
Node *element = pqueue->elements[pos].data;
swap (pqueue, pos, pqueue->size);
pqueue->size--;
pqueue->map[element->j * pqueue->width + element->i] = -1;
sink (pqueue, pos);
}
gboolean
pqueue_has_element (PQueue *pqueue,
Node *data)
{
return pqueue->map[data->j * pqueue->width + data->i] != -1;
}
gboolean
pqueue_is_empty (PQueue *pqueue)
{
return pqueue->size == 0;
}
void
pqueue_free (PQueue *pqueue)
{
g_slice_free1 ((pqueue->max_size + 1) * sizeof (PQelement), pqueue->elements);
g_slice_free1 (pqueue->width * pqueue->height * sizeof (guint), pqueue->map);
g_slice_free (PQueue, pqueue);
}