Welcome to mirror list, hosted at ThFree Co, Russian Federation.

categories_cache.cpp « search - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 233b21b84fee6101c44896311068f1ec81327368 (plain)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "search/categories_cache.hpp"

#include "search/mwm_context.hpp"
#include "search/query_params.hpp"
#include "search/retrieval.hpp"

#include "indexer/ftypes_matcher.hpp"

#include "base/assert.hpp"

#include "std/vector.hpp"

namespace search
{
namespace
{
// Performs pairwise union of adjacent bit vectors
// until at most one bit vector is left.
void UniteCBVs(vector<CBV> & cbvs)
{
  while (cbvs.size() > 1)
  {
    size_t i = 0;
    size_t j = 0;
    for (; j + 1 < cbvs.size(); j += 2)
      cbvs[i++] = cbvs[j].Union(cbvs[j + 1]);
    for (; j < cbvs.size(); ++j)
      cbvs[i++] = move(cbvs[j]);
    cbvs.resize(i);
  }
}
}  // namespace

// CategoriesCache ---------------------------------------------------------------------------------
CBV CategoriesCache::Get(MwmContext const & context)
{
  if (!context.m_handle.IsAlive() || !context.m_value.HasSearchIndex())
    return CBV();

  auto id = context.m_handle.GetId();
  auto const it = m_cache.find(id);
  if (it != m_cache.cend())
    return it->second;

  auto cbv = Load(context);
  m_cache[id] = cbv;
  return cbv;
}

CBV CategoriesCache::Load(MwmContext const & context)
{
  ASSERT(context.m_handle.IsAlive(), ());
  ASSERT(context.m_value.HasSearchIndex(), ());

  QueryParams params;
  params.m_tokens.resize(1);
  params.m_tokens[0].resize(1);

  params.m_types.resize(1);
  params.m_types[0].resize(1);

  vector<CBV> cbvs;

  m_categories.ForEach([&](strings::UniString const & key, uint32_t const type) {
    params.m_tokens[0][0] = key;
    params.m_types[0][0] = type;

    CBV cbv(RetrieveAddressFeatures(context, m_cancellable, params));
    if (!cbv.IsEmpty())
      cbvs.push_back(move(cbv));
  });

  UniteCBVs(cbvs);
  if (cbvs.empty())
    cbvs.emplace_back();

  return cbvs[0];
}

// StreetsCache ------------------------------------------------------------------------------------
StreetsCache::StreetsCache(my::Cancellable const & cancellable)
  : CategoriesCache(ftypes::IsStreetChecker::Instance(), cancellable)
{
}

// VillagesCache -----------------------------------------------------------------------------------
VillagesCache::VillagesCache(my::Cancellable const & cancellable)
  : CategoriesCache(ftypes::IsVillageChecker::Instance(), cancellable)
{
}

// HotelsCache -------------------------------------------------------------------------------------
HotelsCache::HotelsCache(my::Cancellable const & cancellable)
  : CategoriesCache(ftypes::IsHotelChecker::Instance(), cancellable)
{
}
}  // namespace search