Check out the latest version of Routino: svn co http://routino.org/svn/trunk routino
Contents of /trunk/src/results.c
Parent Directory
|
Revision Log
Revision 605 -
(show annotations)
(download)
(as text)
Mon Jan 24 19:32:49 2011 UTC (14 years, 1 month ago) by amb
File MIME type: text/x-csrc
File size: 6761 byte(s)
Mon Jan 24 19:32:49 2011 UTC (14 years, 1 month ago) by amb
File MIME type: text/x-csrc
File size: 6761 byte(s)
Finds routes and obeys turn restrictions (only tested with very simple route and restrictions, more turn restriction testing and regression testing required).
1 | /*************************************** |
2 | Result 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 | /*+ The size of the increment for the Results data structure. +*/ |
30 | #define RESULTS_INCREMENT 64 |
31 | |
32 | |
33 | /*++++++++++++++++++++++++++++++++++++++ |
34 | Allocate a new results list. |
35 | |
36 | Results *NewResultsList Returns the results list. |
37 | |
38 | int nbins The number of bins in the results array. |
39 | ++++++++++++++++++++++++++++++++++++++*/ |
40 | |
41 | Results *NewResultsList(int nbins) |
42 | { |
43 | Results *results; |
44 | uint32_t i; |
45 | |
46 | results=(Results*)malloc(sizeof(Results)); |
47 | |
48 | results->nbins=1; |
49 | results->mask=~0; |
50 | |
51 | while(nbins>>=1) |
52 | { |
53 | results->mask<<=1; |
54 | results->nbins<<=1; |
55 | } |
56 | |
57 | results->mask=~results->mask; |
58 | |
59 | results->alloced=RESULTS_INCREMENT; |
60 | results->number=0; |
61 | |
62 | results->count=(uint32_t*)malloc(results->nbins*sizeof(uint32_t)); |
63 | results->point=(Result***)malloc(results->nbins*sizeof(Result**)); |
64 | |
65 | for(i=0;i<results->nbins;i++) |
66 | { |
67 | results->count[i]=0; |
68 | |
69 | results->point[i]=(Result**)malloc(results->alloced*sizeof(Result*)); |
70 | } |
71 | |
72 | results->data=(Result**)malloc(1*sizeof(Result*)); |
73 | results->data[0]=(Result*)malloc(results->nbins*RESULTS_INCREMENT*sizeof(Result)); |
74 | |
75 | results->start_node=NO_NODE; |
76 | results->prev_segment=NO_SEGMENT; |
77 | |
78 | results->finish_node=NO_NODE; |
79 | results->last_segment=NO_SEGMENT; |
80 | |
81 | return(results); |
82 | } |
83 | |
84 | |
85 | /*++++++++++++++++++++++++++++++++++++++ |
86 | Allocate a new results list. |
87 | |
88 | Results *results The results list to be destroyed. |
89 | ++++++++++++++++++++++++++++++++++++++*/ |
90 | |
91 | void FreeResultsList(Results *results) |
92 | { |
93 | int i,c=(results->number-1)/(results->nbins*RESULTS_INCREMENT); |
94 | |
95 | for(i=c;i>=0;i--) |
96 | free(results->data[i]); |
97 | |
98 | free(results->data); |
99 | |
100 | for(i=0;i<results->nbins;i++) |
101 | free(results->point[i]); |
102 | |
103 | free(results->point); |
104 | |
105 | free(results->count); |
106 | |
107 | free(results); |
108 | } |
109 | |
110 | |
111 | /*++++++++++++++++++++++++++++++++++++++ |
112 | Insert a new result into the results data structure in the right order. |
113 | |
114 | Result *InsertResult Returns the result that has been inserted. |
115 | |
116 | Results *results The results structure to insert into. |
117 | |
118 | index_t node The node that is to be inserted into the results. |
119 | |
120 | index_t segment The segment that is to be inserted into the results. |
121 | ++++++++++++++++++++++++++++++++++++++*/ |
122 | |
123 | Result *InsertResult(Results *results,index_t node,index_t segment) |
124 | { |
125 | Result *result; |
126 | int bin=node&results->mask; |
127 | uint32_t i; |
128 | |
129 | /* Check that the arrays have enough space. */ |
130 | |
131 | if(results->count[bin]==results->alloced) |
132 | { |
133 | results->alloced+=RESULTS_INCREMENT; |
134 | |
135 | for(i=0;i<results->nbins;i++) |
136 | results->point[i]=(Result**)realloc((void*)results->point[i],results->alloced*sizeof(Result*)); |
137 | } |
138 | |
139 | if(results->number && (results->number%RESULTS_INCREMENT)==0 && (results->number%(RESULTS_INCREMENT*results->nbins))==0) |
140 | { |
141 | int c=results->number/(results->nbins*RESULTS_INCREMENT); |
142 | |
143 | results->data=(Result**)realloc((void*)results->data,(c+1)*sizeof(Result*)); |
144 | results->data[c]=(Result*)malloc(results->nbins*RESULTS_INCREMENT*sizeof(Result)); |
145 | } |
146 | |
147 | /* Insert the new entry */ |
148 | |
149 | results->point[bin][results->count[bin]]=&results->data[results->number/(results->nbins*RESULTS_INCREMENT)][results->number%(results->nbins*RESULTS_INCREMENT)]; |
150 | |
151 | results->number++; |
152 | |
153 | results->count[bin]++; |
154 | |
155 | /* Initialise the result */ |
156 | |
157 | result=results->point[bin][results->count[bin]-1]; |
158 | |
159 | result->node=node; |
160 | result->segment=segment; |
161 | |
162 | result->prev=NULL; |
163 | result->next=NULL; |
164 | |
165 | result->score=0; |
166 | result->sortby=0; |
167 | |
168 | result->queued=NOT_QUEUED; |
169 | |
170 | return(result); |
171 | } |
172 | |
173 | |
174 | /*++++++++++++++++++++++++++++++++++++++ |
175 | Find a result; search by node only (don't care about the segment but find the shortest). |
176 | |
177 | Result *FindResult Returns the result that has been found. |
178 | |
179 | Results *results The results structure to search. |
180 | |
181 | index_t node The node that is to be found. |
182 | ++++++++++++++++++++++++++++++++++++++*/ |
183 | |
184 | Result *FindResult1(Results *results,index_t node) |
185 | { |
186 | int bin=node&results->mask; |
187 | score_t best_score=INF_SCORE; |
188 | Result *best_result=NULL; |
189 | int i; |
190 | |
191 | for(i=results->count[bin]-1;i>=0;i--) |
192 | if(results->point[bin][i]->node==node && results->point[bin][i]->score<best_score) |
193 | { |
194 | best_score=results->point[bin][i]->score; |
195 | best_result=results->point[bin][i]; |
196 | } |
197 | |
198 | return(best_result); |
199 | } |
200 | |
201 | |
202 | /*++++++++++++++++++++++++++++++++++++++ |
203 | Find a result; search by node and segment. |
204 | |
205 | Result *FindResult Returns the result that has been found. |
206 | |
207 | Results *results The results structure to search. |
208 | |
209 | index_t node The node that is to be found. |
210 | |
211 | index_t segment The segment that was used to reach this node. |
212 | ++++++++++++++++++++++++++++++++++++++*/ |
213 | |
214 | Result *FindResult(Results *results,index_t node,index_t segment) |
215 | { |
216 | int bin=node&results->mask; |
217 | int i; |
218 | |
219 | for(i=results->count[bin]-1;i>=0;i--) |
220 | if(results->point[bin][i]->node==node && results->point[bin][i]->segment==segment) |
221 | return(results->point[bin][i]); |
222 | |
223 | return(NULL); |
224 | } |
225 | |
226 | |
227 | /*++++++++++++++++++++++++++++++++++++++ |
228 | Find a result from a set of results. |
229 | |
230 | Result *FirstResult Returns the first results from a set of results. |
231 | |
232 | Results *results The set of results. |
233 | ++++++++++++++++++++++++++++++++++++++*/ |
234 | |
235 | Result *FirstResult(Results *results) |
236 | { |
237 | return(&results->data[0][0]); |
238 | } |
239 | |
240 | |
241 | /*++++++++++++++++++++++++++++++++++++++ |
242 | Find a result from a set of results. |
243 | |
244 | Result *NextResult Returns the next result from a set of results. |
245 | |
246 | Results *results The set of results. |
247 | |
248 | Result *result The previous result. |
249 | ++++++++++++++++++++++++++++++++++++++*/ |
250 | |
251 | Result *NextResult(Results *results,Result *result) |
252 | { |
253 | int i,j=0,c=(results->number-1)/(results->nbins*RESULTS_INCREMENT); |
254 | |
255 | for(i=0;i<=c;i++) |
256 | { |
257 | j=result-results->data[i]; |
258 | |
259 | if(j>=0 && j<(results->nbins*RESULTS_INCREMENT)) |
260 | break; |
261 | } |
262 | |
263 | if(++j>=(results->nbins*RESULTS_INCREMENT)) |
264 | {i++;j=0;} |
265 | |
266 | if((i*(results->nbins*RESULTS_INCREMENT)+j)>=results->number) |
267 | return(NULL); |
268 | |
269 | return(&results->data[i][j]); |
270 | } |
Properties
Name | Value |
---|---|
cvs:description | Results data type. |