forked from mapsme/omim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
complex_loader.cpp
72 lines (59 loc) · 1.99 KB
/
complex_loader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "generator/complex_loader.hpp"
#include "indexer/classificator.hpp"
#include "indexer/ftypes_matcher.hpp"
#include "coding/csv_reader.hpp"
#include "base/logging.hpp"
#include "base/stl_helpers.hpp"
#include <memory>
#include <mutex>
#include <utility>
namespace generator
{
bool IsComplex(tree_node::types::Ptr<HierarchyEntry> const & tree)
{
size_t constexpr kNumRequiredTypes = 3;
return tree_node::CountIf(tree, [&](auto const & e) {
auto const & isAttraction = ftypes::AttractionsChecker::Instance();
return isAttraction(e.m_type);
}) >= kNumRequiredTypes;
}
storage::CountryId GetCountry(tree_node::types::Ptr<HierarchyEntry> const & tree)
{
return tree->GetData().m_country;
}
ComplexLoader::ComplexLoader(std::string const & filename)
{
auto trees = hierarchy::LoadHierachy(filename);
base::EraseIf(trees, [](auto const & e){ return !IsComplex(e); });
for (auto const & tree : trees)
m_forests[GetCountry(tree)].Append(tree);
}
tree_node::Forest<HierarchyEntry> const & ComplexLoader::GetForest(
storage::CountryId const & country) const
{
static tree_node::Forest<HierarchyEntry> const kEmpty;
auto const it = m_forests.find(country);
return it == std::cend(m_forests) ? kEmpty : it->second;
}
std::unordered_set<CompositeId> ComplexLoader::GetIdsSet() const
{
std::unordered_set<CompositeId> set;
ForEach([&](auto const &, auto const & forest) {
forest.ForEachTree([&](auto const & tree) {
tree_node::ForEach(tree, [&](auto const & entry) { set.emplace(entry.m_id); });
});
});
return set;
}
ComplexLoader const & GetOrCreateComplexLoader(std::string const & filename)
{
static std::mutex m;
static std::unordered_map<std::string, ComplexLoader> complexLoaders;
std::lock_guard<std::mutex> lock(m);
auto const it = complexLoaders.find(filename);
if (it != std::cend(complexLoaders))
return it->second;
auto const eIt = complexLoaders.emplace(filename, ComplexLoader(filename));
return eIt.first->second;
}
} // namespace generator