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

categories_index.hpp « indexer - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3c3aad877ff6b77381558433ab43f7fda7c27c4f (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
#pragma once

#include "categories_holder.hpp"

#include "base/mem_trie.hpp"

#include "std/string.hpp"
#include "std/vector.hpp"

namespace indexer
{
// This class is used to simplify searches of categories by
// synonyms to their names (in various languages).
// An example usage is helping a user who is trying to add
// a new feature with our editor.
// All category data is taken from data/categories.txt.
// All types returned are those from classificator.
class CategoriesIndex
{
public:
  using TCategory = CategoriesHolder::Category;

  CategoriesIndex() : m_catHolder(&GetDefaultCategories()) {}

  CategoriesIndex(CategoriesHolder const & catHolder) : m_catHolder(&catHolder) {}

  CategoriesIndex(CategoriesIndex && other)
    : m_catHolder(other.m_catHolder), m_trie(move(other.m_trie))
  {
  }

  CategoriesIndex & operator=(CategoriesIndex && other) = default;

  CategoriesHolder const * GetCategoriesHolder() const { return m_catHolder; }
  // Adds all categories that match |type|. Only synonyms
  // in language |lang| are added. See indexer/categories_holder.cpp
  // for language enumeration.
  void AddCategoryByTypeAndLang(uint32_t type, int8_t lang);

  // Adds all categories that match |type|. All known synonyms
  // are added.
  void AddCategoryByTypeAllLangs(uint32_t type);

  // Adds all categories from data/classificator.txt. Only
  // names in language |lang| are added.
  void AddAllCategoriesInLang(int8_t lang);

  // Adds all categories from data/classificator.txt.
  void AddAllCategoriesInAllLangs();

  // Returns all categories that have |query| as a substring. Note
  // that all synonyms for a category are contained in a returned
  // value even if only one language was used when adding this
  // category's name to index.
  // Beware weird results when query is a malformed UTF-8 string.
  void GetCategories(string const & query, vector<TCategory> & result) const;

  // Returns all types that match to categories that have |query| as substring.
  // Beware weird results when query is a malformed UTF-8 string.
  // Note: no types are returned if the query is empty.
  void GetAssociatedTypes(string const & query, vector<uint32_t> & result) const;

#ifdef DEBUG
  inline size_t GetNumTrieNodes() const { return m_trie.GetNumNodes(); }
#endif

private:
  // There is a raw pointer instead of const reference
  // here because this class may be used from Objectvie-C
  // so a default constructor is needed.
  CategoriesHolder const * m_catHolder = nullptr;
  base::MemTrie<string, base::VectorValues<uint32_t>> m_trie;
};
}  // namespace indexer