Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ nobase_dist_HEADERS = \
overpass_api/core/four_field_index.h\
overpass_api/core/geometry.h\
overpass_api/core/index_computations.h\
overpass_api/core/monobound_search.h\
overpass_api/core/parsed_query.h\
overpass_api/core/settings.h\
overpass_api/core/type_area.h\
Expand Down
40 changes: 12 additions & 28 deletions src/overpass_api/core/datatypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <vector>

#include "basic_types.h"
#include "monobound_search.h"
#include "geometry.h"
#include "type_node.h"
#include "type_way.h"
Expand Down Expand Up @@ -102,23 +103,14 @@ struct Pair_Equal_Id {
};


// The callers look up node refs of ways and members of relations, in no particular order.
template < class T >
const T* binary_search_for_id(const std::vector< T >& vect, typename T::Id_Type id)
{
uint32 lower(0);
uint32 upper(vect.size());

while (upper > lower)
{
uint32 pos((upper + lower)/2);
if (id < vect[pos].id)
upper = pos;
else if (vect[pos].id == id)
return &(vect[pos]);
else
lower = pos + 1;
}
return 0;
if (vect.empty())
return 0;
const T& found = vect[monobound_index(vect, id, [](const T& t) -> const typename T::Id_Type& { return t.id; })];
return found.id == id ? &found : 0;
}


Expand Down Expand Up @@ -547,24 +539,16 @@ struct OSM_Element_Metadata_Skeleton
};


// The callers look up members of relations, in no particular order.
template< class TIndex, class TObject >
const std::pair< TIndex, const TObject* >* binary_search_for_pair_id
(const std::vector< std::pair< TIndex, const TObject* > >& vect, typename TObject::Id_Type id)
{
uint32 lower(0);
uint32 upper(vect.size());

while (upper > lower)
{
uint32 pos((upper + lower)/2);
if (id < vect[pos].second->id)
upper = pos;
else if (vect[pos].second->id == id)
return &(vect[pos]);
else
lower = pos + 1;
}
return 0;
if (vect.empty())
return 0;
const std::pair< TIndex, const TObject* >& found = vect[monobound_index(vect, id,
[](const std::pair< TIndex, const TObject* >& p) -> const typename TObject::Id_Type& { return p.second->id; })];
return found.second->id == id ? &found : 0;
}


Expand Down
57 changes: 57 additions & 0 deletions src/overpass_api/core/monobound_search.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/** 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 <https://www.gnu.org/licenses/>.
*/

#ifndef DE__OSM3S___OVERPASS_API__CORE__MONOBOUND_SEARCH_H
#define DE__OSM3S___OVERPASS_API__CORE__MONOBOUND_SEARCH_H

#include <cstddef>
#include <vector>


// Index of the last element of vect whose key(element) isn't greater than id, or 0. vect must be
// sorted by key. The loop always runs log2(size) times and its only data-dependent step is a
// conditional add, which compilers turn into a conditional move: for lookups in no particular
// order, such as the node refs of a way or the members of a relation, there is no branch to
// mispredict. For ascending lookups std::lower_bound's branches predict well; use that there.
template< typename T, typename Key, typename Get_Key >
std::size_t monobound_index(const std::vector< T >& vect, const Key& id, Get_Key key)
{
std::size_t base = 0;
std::size_t n = vect.size();
while (n > 1)
{
std::size_t half = n/2;
base += !(id < key(vect[base + half])) ? half : 0;
n -= half;
}
return base;
}


// Whether the sorted vector ids contains id, like std::binary_search.
template< typename Id_Type >
bool monobound_contains(const std::vector< Id_Type >& ids, const Id_Type& id)
{
if (ids.empty())
return false;
const Id_Type& found = ids[monobound_index(ids, id, [](const Id_Type& i) -> const Id_Type& { return i; })];
return !(found < id) && !(id < found);
}


#endif
11 changes: 6 additions & 5 deletions src/overpass_api/data/abstract_processing.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#define DE__OSM3S___OVERPASS_API__DATA__ABSTRACT_PROCESSING_H

#include "../core/datatypes.h"
#include "../core/monobound_search.h"
#include "../statements/statement.h"
#include "filenames.h"

Expand Down Expand Up @@ -107,7 +108,7 @@ inline bool has_a_child_with_id
it3 != relation.members.end(); ++it3)
{
if (it3->type == type &&
std::binary_search(ids.begin(), ids.end(), it3->ref))
monobound_contains(ids, it3->ref))
return true;
}
return false;
Expand All @@ -121,7 +122,7 @@ inline bool has_a_child_with_id_and_role
it3 != relation.members.end(); ++it3)
{
if (it3->type == type && it3->role == role_id &&
std::binary_search(ids.begin(), ids.end(), it3->ref))
monobound_contains(ids, it3->ref))
return true;
}
return false;
Expand All @@ -137,13 +138,13 @@ inline bool has_a_child_with_id
for (; it3 != pos->end() && *it3 < 0; ++it3)
{
if (*it3 + (int)way.nds.size() >= 0 &&
std::binary_search(ids.begin(), ids.end(), way.nds[*it3 + way.nds.size()]))
monobound_contains(ids, way.nds[*it3 + way.nds.size()]))
return true;
}
for (; it3 != pos->end(); ++it3)
{
if (*it3 > 0 && *it3 < (int)way.nds.size()+1 &&
std::binary_search(ids.begin(), ids.end(), way.nds[*it3-1]))
monobound_contains(ids, way.nds[*it3-1]))
return true;
}
}
Expand All @@ -152,7 +153,7 @@ inline bool has_a_child_with_id
for (std::vector< Node::Id_Type >::const_iterator it3(way.nds.begin());
it3 != way.nds.end(); ++it3)
{
if (std::binary_search(ids.begin(), ids.end(), *it3))
if (monobound_contains(ids, *it3))
return true;
}
}
Expand Down