Routino SVN Repository Browser

Check out the latest version of Routino: svn co http://routino.org/svn/trunk routino

ViewVC logotype

Contents of /trunk/src/queue.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 868 - (show annotations) (download) (as text)
Sat Oct 15 09:44:08 2011 UTC (13 years, 5 months ago) by amb
File MIME type: text/x-csrc
File size: 4715 byte(s)
Refactor the binary heap to reduce the number of comparisons.

1 /***************************************
2 Queue data type functions.
3
4 Part of the Routino routing software.
5 ******************/ /******************
6 This file Copyright 2008-2011 Andrew M. Bishop
7
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU Affero General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Affero General Public License for more details.
17
18 You should have received a copy of the GNU Affero General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ***************************************/
21
22
23 #include <sys/types.h>
24 #include <string.h>
25 #include <stdlib.h>
26
27 #include "results.h"
28
29
30 /*+ A queue of results. +*/
31 struct _Queue
32 {
33 uint32_t nallocated; /*+ The number of entries allocated. +*/
34 uint32_t noccupied; /*+ The number of entries occupied. +*/
35
36 Result **data; /*+ The queue of pointers to results. +*/
37 };
38
39
40 /*++++++++++++++++++++++++++++++++++++++
41 Allocate a new queue.
42
43 Queue *NewQueueList Returns the queue.
44 ++++++++++++++++++++++++++++++++++++++*/
45
46 Queue *NewQueueList(void)
47 {
48 Queue *queue;
49
50 queue=(Queue*)malloc(sizeof(Queue));
51
52 queue->nallocated=1024;
53 queue->noccupied=0;
54
55 queue->data=(Result**)malloc(queue->nallocated*sizeof(Result*));
56
57 return(queue);
58 }
59
60
61 /*++++++++++++++++++++++++++++++++++++++
62 Free a queue.
63
64 Queue *queue The queue to be freed.
65 ++++++++++++++++++++++++++++++++++++++*/
66
67 void FreeQueueList(Queue *queue)
68 {
69 free(queue->data);
70
71 free(queue);
72 }
73
74
75 /*++++++++++++++++++++++++++++++++++++++
76 Insert a new item into the queue in the right place.
77
78 The data is stored in a "Binary Heap" http://en.wikipedia.org/wiki/Binary_heap
79 and this operation is adding an item to the heap.
80
81 Queue *queue The queue to insert the result into.
82
83 Result *result The result to insert into the queue.
84 ++++++++++++++++++++++++++++++++++++++*/
85
86 void InsertInQueue(Queue *queue,Result *result)
87 {
88 uint32_t index;
89
90 if(result->queued==NOT_QUEUED)
91 {
92 queue->noccupied++;
93 index=queue->noccupied;
94
95 if(queue->noccupied==queue->nallocated)
96 {
97 queue->nallocated=2*queue->nallocated;
98 queue->data=(Result**)realloc((void*)queue->data,queue->nallocated*sizeof(Result*));
99 }
100
101 queue->data[index]=result;
102 queue->data[index]->queued=index;
103 }
104 else
105 {
106 index=result->queued;
107 }
108
109 /* Bubble up the new value */
110
111 while(index>1)
112 {
113 uint32_t newindex;
114 Result *temp;
115
116 newindex=index/2;
117
118 if(queue->data[index]->sortby>=queue->data[newindex]->sortby)
119 break;
120
121 temp=queue->data[index];
122 queue->data[index]=queue->data[newindex];
123 queue->data[newindex]=temp;
124
125 queue->data[index]->queued=index;
126 queue->data[newindex]->queued=newindex;
127
128 index=newindex;
129 }
130 }
131
132
133 /*++++++++++++++++++++++++++++++++++++++
134 Pop an item from the front of the queue.
135
136 The data is stored in a "Binary Heap" http://en.wikipedia.org/wiki/Binary_heap
137 and this operation is deleting the root item from the heap.
138
139 Result *PopFromQueue Returns the top item.
140
141 Queue *queue The queue to remove the result from.
142 ++++++++++++++++++++++++++++++++++++++*/
143
144 Result *PopFromQueue(Queue *queue)
145 {
146 uint32_t index;
147 Result *retval;
148
149 if(queue->noccupied==0)
150 return(NULL);
151
152 retval=queue->data[1];
153 retval->queued=NOT_QUEUED;
154
155 index=1;
156
157 queue->data[index]=queue->data[queue->noccupied];
158 queue->noccupied--;
159
160 /* Bubble down the newly promoted value */
161
162 while((2*index)<queue->noccupied)
163 {
164 uint32_t newindex;
165 Result *temp;
166
167 newindex=2*index;
168
169 if(queue->data[newindex]->sortby>queue->data[newindex+1]->sortby)
170 newindex=newindex+1;
171
172 if(queue->data[index]->sortby<=queue->data[newindex]->sortby)
173 break;
174
175 temp=queue->data[newindex];
176 queue->data[newindex]=queue->data[index];
177 queue->data[index]=temp;
178
179 queue->data[index]->queued=index;
180 queue->data[newindex]->queued=newindex;
181
182 index=newindex;
183 }
184
185 if((2*index)==queue->noccupied)
186 {
187 uint32_t newindex;
188 Result *temp;
189
190 newindex=2*index;
191
192 if(queue->data[index]->sortby<=queue->data[newindex]->sortby)
193 ; /* break */
194 else
195 {
196 temp=queue->data[newindex];
197 queue->data[newindex]=queue->data[index];
198 queue->data[index]=temp;
199
200 queue->data[index]->queued=index;
201 queue->data[newindex]->queued=newindex;
202 }
203 }
204
205 return(retval);
206 }

Properties

Name Value
cvs:description Functions for handling the queue of partial routes.