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

feature_offset_match.hpp « search - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4179e29b6f775ed09a3b9a31eab361f19f3639a3 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#pragma once
#include "search/search_common.hpp"
#include "search/search_query.hpp"
#include "search/search_query_params.hpp"

#include "indexer/search_trie.hpp"

#include "base/mutex.hpp"
#include "base/scope_guard.hpp"
#include "base/stl_add.hpp"
#include "base/string_utils.hpp"

#include "std/algorithm.hpp"
#include "std/target_os.hpp"
#include "std/unique_ptr.hpp"
#include "std/unordered_set.hpp"
#include "std/utility.hpp"
#include "std/vector.hpp"


namespace search
{
namespace impl
{
template <class TSrcIter, class TCompIter>
size_t CalcEqualLength(TSrcIter b, TSrcIter e, TCompIter bC, TCompIter eC)
{
  size_t count = 0;
  while ((b != e) && (bC != eC) && (*b++ == *bC++))
    ++count;
  return count;
}

inline trie::DefaultIterator * MoveTrieIteratorToString(trie::DefaultIterator const & trieRoot,
                                                        strings::UniString const & queryS,
                                                        size_t & symbolsMatched,
                                                        bool & bFullEdgeMatched)
{
  symbolsMatched = 0;
  bFullEdgeMatched = false;

  unique_ptr<trie::DefaultIterator> pIter(trieRoot.Clone());

  size_t const szQuery = queryS.size();

  while (symbolsMatched < szQuery)
  {
    bool bMatched = false;

    ASSERT_LESS(pIter->m_edge.size(), std::numeric_limits<uint32_t>::max(), ());
    uint32_t const edgeCount = static_cast<uint32_t>(pIter->m_edge.size());

    for (uint32_t i = 0; i < edgeCount; ++i)
    {
      size_t const szEdge = pIter->m_edge[i].m_str.size();

      size_t const count = CalcEqualLength(
                                        pIter->m_edge[i].m_str.begin(),
                                        pIter->m_edge[i].m_str.end(),
                                        queryS.begin() + symbolsMatched,
                                        queryS.end());

      if ((count > 0) && (count == szEdge || szQuery == count + symbolsMatched))
      {
        pIter.reset(pIter->GoToEdge(i));

        bFullEdgeMatched = (count == szEdge);
        symbolsMatched += count;
        bMatched = true;
        break;
      }
    }

    if (!bMatched)
      return NULL;
  }
  return pIter->Clone();
}

namespace
{
  bool CheckMatchString(strings::UniChar const * rootPrefix,
                        size_t rootPrefixSize,
                        strings::UniString & s)
  {
    if (rootPrefixSize > 0)
    {
      if (s.size() < rootPrefixSize ||
          !StartsWith(s.begin(), s.end(), rootPrefix, rootPrefix + rootPrefixSize))
        return false;

      s = strings::UniString(s.begin() + rootPrefixSize, s.end());
    }

    return true;
  }
}

template <typename F>
void FullMatchInTrie(trie::DefaultIterator const & trieRoot, strings::UniChar const * rootPrefix,
                     size_t rootPrefixSize, strings::UniString s, F & f)
{
  if (!CheckMatchString(rootPrefix, rootPrefixSize, s))
      return;

  size_t symbolsMatched = 0;
  bool bFullEdgeMatched;
  unique_ptr<trie::DefaultIterator> const pIter(
      MoveTrieIteratorToString(trieRoot, s, symbolsMatched, bFullEdgeMatched));

  if (!pIter || (!s.empty() && !bFullEdgeMatched) || symbolsMatched != s.size())
    return;

#if defined(OMIM_OS_IPHONE) && !defined(__clang__)
  // Here is the dummy mutex to avoid mysterious iOS GCC-LLVM bug here.
  static threads::Mutex dummyM;
  threads::MutexGuard dummyG(dummyM);
#endif

  ASSERT_EQUAL ( symbolsMatched, s.size(), () );
  for (size_t i = 0; i < pIter->m_value.size(); ++i)
    f(pIter->m_value[i]);
}

template <typename F>
void PrefixMatchInTrie(trie::DefaultIterator const & trieRoot, strings::UniChar const * rootPrefix,
                       size_t rootPrefixSize, strings::UniString s, F & f)
{
  if (!CheckMatchString(rootPrefix, rootPrefixSize, s))
      return;

  using TQueue = vector<trie::DefaultIterator *>;
  TQueue trieQueue;
  {
    size_t symbolsMatched = 0;
    bool bFullEdgeMatched;
    trie::DefaultIterator * pRootIter =
        MoveTrieIteratorToString(trieRoot, s, symbolsMatched, bFullEdgeMatched);

    UNUSED_VALUE(symbolsMatched);
    UNUSED_VALUE(bFullEdgeMatched);

    if (!pRootIter)
      return;

    trieQueue.push_back(pRootIter);
  }

  // 'f' can throw an exception. So be prepared to delete unprocessed elements.
  MY_SCOPE_GUARD(doDelete, GetRangeDeletor(trieQueue, DeleteFunctor()));

  while (!trieQueue.empty())
  {
    // Next 2 lines don't throw any exceptions while moving
    // ownership from container to smart pointer.
    unique_ptr<trie::DefaultIterator> const pIter(trieQueue.back());
    trieQueue.pop_back();

    for (size_t i = 0; i < pIter->m_value.size(); ++i)
      f(pIter->m_value[i]);

    for (size_t i = 0; i < pIter->m_edge.size(); ++i)
      trieQueue.push_back(pIter->GoToEdge(i));
  }
}

template <class TFilter>
class OffsetIntersecter
{
  using ValueT = trie::ValueReader::ValueType;

  struct HashFn
  {
    size_t operator() (ValueT const & v) const
    {
      return v.m_featureId;
    }
  };
  struct EqualFn
  {
    bool operator() (ValueT const & v1, ValueT const & v2) const
    {
      return (v1.m_featureId == v2.m_featureId);
    }
  };

  using TSet = unordered_set<ValueT, HashFn, EqualFn>;

  TFilter const & m_filter;
  unique_ptr<TSet> m_prevSet;
  unique_ptr<TSet> m_set;

public:
  explicit OffsetIntersecter(TFilter const & filter) : m_filter(filter), m_set(new TSet) {}

  void operator() (ValueT const & v)
  {
    if (m_prevSet && !m_prevSet->count(v))
      return;

    if (!m_filter(v.m_featureId))
      return;

    m_set->insert(v);
  }

  void NextStep()
  {
    if (!m_prevSet)
      m_prevSet.reset(new TSet);

    m_prevSet.swap(m_set);
    m_set->clear();
  }

  template <class ToDo>
  void ForEachResult(ToDo && toDo) const
  {
    if (!m_prevSet)
      return;
    for (auto const & value : *m_prevSet)
      toDo(value);
  }
};
}  // namespace search::impl

struct TrieRootPrefix
{
  trie::DefaultIterator const & m_root;
  strings::UniChar const * m_prefix;
  size_t m_prefixSize;

  TrieRootPrefix(trie::DefaultIterator const & root,
                 trie::DefaultIterator::Edge::EdgeStrT const & edge)
    : m_root(root)
  {
    if (edge.size() == 1)
    {
      m_prefix = 0;
      m_prefixSize = 0;
    }
    else
    {
      m_prefix = &edge[1];
      m_prefixSize = edge.size() - 1;
    }
  }
};

template <class TFilter>
class TrieValuesHolder
{
public:
  TrieValuesHolder(TFilter const & filter) : m_filter(filter) {}

  void Resize(size_t count) { m_holder.resize(count); }

  void SwitchTo(size_t index)
  {
    ASSERT_LESS(index, m_holder.size(), ());
    m_index = index;
  }

  void operator()(Query::TTrieValue const & v)
  {
    if (m_filter(v.m_featureId))
      m_holder[m_index].push_back(v);
  }

  template <class ToDo>
  void ForEachValue(size_t index, ToDo && toDo) const
  {
    for (auto const & value : m_holder[index])
      toDo(value);
  }

private:
  vector<vector<Query::TTrieValue>> m_holder;
  size_t m_index;
  TFilter const & m_filter;
};

// Calls toDo for each feature corresponding to at least one synonym.
// *NOTE* toDo may be called several times for the same feature.
template <typename ToDo>
void MatchTokenInTrie(SearchQueryParams::TSynonymsVector const & syns,
                      TrieRootPrefix const & trieRoot, ToDo && toDo)
{
  for (auto const & syn : syns)
  {
    ASSERT(!syn.empty(), ());
    impl::FullMatchInTrie(trieRoot.m_root, trieRoot.m_prefix, trieRoot.m_prefixSize, syn, toDo);
  }
}

// Calls toDo for each feature whose tokens contains at least one
// synonym as a prefix.
// *NOTE* toDo may be called serveral times for the same feature.
template <typename ToDo>
void MatchTokenPrefixInTrie(SearchQueryParams::TSynonymsVector const & syns,
                            TrieRootPrefix const & trieRoot, ToDo && toDo)
{
  for (auto const & syn : syns)
  {
    ASSERT(!syn.empty(), ());
    impl::PrefixMatchInTrie(trieRoot.m_root, trieRoot.m_prefix, trieRoot.m_prefixSize, syn, toDo);
  }
}

// Fills holder with features whose names correspond to tokens list up to synonyms.
// *NOTE* the same feature may be put in the same holder's slot several times.
template <typename THolder>
void MatchTokensInTrie(vector<SearchQueryParams::TSynonymsVector> const & tokens,
                       TrieRootPrefix const & trieRoot, THolder && holder)
{
  holder.Resize(tokens.size());
  for (size_t i = 0; i < tokens.size(); ++i)
  {
    holder.SwitchTo(i);
    MatchTokenInTrie(tokens[i], trieRoot, holder);
  }
}

// Fills holder with features whose names correspond to tokens list up to synonyms,
// also, last holder's slot will be filled with features corresponding to prefixTokens.
// *NOTE* the same feature may be put in the same holder's slot several times.
template <typename THolder>
void MatchTokensAndPrefixInTrie(vector<SearchQueryParams::TSynonymsVector> const & tokens,
                                SearchQueryParams::TSynonymsVector const & prefixTokens,
                                TrieRootPrefix const & trieRoot, THolder && holder)
{
  MatchTokensInTrie(tokens, trieRoot, holder);

  holder.Resize(tokens.size() + 1);
  holder.SwitchTo(tokens.size());
  MatchTokenPrefixInTrie(prefixTokens, trieRoot, holder);
}

// Fills holder with categories whose description matches to at least one
// token from a search query.
// *NOTE* query prefix will be treated as a complete token in the function.
template <typename THolder>
bool MatchCategoriesInTrie(SearchQueryParams const & params, trie::DefaultIterator const & trieRoot,
                           THolder && holder)
{
  ASSERT_LESS(trieRoot.m_edge.size(), numeric_limits<uint32_t>::max(), ());
  uint32_t const numLangs = static_cast<uint32_t>(trieRoot.m_edge.size());
  for (uint32_t langIx = 0; langIx < numLangs; ++langIx)
  {
    auto const & edge = trieRoot.m_edge[langIx].m_str;
    ASSERT_GREATER_OR_EQUAL(edge.size(), 1, ());
    if (edge[0] == search::kCategoriesLang)
    {
      unique_ptr<trie::DefaultIterator> const catRoot(trieRoot.GoToEdge(langIx));
      MatchTokensInTrie(params.m_tokens, TrieRootPrefix(*catRoot, edge), holder);

      // Last token's prefix is used as a complete token here, to
      // limit the number of features in the last bucket of a
      // holder. Probably, this is a false optimization.
      holder.Resize(params.m_tokens.size() + 1);
      holder.SwitchTo(params.m_tokens.size());
      MatchTokenInTrie(params.m_prefixTokens, TrieRootPrefix(*catRoot, edge), holder);
      return true;
    }
  }
  return false;
}

// Calls toDo with trie root prefix and language code on each language
// allowed by params.
template <typename ToDo>
void ForEachLangPrefix(SearchQueryParams const & params, trie::DefaultIterator const & trieRoot,
                       ToDo && toDo)
{
  ASSERT_LESS(trieRoot.m_edge.size(), numeric_limits<uint32_t>::max(), ());
  uint32_t const numLangs = static_cast<uint32_t>(trieRoot.m_edge.size());
  for (uint32_t langIx = 0; langIx < numLangs; ++langIx)
  {
    auto const & edge = trieRoot.m_edge[langIx].m_str;
    ASSERT_GREATER_OR_EQUAL(edge.size(), 1, ());
    int8_t const lang = static_cast<int8_t>(edge[0]);
    if (edge[0] < search::kCategoriesLang && params.IsLangExist(lang))
    {
      unique_ptr<trie::DefaultIterator> const langRoot(trieRoot.GoToEdge(langIx));
      TrieRootPrefix langPrefix(*langRoot, edge);
      toDo(langPrefix, lang);
    }
  }
}

// Calls toDo for each feature whose description contains *ALL* tokens from a search query.
// Each feature will be passed to toDo only once.
template <typename TFilter, typename ToDo>
void MatchFeaturesInTrie(SearchQueryParams const & params, trie::DefaultIterator const & trieRoot,
                         TFilter const & filter, ToDo && toDo)
{
  TrieValuesHolder<TFilter> categoriesHolder(filter);
  CHECK(MatchCategoriesInTrie(params, trieRoot, categoriesHolder), ("Can't find categories."));

  impl::OffsetIntersecter<TFilter> intersecter(filter);
  for (size_t i = 0; i < params.m_tokens.size(); ++i)
  {
    ForEachLangPrefix(params, trieRoot, [&](TrieRootPrefix & langRoot, int8_t lang)
    {
      MatchTokenInTrie(params.m_tokens[i], langRoot, intersecter);
    });
    categoriesHolder.ForEachValue(i, intersecter);
    intersecter.NextStep();
  }

  if (!params.m_prefixTokens.empty())
  {
    ForEachLangPrefix(params, trieRoot, [&](TrieRootPrefix & langRoot, int8_t /* lang */)
    {
      MatchTokenPrefixInTrie(params.m_prefixTokens, langRoot, intersecter);
    });
    categoriesHolder.ForEachValue(params.m_tokens.size(), intersecter);
    intersecter.NextStep();
  }

  intersecter.ForEachResult(forward<ToDo>(toDo));
}
}  // namespace search