Check out the latest version of Routino: svn co http://routino.org/svn/trunk routino
Contents of /trunk/src/queue.c
Parent Directory
|
Revision Log
Revision 1290 -
(show annotations)
(download)
(as text)
Wed May 1 18:18:38 2013 UTC (11 years, 11 months ago) by amb
File MIME type: text/x-csrc
File size: 5109 byte(s)
Wed May 1 18:18:38 2013 UTC (11 years, 11 months ago) by amb
File MIME type: text/x-csrc
File size: 5109 byte(s)
Move the queue score back into the results structure since it is quicker but uses slightly more memory.
1 | /*************************************** |
2 | Queue data type functions. |
3 | |
4 | Part of the Routino routing software. |
5 | ******************/ /****************** |
6 | This file Copyright 2008-2013 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 <string.h> |
24 | #include <stdlib.h> |
25 | |
26 | #include "results.h" |
27 | |
28 | |
29 | /*+ A queue of results. +*/ |
30 | struct _Queue |
31 | { |
32 | int nincrement; /*+ The amount to increment the queue when full. +*/ |
33 | int nallocated; /*+ The number of entries allocated. +*/ |
34 | int noccupied; /*+ The number of entries occupied. +*/ |
35 | |
36 | Result **results; /*+ The queue of pointers to results. +*/ |
37 | }; |
38 | |
39 | |
40 | /*++++++++++++++++++++++++++++++++++++++ |
41 | Allocate a new queue. |
42 | |
43 | Queue *NewQueueList Returns the queue. |
44 | |
45 | uint8_t log2bins The base 2 logarithm of the initial number of bins in the queue. |
46 | ++++++++++++++++++++++++++++++++++++++*/ |
47 | |
48 | Queue *NewQueueList(uint8_t log2bins) |
49 | { |
50 | Queue *queue; |
51 | |
52 | queue=(Queue*)malloc(sizeof(Queue)); |
53 | |
54 | queue->nincrement=1<<log2bins; |
55 | |
56 | queue->nallocated=queue->nincrement; |
57 | queue->noccupied=0; |
58 | |
59 | queue->results=(Result**)malloc(queue->nallocated*sizeof(Result*)); |
60 | |
61 | return(queue); |
62 | } |
63 | |
64 | |
65 | /*++++++++++++++++++++++++++++++++++++++ |
66 | Free a queue. |
67 | |
68 | Queue *queue The queue to be freed. |
69 | ++++++++++++++++++++++++++++++++++++++*/ |
70 | |
71 | void FreeQueueList(Queue *queue) |
72 | { |
73 | free(queue->results); |
74 | |
75 | free(queue); |
76 | } |
77 | |
78 | |
79 | /*++++++++++++++++++++++++++++++++++++++ |
80 | Insert a new item into the queue in the right place. |
81 | |
82 | The data is stored in a "Binary Heap" http://en.wikipedia.org/wiki/Binary_heap |
83 | and this operation is adding an item to the heap. |
84 | |
85 | Queue *queue The queue to insert the result into. |
86 | |
87 | Result *result The result to insert into the queue. |
88 | |
89 | score_t score The score to use for sorting the node. |
90 | ++++++++++++++++++++++++++++++++++++++*/ |
91 | |
92 | void InsertInQueue(Queue *queue,Result *result,score_t score) |
93 | { |
94 | int index; |
95 | |
96 | if(result->queued==NOT_QUEUED) |
97 | { |
98 | queue->noccupied++; |
99 | index=queue->noccupied; |
100 | |
101 | if(queue->noccupied==queue->nallocated) |
102 | { |
103 | queue->nallocated=queue->nallocated+queue->nincrement; |
104 | queue->results=(Result**)realloc((void*)queue->results,queue->nallocated*sizeof(Result*)); |
105 | } |
106 | |
107 | queue->results[index]=result; |
108 | queue->results[index]->queued=index; |
109 | } |
110 | else |
111 | index=result->queued; |
112 | |
113 | queue->results[index]->sortby=score; |
114 | |
115 | /* Bubble up the new value */ |
116 | |
117 | while(index>1 && |
118 | queue->results[index]->sortby<queue->results[index/2]->sortby) |
119 | { |
120 | int newindex; |
121 | Result *temp; |
122 | |
123 | newindex=index/2; |
124 | |
125 | temp=queue->results[index]; |
126 | queue->results[index]=queue->results[newindex]; |
127 | queue->results[newindex]=temp; |
128 | |
129 | queue->results[index]->queued=index; |
130 | queue->results[newindex]->queued=newindex; |
131 | |
132 | index=newindex; |
133 | } |
134 | } |
135 | |
136 | |
137 | /*++++++++++++++++++++++++++++++++++++++ |
138 | Pop an item from the front of the queue. |
139 | |
140 | The data is stored in a "Binary Heap" http://en.wikipedia.org/wiki/Binary_heap |
141 | and this operation is deleting the root item from the heap. |
142 | |
143 | Result *PopFromQueue Returns the top item. |
144 | |
145 | Queue *queue The queue to remove the result from. |
146 | ++++++++++++++++++++++++++++++++++++++*/ |
147 | |
148 | Result *PopFromQueue(Queue *queue) |
149 | { |
150 | int index; |
151 | Result *retval; |
152 | |
153 | if(queue->noccupied==0) |
154 | return(NULL); |
155 | |
156 | retval=queue->results[1]; |
157 | retval->queued=NOT_QUEUED; |
158 | |
159 | index=1; |
160 | |
161 | queue->results[index]=queue->results[queue->noccupied]; |
162 | |
163 | queue->noccupied--; |
164 | |
165 | /* Bubble down the newly promoted value */ |
166 | |
167 | while((2*index)<queue->noccupied && |
168 | (queue->results[index]->sortby>queue->results[2*index ]->sortby || |
169 | queue->results[index]->sortby>queue->results[2*index+1]->sortby)) |
170 | { |
171 | int newindex; |
172 | Result *temp; |
173 | |
174 | if(queue->results[2*index]->sortby<queue->results[2*index+1]->sortby) |
175 | newindex=2*index; |
176 | else |
177 | newindex=2*index+1; |
178 | |
179 | temp=queue->results[newindex]; |
180 | queue->results[newindex]=queue->results[index]; |
181 | queue->results[index]=temp; |
182 | |
183 | queue->results[index]->queued=index; |
184 | queue->results[newindex]->queued=newindex; |
185 | |
186 | index=newindex; |
187 | } |
188 | |
189 | if((2*index)==queue->noccupied && |
190 | queue->results[index]->sortby>queue->results[2*index]->sortby) |
191 | { |
192 | int newindex; |
193 | Result *temp; |
194 | |
195 | newindex=2*index; |
196 | |
197 | temp=queue->results[newindex]; |
198 | queue->results[newindex]=queue->results[index]; |
199 | queue->results[index]=temp; |
200 | |
201 | queue->results[index]->queued=index; |
202 | queue->results[newindex]->queued=newindex; |
203 | } |
204 | |
205 | return(retval); |
206 | } |
Properties
Name | Value |
---|---|
cvs:description | Functions for handling the queue of partial routes. |