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: 896aa2c181a62fe57cbd55237d7e38b6190b082a (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
#pragma once
#include "search/v2/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.
    int m_stars = 0;          // Valid only if not 0. Used for hotels.
    bool m_isSponsoredHotel = false; // Used for hotels.
    osm::YesNoUnknown m_isOpenNow = osm::Unknown;  // Valid for any result.

    /// True if the struct is already assigned or need to be calculated otherwise.
    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; }
  //@}

  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 v2::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;

  v2::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
{
  vector<Result> m_vec;

  enum StatusT
  {
    NONE,             // default status
    ENDED_CANCELLED,  // search ended with canceling
    ENDED             // search ended itself
  };
  StatusT m_status;

  explicit Results(bool isCancelled)
  {
    m_status = (isCancelled ? ENDED_CANCELLED : ENDED);
  }

public:
  Results() : m_status(NONE) {}

  /// @name To implement end of search notification.
  //@{
  static Results GetEndMarker(bool isCancelled) { return Results(isCancelled); }
  bool IsEndMarker() const { return (m_status != NONE); }
  bool IsEndedNormal() const { return (m_status == ENDED); }
  //@}

  bool AddResult(Result && res);
  /// Fast function that don't do any duplicates checks.
  /// Used in viewport search only.
  void AddResultNoChecks(Result && res)
  {
    ASSERT_LESS(m_vec.size(), numeric_limits<int32_t>::max(), ());
    res.SetPositionInResults(static_cast<int32_t>(m_vec.size()));
    m_vec.push_back(move(res));
  }

  inline void Clear() { m_vec.clear(); }

  typedef vector<Result>::const_iterator IterT;
  inline IterT Begin() const { return m_vec.begin(); }
  inline IterT End() const { return m_vec.end(); }

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

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

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

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

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;

  friend string DebugPrint(AddressInfo const & info);

  void Clear();
};

string DebugPrint(search::Result const &);

}  // namespace search