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

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

#include "../std/vector.hpp"
#include "../std/map.hpp"
#include "../std/string.hpp"
#include "../std/iostream.hpp"
#include "../std/shared_ptr.hpp"


class Reader;

class CategoriesHolder
{
public:
  struct Category
  {
    static const uint8_t EMPTY_PREFIX_LENGTH = 10;

    struct Name
    {
      string m_name;
      int8_t m_lang;
      uint8_t m_prefixLengthToSuggest;
    };

    vector<Name> m_synonyms;

    inline void Swap(Category & r)
    {
      m_synonyms.swap(r.m_synonyms);
    }
  };

private:
  typedef strings::UniString StringT;
  typedef multimap<uint32_t, shared_ptr<Category> > ContainerT;
  typedef ContainerT::const_iterator IteratorT;

  multimap<uint32_t, shared_ptr<Category> > m_type2cat;
  multimap<StringT, uint32_t> m_name2type;

public:
  CategoriesHolder() {}
  /// Takes ownership of reader.
  explicit CategoriesHolder(Reader * reader);

  void LoadFromStream(istream & s);

  template <class ToDo>
  void ForEachCategory(ToDo toDo) const
  {
    for (IteratorT i = m_type2cat.begin(); i != m_type2cat.end(); ++i)
      toDo(*i->second);
  }

  template <class ToDo>
  void ForEachName(ToDo toDo) const
  {
    for (IteratorT i = m_type2cat.begin(); i != m_type2cat.end(); ++i)
      for (size_t j = 0; j < i->second->m_synonyms.size(); ++j)
        toDo(i->second->m_synonyms[j]);
  }

  template <class ToDo>
  void ForEachTypeByName(StringT const & name, ToDo toDo) const
  {
    typedef typename multimap<StringT, uint32_t>::const_iterator IterT;

    pair<IterT, IterT> range = m_name2type.equal_range(name);
    while (range.first != range.second)
    {
      toDo(range.first->second);
      ++range.first;
    }
  }

  /// Search name for type with preffered language.
  /// If no name for this language, return first (en) name.
  /// @return false if no categories for type.
  bool GetNameByType(uint32_t type, int8_t lang, string & name) const;

  inline bool IsTypeExist(uint32_t type) const
  {
    // pass any language
    string dummy;
    return GetNameByType(type, 0, dummy);
  }

  inline void Swap(CategoriesHolder & r)
  {
    m_type2cat.swap(r.m_type2cat);
    m_name2type.swap(r.m_name2type);
  }

private:
  void AddCategory(Category & cat, vector<uint32_t> & types);
};

inline void swap(CategoriesHolder & a, CategoriesHolder & b)
{
  return a.Swap(b);
}