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

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

#include "indexer/feature_data.hpp"
#include "indexer/ftypes_mapping.hpp"

#include "coding/csv_reader.hpp"

#include "platform/platform.hpp"

#include "base/assert.hpp"
#include "base/logging.hpp"

#include <array>
#include <cstdint>
#include <initializer_list>
#include <sstream>
#include <string>
#include <utility>

namespace ftraits
{
template <typename Base, typename Value>
class TraitsBase
{
public:
  static bool GetValue(feature::TypesHolder const & types, Value & value)
  {
    auto const & instance = Instance();
    auto const it = Find(types);
    if (!instance.m_matcher.IsValid(it))
      return false;

    value = it->second;
    return true;
  }

  static bool GetType(feature::TypesHolder const & types, uint32_t & type)
  {
    auto const & instance = Instance();
    auto const it = Find(types);
    if (!instance.m_matcher.IsValid(it))
      return false;

    type = it->first;
    return true;
  }

private:
  using ConstIterator = typename ftypes::HashMapMatcher<uint32_t, Value>::ConstIterator;

  static ConstIterator Find(feature::TypesHolder const & types)
  {
    auto const & instance = Instance();

    auto const excluded = instance.m_excluded.Find(types);
    if (instance.m_excluded.IsValid(excluded))
      return instance.m_matcher.End();

    return instance.m_matcher.Find(types);
  }

protected:
  static TraitsBase const & Instance()
  {
    static Base instance;
    return instance;
  }

  ftypes::HashMapMatcher<uint32_t, Value> m_matcher;
  ftypes::HashSetMatcher<uint32_t> m_excluded;
};

enum UGCType
{
  UGCTYPE_NONE = 0u,
  UGCTYPE_RATING = 1u << 0,   // 1
  UGCTYPE_REVIEWS = 1u << 1,  // 2
  UGCTYPE_DETAILS = 1u << 2   // 4
};

using UGCTypeMask = unsigned;
using UGCRatingCategories = std::vector<std::string>;

struct UGCItem
{
  UGCItem() {}
  UGCItem(UGCTypeMask m, UGCRatingCategories && c)
    : m_mask(m), m_categories(std::move(c))
  {
  }

  UGCTypeMask m_mask = UGCTYPE_NONE;
  UGCRatingCategories m_categories;
};

class UGC : public TraitsBase<UGC, UGCItem>
{
  friend class TraitsBase;

  std::array<UGCType, 3> const m_masks = {{UGCTYPE_RATING, UGCTYPE_REVIEWS, UGCTYPE_DETAILS}};

  UGC()
  {
    coding::CSVReader reader;
    auto const fileReader = GetPlatform().GetReader("ugc_types.csv");
    reader.Read(*fileReader, [this](coding::CSVReader::Row const & row) {
      size_t constexpr kTypePos = 0;
      size_t constexpr kCategoriesPos = 4;

      ASSERT_EQUAL(row.size(), 5, ());

      UGCItem item(ReadMasks(row), ParseByWhitespaces(row[kCategoriesPos]));
      auto typePath = ParseByWhitespaces(row[kTypePos]);

      if (IsUGCAvailable(item.m_mask))
        m_matcher.AppendType(std::move(typePath), std::move(item));
      else
        m_excluded.AppendType(std::move(typePath));
    });
  }

  UGCTypeMask ReadMasks(coding::CSVReader::Row const & row)
  {
    size_t constexpr kMasksBegin = 1;
    size_t constexpr kMasksEnd = 4;

    UGCTypeMask maskType = UGCTYPE_NONE;
    for (size_t i = kMasksBegin; i < kMasksEnd; i++)
    {
      int flag;
      if (!strings::to_int(row[i], flag))
      {
        LOG(LERROR, ("File ugc_types.csv must contain a bit mask of supported ugc traits!"));
        return UGCTYPE_NONE;
      }

      if (flag)
        maskType |= m_masks[i - 1];
    }

    return maskType;
  }

  std::vector<std::string> ParseByWhitespaces(std::string const & str)
  {
    std::istringstream iss(str);
    return {std::istream_iterator<std::string>(iss), std::istream_iterator<std::string>()};
  }

public:
  static bool IsUGCAvailable(UGCTypeMask mask) { return mask != UGCTYPE_NONE; }
  static bool IsRatingAvailable(UGCTypeMask mask) { return mask & UGCTYPE_RATING; }
  static bool IsReviewsAvailable(UGCTypeMask mask) { return mask & UGCTYPE_REVIEWS; }
  static bool IsDetailsAvailable(UGCTypeMask mask) { return mask & UGCTYPE_DETAILS; }

  static bool IsUGCAvailable(feature::TypesHolder const & types)
  {
    UGCItem item;
    return GetValue(types, item) ? IsUGCAvailable(item.m_mask) : false;
  }
  static bool IsRatingAvailable(feature::TypesHolder const & types)
  {
    UGCItem item;
    return GetValue(types, item) ? IsRatingAvailable(item.m_mask) : false;
  }
  static bool IsReviewsAvailable(feature::TypesHolder const & types)
  {
    UGCItem item;
    return GetValue(types, item) ? IsReviewsAvailable(item.m_mask) : false;
  }
  static bool IsDetailsAvailable(feature::TypesHolder const & types)
  {
    UGCItem item;
    return GetValue(types, item) ? IsDetailsAvailable(item.m_mask) : false;
  }
  static UGCRatingCategories GetCategories(feature::TypesHolder const & types)
  {
    UGCItem item;
    GetValue(types, item);
    return item.m_categories;
  }
};

enum class WheelchairAvailability
{
  No,
  Yes,
  Limited
};

inline std::string DebugPrint(WheelchairAvailability wheelchair)
{
  switch (wheelchair)
  {
  case WheelchairAvailability::No: return "No";
  case WheelchairAvailability::Yes: return "Yes";
  case WheelchairAvailability::Limited: return "Limited";
  }
}

class Wheelchair : public TraitsBase<Wheelchair, WheelchairAvailability>
{
  friend class TraitsBase;

  using TypesInitializer = std::initializer_list<std::initializer_list<char const *>>;

  Wheelchair()
  {
    m_matcher.Append<TypesInitializer>({{"wheelchair", "no"}}, WheelchairAvailability::No);
    m_matcher.Append<TypesInitializer>({{"wheelchair", "yes"}}, WheelchairAvailability::Yes);
    m_matcher.Append<TypesInitializer>({{"wheelchair", "limited"}},
                                       WheelchairAvailability::Limited);
  }
};

}  // namespace ftraits