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 1166 -
(hide annotations)
(download)
(as text)
Tue Nov 20 16:12:08 2012 UTC (12 years, 4 months ago) by amb
File MIME type: text/x-csrc
File size: 18521 byte(s)
Tue Nov 20 16:12:08 2012 UTC (12 years, 4 months ago) by amb
File MIME type: text/x-csrc
File size: 18521 byte(s)
Replace all assert statements with a custom error message that explains the cause and suggests a solution.
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 <stdlib.h> | ||
24 | #include <string.h> | ||
25 | |||
26 | amb | 955 | #include "types.h" |
27 | amb | 228 | #include "ways.h" |
28 | amb | 110 | |
29 | amb | 955 | #include "typesx.h" |
30 | amb | 1092 | #include "segmentsx.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 | 1146 | /*+ The option to apply changes (needed to suppress some error log messages) +*/ |
44 | extern int option_changes; | ||
45 | |||
46 | amb | 680 | /* Local variables */ |
47 | |||
48 | amb | 1094 | /*+ Temporary file-local variables for use by the sort functions. +*/ |
49 | amb | 284 | static WaysX *sortwaysx; |
50 | amb | 1100 | static SegmentsX *sortsegmentsx; |
51 | amb | 284 | |
52 | amb | 1100 | /* Local functions */ |
53 | amb | 680 | |
54 | amb | 499 | static int sort_by_id(WayX *a,WayX *b); |
55 | amb | 1160 | static int deduplicate_by_id(WayX *wayx,index_t index); |
56 | |||
57 | amb | 1129 | static int sort_by_name(WayX *a,WayX *b); |
58 | amb | 1160 | static int index_by_id(WayX *wayx,index_t index); |
59 | amb | 310 | |
60 | amb | 1114 | static int delete_unused(WayX *wayx,index_t index); |
61 | amb | 1160 | static int sort_by_name_and_prop_and_id(WayX *a,WayX *b); |
62 | amb | 1094 | static int deduplicate_and_index_by_compact_id(WayX *wayx,index_t index); |
63 | amb | 272 | |
64 | amb | 110 | |
65 | /*++++++++++++++++++++++++++++++++++++++ | ||
66 | amb | 326 | Allocate a new way list (create a new file or open an existing one). |
67 | amb | 110 | |
68 | WaysX *NewWayList Returns the way list. | ||
69 | amb | 326 | |
70 | amb | 1123 | int append Set to 1 if the file is to be opened for appending. |
71 | |||
72 | amb | 1139 | int readonly Set to 1 if the file is to be opened for reading. |
73 | amb | 110 | ++++++++++++++++++++++++++++++++++++++*/ |
74 | |||
75 | amb | 1158 | WaysX *NewWayList(int append,int readonly) |
76 | amb | 110 | { |
77 | WaysX *waysx; | ||
78 | |||
79 | amb | 213 | waysx=(WaysX*)calloc(1,sizeof(WaysX)); |
80 | amb | 110 | |
81 | amb | 1166 | logassert(waysx,"Failed to allocate memory (try using slim mode?)"); /* Check calloc() worked */ |
82 | amb | 243 | |
83 | amb | 1120 | waysx->filename =(char*)malloc(strlen(option_tmpdirname)+32); |
84 | waysx->filename_tmp=(char*)malloc(strlen(option_tmpdirname)+32); | ||
85 | amb | 216 | |
86 | amb | 1120 | sprintf(waysx->filename ,"%s/waysx.parsed.mem",option_tmpdirname); |
87 | sprintf(waysx->filename_tmp,"%s/waysx.%p.tmp" ,option_tmpdirname,(void*)waysx); | ||
88 | amb | 262 | |
89 | amb | 1123 | if(append || readonly) |
90 | if(ExistsFile(waysx->filename)) | ||
91 | { | ||
92 | off_t size,position=0; | ||
93 | int fd; | ||
94 | amb | 326 | |
95 | amb | 1123 | size=SizeFile(waysx->filename); |
96 | amb | 326 | |
97 | amb | 1123 | fd=ReOpenFile(waysx->filename); |
98 | amb | 326 | |
99 | amb | 1123 | while(position<size) |
100 | { | ||
101 | FILESORT_VARINT waysize; | ||
102 | amb | 326 | |
103 | amb | 1123 | SeekReadFile(fd,&waysize,FILESORT_VARSIZE,position); |
104 | amb | 326 | |
105 | amb | 1123 | waysx->number++; |
106 | position+=waysize+FILESORT_VARSIZE; | ||
107 | } | ||
108 | |||
109 | CloseFile(fd); | ||
110 | amb | 1139 | |
111 | RenameFile(waysx->filename,waysx->filename_tmp); | ||
112 | amb | 326 | } |
113 | |||
114 | amb | 1123 | if(append) |
115 | amb | 1139 | waysx->fd=OpenFileAppend(waysx->filename_tmp); |
116 | amb | 1123 | else if(!readonly) |
117 | amb | 1139 | waysx->fd=OpenFileNew(waysx->filename_tmp); |
118 | amb | 326 | else |
119 | amb | 1123 | waysx->fd=-1; |
120 | amb | 326 | |
121 | amb | 262 | |
122 | amb | 1120 | waysx->nfilename_tmp=(char*)malloc(strlen(option_tmpdirname)+32); |
123 | |||
124 | sprintf(waysx->nfilename_tmp,"%s/waynames.%p.tmp",option_tmpdirname,(void*)waysx); | ||
125 | |||
126 | amb | 110 | return(waysx); |
127 | } | ||
128 | |||
129 | |||
130 | /*++++++++++++++++++++++++++++++++++++++ | ||
131 | amb | 226 | Free a way list. |
132 | |||
133 | amb | 681 | WaysX *waysx The set of ways to be freed. |
134 | amb | 1151 | |
135 | int preserve If set then the results file is to be preserved. | ||
136 | amb | 226 | ++++++++++++++++++++++++++++++++++++++*/ |
137 | |||
138 | amb | 1151 | void FreeWayList(WaysX *waysx,int preserve) |
139 | amb | 226 | { |
140 | amb | 1151 | if(preserve) |
141 | RenameFile(waysx->filename_tmp,waysx->filename); | ||
142 | else | ||
143 | DeleteFile(waysx->filename_tmp); | ||
144 | amb | 1120 | |
145 | amb | 283 | free(waysx->filename); |
146 | amb | 1120 | free(waysx->filename_tmp); |
147 | amb | 262 | |
148 | amb | 226 | if(waysx->idata) |
149 | free(waysx->idata); | ||
150 | |||
151 | amb | 1093 | if(waysx->cdata) |
152 | free(waysx->cdata); | ||
153 | |||
154 | amb | 1120 | DeleteFile(waysx->nfilename_tmp); |
155 | amb | 326 | |
156 | amb | 1120 | free(waysx->nfilename_tmp); |
157 | amb | 226 | |
158 | free(waysx); | ||
159 | } | ||
160 | |||
161 | |||
162 | /*++++++++++++++++++++++++++++++++++++++ | ||
163 | amb | 493 | Append a single way to an unsorted way list. |
164 | amb | 203 | |
165 | amb | 682 | WaysX *waysx The set of ways to process. |
166 | amb | 110 | |
167 | amb | 262 | way_t id The ID of the way. |
168 | amb | 110 | |
169 | amb | 262 | Way *way The way data itself. |
170 | amb | 110 | |
171 | amb | 262 | const char *name The name or reference of the way. |
172 | ++++++++++++++++++++++++++++++++++++++*/ | ||
173 | amb | 110 | |
174 | amb | 1161 | void AppendWayList(WaysX *waysx,way_t id,Way *way,const char *name) |
175 | amb | 262 | { |
176 | WayX wayx; | ||
177 | amb | 311 | FILESORT_VARINT size; |
178 | amb | 110 | |
179 | amb | 262 | wayx.id=id; |
180 | wayx.way=*way; | ||
181 | |||
182 | amb | 310 | size=sizeof(WayX)+strlen(name)+1; |
183 | |||
184 | amb | 311 | WriteFile(waysx->fd,&size,FILESORT_VARSIZE); |
185 | amb | 262 | WriteFile(waysx->fd,&wayx,sizeof(WayX)); |
186 | amb | 310 | WriteFile(waysx->fd,name,strlen(name)+1); |
187 | amb | 262 | |
188 | amb | 650 | waysx->number++; |
189 | amb | 466 | |
190 | amb | 1166 | logassert(waysx->number!=0,"Too many ways (change index_t to 64-bits?)"); /* Zero marks the high-water mark for ways. */ |
191 | amb | 110 | } |
192 | |||
193 | |||
194 | /*++++++++++++++++++++++++++++++++++++++ | ||
195 | amb | 1120 | Finish appending ways and change the filename over. |
196 | |||
197 | WaysX *waysx The ways that have been appended. | ||
198 | ++++++++++++++++++++++++++++++++++++++*/ | ||
199 | |||
200 | amb | 1151 | void FinishWayList(WaysX *waysx) |
201 | amb | 1120 | { |
202 | amb | 1136 | if(waysx->fd!=-1) |
203 | waysx->fd=CloseFile(waysx->fd); | ||
204 | amb | 1120 | } |
205 | |||
206 | |||
207 | /*++++++++++++++++++++++++++++++++++++++ | ||
208 | amb | 1160 | Find a particular way index. |
209 | |||
210 | index_t IndexWayX Returns the index of the extended way with the specified id. | ||
211 | |||
212 | WaysX *waysx The set of ways to process. | ||
213 | |||
214 | way_t id The way id to look for. | ||
215 | ++++++++++++++++++++++++++++++++++++++*/ | ||
216 | |||
217 | index_t IndexWayX(WaysX *waysx,way_t id) | ||
218 | { | ||
219 | index_t start=0; | ||
220 | index_t end=waysx->number-1; | ||
221 | index_t mid; | ||
222 | |||
223 | /* Binary search - search key exact match only is required. | ||
224 | * | ||
225 | * # <- start | Check mid and move start or end if it doesn't match | ||
226 | * # | | ||
227 | * # | Since an exact match is wanted we can set end=mid-1 | ||
228 | * # <- mid | or start=mid+1 because we know that mid doesn't match. | ||
229 | * # | | ||
230 | * # | Eventually either end=start or end=start+1 and one of | ||
231 | * # <- end | start or end is the wanted one. | ||
232 | */ | ||
233 | |||
234 | if(end<start) /* There are no ways */ | ||
235 | return(NO_WAY); | ||
236 | else if(id<waysx->idata[start]) /* Check key is not before start */ | ||
237 | return(NO_WAY); | ||
238 | else if(id>waysx->idata[end]) /* Check key is not after end */ | ||
239 | return(NO_WAY); | ||
240 | else | ||
241 | { | ||
242 | do | ||
243 | { | ||
244 | mid=(start+end)/2; /* Choose mid point */ | ||
245 | |||
246 | if(waysx->idata[mid]<id) /* Mid point is too low */ | ||
247 | start=mid+1; | ||
248 | else if(waysx->idata[mid]>id) /* Mid point is too high */ | ||
249 | end=mid?(mid-1):mid; | ||
250 | else /* Mid point is correct */ | ||
251 | return(mid); | ||
252 | } | ||
253 | while((end-start)>1); | ||
254 | |||
255 | if(waysx->idata[start]==id) /* Start is correct */ | ||
256 | return(start); | ||
257 | |||
258 | if(waysx->idata[end]==id) /* End is correct */ | ||
259 | return(end); | ||
260 | } | ||
261 | |||
262 | return(NO_WAY); | ||
263 | } | ||
264 | |||
265 | |||
266 | /*++++++++++++++++++++++++++++++++++++++ | ||
267 | amb | 224 | Sort the list of ways. |
268 | amb | 110 | |
269 | amb | 682 | WaysX *waysx The set of ways to process. |
270 | amb | 110 | ++++++++++++++++++++++++++++++++++++++*/ |
271 | |||
272 | amb | 682 | void SortWayList(WaysX *waysx) |
273 | amb | 110 | { |
274 | amb | 1129 | index_t xnumber; |
275 | amb | 555 | int fd; |
276 | amb | 1129 | |
277 | /* Print the start message */ | ||
278 | |||
279 | printf_first("Sorting Ways"); | ||
280 | |||
281 | /* Re-open the file read-only and a new file writeable */ | ||
282 | |||
283 | waysx->fd=ReOpenFile(waysx->filename_tmp); | ||
284 | |||
285 | DeleteFile(waysx->filename_tmp); | ||
286 | |||
287 | fd=OpenFileNew(waysx->filename_tmp); | ||
288 | |||
289 | /* Sort the ways by ID and index them */ | ||
290 | |||
291 | xnumber=waysx->number; | ||
292 | |||
293 | waysx->number=filesort_vary(waysx->fd,fd,NULL, | ||
294 | (int (*)(const void*,const void*))sort_by_id, | ||
295 | amb | 1135 | (int (*)(void*,index_t))deduplicate_by_id); |
296 | amb | 1129 | |
297 | /* Close the files */ | ||
298 | |||
299 | waysx->fd=CloseFile(waysx->fd); | ||
300 | CloseFile(fd); | ||
301 | |||
302 | /* Print the final message */ | ||
303 | |||
304 | printf_last("Sorted Ways: Ways=%"Pindex_t" Duplicates=%"Pindex_t,xnumber,xnumber-waysx->number); | ||
305 | } | ||
306 | |||
307 | |||
308 | /*++++++++++++++++++++++++++++++++++++++ | ||
309 | amb | 1160 | Sort the ways into id order. |
310 | |||
311 | int sort_by_id Returns the comparison of the id fields. | ||
312 | |||
313 | WayX *a The first extended way. | ||
314 | |||
315 | WayX *b The second extended way. | ||
316 | ++++++++++++++++++++++++++++++++++++++*/ | ||
317 | |||
318 | static int sort_by_id(WayX *a,WayX *b) | ||
319 | { | ||
320 | way_t a_id=a->id; | ||
321 | way_t b_id=b->id; | ||
322 | |||
323 | if(a_id<b_id) | ||
324 | return(-1); | ||
325 | else if(a_id>b_id) | ||
326 | return(1); | ||
327 | else | ||
328 | return(-FILESORT_PRESERVE_ORDER(a,b)); /* latest version first */ | ||
329 | } | ||
330 | |||
331 | |||
332 | /*++++++++++++++++++++++++++++++++++++++ | ||
333 | Discard duplicate ways. | ||
334 | |||
335 | int deduplicate_by_id Return 1 if the value is to be kept, otherwise 0. | ||
336 | |||
337 | WayX *wayx The extended way. | ||
338 | |||
339 | index_t index The number of sorted ways that have already been written to the output file. | ||
340 | ++++++++++++++++++++++++++++++++++++++*/ | ||
341 | |||
342 | static int deduplicate_by_id(WayX *wayx,index_t index) | ||
343 | { | ||
344 | static way_t previd=NO_WAY_ID; | ||
345 | |||
346 | if(wayx->id!=previd) | ||
347 | { | ||
348 | previd=wayx->id; | ||
349 | |||
350 | if(wayx->way.type==WAY_DELETED) | ||
351 | return(0); | ||
352 | else | ||
353 | return(1); | ||
354 | } | ||
355 | else | ||
356 | { | ||
357 | if(!option_changes) | ||
358 | logerror("Way %"Pway_t" is duplicated.\n",wayx->id); | ||
359 | |||
360 | return(0); | ||
361 | } | ||
362 | } | ||
363 | |||
364 | |||
365 | /*++++++++++++++++++++++++++++++++++++++ | ||
366 | amb | 1129 | Extract the way names from the ways and reference the list of names from the ways. |
367 | |||
368 | WaysX *waysx The set of ways to process. | ||
369 | amb | 1136 | |
370 | int preserve If set to 1 then keep the old data file otherwise delete it. | ||
371 | amb | 1129 | ++++++++++++++++++++++++++++++++++++++*/ |
372 | |||
373 | amb | 1136 | void ExtractWayNames(WaysX *waysx,int preserve) |
374 | amb | 1129 | { |
375 | index_t i; | ||
376 | int fd; | ||
377 | amb | 310 | char *names[2]={NULL,NULL}; |
378 | int namelen[2]={0,0}; | ||
379 | amb | 499 | int nnames=0; |
380 | amb | 310 | uint32_t lastlength=0; |
381 | amb | 110 | |
382 | amb | 266 | /* Print the start message */ |
383 | |||
384 | amb | 519 | printf_first("Sorting Ways by Name"); |
385 | amb | 110 | |
386 | amb | 555 | /* Re-open the file read-only and a new file writeable */ |
387 | |||
388 | amb | 1120 | waysx->fd=ReOpenFile(waysx->filename_tmp); |
389 | amb | 216 | |
390 | amb | 1136 | if(preserve) |
391 | RenameFile(waysx->filename_tmp,waysx->filename); | ||
392 | else | ||
393 | DeleteFile(waysx->filename_tmp); | ||
394 | amb | 262 | |
395 | amb | 1120 | fd=OpenFileNew(waysx->filename_tmp); |
396 | amb | 310 | |
397 | amb | 499 | /* Sort the ways to allow separating the names */ |
398 | amb | 310 | |
399 | amb | 1106 | filesort_vary(waysx->fd,fd,NULL, |
400 | amb | 1129 | (int (*)(const void*,const void*))sort_by_name, |
401 | amb | 1106 | NULL); |
402 | amb | 310 | |
403 | /* Close the files */ | ||
404 | |||
405 | amb | 612 | waysx->fd=CloseFile(waysx->fd); |
406 | amb | 310 | CloseFile(fd); |
407 | |||
408 | /* Print the final message */ | ||
409 | |||
410 | amb | 790 | printf_last("Sorted Ways by Name: Ways=%"Pindex_t,waysx->number); |
411 | amb | 310 | |
412 | |||
413 | /* Print the start message */ | ||
414 | |||
415 | amb | 519 | printf_first("Separating Way Names: Ways=0 Names=0"); |
416 | amb | 310 | |
417 | amb | 555 | /* Re-open the file read-only and new files writeable */ |
418 | amb | 310 | |
419 | amb | 1120 | waysx->fd=ReOpenFile(waysx->filename_tmp); |
420 | amb | 310 | |
421 | amb | 1120 | DeleteFile(waysx->filename_tmp); |
422 | amb | 262 | |
423 | amb | 1120 | fd=OpenFileNew(waysx->filename_tmp); |
424 | amb | 272 | |
425 | amb | 1120 | waysx->nfd=OpenFileNew(waysx->nfilename_tmp); |
426 | amb | 555 | |
427 | amb | 499 | /* Copy from the single file into two files */ |
428 | amb | 310 | |
429 | amb | 650 | for(i=0;i<waysx->number;i++) |
430 | amb | 310 | { |
431 | WayX wayx; | ||
432 | amb | 311 | FILESORT_VARINT size; |
433 | amb | 310 | |
434 | amb | 311 | ReadFile(waysx->fd,&size,FILESORT_VARSIZE); |
435 | amb | 310 | |
436 | if(namelen[nnames%2]<size) | ||
437 | names[nnames%2]=(char*)realloc((void*)names[nnames%2],namelen[nnames%2]=size); | ||
438 | |||
439 | ReadFile(waysx->fd,&wayx,sizeof(WayX)); | ||
440 | ReadFile(waysx->fd,names[nnames%2],size-sizeof(WayX)); | ||
441 | |||
442 | if(nnames==0 || strcmp(names[0],names[1])) | ||
443 | { | ||
444 | amb | 555 | WriteFile(waysx->nfd,names[nnames%2],size-sizeof(WayX)); |
445 | amb | 310 | |
446 | lastlength=waysx->nlength; | ||
447 | waysx->nlength+=size-sizeof(WayX); | ||
448 | |||
449 | nnames++; | ||
450 | } | ||
451 | |||
452 | wayx.way.name=lastlength; | ||
453 | |||
454 | WriteFile(fd,&wayx,sizeof(WayX)); | ||
455 | |||
456 | amb | 757 | if(!((i+1)%1000)) |
457 | amb | 790 | printf_middle("Separating Way Names: Ways=%"Pindex_t" Names=%"Pindex_t,i+1,nnames); |
458 | amb | 310 | } |
459 | |||
460 | if(names[0]) free(names[0]); | ||
461 | if(names[1]) free(names[1]); | ||
462 | |||
463 | /* Close the files */ | ||
464 | |||
465 | amb | 612 | waysx->fd=CloseFile(waysx->fd); |
466 | amb | 310 | CloseFile(fd); |
467 | |||
468 | amb | 612 | waysx->nfd=CloseFile(waysx->nfd); |
469 | amb | 310 | |
470 | /* Print the final message */ | ||
471 | |||
472 | amb | 1092 | printf_last("Separated Way Names: Ways=%"Pindex_t" Names=%"Pindex_t,waysx->number,nnames); |
473 | amb | 310 | |
474 | |||
475 | /* Print the start message */ | ||
476 | |||
477 | amb | 519 | printf_first("Sorting Ways"); |
478 | amb | 310 | |
479 | amb | 555 | /* Re-open the file read-only and a new file writeable */ |
480 | amb | 310 | |
481 | amb | 1120 | waysx->fd=ReOpenFile(waysx->filename_tmp); |
482 | amb | 310 | |
483 | amb | 1120 | DeleteFile(waysx->filename_tmp); |
484 | amb | 310 | |
485 | amb | 1120 | fd=OpenFileNew(waysx->filename_tmp); |
486 | amb | 310 | |
487 | amb | 1135 | /* Allocate the array of indexes */ |
488 | |||
489 | waysx->idata=(way_t*)malloc(waysx->number*sizeof(way_t)); | ||
490 | |||
491 | amb | 1166 | logassert(waysx->idata,"Failed to allocate memory (try using slim mode?)"); /* Check malloc() worked */ |
492 | amb | 1135 | |
493 | amb | 1129 | /* Sort the ways by ID */ |
494 | amb | 272 | |
495 | amb | 1135 | sortwaysx=waysx; |
496 | |||
497 | amb | 1129 | filesort_fixed(waysx->fd,fd,sizeof(WayX),NULL, |
498 | (int (*)(const void*,const void*))sort_by_id, | ||
499 | amb | 1135 | (int (*)(void*,index_t))index_by_id); |
500 | amb | 262 | |
501 | amb | 555 | /* Close the files */ |
502 | amb | 262 | |
503 | amb | 612 | waysx->fd=CloseFile(waysx->fd); |
504 | amb | 262 | CloseFile(fd); |
505 | |||
506 | amb | 266 | /* Print the final message */ |
507 | |||
508 | amb | 1129 | printf_last("Sorted Ways: Ways=%"Pindex_t,waysx->number); |
509 | amb | 499 | } |
510 | |||
511 | |||
512 | /*++++++++++++++++++++++++++++++++++++++ | ||
513 | amb | 1160 | Sort the ways into name order and then id order. |
514 | |||
515 | int sort_by_name Returns the comparison of the name fields. | ||
516 | |||
517 | WayX *a The first extended Way. | ||
518 | |||
519 | WayX *b The second extended Way. | ||
520 | ++++++++++++++++++++++++++++++++++++++*/ | ||
521 | |||
522 | static int sort_by_name(WayX *a,WayX *b) | ||
523 | { | ||
524 | int compare; | ||
525 | char *a_name=(char*)a+sizeof(WayX); | ||
526 | char *b_name=(char*)b+sizeof(WayX); | ||
527 | |||
528 | compare=strcmp(a_name,b_name); | ||
529 | |||
530 | if(compare) | ||
531 | return(compare); | ||
532 | else | ||
533 | return(FILESORT_PRESERVE_ORDER(a,b)); | ||
534 | } | ||
535 | |||
536 | |||
537 | /*++++++++++++++++++++++++++++++++++++++ | ||
538 | Create the index of identifiers. | ||
539 | |||
540 | int index_by_id Return 1 if the value is to be kept, otherwise 0. | ||
541 | |||
542 | WayX *wayx The extended way. | ||
543 | |||
544 | index_t index The number of sorted ways that have already been written to the output file. | ||
545 | ++++++++++++++++++++++++++++++++++++++*/ | ||
546 | |||
547 | static int index_by_id(WayX *wayx,index_t index) | ||
548 | { | ||
549 | sortwaysx->idata[index]=wayx->id; | ||
550 | |||
551 | return(1); | ||
552 | } | ||
553 | |||
554 | |||
555 | /*++++++++++++++++++++++++++++++++++++++ | ||
556 | amb | 1100 | Compact the way list, removing duplicated ways and unused ways. |
557 | amb | 499 | |
558 | amb | 1100 | WaysX *waysx The set of ways to process. |
559 | amb | 1092 | |
560 | amb | 1100 | SegmentsX *segmentsx The set of segments to check. |
561 | amb | 499 | ++++++++++++++++++++++++++++++++++++++*/ |
562 | |||
563 | amb | 1100 | void CompactWayList(WaysX *waysx,SegmentsX *segmentsx) |
564 | amb | 499 | { |
565 | amb | 1100 | int fd; |
566 | index_t cnumber; | ||
567 | amb | 499 | |
568 | /* Print the start message */ | ||
569 | |||
570 | amb | 1094 | printf_first("Sorting Ways and Compacting"); |
571 | amb | 1092 | |
572 | amb | 1093 | /* Allocate the array of indexes */ |
573 | amb | 499 | |
574 | amb | 1093 | waysx->cdata=(index_t*)malloc(waysx->number*sizeof(index_t)); |
575 | amb | 499 | |
576 | amb | 1166 | logassert(waysx->cdata,"Failed to allocate memory (try using slim mode?)"); /* Check malloc() worked */ |
577 | amb | 499 | |
578 | amb | 1100 | /* Re-open the file read-only and a new file writeable */ |
579 | amb | 499 | |
580 | amb | 1120 | waysx->fd=ReOpenFile(waysx->filename_tmp); |
581 | amb | 1093 | |
582 | amb | 1120 | DeleteFile(waysx->filename_tmp); |
583 | amb | 1100 | |
584 | amb | 1120 | fd=OpenFileNew(waysx->filename_tmp); |
585 | amb | 1100 | |
586 | amb | 1094 | /* Sort the ways to allow compacting according to the properties */ |
587 | amb | 499 | |
588 | amb | 1094 | sortwaysx=waysx; |
589 | amb | 1100 | sortsegmentsx=segmentsx; |
590 | amb | 499 | |
591 | amb | 1114 | cnumber=filesort_fixed(waysx->fd,fd,sizeof(WayX),(int (*)(void*,index_t))delete_unused, |
592 | amb | 1106 | (int (*)(const void*,const void*))sort_by_name_and_prop_and_id, |
593 | (int (*)(void*,index_t))deduplicate_and_index_by_compact_id); | ||
594 | amb | 499 | |
595 | amb | 1100 | /* Close the files */ |
596 | amb | 499 | |
597 | amb | 1094 | waysx->fd=CloseFile(waysx->fd); |
598 | amb | 1100 | CloseFile(fd); |
599 | amb | 499 | |
600 | /* Print the final message */ | ||
601 | |||
602 | amb | 1100 | printf_last("Sorted and Compacted Ways: Ways=%"Pindex_t" Unique=%"Pindex_t,waysx->number,cnumber); |
603 | waysx->number=cnumber; | ||
604 | amb | 499 | |
605 | amb | 1100 | free(segmentsx->usedway); |
606 | segmentsx->usedway=NULL; | ||
607 | amb | 224 | } |
608 | |||
609 | |||
610 | /*++++++++++++++++++++++++++++++++++++++ | ||
611 | amb | 1160 | Delete the ways that are no longer being used. |
612 | amb | 224 | |
613 | amb | 1160 | int delete_unused Return 1 if the value is to be kept, otherwise 0. |
614 | amb | 262 | |
615 | amb | 1160 | WayX *wayx The extended way. |
616 | amb | 262 | |
617 | amb | 1160 | index_t index The number of unsorted ways that have been read from the input file. |
618 | amb | 262 | ++++++++++++++++++++++++++++++++++++++*/ |
619 | |||
620 | amb | 1160 | static int delete_unused(WayX *wayx,index_t index) |
621 | amb | 262 | { |
622 | amb | 1160 | if(sortsegmentsx && !IsBitSet(sortsegmentsx->usedway,index)) |
623 | { | ||
624 | sortwaysx->cdata[index]=NO_WAY; | ||
625 | amb | 262 | |
626 | amb | 1160 | return(0); |
627 | } | ||
628 | amb | 262 | else |
629 | amb | 1160 | { |
630 | wayx->id=index; | ||
631 | amb | 262 | |
632 | amb | 1160 | return(1); |
633 | } | ||
634 | amb | 499 | } |
635 | |||
636 | |||
637 | /*++++++++++++++++++++++++++++++++++++++ | ||
638 | Sort the ways into name, properties and id order. | ||
639 | |||
640 | int sort_by_name_and_prop_and_id Returns the comparison of the name, properties and id fields. | ||
641 | |||
642 | WayX *a The first extended Way. | ||
643 | |||
644 | WayX *b The second extended Way. | ||
645 | ++++++++++++++++++++++++++++++++++++++*/ | ||
646 | |||
647 | static int sort_by_name_and_prop_and_id(WayX *a,WayX *b) | ||
648 | { | ||
649 | int compare; | ||
650 | index_t a_name=a->way.name; | ||
651 | index_t b_name=b->way.name; | ||
652 | |||
653 | if(a_name<b_name) | ||
654 | return(-1); | ||
655 | else if(a_name>b_name) | ||
656 | return(1); | ||
657 | |||
658 | amb | 310 | compare=WaysCompare(&a->way,&b->way); |
659 | |||
660 | if(compare) | ||
661 | return(compare); | ||
662 | |||
663 | return(sort_by_id(a,b)); | ||
664 | } | ||
665 | |||
666 | |||
667 | /*++++++++++++++++++++++++++++++++++++++ | ||
668 | amb | 1113 | Create the index of compacted Way identifiers and ignore Ways with duplicated properties. |
669 | |||
670 | int deduplicate_and_index_by_compact_id Return 1 if the value is to be kept, otherwise 0. | ||
671 | |||
672 | WayX *wayx The extended way. | ||
673 | |||
674 | index_t index The number of sorted ways that have already been written to the output file. | ||
675 | ++++++++++++++++++++++++++++++++++++++*/ | ||
676 | |||
677 | static int deduplicate_and_index_by_compact_id(WayX *wayx,index_t index) | ||
678 | { | ||
679 | static Way lastway; | ||
680 | |||
681 | amb | 1100 | if(index==0 || wayx->way.name!=lastway.name || WaysCompare(&lastway,&wayx->way)) |
682 | amb | 1094 | { |
683 | amb | 1100 | lastway=wayx->way; |
684 | amb | 1094 | |
685 | amb | 1100 | sortwaysx->cdata[wayx->id]=index; |
686 | amb | 1094 | |
687 | amb | 1100 | wayx->id=index; |
688 | |||
689 | return(1); | ||
690 | amb | 1094 | } |
691 | amb | 1100 | else |
692 | { | ||
693 | sortwaysx->cdata[wayx->id]=index-1; | ||
694 | amb | 1094 | |
695 | amb | 1100 | return(0); |
696 | } | ||
697 | amb | 1094 | } |
698 | |||
699 | |||
700 | /*++++++++++++++++++++++++++++++++++++++ | ||
701 | amb | 285 | Save the way list to a file. |
702 | |||
703 | amb | 682 | WaysX *waysx The set of ways to save. |
704 | amb | 285 | |
705 | const char *filename The name of the file to save. | ||
706 | ++++++++++++++++++++++++++++++++++++++*/ | ||
707 | |||
708 | amb | 682 | void SaveWayList(WaysX *waysx,const char *filename) |
709 | amb | 285 | { |
710 | index_t i; | ||
711 | amb | 555 | int fd; |
712 | amb | 310 | int position=0; |
713 | amb | 1104 | WayX wayx; |
714 | amb | 499 | WaysFile waysfile={0}; |
715 | amb | 529 | highways_t highways=0; |
716 | transports_t allow=0; | ||
717 | amb | 530 | properties_t props=0; |
718 | amb | 285 | |
719 | amb | 461 | /* Print the start message */ |
720 | |||
721 | amb | 519 | printf_first("Writing Ways: Ways=0"); |
722 | amb | 285 | |
723 | amb | 1104 | /* Re-open the files */ |
724 | amb | 461 | |
725 | amb | 1120 | waysx->fd=ReOpenFile(waysx->filename_tmp); |
726 | waysx->nfd=ReOpenFile(waysx->nfilename_tmp); | ||
727 | amb | 285 | |
728 | amb | 461 | /* Write out the ways data */ |
729 | amb | 285 | |
730 | amb | 502 | fd=OpenFileNew(filename); |
731 | amb | 285 | |
732 | amb | 461 | SeekFile(fd,sizeof(WaysFile)); |
733 | amb | 285 | |
734 | for(i=0;i<waysx->number;i++) | ||
735 | { | ||
736 | amb | 1104 | ReadFile(waysx->fd,&wayx,sizeof(WayX)); |
737 | amb | 285 | |
738 | amb | 1104 | highways|=HIGHWAYS(wayx.way.type); |
739 | allow |=wayx.way.allow; | ||
740 | props |=wayx.way.props; | ||
741 | amb | 398 | |
742 | amb | 1104 | WriteFile(fd,&wayx.way,sizeof(Way)); |
743 | amb | 309 | |
744 | amb | 757 | if(!((i+1)%1000)) |
745 | amb | 790 | printf_middle("Writing Ways: Ways=%"Pindex_t,i+1); |
746 | amb | 285 | } |
747 | |||
748 | amb | 461 | /* Write out the ways names */ |
749 | amb | 285 | |
750 | amb | 1100 | SeekFile(fd,sizeof(WaysFile)+(off_t)waysx->number*sizeof(Way)); |
751 | amb | 461 | |
752 | amb | 309 | while(position<waysx->nlength) |
753 | { | ||
754 | int len=1024; | ||
755 | char temp[1024]; | ||
756 | |||
757 | if((waysx->nlength-position)<1024) | ||
758 | len=waysx->nlength-position; | ||
759 | |||
760 | amb | 555 | ReadFile(waysx->nfd,temp,len); |
761 | amb | 1104 | |
762 | amb | 309 | WriteFile(fd,temp,len); |
763 | |||
764 | position+=len; | ||
765 | } | ||
766 | |||
767 | amb | 1104 | /* Close the files */ |
768 | amb | 309 | |
769 | amb | 1104 | waysx->fd=CloseFile(waysx->fd); |
770 | amb | 612 | waysx->nfd=CloseFile(waysx->nfd); |
771 | amb | 555 | |
772 | amb | 461 | /* Write out the header structure */ |
773 | |||
774 | amb | 1100 | waysfile.number =waysx->number; |
775 | amb | 461 | |
776 | amb | 526 | waysfile.highways=highways; |
777 | waysfile.allow =allow; | ||
778 | waysfile.props =props; | ||
779 | amb | 461 | |
780 | SeekFile(fd,0); | ||
781 | WriteFile(fd,&waysfile,sizeof(WaysFile)); | ||
782 | |||
783 | amb | 285 | CloseFile(fd); |
784 | |||
785 | amb | 461 | /* Print the final message */ |
786 | |||
787 | amb | 1100 | printf_last("Wrote Ways: Ways=%"Pindex_t,waysx->number); |
788 | amb | 285 | } |
Properties
Name | Value |
---|---|
cvs:description | Extended ways functions. |