diff options
author | Olga Khlopkova <o.khlopkova@corp.mail.ru> | 2020-11-26 12:19:20 +0300 |
---|---|---|
committer | Vladimir Byko-Ianko <bykoianko@gmail.com> | 2020-12-01 10:07:36 +0300 |
commit | 843d93527624730d0e82fcfbadda9146454cb0cc (patch) | |
tree | e2497816fa21b800d21a53cd8f1455c7566ebcaa /routing | |
parent | ec082825900e1f8f5d0f74372ecc68e523585c51 (diff) |
[routing] AbsentRegionsFinder as a replacement for OnlineAbsentFetcher.
Diffstat (limited to 'routing')
-rw-r--r-- | routing/absent_regions_finder.cpp | 71 | ||||
-rw-r--r-- | routing/absent_regions_finder.hpp | 50 |
2 files changed, 121 insertions, 0 deletions
diff --git a/routing/absent_regions_finder.cpp b/routing/absent_regions_finder.cpp new file mode 100644 index 0000000000..786a572956 --- /dev/null +++ b/routing/absent_regions_finder.cpp @@ -0,0 +1,71 @@ +#include "routing/absent_regions_finder.hpp" + +namespace routing +{ +AbsentRegionsFinder::AbsentRegionsFinder(CountryFileGetterFn const & countryFileGetter, + LocalFileCheckerFn const & localFileChecker, + std::shared_ptr<NumMwmIds> numMwmIds, + DataSource & dataSource) + : m_countryFileGetterFn(countryFileGetter) + , m_localFileCheckerFn(localFileChecker) + , m_numMwmIds(std::move(numMwmIds)) + , m_dataSource(dataSource) +{ + CHECK(m_countryFileGetterFn, ()); + CHECK(m_localFileCheckerFn, ()); +} + +void AbsentRegionsFinder::GenerateAbsentRegions(Checkpoints const & checkpoints, + RouterDelegate const & delegate) +{ + if (m_routerThread) + { + m_routerThread->Cancel(); + m_routerThread.reset(); + } + + if (AreCheckpointsInSameMwm(checkpoints)) + return; + + std::unique_ptr<RegionsRouter> router = std::make_unique<RegionsRouter>( + m_countryFileGetterFn, m_numMwmIds, m_dataSource, delegate, checkpoints); + + // iOS can't reuse threads. So we need to recreate the thread. + m_routerThread = std::make_unique<threads::Thread>(); + m_routerThread->Create(move(router)); +} + +void AbsentRegionsFinder::GetAbsentRegions(std::set<std::string> & absentCountries) +{ + absentCountries.clear(); + + if (!m_routerThread) + return; + + m_routerThread->Join(); + + for (auto const & mwmName : m_routerThread->GetRoutineAs<RegionsRouter>()->GetMwmNames()) + { + if (mwmName.empty() || m_localFileCheckerFn(mwmName)) + continue; + + absentCountries.emplace(mwmName); + } + + m_routerThread.reset(); +} + +bool AbsentRegionsFinder::AreCheckpointsInSameMwm(Checkpoints const & checkpoints) const +{ + for (size_t i = 0; i < checkpoints.GetNumSubroutes(); ++i) + { + if (m_countryFileGetterFn(checkpoints.GetPoint(i)) != + m_countryFileGetterFn(checkpoints.GetPoint(i + 1))) + { + return false; + } + } + + return true; +} +} // namespace routing diff --git a/routing/absent_regions_finder.hpp b/routing/absent_regions_finder.hpp new file mode 100644 index 0000000000..7aa45effff --- /dev/null +++ b/routing/absent_regions_finder.hpp @@ -0,0 +1,50 @@ +#pragma once + +#include "routing/checkpoints.hpp" +#include "routing/regions_router.hpp" +#include "routing/router_delegate.hpp" + +#include "routing_common/num_mwm_id.hpp" + +#include "indexer/data_source.hpp" + +#include "geometry/rect2d.hpp" +#include "geometry/tree4d.hpp" + +#include "base/thread.hpp" + +#include <functional> +#include <memory> +#include <set> +#include <string> + +namespace routing +{ +using LocalFileCheckerFn = std::function<bool(std::string const &)>; + +// Encapsulates generation of mwm names of absent regions needed for building the route between +// |checkpoints|. For this purpose the new thread is used. +class AbsentRegionsFinder +{ +public: + AbsentRegionsFinder(CountryFileGetterFn const & countryFileGetter, + LocalFileCheckerFn const & localFileChecker, + std::shared_ptr<NumMwmIds> numMwmIds, DataSource & dataSource); + + // Creates new thread |m_routerThread| and starts routing in it. + void GenerateAbsentRegions(Checkpoints const & checkpoints, RouterDelegate const & delegate); + // Waits for the routing thread |m_routerThread| to finish and returns results from it. + void GetAbsentRegions(std::set<std::string> & absentCountries); + +private: + bool AreCheckpointsInSameMwm(Checkpoints const & checkpoints) const; + + CountryFileGetterFn const m_countryFileGetterFn; + LocalFileCheckerFn const m_localFileCheckerFn; + + std::shared_ptr<NumMwmIds> m_numMwmIds; + DataSource & m_dataSource; + + std::unique_ptr<threads::Thread> m_routerThread; +}; +} // namespace routing |