Check out the latest version of Routino: svn co http://routino.org/svn/trunk routino
Annotation of /trunk/src/waysx.c
Parent Directory
|
Revision Log
Revision 1090 -
(hide annotations)
(download)
(as text)
Wed Oct 17 18:13:51 2012 UTC (12 years, 5 months ago) by amb
File MIME type: text/x-csrc
File size: 15375 byte(s)
Wed Oct 17 18:13:51 2012 UTC (12 years, 5 months ago) by amb
File MIME type: text/x-csrc
File size: 15375 byte(s)
Rename the WayX->prop entry to WayX->cid to disambiguate it.
1 | amb | 110 | /*************************************** |
2 | Extended Way data type functions. | ||
3 | amb | 151 | |
4 | Part of the Routino routing software. | ||
5 | amb | 110 | ******************/ /****************** |
6 | amb | 948 | This file Copyright 2008-2012 Andrew M. Bishop |
7 | amb | 110 | |
8 | amb | 151 | 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 | amb | 110 | ***************************************/ |
21 | |||
22 | |||
23 | #include <assert.h> | ||
24 | #include <stdlib.h> | ||
25 | #include <string.h> | ||
26 | |||
27 | amb | 955 | #include "types.h" |
28 | amb | 228 | #include "ways.h" |
29 | amb | 110 | |
30 | amb | 955 | #include "typesx.h" |
31 | amb | 449 | #include "waysx.h" |
32 | amb | 110 | |
33 | amb | 449 | #include "files.h" |
34 | amb | 519 | #include "logging.h" |
35 | amb | 532 | #include "sorting.h" |
36 | amb | 449 | |
37 | |||
38 | amb | 680 | /* Global variables */ |
39 | amb | 110 | |
40 | amb | 289 | /*+ The command line '--tmpdir' option or its default value. +*/ |
41 | amb | 284 | extern char *option_tmpdirname; |
42 | amb | 110 | |
43 | amb | 680 | |
44 | /* Local variables */ | ||
45 | |||
46 | amb | 284 | /*+ A temporary file-local variable for use by the sort functions. +*/ |
47 | static WaysX *sortwaysx; | ||
48 | |||
49 | amb | 680 | |
50 | amb | 110 | /* Functions */ |
51 | |||
52 | amb | 499 | static int sort_by_id(WayX *a,WayX *b); |
53 | static int sort_by_name_and_id(WayX *a,WayX *b); | ||
54 | amb | 310 | static int sort_by_name_and_prop_and_id(WayX *a,WayX *b); |
55 | |||
56 | amb | 499 | static int deduplicate_and_index_by_id(WayX *wayx,index_t index); |
57 | amb | 272 | |
58 | amb | 110 | |
59 | /*++++++++++++++++++++++++++++++++++++++ | ||
60 | amb | 326 | Allocate a new way list (create a new file or open an existing one). |
61 | amb | 110 | |
62 | WaysX *NewWayList Returns the way list. | ||
63 | amb | 326 | |
64 | int append Set to 1 if the file is to be opened for appending (now or later). | ||
65 | amb | 110 | ++++++++++++++++++++++++++++++++++++++*/ |
66 | |||
67 | amb | 326 | WaysX *NewWayList(int append) |
68 | amb | 110 | { |
69 | WaysX *waysx; | ||
70 | |||
71 | amb | 213 | waysx=(WaysX*)calloc(1,sizeof(WaysX)); |
72 | amb | 110 | |
73 | amb | 243 | assert(waysx); /* Check calloc() worked */ |
74 | |||
75 | amb | 284 | waysx->filename=(char*)malloc(strlen(option_tmpdirname)+32); |
76 | amb | 216 | |
77 | amb | 326 | if(append) |
78 | amb | 447 | sprintf(waysx->filename,"%s/waysx.input.tmp",option_tmpdirname); |
79 | amb | 326 | else |
80 | amb | 788 | sprintf(waysx->filename,"%s/waysx.%p.tmp",option_tmpdirname,(void*)waysx); |
81 | amb | 262 | |
82 | amb | 326 | if(append) |
83 | { | ||
84 | amb | 331 | off_t size,position=0; |
85 | amb | 326 | |
86 | amb | 502 | waysx->fd=OpenFileAppend(waysx->filename); |
87 | amb | 326 | |
88 | amb | 331 | size=SizeFile(waysx->filename); |
89 | amb | 326 | |
90 | amb | 331 | while(position<size) |
91 | amb | 326 | { |
92 | amb | 331 | FILESORT_VARINT waysize; |
93 | amb | 326 | |
94 | amb | 887 | SeekReadFile(waysx->fd,&waysize,FILESORT_VARSIZE,position); |
95 | amb | 326 | |
96 | amb | 650 | waysx->number++; |
97 | amb | 331 | position+=waysize+FILESORT_VARSIZE; |
98 | amb | 326 | } |
99 | |||
100 | amb | 331 | SeekFile(waysx->fd,size); |
101 | amb | 326 | } |
102 | else | ||
103 | amb | 502 | waysx->fd=OpenFileNew(waysx->filename); |
104 | amb | 326 | |
105 | amb | 284 | waysx->nfilename=(char*)malloc(strlen(option_tmpdirname)+32); |
106 | amb | 788 | sprintf(waysx->nfilename,"%s/waynames.%p.tmp",option_tmpdirname,(void*)waysx); |
107 | amb | 262 | |
108 | amb | 110 | return(waysx); |
109 | } | ||
110 | |||
111 | |||
112 | /*++++++++++++++++++++++++++++++++++++++ | ||
113 | amb | 226 | Free a way list. |
114 | |||
115 | amb | 681 | WaysX *waysx The set of ways to be freed. |
116 | amb | 326 | |
117 | amb | 680 | int keep Set to 1 if the file is to be kept (for appending later). |
118 | amb | 226 | ++++++++++++++++++++++++++++++++++++++*/ |
119 | |||
120 | amb | 326 | void FreeWayList(WaysX *waysx,int keep) |
121 | amb | 226 | { |
122 | amb | 326 | if(!keep) |
123 | DeleteFile(waysx->filename); | ||
124 | |||
125 | amb | 283 | free(waysx->filename); |
126 | amb | 262 | |
127 | amb | 226 | if(waysx->idata) |
128 | free(waysx->idata); | ||
129 | |||
130 | amb | 262 | DeleteFile(waysx->nfilename); |
131 | amb | 326 | |
132 | amb | 283 | free(waysx->nfilename); |
133 | amb | 226 | |
134 | free(waysx); | ||
135 | } | ||
136 | |||
137 | |||
138 | /*++++++++++++++++++++++++++++++++++++++ | ||
139 | amb | 493 | Append a single way to an unsorted way list. |
140 | amb | 203 | |
141 | amb | 682 | WaysX *waysx The set of ways to process. |
142 | amb | 110 | |
143 | amb | 262 | way_t id The ID of the way. |
144 | amb | 110 | |
145 | amb | 262 | Way *way The way data itself. |
146 | amb | 110 | |
147 | amb | 262 | const char *name The name or reference of the way. |
148 | ++++++++++++++++++++++++++++++++++++++*/ | ||
149 | amb | 110 | |
150 | amb | 682 | void AppendWay(WaysX *waysx,way_t id,Way *way,const char *name) |
151 | amb | 262 | { |
152 | WayX wayx; | ||
153 | amb | 311 | FILESORT_VARINT size; |
154 | amb | 110 | |
155 | amb | 262 | wayx.id=id; |
156 | amb | 1090 | wayx.cid=0; |
157 | amb | 262 | wayx.way=*way; |
158 | |||
159 | amb | 310 | size=sizeof(WayX)+strlen(name)+1; |
160 | |||
161 | amb | 311 | WriteFile(waysx->fd,&size,FILESORT_VARSIZE); |
162 | amb | 262 | WriteFile(waysx->fd,&wayx,sizeof(WayX)); |
163 | amb | 310 | WriteFile(waysx->fd,name,strlen(name)+1); |
164 | amb | 262 | |
165 | amb | 650 | waysx->number++; |
166 | amb | 466 | |
167 | amb | 1065 | assert(waysx->number!=0); /* Zero marks the high-water mark for ways. */ |
168 | amb | 110 | } |
169 | |||
170 | |||
171 | /*++++++++++++++++++++++++++++++++++++++ | ||
172 | amb | 224 | Sort the list of ways. |
173 | amb | 110 | |
174 | amb | 682 | WaysX *waysx The set of ways to process. |
175 | amb | 110 | ++++++++++++++++++++++++++++++++++++++*/ |
176 | |||
177 | amb | 682 | void SortWayList(WaysX *waysx) |
178 | amb | 110 | { |
179 | amb | 650 | index_t i,xnumber; |
180 | amb | 555 | int fd; |
181 | amb | 310 | char *names[2]={NULL,NULL}; |
182 | int namelen[2]={0,0}; | ||
183 | amb | 499 | int nnames=0; |
184 | amb | 310 | uint32_t lastlength=0; |
185 | amb | 110 | |
186 | amb | 266 | /* Print the start message */ |
187 | |||
188 | amb | 519 | printf_first("Sorting Ways by Name"); |
189 | amb | 110 | |
190 | amb | 555 | /* Close the file (finished appending) */ |
191 | amb | 203 | |
192 | amb | 612 | waysx->fd=CloseFile(waysx->fd); |
193 | amb | 555 | |
194 | /* Re-open the file read-only and a new file writeable */ | ||
195 | |||
196 | amb | 262 | waysx->fd=ReOpenFile(waysx->filename); |
197 | amb | 216 | |
198 | amb | 310 | DeleteFile(waysx->filename); |
199 | amb | 262 | |
200 | amb | 502 | fd=OpenFileNew(waysx->filename); |
201 | amb | 310 | |
202 | amb | 499 | /* Sort the ways to allow separating the names */ |
203 | amb | 310 | |
204 | amb | 499 | filesort_vary(waysx->fd,fd,(int (*)(const void*,const void*))sort_by_name_and_id,NULL); |
205 | amb | 310 | |
206 | /* Close the files */ | ||
207 | |||
208 | amb | 612 | waysx->fd=CloseFile(waysx->fd); |
209 | amb | 310 | CloseFile(fd); |
210 | |||
211 | /* Print the final message */ | ||
212 | |||
213 | amb | 790 | printf_last("Sorted Ways by Name: Ways=%"Pindex_t,waysx->number); |
214 | amb | 310 | |
215 | |||
216 | /* Print the start message */ | ||
217 | |||
218 | amb | 519 | printf_first("Separating Way Names: Ways=0 Names=0"); |
219 | amb | 310 | |
220 | amb | 555 | /* Re-open the file read-only and new files writeable */ |
221 | amb | 310 | |
222 | waysx->fd=ReOpenFile(waysx->filename); | ||
223 | |||
224 | amb | 272 | DeleteFile(waysx->filename); |
225 | amb | 262 | |
226 | amb | 502 | fd=OpenFileNew(waysx->filename); |
227 | amb | 272 | |
228 | amb | 555 | waysx->nfd=OpenFileNew(waysx->nfilename); |
229 | |||
230 | amb | 499 | /* Copy from the single file into two files */ |
231 | amb | 310 | |
232 | amb | 650 | for(i=0;i<waysx->number;i++) |
233 | amb | 310 | { |
234 | WayX wayx; | ||
235 | amb | 311 | FILESORT_VARINT size; |
236 | amb | 310 | |
237 | amb | 311 | ReadFile(waysx->fd,&size,FILESORT_VARSIZE); |
238 | amb | 310 | |
239 | if(namelen[nnames%2]<size) | ||
240 | names[nnames%2]=(char*)realloc((void*)names[nnames%2],namelen[nnames%2]=size); | ||
241 | |||
242 | ReadFile(waysx->fd,&wayx,sizeof(WayX)); | ||
243 | ReadFile(waysx->fd,names[nnames%2],size-sizeof(WayX)); | ||
244 | |||
245 | if(nnames==0 || strcmp(names[0],names[1])) | ||
246 | { | ||
247 | amb | 555 | WriteFile(waysx->nfd,names[nnames%2],size-sizeof(WayX)); |
248 | amb | 310 | |
249 | lastlength=waysx->nlength; | ||
250 | waysx->nlength+=size-sizeof(WayX); | ||
251 | |||
252 | nnames++; | ||
253 | } | ||
254 | |||
255 | wayx.way.name=lastlength; | ||
256 | |||
257 | WriteFile(fd,&wayx,sizeof(WayX)); | ||
258 | |||
259 | amb | 757 | if(!((i+1)%1000)) |
260 | amb | 790 | printf_middle("Separating Way Names: Ways=%"Pindex_t" Names=%"Pindex_t,i+1,nnames); |
261 | amb | 310 | } |
262 | |||
263 | if(names[0]) free(names[0]); | ||
264 | if(names[1]) free(names[1]); | ||
265 | |||
266 | /* Close the files */ | ||
267 | |||
268 | amb | 612 | waysx->fd=CloseFile(waysx->fd); |
269 | amb | 310 | CloseFile(fd); |
270 | |||
271 | amb | 612 | waysx->nfd=CloseFile(waysx->nfd); |
272 | amb | 310 | |
273 | /* Print the final message */ | ||
274 | |||
275 | amb | 790 | printf_last("Separated Way Names: Ways=%"Pindex_t" Names=%"Pindex_t" ",waysx->number,nnames); |
276 | amb | 310 | |
277 | |||
278 | /* Print the start message */ | ||
279 | |||
280 | amb | 519 | printf_first("Sorting Ways"); |
281 | amb | 310 | |
282 | amb | 555 | /* Re-open the file read-only and a new file writeable */ |
283 | amb | 310 | |
284 | waysx->fd=ReOpenFile(waysx->filename); | ||
285 | |||
286 | DeleteFile(waysx->filename); | ||
287 | |||
288 | amb | 502 | fd=OpenFileNew(waysx->filename); |
289 | amb | 310 | |
290 | amb | 272 | /* Allocate the array of indexes */ |
291 | |||
292 | amb | 650 | waysx->idata=(way_t*)malloc(waysx->number*sizeof(way_t)); |
293 | amb | 262 | |
294 | amb | 243 | assert(waysx->idata); /* Check malloc() worked */ |
295 | |||
296 | amb | 310 | /* Sort the ways by index and index them */ |
297 | amb | 203 | |
298 | amb | 650 | xnumber=waysx->number; |
299 | amb | 499 | waysx->number=0; |
300 | |||
301 | amb | 272 | sortwaysx=waysx; |
302 | amb | 215 | |
303 | amb | 948 | waysx->number=filesort_fixed(waysx->fd,fd,sizeof(WayX),(int (*)(const void*,const void*))sort_by_id,(int (*)(void*,index_t))deduplicate_and_index_by_id); |
304 | amb | 215 | |
305 | amb | 555 | /* Close the files */ |
306 | amb | 262 | |
307 | amb | 612 | waysx->fd=CloseFile(waysx->fd); |
308 | amb | 262 | CloseFile(fd); |
309 | |||
310 | amb | 266 | /* Print the final message */ |
311 | |||
312 | amb | 790 | printf_last("Sorted Ways: Ways=%"Pindex_t" Duplicates=%"Pindex_t,xnumber,xnumber-waysx->number); |
313 | amb | 499 | } |
314 | |||
315 | |||
316 | /*++++++++++++++++++++++++++++++++++++++ | ||
317 | Compact the list of ways. | ||
318 | |||
319 | amb | 682 | WaysX *waysx The set of ways to process. |
320 | amb | 499 | ++++++++++++++++++++++++++++++++++++++*/ |
321 | |||
322 | amb | 682 | void CompactWayList(WaysX *waysx) |
323 | amb | 499 | { |
324 | index_t i; | ||
325 | int fd; | ||
326 | Way lastway; | ||
327 | |||
328 | /* Print the start message */ | ||
329 | |||
330 | amb | 519 | printf_first("Sorting Ways by Properties"); |
331 | amb | 499 | |
332 | amb | 555 | /* Re-open the file read-only and a new file writeable */ |
333 | amb | 499 | |
334 | waysx->fd=ReOpenFile(waysx->filename); | ||
335 | |||
336 | DeleteFile(waysx->filename); | ||
337 | |||
338 | amb | 502 | fd=OpenFileNew(waysx->filename); |
339 | amb | 499 | |
340 | /* Sort the ways to allow compacting according to he properties */ | ||
341 | |||
342 | filesort_fixed(waysx->fd,fd,sizeof(WayX),(int (*)(const void*,const void*))sort_by_name_and_prop_and_id,NULL); | ||
343 | |||
344 | /* Close the files */ | ||
345 | |||
346 | amb | 612 | waysx->fd=CloseFile(waysx->fd); |
347 | amb | 499 | CloseFile(fd); |
348 | |||
349 | /* Print the final message */ | ||
350 | |||
351 | amb | 790 | printf_last("Sorted Ways by Properties: Ways=%"Pindex_t,waysx->number); |
352 | amb | 499 | |
353 | |||
354 | /* Print the start message */ | ||
355 | |||
356 | amb | 519 | printf_first("Compacting Ways: Ways=0 Properties=0"); |
357 | amb | 499 | |
358 | amb | 555 | /* Re-open the file read-only and a new file writeable */ |
359 | amb | 499 | |
360 | waysx->fd=ReOpenFile(waysx->filename); | ||
361 | |||
362 | DeleteFile(waysx->filename); | ||
363 | |||
364 | amb | 502 | fd=OpenFileNew(waysx->filename); |
365 | amb | 499 | |
366 | /* Update the way as we go using the sorted index */ | ||
367 | |||
368 | waysx->cnumber=0; | ||
369 | |||
370 | for(i=0;i<waysx->number;i++) | ||
371 | { | ||
372 | WayX wayx; | ||
373 | |||
374 | ReadFile(waysx->fd,&wayx,sizeof(WayX)); | ||
375 | |||
376 | if(waysx->cnumber==0 || wayx.way.name!=lastway.name || WaysCompare(&lastway,&wayx.way)) | ||
377 | { | ||
378 | lastway=wayx.way; | ||
379 | |||
380 | waysx->cnumber++; | ||
381 | } | ||
382 | |||
383 | amb | 1090 | wayx.cid=waysx->cnumber-1; |
384 | amb | 499 | |
385 | WriteFile(fd,&wayx,sizeof(WayX)); | ||
386 | |||
387 | amb | 757 | if(!((i+1)%1000)) |
388 | amb | 790 | printf_middle("Compacting Ways: Ways=%"Pindex_t" Properties=%"Pindex_t,i+1,waysx->cnumber); |
389 | amb | 499 | } |
390 | |||
391 | /* Close the files */ | ||
392 | |||
393 | amb | 612 | waysx->fd=CloseFile(waysx->fd); |
394 | amb | 499 | CloseFile(fd); |
395 | |||
396 | /* Print the final message */ | ||
397 | |||
398 | amb | 790 | printf_last("Compacted Ways: Ways=%"Pindex_t" Properties=%"Pindex_t" ",waysx->number,waysx->cnumber); |
399 | amb | 499 | |
400 | |||
401 | /* Print the start message */ | ||
402 | |||
403 | amb | 519 | printf_first("Sorting Ways"); |
404 | amb | 499 | |
405 | amb | 555 | /* Re-open the file read-only and a new file writeable */ |
406 | amb | 499 | |
407 | waysx->fd=ReOpenFile(waysx->filename); | ||
408 | |||
409 | DeleteFile(waysx->filename); | ||
410 | |||
411 | amb | 502 | fd=OpenFileNew(waysx->filename); |
412 | amb | 499 | |
413 | /* Sort the ways by index */ | ||
414 | |||
415 | filesort_fixed(waysx->fd,fd,sizeof(WayX),(int (*)(const void*,const void*))sort_by_id,NULL); | ||
416 | |||
417 | amb | 555 | /* Close the files */ |
418 | amb | 499 | |
419 | amb | 612 | waysx->fd=CloseFile(waysx->fd); |
420 | amb | 499 | CloseFile(fd); |
421 | |||
422 | /* Print the final message */ | ||
423 | |||
424 | amb | 790 | printf_last("Sorted Ways: Ways=%"Pindex_t,waysx->number); |
425 | amb | 224 | } |
426 | |||
427 | |||
428 | /*++++++++++++++++++++++++++++++++++++++ | ||
429 | amb | 262 | Sort the ways into id order. |
430 | amb | 224 | |
431 | amb | 262 | int sort_by_id Returns the comparison of the id fields. |
432 | |||
433 | amb | 272 | WayX *a The first extended way. |
434 | amb | 262 | |
435 | amb | 272 | WayX *b The second extended way. |
436 | amb | 262 | ++++++++++++++++++++++++++++++++++++++*/ |
437 | |||
438 | amb | 272 | static int sort_by_id(WayX *a,WayX *b) |
439 | amb | 262 | { |
440 | amb | 272 | way_t a_id=a->id; |
441 | way_t b_id=b->id; | ||
442 | amb | 262 | |
443 | if(a_id<b_id) | ||
444 | return(-1); | ||
445 | else if(a_id>b_id) | ||
446 | return(1); | ||
447 | else | ||
448 | return(0); | ||
449 | } | ||
450 | |||
451 | |||
452 | /*++++++++++++++++++++++++++++++++++++++ | ||
453 | amb | 680 | Sort the ways into name order and then id order. |
454 | amb | 272 | |
455 | amb | 499 | int sort_by_name_and_id Returns the comparison of the name and id fields. |
456 | amb | 272 | |
457 | amb | 310 | WayX *a The first extended Way. |
458 | |||
459 | WayX *b The second extended Way. | ||
460 | ++++++++++++++++++++++++++++++++++++++*/ | ||
461 | |||
462 | amb | 499 | static int sort_by_name_and_id(WayX *a,WayX *b) |
463 | amb | 310 | { |
464 | int compare; | ||
465 | char *a_name=(char*)a+sizeof(WayX); | ||
466 | char *b_name=(char*)b+sizeof(WayX); | ||
467 | |||
468 | compare=strcmp(a_name,b_name); | ||
469 | |||
470 | if(compare) | ||
471 | return(compare); | ||
472 | |||
473 | amb | 499 | return(sort_by_id(a,b)); |
474 | } | ||
475 | |||
476 | |||
477 | /*++++++++++++++++++++++++++++++++++++++ | ||
478 | Sort the ways into name, properties and id order. | ||
479 | |||
480 | int sort_by_name_and_prop_and_id Returns the comparison of the name, properties and id fields. | ||
481 | |||
482 | WayX *a The first extended Way. | ||
483 | |||
484 | WayX *b The second extended Way. | ||
485 | ++++++++++++++++++++++++++++++++++++++*/ | ||
486 | |||
487 | static int sort_by_name_and_prop_and_id(WayX *a,WayX *b) | ||
488 | { | ||
489 | int compare; | ||
490 | index_t a_name=a->way.name; | ||
491 | index_t b_name=b->way.name; | ||
492 | |||
493 | if(a_name<b_name) | ||
494 | return(-1); | ||
495 | else if(a_name>b_name) | ||
496 | return(1); | ||
497 | |||
498 | amb | 310 | compare=WaysCompare(&a->way,&b->way); |
499 | |||
500 | if(compare) | ||
501 | return(compare); | ||
502 | |||
503 | return(sort_by_id(a,b)); | ||
504 | } | ||
505 | |||
506 | |||
507 | /*++++++++++++++++++++++++++++++++++++++ | ||
508 | amb | 680 | Create the index of identifiers and discard duplicate ways. |
509 | amb | 310 | |
510 | amb | 680 | int deduplicate_and_index_by_id Return 1 if the value is to be kept, otherwise 0. |
511 | amb | 310 | |
512 | amb | 272 | WayX *wayx The extended way. |
513 | |||
514 | index_t index The index of this way in the total. | ||
515 | ++++++++++++++++++++++++++++++++++++++*/ | ||
516 | |||
517 | amb | 499 | static int deduplicate_and_index_by_id(WayX *wayx,index_t index) |
518 | amb | 272 | { |
519 | amb | 310 | static way_t previd; |
520 | |||
521 | if(index==0 || wayx->id!=previd) | ||
522 | amb | 272 | { |
523 | amb | 310 | previd=wayx->id; |
524 | amb | 272 | |
525 | amb | 499 | sortwaysx->idata[index]=wayx->id; |
526 | |||
527 | amb | 272 | return(1); |
528 | } | ||
529 | amb | 812 | else |
530 | { | ||
531 | logerror("Way %"Pway_t" is duplicated.\n",wayx->id); | ||
532 | amb | 272 | |
533 | amb | 812 | return(0); |
534 | } | ||
535 | amb | 272 | } |
536 | |||
537 | |||
538 | /*++++++++++++++++++++++++++++++++++++++ | ||
539 | amb | 285 | Find a particular way index. |
540 | |||
541 | index_t IndexWayX Returns the index of the extended way with the specified id. | ||
542 | |||
543 | amb | 682 | WaysX *waysx The set of ways to process. |
544 | amb | 285 | |
545 | way_t id The way id to look for. | ||
546 | ++++++++++++++++++++++++++++++++++++++*/ | ||
547 | |||
548 | amb | 682 | index_t IndexWayX(WaysX *waysx,way_t id) |
549 | amb | 285 | { |
550 | amb | 780 | index_t start=0; |
551 | index_t end=waysx->number-1; | ||
552 | index_t mid; | ||
553 | amb | 285 | |
554 | /* Binary search - search key exact match only is required. | ||
555 | * | ||
556 | * # <- start | Check mid and move start or end if it doesn't match | ||
557 | * # | | ||
558 | * # | Since an exact match is wanted we can set end=mid-1 | ||
559 | * # <- mid | or start=mid+1 because we know that mid doesn't match. | ||
560 | * # | | ||
561 | * # | Eventually either end=start or end=start+1 and one of | ||
562 | * # <- end | start or end is the wanted one. | ||
563 | */ | ||
564 | |||
565 | if(end<start) /* There are no ways */ | ||
566 | return(NO_WAY); | ||
567 | else if(id<waysx->idata[start]) /* Check key is not before start */ | ||
568 | return(NO_WAY); | ||
569 | else if(id>waysx->idata[end]) /* Check key is not after end */ | ||
570 | return(NO_WAY); | ||
571 | else | ||
572 | { | ||
573 | do | ||
574 | { | ||
575 | mid=(start+end)/2; /* Choose mid point */ | ||
576 | |||
577 | if(waysx->idata[mid]<id) /* Mid point is too low */ | ||
578 | start=mid+1; | ||
579 | else if(waysx->idata[mid]>id) /* Mid point is too high */ | ||
580 | amb | 843 | end=mid?(mid-1):mid; |
581 | amb | 285 | else /* Mid point is correct */ |
582 | return(mid); | ||
583 | } | ||
584 | while((end-start)>1); | ||
585 | |||
586 | if(waysx->idata[start]==id) /* Start is correct */ | ||
587 | return(start); | ||
588 | |||
589 | if(waysx->idata[end]==id) /* End is correct */ | ||
590 | return(end); | ||
591 | } | ||
592 | |||
593 | return(NO_WAY); | ||
594 | } | ||
595 | |||
596 | |||
597 | /*++++++++++++++++++++++++++++++++++++++ | ||
598 | Save the way list to a file. | ||
599 | |||
600 | amb | 682 | WaysX *waysx The set of ways to save. |
601 | amb | 285 | |
602 | const char *filename The name of the file to save. | ||
603 | ++++++++++++++++++++++++++++++++++++++*/ | ||
604 | |||
605 | amb | 682 | void SaveWayList(WaysX *waysx,const char *filename) |
606 | amb | 285 | { |
607 | index_t i; | ||
608 | amb | 555 | int fd; |
609 | amb | 310 | int position=0; |
610 | amb | 499 | WaysFile waysfile={0}; |
611 | amb | 529 | highways_t highways=0; |
612 | transports_t allow=0; | ||
613 | amb | 530 | properties_t props=0; |
614 | amb | 285 | |
615 | amb | 461 | /* Print the start message */ |
616 | |||
617 | amb | 519 | printf_first("Writing Ways: Ways=0"); |
618 | amb | 285 | |
619 | amb | 555 | /* Map into memory / open the file */ |
620 | amb | 461 | |
621 | amb | 452 | #if !SLIM |
622 | amb | 651 | waysx->data=MapFile(waysx->filename); |
623 | amb | 555 | #else |
624 | waysx->fd=ReOpenFile(waysx->filename); | ||
625 | amb | 452 | #endif |
626 | amb | 285 | |
627 | amb | 461 | /* Write out the ways data */ |
628 | amb | 285 | |
629 | amb | 502 | fd=OpenFileNew(filename); |
630 | amb | 285 | |
631 | amb | 461 | SeekFile(fd,sizeof(WaysFile)); |
632 | amb | 285 | |
633 | for(i=0;i<waysx->number;i++) | ||
634 | { | ||
635 | amb | 309 | WayX *wayx=LookupWayX(waysx,i,1); |
636 | amb | 285 | |
637 | amb | 526 | highways|=HIGHWAYS(wayx->way.type); |
638 | allow |=wayx->way.allow; | ||
639 | props |=wayx->way.props; | ||
640 | amb | 398 | |
641 | amb | 1090 | SeekWriteFile(fd,&wayx->way,sizeof(Way),sizeof(WaysFile)+(off_t)wayx->cid*sizeof(Way)); |
642 | amb | 309 | |
643 | amb | 757 | if(!((i+1)%1000)) |
644 | amb | 790 | printf_middle("Writing Ways: Ways=%"Pindex_t,i+1); |
645 | amb | 285 | } |
646 | |||
647 | amb | 555 | /* Unmap from memory / close the file */ |
648 | amb | 398 | |
649 | amb | 452 | #if !SLIM |
650 | amb | 651 | waysx->data=UnmapFile(waysx->filename); |
651 | amb | 555 | #else |
652 | amb | 612 | waysx->fd=CloseFile(waysx->fd); |
653 | amb | 452 | #endif |
654 | amb | 285 | |
655 | amb | 461 | /* Write out the ways names */ |
656 | amb | 285 | |
657 | amb | 464 | SeekFile(fd,sizeof(WaysFile)+(off_t)waysx->cnumber*sizeof(Way)); |
658 | amb | 461 | |
659 | amb | 555 | waysx->nfd=ReOpenFile(waysx->nfilename); |
660 | amb | 285 | |
661 | amb | 309 | while(position<waysx->nlength) |
662 | { | ||
663 | int len=1024; | ||
664 | char temp[1024]; | ||
665 | |||
666 | if((waysx->nlength-position)<1024) | ||
667 | len=waysx->nlength-position; | ||
668 | |||
669 | amb | 555 | ReadFile(waysx->nfd,temp,len); |
670 | amb | 309 | WriteFile(fd,temp,len); |
671 | |||
672 | position+=len; | ||
673 | } | ||
674 | |||
675 | amb | 555 | /* Close the file */ |
676 | amb | 309 | |
677 | amb | 612 | waysx->nfd=CloseFile(waysx->nfd); |
678 | amb | 555 | |
679 | amb | 461 | /* Write out the header structure */ |
680 | |||
681 | amb | 526 | waysfile.number =waysx->cnumber; |
682 | amb | 461 | waysfile.onumber=waysx->number; |
683 | |||
684 | amb | 526 | waysfile.highways=highways; |
685 | waysfile.allow =allow; | ||
686 | waysfile.props =props; | ||
687 | amb | 461 | |
688 | SeekFile(fd,0); | ||
689 | WriteFile(fd,&waysfile,sizeof(WaysFile)); | ||
690 | |||
691 | amb | 285 | CloseFile(fd); |
692 | |||
693 | amb | 461 | /* Print the final message */ |
694 | |||
695 | amb | 790 | printf_last("Wrote Ways: Ways=%"Pindex_t,waysx->number); |
696 | amb | 285 | } |
Properties
Name | Value |
---|---|
cvs:description | Extended ways functions. |