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

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

#include "indexer/feature_decl.hpp"

#include "editor/yes_no_unknown.hpp"

#include "geometry/point2d.hpp"
#include "geometry/rect2d.hpp"

#include "base/buffer_vector.hpp"

#include "std/string.hpp"


namespace search
{
// Search result. Search returns a list of them, ordered by score.
class Result
{
public:
  enum ResultType
  {
    RESULT_FEATURE,
    RESULT_LATLON,
    RESULT_SUGGEST_PURE,
    RESULT_SUGGEST_FROM_FEATURE
  };

  /// Metadata for search results. Considered valid if m_resultType == RESULT_FEATURE.
  struct Metadata
  {
    string m_cuisine;                              // Valid only if not empty. Used for restaurants.

    // Following fields are used for hotels only.
    string m_hotelApproximatePricing;
    string m_hotelRating;
    int m_stars = 0;
    bool m_isSponsoredHotel = false;
    bool m_isHotel = false;

    osm::YesNoUnknown m_isOpenNow = osm::Unknown;  // Valid for any result.

    bool m_isInitialized = false;
  };

  /// For RESULT_FEATURE.
  Result(FeatureID const & id, m2::PointD const & pt, string const & str, string const & address,
         string const & type, uint32_t featureType, Metadata const & meta);

  /// For RESULT_LATLON.
  Result(m2::PointD const & pt, string const & latlon, string const & address);

  /// For RESULT_SUGGESTION_PURE.
  Result(string const & str, string const & suggest);

  /// For RESULT_SUGGESTION_FROM_FEATURE.
  Result(Result const & res, string const & suggest);

  /// Strings that is displayed in the GUI.
  //@{
  string const & GetString() const { return m_str; }
  string const & GetAddress() const { return m_address; }
  string const & GetFeatureType() const { return m_type; }
  string const & GetCuisine() const { return m_metadata.m_cuisine; }
  string const & GetHotelRating() const { return m_metadata.m_hotelRating; }
  string const & GetHotelApproximatePricing() const { return m_metadata.m_hotelApproximatePricing; }
  //@}

  osm::YesNoUnknown IsOpenNow() const { return m_metadata.m_isOpenNow; }
  int GetStarsCount() const { return m_metadata.m_stars; }

  bool IsSuggest() const;
  bool HasPoint() const;

  /// Type of the result.
  ResultType GetResultType() const;

  /// Feature id in mwm.
  /// @precondition GetResultType() == RESULT_FEATURE
  FeatureID const & GetFeatureID() const;

  /// Center point of a feature.
  /// @precondition HasPoint() == true
  m2::PointD GetFeatureCenter() const;

  /// String to write in the search box.
  /// @precondition IsSuggest() == true
  char const * GetSuggestionString() const;

  bool IsEqualSuggest(Result const & r) const;
  bool IsEqualFeature(Result const & r) const;

  void AddHighlightRange(pair<uint16_t, uint16_t> const & range);
  pair<uint16_t, uint16_t> const & GetHighlightRange(size_t idx) const;
  inline size_t GetHighlightRangesCount() const { return m_hightlightRanges.size(); }

  void AppendCity(string const & name);

  int32_t GetPositionInResults() const { return m_positionInResults; }
  void SetPositionInResults(int32_t pos) { m_positionInResults = pos; }

  inline RankingInfo const & GetRankingInfo() const { return m_info; }

  template <typename TInfo>
  inline void SetRankingInfo(TInfo && info)
  {
    m_info = forward<TInfo>(info);
  }

  // Returns a representation of this result that is
  // sent to the statistics servers and later used to measure
  // the quality of our search engine.
  string ToStringForStats() const;

private:
  FeatureID m_id;
  m2::PointD m_center;
  string m_str, m_address, m_type;
  uint32_t m_featureType;
  string m_suggestionStr;
  buffer_vector<pair<uint16_t, uint16_t>, 4> m_hightlightRanges;

  RankingInfo m_info;

  // The position that this result occupied in the vector returned
  // by a search query. -1 if undefined.
  int32_t m_positionInResults = -1;

public:
  Metadata m_metadata;
};

class Results
{
public:
  using Iter = vector<Result>::const_iterator;

  Results();

  inline bool IsEndMarker() const { return m_status != Status::None; }
  inline bool IsEndedNormal() const { return m_status == Status::EndedNormal; }
  inline bool IsEndedCancelled() const { return m_status == Status::EndedCancelled; }

  void SetEndMarker(bool cancelled)
  {
    m_status = cancelled ? Status::EndedCancelled : Status::EndedNormal;
  }

  bool AddResult(Result && result);

  // Fast version of AddResult() that doesn't do any duplicates checks.
  void AddResultNoChecks(Result && result);

  void Clear();

  inline Iter begin() const { return m_results.begin(); }
  inline Iter end() const { return m_results.end(); }

  inline size_t GetCount() const { return m_results.size(); }
  size_t GetSuggestsCount() const;

  inline Result & GetResult(size_t i)
  {
    ASSERT_LESS(i, m_results.size(), ());
    return m_results[i];
  }

  inline Result const & GetResult(size_t i) const
  {
    ASSERT_LESS(i, m_results.size(), ());
    return m_results[i];
  }

  inline void Swap(Results & rhs) { m_results.swap(rhs.m_results); }

private:
  enum class Status
  {
    None,
    EndedCancelled,
    EndedNormal
  };

  // Inserts |result| in |m_results| at position denoted by |where|.
  //
  // *NOTE* all iterators, references and pointers to |m_results| are
  // invalid after the call.
  void InsertResult(vector<Result>::iterator where, Result && result);

  vector<Result> m_results;
  Status m_status;
};

struct AddressInfo
{
  string m_country, m_city, m_street, m_house, m_name;
  vector<string> m_types;
  double m_distanceMeters = -1.0;

  string GetPinName() const;    // Caroline
  string GetPinType() const;    // shop

  string FormatPinText() const; // Caroline (clothes shop)
  string FormatTypes() const;   // clothes shop
  string GetBestType() const;
  bool IsEmptyName() const;

  enum AddressType { DEFAULT, SEARCH_RESULT };
  // 7 vulica Frunze
  string FormatHouseAndStreet(AddressType type = DEFAULT) const;
  // 7 vulica Frunze, Minsk, Belarus
  string FormatAddress(AddressType type = DEFAULT) const;
  // Caroline, 7 vulica Frunze, Minsk, Belarus
  string FormatNameAndAddress(AddressType type = DEFAULT) const;

  void Clear();

  friend string DebugPrint(AddressInfo const & info);
};

string DebugPrint(search::Result const & result);
}  // namespace search