From ae88c7c5d78bbad8d6767729799c4da9b5306f88 Mon Sep 17 00:00:00 2001 From: Sergey Magidovich Date: Wed, 23 Mar 2016 15:03:11 +0300 Subject: [PATCH] Reduce number of iterations in matching algorithm. --- editor/changeset_wrapper.cpp | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/editor/changeset_wrapper.cpp b/editor/changeset_wrapper.cpp index 8c11cd3e8a..73e73e91ed 100644 --- a/editor/changeset_wrapper.cpp +++ b/editor/changeset_wrapper.cpp @@ -9,6 +9,7 @@ #include "base/macros.hpp" #include "std/algorithm.hpp" +#include "std/random.hpp" #include "std/sstream.hpp" #include "private.h" @@ -32,6 +33,31 @@ bool OsmFeatureHasTags(pugi::xml_node const & osmFt) { return osmFt.child("tag"); } + +vector NaiveSample(vector const & source, size_t count) +{ + count = min(count, source.size()); + vector result; + result.reserve(count); + vector indexes; + indexes.reserve(count); + + mt19937 engine; + uniform_int_distribution<> distrib(0, source.size()); + + while (count--) + { + size_t index; + do + { + index = distrib(engine); + } while (find(begin(indexes), end(indexes), index) != end(indexes)); + result.push_back(source[index]); + indexes.push_back(index); + } + + return result; +} } // namespace namespace pugi @@ -115,9 +141,10 @@ XMLFeature ChangesetWrapper::GetMatchingNodeFeatureFromOSM(m2::PointD const & ce XMLFeature ChangesetWrapper::GetMatchingAreaFeatureFromOSM(vector const & geometry) { - // TODO: Make two/four requests using points on inscribed rectagle. + auto const kSamplePointsCount = 3; bool hasRelation = false; - for (auto const & pt : geometry) + // Try several points in case of poor osm response. + for (auto const & pt : NaiveSample(geometry, kSamplePointsCount)) { ms::LatLon const ll = MercatorBounds::ToLatLon(pt); pugi::xml_document doc;