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

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

#include "search/categories_cache.hpp"
#include "search/cbv.hpp"
#include "search/mwm_context.hpp"

#include "indexer/ftypes_matcher.hpp"
#include "indexer/mwm_set.hpp"

#include "std/map.hpp"
#include "std/shared_ptr.hpp"
#include "std/sstream.hpp"
#include "std/string.hpp"
#include "std/unique_ptr.hpp"
#include "std/utility.hpp"
#include "std/vector.hpp"

class FeatureType;

namespace search
{
namespace hotels_filter
{
struct Rating
{
  using Value = float;

  static Value const kDefault;

  static bool Lt(Value lhs, Value rhs) { return lhs + 0.05 < rhs; }
  static bool Gt(Value lhs, Value rhs) { return lhs > rhs + 0.05; }
  static bool Eq(Value lhs, Value rhs) { return !Lt(lhs, rhs) && !Gt(lhs, rhs); }

  template <typename Description>
  static Value Select(Description const & d)
  {
    return d.m_rating;
  }

  static char const * Name() { return "Rating"; }
};

struct PriceRate
{
  using Value = int;

  static Value const kDefault;

  static bool Lt(Value lhs, Value rhs) { return lhs < rhs; }
  static bool Gt(Value lhs, Value rhs) { return lhs > rhs; }
  static bool Eq(Value lhs, Value rhs) { return lhs == rhs; }

  template <typename Description>
  static Value Select(Description const & d)
  {
    return d.m_priceRate;
  }

  static char const * Name() { return "PriceRate"; }
};

struct Description
{
  void FromFeature(FeatureType & ft);

  Rating::Value m_rating = Rating::kDefault;
  PriceRate::Value m_priceRate = PriceRate::kDefault;
  unsigned m_types = 0;
};

struct Rule
{
  virtual ~Rule() = default;

  static bool IsIdentical(shared_ptr<Rule> const & lhs, shared_ptr<Rule> const & rhs);

  virtual bool Matches(Description const & d) const = 0;
  virtual bool IdenticalTo(Rule const & rhs) const = 0;
  virtual string ToString() const = 0;
};

string DebugPrint(Rule const & rule);

template <typename Field>
struct EqRule final : public Rule
{
  using Value = typename Field::Value;

  explicit EqRule(Value value) : m_value(value) {}

  // Rule overrides:
  bool Matches(Description const & d) const override
  {
    return Field::Eq(Field::Select(d), m_value);
  }

  bool IdenticalTo(Rule const & rhs) const override
  {
    auto const * r = dynamic_cast<EqRule const *>(&rhs);
    return r && Field::Eq(r->m_value, m_value);
  }

  string ToString() const override
  {
    ostringstream os;
    os << "[ " << Field::Name() << " == " << m_value << " ]";
    return os.str();
  }

  Value const m_value;
};

template <typename Field>
struct LtRule final : public Rule
{
  using Value = typename Field::Value;

  explicit LtRule(Value value) : m_value(value) {}

  // Rule overrides:
  bool Matches(Description const & d) const override
  {
    return Field::Lt(Field::Select(d), m_value);
  }

  bool IdenticalTo(Rule const & rhs) const override
  {
    auto const * r = dynamic_cast<LtRule const *>(&rhs);
    return r && Field::Eq(r->m_value, m_value);
  }

  string ToString() const override
  {
    ostringstream os;
    os << "[ " << Field::Name() << " < " << m_value << " ]";
    return os.str();
  }

  Value const m_value;
};

template <typename Field>
struct LeRule final : public Rule
{
  using Value = typename Field::Value;

  explicit LeRule(Value value) : m_value(value) {}

  // Rule overrides:
  bool Matches(Description const & d) const override
  {
    auto const value = Field::Select(d);
    return Field::Lt(value, m_value) || Field::Eq(value, m_value);
  }

  bool IdenticalTo(Rule const & rhs) const override
  {
    auto const * r = dynamic_cast<LeRule const *>(&rhs);
    return r && Field::Eq(r->m_value, m_value);
  }

  string ToString() const override
  {
    ostringstream os;
    os << "[ " << Field::Name() << " <= " << m_value << " ]";
    return os.str();
  }

  Value const m_value;
};

template <typename Field>
struct GtRule final : public Rule
{
  using Value = typename Field::Value;

  explicit GtRule(Value value) : m_value(value) {}

  // Rule overrides:
  bool Matches(Description const & d) const override
  {
    return Field::Gt(Field::Select(d), m_value);
  }

  bool IdenticalTo(Rule const & rhs) const override
  {
    auto const * r = dynamic_cast<GtRule const *>(&rhs);
    return r && Field::Eq(r->m_value, m_value);
  }

  string ToString() const override
  {
    ostringstream os;
    os << "[ " << Field::Name() << " > " << m_value << " ]";
    return os.str();
  }

  Value const m_value;
};

template <typename Field>
struct GeRule final : public Rule
{
  using Value = typename Field::Value;

  explicit GeRule(Value value) : m_value(value) {}

  // Rule overrides:
  bool Matches(Description const & d) const override
  {
    auto const value = Field::Select(d);
    return Field::Gt(value, m_value) || Field::Eq(value, m_value);
  }

  bool IdenticalTo(Rule const & rhs) const override
  {
    auto const * r = dynamic_cast<GeRule const *>(&rhs);
    return r && Field::Eq(r->m_value, m_value);
  }

  string ToString() const override
  {
    ostringstream os;
    os << "[ " << Field::Name() << " >= " << m_value << " ]";
    return os.str();
  }

  Value const m_value;
};

struct AndRule final : public Rule
{
  AndRule(shared_ptr<Rule> lhs, shared_ptr<Rule> rhs) : m_lhs(move(lhs)), m_rhs(move(rhs)) {}

  // Rule overrides:
  bool Matches(Description const & d) const override
  {
    bool matches = true;
    if (m_lhs)
      matches = matches && m_lhs->Matches(d);
    if (m_rhs)
      matches = matches && m_rhs->Matches(d);
    return matches;
  }

  bool IdenticalTo(Rule const & rhs) const override
  {
    auto const * r = dynamic_cast<AndRule const *>(&rhs);
    return r && IsIdentical(m_lhs, r->m_lhs) && IsIdentical(m_rhs, r->m_rhs);
  }

  string ToString() const override
  {
    ostringstream os;
    os << "[";
    os << (m_lhs ? m_lhs->ToString() : "<none>");
    os << " && ";
    os << (m_rhs ? m_rhs->ToString() : "<none>");
    os << "]";
    return os.str();
  }

  shared_ptr<Rule> m_lhs;
  shared_ptr<Rule> m_rhs;
};

struct OrRule final : public Rule
{
  OrRule(shared_ptr<Rule> lhs, shared_ptr<Rule> rhs) : m_lhs(move(lhs)), m_rhs(move(rhs)) {}

  // Rule overrides:
  bool Matches(Description const & d) const override
  {
    bool matches = false;
    if (m_lhs)
      matches = matches || m_lhs->Matches(d);
    if (m_rhs)
      matches = matches || m_rhs->Matches(d);
    return matches;
  }

  bool IdenticalTo(Rule const & rhs) const override
  {
    auto const * r = dynamic_cast<OrRule const *>(&rhs);
    return r && IsIdentical(m_lhs, r->m_lhs) && IsIdentical(m_rhs, r->m_rhs);
  }

  string ToString() const override
  {
    ostringstream os;
    os << "[";
    os << (m_lhs ? m_lhs->ToString() : "<none>");
    os << " || ";
    os << (m_rhs ? m_rhs->ToString() : "<none>");
    os << "]";
    return os.str();
  }

  shared_ptr<Rule> m_lhs;
  shared_ptr<Rule> m_rhs;
};

struct OneOfRule final : public Rule
{
  explicit OneOfRule(unsigned types) : m_types(types) {}

  // Rule overrides:
  bool Matches(Description const & d) const override { return (d.m_types & m_types) != 0; }

  bool IdenticalTo(Rule const & rhs) const override
  {
    auto const * r = dynamic_cast<OneOfRule const *>(&rhs);
    return r && m_types == r->m_types;
  }

  string ToString() const override
  {
    ostringstream os;
    os << "[ one of:";
    for (size_t i = 0; i < static_cast<size_t>(ftypes::IsHotelChecker::Type::Count); ++i)
    {
      unsigned const bit = 1U << i;
      if ((m_types & bit) == 0)
        continue;

      auto const type = static_cast<ftypes::IsHotelChecker::Type>(i);
      os << " " << ftypes::IsHotelChecker::GetHotelTypeTag(type);
    }
    os << " ]";
    return os.str();
  }

  unsigned m_types;
};

template <typename Field>
shared_ptr<Rule> Eq(typename Field::Value value)
{
  return make_shared<EqRule<Field>>(value);
}

template <typename Field>
shared_ptr<Rule> Lt(typename Field::Value value)
{
  return make_shared<LtRule<Field>>(value);
}

template <typename Field>
shared_ptr<Rule> Le(typename Field::Value value)
{
  return make_shared<LeRule<Field>>(value);
}

template <typename Field>
inline shared_ptr<Rule> Gt(typename Field::Value value)
{
  return make_shared<GtRule<Field>>(value);
}

template <typename Field>
shared_ptr<Rule> Ge(typename Field::Value value)
{
  return make_shared<GeRule<Field>>(value);
}

inline shared_ptr<Rule> And(shared_ptr<Rule> lhs, shared_ptr<Rule> rhs)
{
  return make_shared<AndRule>(lhs, rhs);
}

inline shared_ptr<Rule> Or(shared_ptr<Rule> lhs, shared_ptr<Rule> rhs)
{
  return make_shared<OrRule>(lhs, rhs);
}

inline shared_ptr<Rule> Is(ftypes::IsHotelChecker::Type type)
{
  CHECK(type != ftypes::IsHotelChecker::Type::Count, ());
  return make_shared<OneOfRule>(1U << static_cast<unsigned>(type));
}

inline shared_ptr<Rule> OneOf(unsigned types) { return make_shared<OneOfRule>(types); }

class HotelsFilter
{
public:
  using Descriptions = vector<pair<uint32_t, Description>>;

  class ScopedFilter
  {
  public:
    ScopedFilter(MwmSet::MwmId const & mwmId, Descriptions const & descriptions,
                 shared_ptr<Rule> rule);

    bool Matches(FeatureID const & fid) const;

  private:
    MwmSet::MwmId const m_mwmId;
    Descriptions const & m_descriptions;
    shared_ptr<Rule> const m_rule;
  };

  HotelsFilter(HotelsCache & hotels);

  unique_ptr<ScopedFilter> MakeScopedFilter(MwmContext const & context, shared_ptr<Rule> rule);

  void ClearCaches();

private:
  Descriptions const & GetDescriptions(MwmContext const & context);

  HotelsCache & m_hotels;
  map<MwmSet::MwmId, Descriptions> m_descriptions;
};
}  // namespace hotels_filter
}  // namespace search