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 1297 -
(hide annotations)
(download)
(as text)
Tue May 7 14:41:11 2013 UTC (11 years, 10 months ago) by amb
File MIME type: text/x-csrc
File size: 18338 byte(s)
Tue May 7 14:41:11 2013 UTC (11 years, 10 months ago) by amb
File MIME type: text/x-csrc
File size: 18338 byte(s)
Add cache functions for NodesX, SegmentsX and WaysX to speed up the planetsplitter in slim mode.
1 | amb | 110 | /*************************************** |
2 | Extended Way data type functions. | ||
3 | amb | 151 | |
4 | Part of the Routino routing software. | ||
5 | amb | 110 | ******************/ /****************** |
6 | amb | 1297 | This file Copyright 2008-2013 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 | 680 | /* Local variables */ |
44 | |||
45 | amb | 1094 | /*+ Temporary file-local variables for use by the sort functions. +*/ |
46 | amb | 284 | static WaysX *sortwaysx; |
47 | amb | 1100 | static SegmentsX *sortsegmentsx; |
48 | amb | 284 | |
49 | amb | 1100 | /* Local functions */ |
50 | amb | 680 | |
51 | amb | 499 | static int sort_by_id(WayX *a,WayX *b); |
52 | amb | 1160 | static int deduplicate_by_id(WayX *wayx,index_t index); |
53 | |||
54 | amb | 1129 | static int sort_by_name(WayX *a,WayX *b); |
55 | amb | 1160 | static int index_by_id(WayX *wayx,index_t index); |
56 | amb | 310 | |
57 | amb | 1114 | static int delete_unused(WayX *wayx,index_t index); |
58 | amb | 1160 | static int sort_by_name_and_prop_and_id(WayX *a,WayX *b); |
59 | amb | 1094 | static int deduplicate_and_index_by_compact_id(WayX *wayx,index_t index); |
60 | amb | 272 | |
61 | amb | 110 | |
62 | /*++++++++++++++++++++++++++++++++++++++ | ||
63 | amb | 326 | Allocate a new way list (create a new file or open an existing one). |
64 | amb | 110 | |
65 | WaysX *NewWayList Returns the way list. | ||
66 | amb | 326 | |
67 | amb | 1123 | int append Set to 1 if the file is to be opened for appending. |
68 | |||
69 | amb | 1139 | int readonly Set to 1 if the file is to be opened for reading. |
70 | amb | 110 | ++++++++++++++++++++++++++++++++++++++*/ |
71 | |||
72 | amb | 1158 | WaysX *NewWayList(int append,int readonly) |
73 | amb | 110 | { |
74 | WaysX *waysx; | ||
75 | |||
76 | amb | 213 | waysx=(WaysX*)calloc(1,sizeof(WaysX)); |
77 | amb | 110 | |
78 | amb | 1166 | logassert(waysx,"Failed to allocate memory (try using slim mode?)"); /* Check calloc() worked */ |
79 | amb | 243 | |
80 | amb | 1120 | waysx->filename =(char*)malloc(strlen(option_tmpdirname)+32); |
81 | waysx->filename_tmp=(char*)malloc(strlen(option_tmpdirname)+32); | ||
82 | amb | 216 | |
83 | amb | 1120 | sprintf(waysx->filename ,"%s/waysx.parsed.mem",option_tmpdirname); |
84 | sprintf(waysx->filename_tmp,"%s/waysx.%p.tmp" ,option_tmpdirname,(void*)waysx); | ||
85 | amb | 262 | |
86 | amb | 1123 | if(append || readonly) |
87 | if(ExistsFile(waysx->filename)) | ||
88 | { | ||
89 | off_t size,position=0; | ||
90 | int fd; | ||
91 | amb | 326 | |
92 | amb | 1123 | size=SizeFile(waysx->filename); |
93 | amb | 326 | |
94 | amb | 1123 | fd=ReOpenFile(waysx->filename); |
95 | amb | 326 | |
96 | amb | 1123 | while(position<size) |
97 | { | ||
98 | FILESORT_VARINT waysize; | ||
99 | amb | 326 | |
100 | amb | 1123 | SeekReadFile(fd,&waysize,FILESORT_VARSIZE,position); |
101 | amb | 326 | |
102 | amb | 1123 | waysx->number++; |
103 | position+=waysize+FILESORT_VARSIZE; | ||
104 | } | ||
105 | |||
106 | CloseFile(fd); | ||
107 | amb | 1139 | |
108 | RenameFile(waysx->filename,waysx->filename_tmp); | ||
109 | amb | 326 | } |
110 | |||
111 | amb | 1123 | if(append) |
112 | amb | 1139 | waysx->fd=OpenFileAppend(waysx->filename_tmp); |
113 | amb | 1123 | else if(!readonly) |
114 | amb | 1139 | waysx->fd=OpenFileNew(waysx->filename_tmp); |
115 | amb | 326 | else |
116 | amb | 1123 | waysx->fd=-1; |
117 | amb | 326 | |
118 | amb | 1297 | #if SLIM |
119 | waysx->cache=NewWayXCache(); | ||
120 | #endif | ||
121 | amb | 262 | |
122 | amb | 1297 | |
123 | amb | 1120 | waysx->nfilename_tmp=(char*)malloc(strlen(option_tmpdirname)+32); |
124 | |||
125 | sprintf(waysx->nfilename_tmp,"%s/waynames.%p.tmp",option_tmpdirname,(void*)waysx); | ||
126 | |||
127 | amb | 110 | return(waysx); |
128 | } | ||
129 | |||
130 | |||
131 | /*++++++++++++++++++++++++++++++++++++++ | ||
132 | amb | 226 | Free a way list. |
133 | |||
134 | amb | 681 | WaysX *waysx The set of ways to be freed. |
135 | amb | 1151 | |
136 | amb | 1167 | int keep If set then the results file is to be kept. |
137 | amb | 226 | ++++++++++++++++++++++++++++++++++++++*/ |
138 | |||
139 | amb | 1167 | void FreeWayList(WaysX *waysx,int keep) |
140 | amb | 226 | { |
141 | amb | 1167 | if(keep) |
142 | amb | 1151 | RenameFile(waysx->filename_tmp,waysx->filename); |
143 | else | ||
144 | DeleteFile(waysx->filename_tmp); | ||
145 | amb | 1120 | |
146 | amb | 283 | free(waysx->filename); |
147 | amb | 1120 | free(waysx->filename_tmp); |
148 | amb | 262 | |
149 | amb | 226 | if(waysx->idata) |
150 | free(waysx->idata); | ||
151 | |||
152 | amb | 1093 | if(waysx->cdata) |
153 | free(waysx->cdata); | ||
154 | |||
155 | amb | 1120 | DeleteFile(waysx->nfilename_tmp); |
156 | amb | 326 | |
157 | amb | 1120 | free(waysx->nfilename_tmp); |
158 | amb | 226 | |
159 | amb | 1297 | #if SLIM |
160 | DeleteWayXCache(waysx->cache); | ||
161 | #endif | ||
162 | |||
163 | amb | 226 | free(waysx); |
164 | } | ||
165 | |||
166 | |||
167 | /*++++++++++++++++++++++++++++++++++++++ | ||
168 | amb | 493 | Append a single way to an unsorted way list. |
169 | amb | 203 | |
170 | amb | 682 | WaysX *waysx The set of ways to process. |
171 | amb | 110 | |
172 | amb | 262 | way_t id The ID of the way. |
173 | amb | 110 | |
174 | amb | 262 | Way *way The way data itself. |
175 | amb | 110 | |
176 | amb | 262 | const char *name The name or reference of the way. |
177 | ++++++++++++++++++++++++++++++++++++++*/ | ||
178 | amb | 110 | |
179 | amb | 1161 | void AppendWayList(WaysX *waysx,way_t id,Way *way,const char *name) |
180 | amb | 262 | { |
181 | WayX wayx; | ||
182 | amb | 311 | FILESORT_VARINT size; |
183 | amb | 110 | |
184 | amb | 262 | wayx.id=id; |
185 | wayx.way=*way; | ||
186 | |||
187 | amb | 310 | size=sizeof(WayX)+strlen(name)+1; |
188 | |||
189 | amb | 311 | WriteFile(waysx->fd,&size,FILESORT_VARSIZE); |
190 | amb | 262 | WriteFile(waysx->fd,&wayx,sizeof(WayX)); |
191 | amb | 310 | WriteFile(waysx->fd,name,strlen(name)+1); |
192 | amb | 262 | |
193 | amb | 650 | waysx->number++; |
194 | amb | 466 | |
195 | amb | 1166 | logassert(waysx->number!=0,"Too many ways (change index_t to 64-bits?)"); /* Zero marks the high-water mark for ways. */ |
196 | amb | 110 | } |
197 | |||
198 | |||
199 | /*++++++++++++++++++++++++++++++++++++++ | ||
200 | amb | 1120 | Finish appending ways and change the filename over. |
201 | |||
202 | WaysX *waysx The ways that have been appended. | ||
203 | ++++++++++++++++++++++++++++++++++++++*/ | ||
204 | |||
205 | amb | 1151 | void FinishWayList(WaysX *waysx) |
206 | amb | 1120 | { |
207 | amb | 1136 | if(waysx->fd!=-1) |
208 | waysx->fd=CloseFile(waysx->fd); | ||
209 | amb | 1120 | } |
210 | |||
211 | |||
212 | /*++++++++++++++++++++++++++++++++++++++ | ||
213 | amb | 1160 | Find a particular way index. |
214 | |||
215 | index_t IndexWayX Returns the index of the extended way with the specified id. | ||
216 | |||
217 | WaysX *waysx The set of ways to process. | ||
218 | |||
219 | way_t id The way id to look for. | ||
220 | ++++++++++++++++++++++++++++++++++++++*/ | ||
221 | |||
222 | index_t IndexWayX(WaysX *waysx,way_t id) | ||
223 | { | ||
224 | index_t start=0; | ||
225 | index_t end=waysx->number-1; | ||
226 | index_t mid; | ||
227 | |||
228 | amb | 1198 | if(waysx->number==0) /* There are no ways */ |
229 | return(NO_WAY); | ||
230 | |||
231 | if(id<waysx->idata[start]) /* Key is before start */ | ||
232 | return(NO_WAY); | ||
233 | |||
234 | if(id>waysx->idata[end]) /* Key is after end */ | ||
235 | return(NO_WAY); | ||
236 | |||
237 | amb | 1160 | /* Binary search - search key exact match only is required. |
238 | * | ||
239 | * # <- start | Check mid and move start or end if it doesn't match | ||
240 | * # | | ||
241 | * # | Since an exact match is wanted we can set end=mid-1 | ||
242 | * # <- mid | or start=mid+1 because we know that mid doesn't match. | ||
243 | * # | | ||
244 | * # | Eventually either end=start or end=start+1 and one of | ||
245 | * # <- end | start or end is the wanted one. | ||
246 | */ | ||
247 | |||
248 | amb | 1198 | do |
249 | amb | 1160 | { |
250 | amb | 1198 | mid=(start+end)/2; /* Choose mid point */ |
251 | amb | 1160 | |
252 | amb | 1198 | if(waysx->idata[mid]<id) /* Mid point is too low */ |
253 | start=mid+1; | ||
254 | else if(waysx->idata[mid]>id) /* Mid point is too high */ | ||
255 | end=mid?(mid-1):mid; | ||
256 | else /* Mid point is correct */ | ||
257 | return(mid); | ||
258 | } | ||
259 | while((end-start)>1); | ||
260 | amb | 1160 | |
261 | amb | 1198 | if(waysx->idata[start]==id) /* Start is correct */ |
262 | return(start); | ||
263 | amb | 1160 | |
264 | amb | 1198 | if(waysx->idata[end]==id) /* End is correct */ |
265 | return(end); | ||
266 | amb | 1160 | |
267 | return(NO_WAY); | ||
268 | } | ||
269 | |||
270 | |||
271 | /*++++++++++++++++++++++++++++++++++++++ | ||
272 | amb | 224 | Sort the list of ways. |
273 | amb | 110 | |
274 | amb | 682 | WaysX *waysx The set of ways to process. |
275 | amb | 110 | ++++++++++++++++++++++++++++++++++++++*/ |
276 | |||
277 | amb | 682 | void SortWayList(WaysX *waysx) |
278 | amb | 110 | { |
279 | amb | 1129 | index_t xnumber; |
280 | amb | 555 | int fd; |
281 | amb | 1129 | |
282 | /* Print the start message */ | ||
283 | |||
284 | printf_first("Sorting Ways"); | ||
285 | |||
286 | /* Re-open the file read-only and a new file writeable */ | ||
287 | |||
288 | waysx->fd=ReOpenFile(waysx->filename_tmp); | ||
289 | |||
290 | DeleteFile(waysx->filename_tmp); | ||
291 | |||
292 | fd=OpenFileNew(waysx->filename_tmp); | ||
293 | |||
294 | /* Sort the ways by ID and index them */ | ||
295 | |||
296 | xnumber=waysx->number; | ||
297 | |||
298 | waysx->number=filesort_vary(waysx->fd,fd,NULL, | ||
299 | (int (*)(const void*,const void*))sort_by_id, | ||
300 | amb | 1135 | (int (*)(void*,index_t))deduplicate_by_id); |
301 | amb | 1129 | |
302 | /* Close the files */ | ||
303 | |||
304 | waysx->fd=CloseFile(waysx->fd); | ||
305 | CloseFile(fd); | ||
306 | |||
307 | /* Print the final message */ | ||
308 | |||
309 | printf_last("Sorted Ways: Ways=%"Pindex_t" Duplicates=%"Pindex_t,xnumber,xnumber-waysx->number); | ||
310 | } | ||
311 | |||
312 | |||
313 | /*++++++++++++++++++++++++++++++++++++++ | ||
314 | amb | 1160 | Sort the ways into id order. |
315 | |||
316 | int sort_by_id Returns the comparison of the id fields. | ||
317 | |||
318 | WayX *a The first extended way. | ||
319 | |||
320 | WayX *b The second extended way. | ||
321 | ++++++++++++++++++++++++++++++++++++++*/ | ||
322 | |||
323 | static int sort_by_id(WayX *a,WayX *b) | ||
324 | { | ||
325 | way_t a_id=a->id; | ||
326 | way_t b_id=b->id; | ||
327 | |||
328 | if(a_id<b_id) | ||
329 | return(-1); | ||
330 | else if(a_id>b_id) | ||
331 | return(1); | ||
332 | else | ||
333 | return(-FILESORT_PRESERVE_ORDER(a,b)); /* latest version first */ | ||
334 | } | ||
335 | |||
336 | |||
337 | /*++++++++++++++++++++++++++++++++++++++ | ||
338 | Discard duplicate ways. | ||
339 | |||
340 | int deduplicate_by_id Return 1 if the value is to be kept, otherwise 0. | ||
341 | |||
342 | WayX *wayx The extended way. | ||
343 | |||
344 | index_t index The number of sorted ways that have already been written to the output file. | ||
345 | ++++++++++++++++++++++++++++++++++++++*/ | ||
346 | |||
347 | static int deduplicate_by_id(WayX *wayx,index_t index) | ||
348 | { | ||
349 | static way_t previd=NO_WAY_ID; | ||
350 | |||
351 | if(wayx->id!=previd) | ||
352 | { | ||
353 | previd=wayx->id; | ||
354 | |||
355 | if(wayx->way.type==WAY_DELETED) | ||
356 | return(0); | ||
357 | else | ||
358 | return(1); | ||
359 | } | ||
360 | else | ||
361 | return(0); | ||
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 | amb | 1167 | int keep If set to 1 then keep the old data file otherwise delete it. |
371 | amb | 1129 | ++++++++++++++++++++++++++++++++++++++*/ |
372 | |||
373 | amb | 1167 | void ExtractWayNames(WaysX *waysx,int keep) |
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 | 1167 | if(keep) |
391 | amb | 1136 | 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 | amb | 1208 | if(waysx->number==0) |
569 | return; | ||
570 | |||
571 | amb | 499 | /* Print the start message */ |
572 | |||
573 | amb | 1094 | printf_first("Sorting Ways and Compacting"); |
574 | amb | 1092 | |
575 | amb | 1093 | /* Allocate the array of indexes */ |
576 | amb | 499 | |
577 | amb | 1093 | waysx->cdata=(index_t*)malloc(waysx->number*sizeof(index_t)); |
578 | amb | 499 | |
579 | amb | 1166 | logassert(waysx->cdata,"Failed to allocate memory (try using slim mode?)"); /* Check malloc() worked */ |
580 | amb | 499 | |
581 | amb | 1100 | /* Re-open the file read-only and a new file writeable */ |
582 | amb | 499 | |
583 | amb | 1120 | waysx->fd=ReOpenFile(waysx->filename_tmp); |
584 | amb | 1093 | |
585 | amb | 1120 | DeleteFile(waysx->filename_tmp); |
586 | amb | 1100 | |
587 | amb | 1120 | fd=OpenFileNew(waysx->filename_tmp); |
588 | amb | 1100 | |
589 | amb | 1094 | /* Sort the ways to allow compacting according to the properties */ |
590 | amb | 499 | |
591 | amb | 1094 | sortwaysx=waysx; |
592 | amb | 1100 | sortsegmentsx=segmentsx; |
593 | amb | 499 | |
594 | amb | 1114 | cnumber=filesort_fixed(waysx->fd,fd,sizeof(WayX),(int (*)(void*,index_t))delete_unused, |
595 | amb | 1106 | (int (*)(const void*,const void*))sort_by_name_and_prop_and_id, |
596 | (int (*)(void*,index_t))deduplicate_and_index_by_compact_id); | ||
597 | amb | 499 | |
598 | amb | 1100 | /* Close the files */ |
599 | amb | 499 | |
600 | amb | 1094 | waysx->fd=CloseFile(waysx->fd); |
601 | amb | 1100 | CloseFile(fd); |
602 | amb | 499 | |
603 | /* Print the final message */ | ||
604 | |||
605 | amb | 1100 | printf_last("Sorted and Compacted Ways: Ways=%"Pindex_t" Unique=%"Pindex_t,waysx->number,cnumber); |
606 | waysx->number=cnumber; | ||
607 | amb | 499 | |
608 | amb | 1100 | free(segmentsx->usedway); |
609 | segmentsx->usedway=NULL; | ||
610 | amb | 224 | } |
611 | |||
612 | |||
613 | /*++++++++++++++++++++++++++++++++++++++ | ||
614 | amb | 1160 | Delete the ways that are no longer being used. |
615 | amb | 224 | |
616 | amb | 1160 | int delete_unused Return 1 if the value is to be kept, otherwise 0. |
617 | amb | 262 | |
618 | amb | 1160 | WayX *wayx The extended way. |
619 | amb | 262 | |
620 | amb | 1160 | index_t index The number of unsorted ways that have been read from the input file. |
621 | amb | 262 | ++++++++++++++++++++++++++++++++++++++*/ |
622 | |||
623 | amb | 1160 | static int delete_unused(WayX *wayx,index_t index) |
624 | amb | 262 | { |
625 | amb | 1160 | if(sortsegmentsx && !IsBitSet(sortsegmentsx->usedway,index)) |
626 | { | ||
627 | sortwaysx->cdata[index]=NO_WAY; | ||
628 | amb | 262 | |
629 | amb | 1160 | return(0); |
630 | } | ||
631 | amb | 262 | else |
632 | amb | 1160 | { |
633 | wayx->id=index; | ||
634 | amb | 262 | |
635 | amb | 1160 | return(1); |
636 | } | ||
637 | amb | 499 | } |
638 | |||
639 | |||
640 | /*++++++++++++++++++++++++++++++++++++++ | ||
641 | Sort the ways into name, properties and id order. | ||
642 | |||
643 | int sort_by_name_and_prop_and_id Returns the comparison of the name, properties and id fields. | ||
644 | |||
645 | WayX *a The first extended Way. | ||
646 | |||
647 | WayX *b The second extended Way. | ||
648 | ++++++++++++++++++++++++++++++++++++++*/ | ||
649 | |||
650 | static int sort_by_name_and_prop_and_id(WayX *a,WayX *b) | ||
651 | { | ||
652 | int compare; | ||
653 | index_t a_name=a->way.name; | ||
654 | index_t b_name=b->way.name; | ||
655 | |||
656 | if(a_name<b_name) | ||
657 | return(-1); | ||
658 | else if(a_name>b_name) | ||
659 | return(1); | ||
660 | |||
661 | amb | 310 | compare=WaysCompare(&a->way,&b->way); |
662 | |||
663 | if(compare) | ||
664 | return(compare); | ||
665 | |||
666 | return(sort_by_id(a,b)); | ||
667 | } | ||
668 | |||
669 | |||
670 | /*++++++++++++++++++++++++++++++++++++++ | ||
671 | amb | 1113 | Create the index of compacted Way identifiers and ignore Ways with duplicated properties. |
672 | |||
673 | int deduplicate_and_index_by_compact_id Return 1 if the value is to be kept, otherwise 0. | ||
674 | |||
675 | WayX *wayx The extended way. | ||
676 | |||
677 | index_t index The number of sorted ways that have already been written to the output file. | ||
678 | ++++++++++++++++++++++++++++++++++++++*/ | ||
679 | |||
680 | static int deduplicate_and_index_by_compact_id(WayX *wayx,index_t index) | ||
681 | { | ||
682 | static Way lastway; | ||
683 | |||
684 | amb | 1100 | if(index==0 || wayx->way.name!=lastway.name || WaysCompare(&lastway,&wayx->way)) |
685 | amb | 1094 | { |
686 | amb | 1100 | lastway=wayx->way; |
687 | amb | 1094 | |
688 | amb | 1100 | sortwaysx->cdata[wayx->id]=index; |
689 | amb | 1094 | |
690 | amb | 1100 | wayx->id=index; |
691 | |||
692 | return(1); | ||
693 | amb | 1094 | } |
694 | amb | 1100 | else |
695 | { | ||
696 | sortwaysx->cdata[wayx->id]=index-1; | ||
697 | amb | 1094 | |
698 | amb | 1100 | return(0); |
699 | } | ||
700 | amb | 1094 | } |
701 | |||
702 | |||
703 | /*++++++++++++++++++++++++++++++++++++++ | ||
704 | amb | 285 | Save the way list to a file. |
705 | |||
706 | amb | 682 | WaysX *waysx The set of ways to save. |
707 | amb | 285 | |
708 | const char *filename The name of the file to save. | ||
709 | ++++++++++++++++++++++++++++++++++++++*/ | ||
710 | |||
711 | amb | 682 | void SaveWayList(WaysX *waysx,const char *filename) |
712 | amb | 285 | { |
713 | index_t i; | ||
714 | amb | 555 | int fd; |
715 | amb | 310 | int position=0; |
716 | amb | 1104 | WayX wayx; |
717 | amb | 499 | WaysFile waysfile={0}; |
718 | amb | 529 | highways_t highways=0; |
719 | transports_t allow=0; | ||
720 | amb | 530 | properties_t props=0; |
721 | amb | 285 | |
722 | amb | 461 | /* Print the start message */ |
723 | |||
724 | amb | 519 | printf_first("Writing Ways: Ways=0"); |
725 | amb | 285 | |
726 | amb | 1104 | /* Re-open the files */ |
727 | amb | 461 | |
728 | amb | 1120 | waysx->fd=ReOpenFile(waysx->filename_tmp); |
729 | waysx->nfd=ReOpenFile(waysx->nfilename_tmp); | ||
730 | amb | 285 | |
731 | amb | 461 | /* Write out the ways data */ |
732 | amb | 285 | |
733 | amb | 502 | fd=OpenFileNew(filename); |
734 | amb | 285 | |
735 | amb | 461 | SeekFile(fd,sizeof(WaysFile)); |
736 | amb | 285 | |
737 | for(i=0;i<waysx->number;i++) | ||
738 | { | ||
739 | amb | 1104 | ReadFile(waysx->fd,&wayx,sizeof(WayX)); |
740 | amb | 285 | |
741 | amb | 1104 | highways|=HIGHWAYS(wayx.way.type); |
742 | allow |=wayx.way.allow; | ||
743 | props |=wayx.way.props; | ||
744 | amb | 398 | |
745 | amb | 1104 | WriteFile(fd,&wayx.way,sizeof(Way)); |
746 | amb | 309 | |
747 | amb | 757 | if(!((i+1)%1000)) |
748 | amb | 790 | printf_middle("Writing Ways: Ways=%"Pindex_t,i+1); |
749 | amb | 285 | } |
750 | |||
751 | amb | 461 | /* Write out the ways names */ |
752 | amb | 285 | |
753 | amb | 1100 | SeekFile(fd,sizeof(WaysFile)+(off_t)waysx->number*sizeof(Way)); |
754 | amb | 461 | |
755 | amb | 309 | while(position<waysx->nlength) |
756 | { | ||
757 | int len=1024; | ||
758 | char temp[1024]; | ||
759 | |||
760 | if((waysx->nlength-position)<1024) | ||
761 | len=waysx->nlength-position; | ||
762 | |||
763 | amb | 555 | ReadFile(waysx->nfd,temp,len); |
764 | amb | 1104 | |
765 | amb | 309 | WriteFile(fd,temp,len); |
766 | |||
767 | position+=len; | ||
768 | } | ||
769 | |||
770 | amb | 1104 | /* Close the files */ |
771 | amb | 309 | |
772 | amb | 1104 | waysx->fd=CloseFile(waysx->fd); |
773 | amb | 612 | waysx->nfd=CloseFile(waysx->nfd); |
774 | amb | 555 | |
775 | amb | 461 | /* Write out the header structure */ |
776 | |||
777 | amb | 1100 | waysfile.number =waysx->number; |
778 | amb | 461 | |
779 | amb | 526 | waysfile.highways=highways; |
780 | waysfile.allow =allow; | ||
781 | waysfile.props =props; | ||
782 | amb | 461 | |
783 | SeekFile(fd,0); | ||
784 | WriteFile(fd,&waysfile,sizeof(WaysFile)); | ||
785 | |||
786 | amb | 285 | CloseFile(fd); |
787 | |||
788 | amb | 461 | /* Print the final message */ |
789 | |||
790 | amb | 1100 | printf_last("Wrote Ways: Ways=%"Pindex_t,waysx->number); |
791 | amb | 285 | } |
Properties
Name | Value |
---|---|
cvs:description | Extended ways functions. |