diff --git a/src/Makefile.am b/src/Makefile.am index b074e52fe..5831a68a4 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -244,6 +244,7 @@ nobase_dist_HEADERS = \ overpass_api/data/regular_expression.h\ overpass_api/data/relation_geometry_store.h\ overpass_api/data/set_comparison.h\ + overpass_api/data/sorted_ids.h\ overpass_api/data/tag_store.h\ overpass_api/data/tags_global_reader.h\ overpass_api/data/tilewise_geometry.h\ diff --git a/src/overpass_api/data/filter_ids_by_tags.h b/src/overpass_api/data/filter_ids_by_tags.h index afa2b24cb..89fcf602e 100644 --- a/src/overpass_api/data/filter_ids_by_tags.h +++ b/src/overpass_api/data/filter_ids_by_tags.h @@ -19,6 +19,8 @@ #ifndef DE__OSM3S___OVERPASS_API__DATA__FILTER_IDS_BY_TAGS_H #define DE__OSM3S___OVERPASS_API__DATA__FILTER_IDS_BY_TAGS_H +#include "sorted_ids.h" + template< typename Id_Type > void filter_ids_by_tags @@ -36,6 +38,7 @@ void filter_ids_by_tags std::map< std::string, std::pair< std::string, std::vector< Regular_Expression* > > >::const_iterator key_it = keys.begin(); std::vector< Id_Type > old_ids; + typename std::vector< Id_Type >::size_type old_pos = 0; std::vector< uint64 > matched_by_key_regexes; std::vector< uint64 > matched_by_both_regexes; std::vector< std::vector< Id_Type > > matched_ids(key_regexes.size()); @@ -96,11 +99,11 @@ void filter_ids_by_tags } } - if (key_relevant && valid && std::binary_search(old_ids.begin(), old_ids.end(), tag_it.object())) + if (key_relevant && valid && sorted_contains(old_ids, old_pos, tag_it.object())) new_ids.push_back(tag_it.object()); if (!matched_by_both_regexes.empty() && - (std::binary_search(old_ids.begin(), old_ids.end(), tag_it.object()) || + (sorted_contains(old_ids, old_pos, tag_it.object()) || std::binary_search(new_ids.begin(), new_ids.end(), tag_it.object()))) { for (std::vector< uint64 >::const_iterator reg_it = matched_by_both_regexes.begin(); diff --git a/src/overpass_api/data/set_comparison.cc b/src/overpass_api/data/set_comparison.cc index 3db91e234..0ac2e9b66 100644 --- a/src/overpass_api/data/set_comparison.cc +++ b/src/overpass_api/data/set_comparison.cc @@ -18,6 +18,7 @@ #include "../statements/evaluator.h" #include "set_comparison.h" +#include "sorted_ids.h" #include #include @@ -292,13 +293,14 @@ void Set_Comparison::tags_quadtile typename std::map< Index, std::vector< Object > >::const_iterator item_it(items.begin()); + typename std::vector< typename Object::Id_Type >::size_type pos = 0; // print the result while (item_it != items.end()) { for (typename std::vector< Object >::const_iterator it2(item_it->second.begin()); it2 != item_it->second.end(); ++it2) { - if (std::binary_search(id_list.begin(), id_list.end(), it2->id)) + if (sorted_contains(id_list, pos, it2->id)) print_item(extra_data, item_it->first.val(), *it2, tag_store.get(item_it->first, *it2), meta_printer.get(item_it->first, it2->id), extra_data.users); } @@ -320,12 +322,13 @@ void Set_Comparison::tags_quadtile_attic typename std::map< Index, std::vector< Attic< Object > > >::const_iterator item_it(items.begin()); + typename std::vector< typename Object::Id_Type >::size_type pos = 0; while (item_it != items.end()) { for (typename std::vector< Attic< Object > >::const_iterator it2(item_it->second.begin()); it2 != item_it->second.end(); ++it2) { - if (std::binary_search(id_list.begin(), id_list.end(), it2->id)) + if (sorted_contains(id_list, pos, it2->id)) print_item(extra_data, item_it->first.val(), *it2, tag_store.get(item_it->first, *it2), meta_printer.get(item_it->first, it2->id, it2->timestamp), extra_data.users); } diff --git a/src/overpass_api/data/sorted_ids.h b/src/overpass_api/data/sorted_ids.h new file mode 100644 index 000000000..6a4e8056b --- /dev/null +++ b/src/overpass_api/data/sorted_ids.h @@ -0,0 +1,46 @@ +/** Copyright Olbricht, Roland + * + * This file is part of Overpass_API. + * + * Overpass_API is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * Overpass_API is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with Overpass_API. If not, see . + */ + +#ifndef DE__OSM3S___OVERPASS_API__DATA__SORTED_IDS_H +#define DE__OSM3S___OVERPASS_API__DATA__SORTED_IDS_H + +#include +#include + + +// Whether the sorted vector ids contains id, like std::binary_search. pos is where the previous +// lookup ended: callers mostly look up ascending ids, so the search gallops forward from there. +// It starts over when id isn't greater than the element before pos, so any pos, and any order of +// lookups, gives the right answer. +template< class Id_Type > +bool sorted_contains(const std::vector< Id_Type >& ids, typename std::vector< Id_Type >::size_type& pos, + const Id_Type& id) +{ + if (pos > ids.size() || (pos > 0 && !(ids[pos-1] < id))) + pos = 0; + typename std::vector< Id_Type >::size_type remaining = ids.size() - pos; + typename std::vector< Id_Type >::size_type bound = 1; + while (bound < remaining && ids[pos + bound] < id) + bound *= 2; + pos = std::lower_bound(ids.begin() + pos + bound/2, ids.begin() + pos + std::min(bound, remaining), id) + - ids.begin(); + return pos < ids.size() && !(id < ids[pos]); +} + + +#endif diff --git a/src/overpass_api/data/tag_store.h b/src/overpass_api/data/tag_store.h index d1f565233..f9356dbf8 100644 --- a/src/overpass_api/data/tag_store.h +++ b/src/overpass_api/data/tag_store.h @@ -21,6 +21,7 @@ #include "filenames.h" +#include "sorted_ids.h" #include "../core/datatypes.h" #include "../../template_db/block_backend.h" #include "../../template_db/transaction.h" @@ -226,13 +227,14 @@ void collect_tags typename Block_Backend< Tag_Index_Local, Id_Type >::Range_Iterator& tag_it, const std::vector< Id_Type >& ids, uint32 coarse_index) { + typename std::vector< Id_Type >::size_type pos = 0; while ((!(tag_it == items_db.range_end())) && (((tag_it.index().index) & 0x7fffff00) < coarse_index)) ++tag_it; while ((!(tag_it == items_db.range_end())) && (((tag_it.index().index) & 0x7fffff00) == coarse_index)) { - if ((binary_search(ids.begin(), ids.end(), tag_it.object()))) + if (sorted_contains(ids, pos, tag_it.object())) tags_by_id[tag_it.object()].push_back (std::make_pair(tag_it.index().key, tag_it.index().value)); ++tag_it; @@ -249,6 +251,8 @@ void collect_tags_framed uint32 coarse_index, Id_Type lower_id_bound, Id_Type upper_id_bound) { + const std::vector< Id_Type >& ids = ids_by_coarse[coarse_index]; + typename std::vector< Id_Type >::size_type pos = 0; while ((!(tag_it == items_db.range_end())) && (((tag_it.index().index) & 0x7fffff00) < coarse_index)) ++tag_it; @@ -257,8 +261,7 @@ void collect_tags_framed { if (!(tag_it.object() < lower_id_bound) && (tag_it.object() < upper_id_bound) && - (binary_search(ids_by_coarse[coarse_index].begin(), - ids_by_coarse[coarse_index].end(), tag_it.object()))) + sorted_contains(ids, pos, tag_it.object())) tags_by_id[tag_it.object()].push_back (std::make_pair(tag_it.index().key, tag_it.index().value)); ++tag_it; diff --git a/src/overpass_api/statements/query.cc b/src/overpass_api/statements/query.cc index 76b2d190f..b133b2560 100644 --- a/src/overpass_api/statements/query.cc +++ b/src/overpass_api/statements/query.cc @@ -20,6 +20,7 @@ #include "../../template_db/random_file.h" #include "../core/settings.h" #include "../data/abstract_processing.h" +#include "../data/sorted_ids.h" #include "../data/collect_members.h" #include "../data/filenames.h" #include "../data/filter_by_tags.h" @@ -301,11 +302,12 @@ void filter_ids_by_ntags bool valid = false; std::string last_value = void_tag_value() + " "; + typename std::vector< Id_Type >::size_type pos = 0; while ((!(tag_it == items_db.range_end())) && ((tag_it.index().index) & 0x7fffff00) == coarse_index && tag_it.index().key == key_it->first) { - if (std::binary_search(new_ids.begin(), new_ids.end(), tag_it.object())) + if (sorted_contains(new_ids, pos, tag_it.object())) { std::pair< uint64, uint64 >& timestamp_ref = timestamps[tag_it.object()]; timestamp_ref.second = NOW; @@ -333,7 +335,7 @@ void filter_ids_by_ntags ((attic_tag_it.index().index) & 0x7fffff00) == coarse_index && attic_tag_it.index().key == key_it->first) { - if (std::binary_search(new_ids.begin(), new_ids.end(), Id_Type(attic_tag_it.object()))) + if (sorted_contains(new_ids, pos, Id_Type(attic_tag_it.object()))) { std::pair< uint64, uint64 >& timestamp_ref = timestamps[attic_tag_it.object()]; if (timestamp < attic_tag_it.object().timestamp && @@ -397,13 +399,14 @@ void filter_by_ids( for (typename std::map< uint32, std::vector< typename TObject::Id_Type > >::const_iterator it = ids_by_coarse.begin(); it != ids_by_coarse.end(); ++it) { + typename std::vector< typename TObject::Id_Type >::size_type pos = 0; while ((item_it != items.end()) && ((item_it->first.val() & 0x7fffff00) == it->first)) { for (typename std::vector< TObject >::const_iterator eit = item_it->second.begin(); eit != item_it->second.end(); ++eit) { - if (std::binary_search(it->second.begin(), it->second.end(), eit->id)) + if (sorted_contains(it->second, pos, eit->id)) result[item_it->first.val()].push_back(*eit); } ++item_it; @@ -418,13 +421,14 @@ void filter_by_ids( for (typename std::map< uint32, std::vector< typename TObject::Id_Type > >::const_iterator it = ids_by_coarse.begin(); it != ids_by_coarse.end(); ++it) { + typename std::vector< typename TObject::Id_Type >::size_type pos = 0; while ((item_it != items.end()) && ((item_it->first.val() & 0x7fffff00) == it->first)) { for (typename std::vector< TObject >::const_iterator eit = item_it->second.begin(); eit != item_it->second.end(); ++eit) { - if (std::binary_search(it->second.begin(), it->second.end(), eit->id)) + if (sorted_contains(it->second, pos, eit->id)) result[item_it->first.val()].push_back(*eit); } ++item_it; @@ -436,7 +440,7 @@ void filter_by_ids( for (typename std::vector< Attic< TObject > >::const_iterator eit = attic_item_it->second.begin(); eit != attic_item_it->second.end(); ++eit) { - if (std::binary_search(it->second.begin(), it->second.end(), eit->id)) + if (sorted_contains(it->second, pos, eit->id)) attic_result[attic_item_it->first.val()].push_back(*eit); } ++attic_item_it; @@ -634,13 +638,14 @@ void Query_Statement::filter_by_tags filter_ids_by_tags_old(ids_by_coarse_, key_union, regkey_regexes, items_db, tag_it, it->first); + typename std::vector< typename TObject::Id_Type >::size_type pos = 0; while ((item_it != items.end()) && ((item_it->first.val() & 0x7fffff00) == it->first)) { for (typename std::vector< TObject >::const_iterator eit = item_it->second.begin(); eit != item_it->second.end(); ++eit) { - if (std::binary_search(ids_by_coarse_.begin(), ids_by_coarse_.end(), eit->id)) + if (sorted_contains(ids_by_coarse_, pos, eit->id)) result[item_it->first.val()].push_back(*eit); } ++item_it;