diff options
author | sleske <sleske@ffa7fe5e-494d-0410-b361-a75ebd5db220> | 2014-05-29 13:36:36 +0000 |
---|---|---|
committer | sleske <sleske@ffa7fe5e-494d-0410-b361-a75ebd5db220> | 2014-05-29 13:36:36 +0000 |
commit | 8ff5af31abfd3d3b9dbc4b73fdc2c6d5ba4d53d2 (patch) | |
tree | c846b0acad716c9711bc07eda05378e5f702941f /navit/maptool | |
parent | 973c837a08a7929ba1c1406f88efe509b13d078d (diff) | |
download | navit-svn-8ff5af31abfd3d3b9dbc4b73fdc2c6d5ba4d53d2.tar.gz |
Refactor:maptool:Refactor node_item_get, extract node_item_find_index_in_ordered_list.
git-svn-id: http://svn.code.sf.net/p/navit/code/trunk/navit@5786 ffa7fe5e-494d-0410-b361-a75ebd5db220
Diffstat (limited to 'navit/maptool')
-rw-r--r-- | navit/maptool/osm.c | 91 |
1 files changed, 48 insertions, 43 deletions
diff --git a/navit/maptool/osm.c b/navit/maptool/osm.c index 383eafef..2b1de14d 100644 --- a/navit/maptool/osm.c +++ b/navit/maptool/osm.c @@ -1351,59 +1351,64 @@ clear_node_item_buffer(void) } } -static struct node_item * -node_item_get(osmid id) -{ - struct node_item *ni=(struct node_item *)(node_buffer.base); - int count=node_buffer.size/sizeof(struct node_item); - int interval=count/4; - int p=count/2; - if(interval==0) { - // If fewer than 4 nodes defined so far set interval to 1 to - // avoid infinite loop - interval = 1; - } - if (node_hash) { - int i; - i=(int)(long)(g_hash_table_lookup(node_hash, (gpointer)(long)(id))); - return ni+i; - } - if (ni[0].id > id) - return NULL; - if (ni[count-1].id < id) - return NULL; - while (ni[p].id != id) { +static int +node_item_find_index_in_ordered_list(osmid id) +{ + struct node_item *node_buffer_base=(struct node_item *)(node_buffer.base); + int node_count=node_buffer.size/sizeof(struct node_item); + int search_step=node_count>4 ? node_count/4 : 1; + int search_index=node_count/2; + if (node_buffer_base[0].id > id) + return -1; + if (node_buffer_base[node_count-1].id < id) + return -1; + while (node_buffer_base[search_index].id != id) { #if 0 - fprintf(stderr,"p=%d count=%d interval=%d id=%d ni[p].id=%d\n", p, count, interval, id, ni[p].id); + fprintf(stderr,"search_index=%d node_count=%d search_step=%d id=%d node_buffer_base[search_index].id=%d\n", + search_index, node_count, search_step, id, node_buffer_base[search_index].id); #endif - if (ni[p].id < id) { - p+=interval; - if (interval == 1) { - if (p >= count) - return NULL; - if (ni[p].id > id) - return NULL; + if (node_buffer_base[search_index].id < id) { + search_index+=search_step; + if (search_step == 1) { + if (search_index >= node_count) + return -1; + if (node_buffer_base[search_index].id > id) + return -1; } else { - if (p >= count) - p=count-1; + if (search_index >= node_count) + search_index=node_count-1; } } else { - p-=interval; - if (interval == 1) { - if (p < 0) - return NULL; - if (ni[p].id < id) - return NULL; + search_index-=search_step; + if (search_step == 1) { + if (search_index < 0) + return -1; + if (node_buffer_base[search_index].id < id) + return -1; } else { - if (p < 0) - p=0; + if (search_index < 0) + search_index=0; } } - if (interval > 1) - interval/=2; + if (search_step > 1) + search_step/=2; } - return &ni[p]; + return search_index; } + +static struct node_item * +node_item_get(osmid id) +{ + struct node_item *node_buffer_base=(struct node_item *)(node_buffer.base); + int result_index; + if (node_hash) { + result_index=(int)(long)(g_hash_table_lookup(node_hash, (gpointer)(long)(id))); + } else { + result_index=node_item_find_index_in_ordered_list(id); + } + return result_index!=-1 ? node_buffer_base+result_index : NULL; +} + #if 0 static int load_node(FILE *coords, int p, struct node_item *ret) |