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
|
#include "search/result.hpp"
#include "search/common.hpp"
#include "search/geometry_utils.hpp"
#include "indexer/classificator.hpp"
#include "base/string_utils.hpp"
#include <sstream>
using namespace std;
namespace search
{
// Result ------------------------------------------------------------------------------------------
Result::Result(FeatureID const & id, m2::PointD const & pt, string const & str,
string const & address, uint32_t featureType, Metadata const & meta)
: m_resultType(Type::Feature)
, m_id(id)
, m_center(pt)
, m_str(str)
, m_address(address)
, m_featureType(featureType)
, m_metadata(meta)
{
}
Result::Result(m2::PointD const & pt, string const & latlon, string const & address)
: m_resultType(Type::LatLon), m_center(pt), m_str(latlon), m_address(address)
{
}
Result::Result(string const & str, string const & suggest)
: m_resultType(Type::PureSuggest), m_str(str), m_suggestionStr(suggest)
{
}
Result::Result(Result const & res, string const & suggest)
: m_id(res.m_id)
, m_center(res.m_center)
, m_str(res.m_str)
, m_address(res.m_address)
, m_featureType(res.m_featureType)
, m_suggestionStr(suggest)
, m_hightlightRanges(res.m_hightlightRanges)
{
m_resultType = m_id.IsValid() ? Type::SuggestFromFeature : Type::PureSuggest;
}
bool Result::IsSuggest() const
{
return m_resultType == Type::SuggestFromFeature || m_resultType == Type::PureSuggest;
}
bool Result::HasPoint() const
{
return m_resultType != Type::PureSuggest;
}
FeatureID const & Result::GetFeatureID() const
{
ASSERT_EQUAL(m_resultType, Type::Feature, (m_resultType));
return m_id;
}
uint32_t Result::GetFeatureType() const
{
ASSERT_EQUAL(m_resultType, Type::Feature, (m_resultType));
return m_featureType;
}
m2::PointD Result::GetFeatureCenter() const
{
ASSERT(HasPoint(), ());
return m_center;
}
string const & Result::GetSuggestionString() const
{
ASSERT(IsSuggest(), ());
return m_suggestionStr;
}
bool Result::IsEqualSuggest(Result const & r) const
{
return m_suggestionStr == r.m_suggestionStr;
}
bool Result::IsEqualFeature(Result const & r) const
{
if (m_resultType != r.m_resultType)
return false;
ASSERT_EQUAL(m_resultType, Result::Type::Feature, ());
ASSERT(m_id.IsValid() && r.m_id.IsValid(), ());
if (m_id == r.m_id)
return true;
// This function is used to filter duplicate results in cases:
// - emitted World.mwm and Country.mwm
// - after additional search in all mwm
// so it's suitable here to test for 500m
return m_str == r.m_str && m_address == r.m_address && m_featureType == r.m_featureType &&
PointDistance(m_center, r.m_center) < 500.0;
}
void Result::AddHighlightRange(pair<uint16_t, uint16_t> const & range)
{
m_hightlightRanges.push_back(range);
}
pair<uint16_t, uint16_t> const & Result::GetHighlightRange(size_t idx) const
{
ASSERT(idx < m_hightlightRanges.size(), ());
return m_hightlightRanges[idx];
}
void Result::PrependCity(string const & city)
{
// It is expected that if |m_address| is not empty,
// it starts with the region name. Avoid duplication
// in the case where this region name coincides with
// the city name and prepend otherwise.
strings::SimpleTokenizer tok(m_address, ",");
if (tok && *tok != city)
m_address = city + ", " + m_address;
}
string Result::ToStringForStats() const
{
string readableType;
if (GetResultType() == Type::Feature)
readableType = classif().GetReadableObjectName(m_featureType);
string s;
s.append(GetString());
s.append("|");
s.append(readableType);
s.append("|");
s.append(IsSuggest() ? "1" : "0");
return s;
}
string DebugPrint(Result::Type type)
{
switch (type)
{
case Result::Type::Feature: return "Feature";
case Result::Type::LatLon: return "LatLon";
case Result::Type::PureSuggest: return "PureSuggest";
case Result::Type::SuggestFromFeature: return "SuggestFromFeature";
}
return "Unknown";
}
string DebugPrint(Result const & result)
{
string readableType;
if (result.GetResultType() == Result::Type::Feature)
readableType = classif().GetReadableObjectName(result.GetFeatureType());
ostringstream os;
os << "Result [";
os << "name: " << result.GetString() << ", ";
os << "type: " << readableType << ", ";
os << "info: " << DebugPrint(result.GetRankingInfo());
os << "]";
return os.str();
}
// Results -----------------------------------------------------------------------------------------
Results::Results()
{
Clear();
}
bool Results::AddResult(Result && result)
{
// Find first feature result.
auto it = find_if(m_results.begin(), m_results.end(),
[](Result const & r) { return r.GetResultType() == Result::Type::Feature; });
if (result.IsSuggest())
{
auto const d = distance(m_results.begin(), it);
if (d >= static_cast<decltype(d)>(kMaxNumSuggests))
return false;
for (auto i = m_results.begin(); i != it; ++i)
if (result.IsEqualSuggest(*i))
return false;
InsertResult(it, move(result));
}
else
{
for (; it != m_results.end(); ++it)
{
if (result.IsEqualFeature(*it))
return false;
}
InsertResult(m_results.end(), move(result));
}
return true;
}
void Results::AddResultNoChecks(Result && result)
{
InsertResult(m_results.end(), move(result));
}
void Results::AddResultsNoChecks(ConstIter first, ConstIter last)
{
while (first != last)
{
auto resultCopy = *first++;
AddResultNoChecks(move(resultCopy));
}
}
void Results::AddBookmarkResult(bookmarks::Result const & result)
{
m_bookmarksResults.push_back(result);
}
void Results::Clear()
{
m_results.clear();
m_bookmarksResults.clear();
m_status = Status::None;
m_hotelsClassif.Clear();
}
size_t Results::GetSuggestsCount() const
{
size_t i = 0;
// Suggests always go first, so we need to compute length of prefix
// of suggests.
while (i < m_results.size() && m_results[i].IsSuggest())
++i;
return i;
}
bookmarks::Results const & Results::GetBookmarksResults() const
{
return m_bookmarksResults;
}
void Results::InsertResult(vector<Result>::iterator where, Result && result)
{
ASSERT_LESS(m_results.size(), numeric_limits<int32_t>::max(), ());
for (auto it = where; it != m_results.end(); ++it)
{
auto & r = *it;
auto const position = r.GetPositionInResults();
r.SetPositionInResults(position + 1);
}
result.SetPositionInResults(static_cast<int32_t>(distance(m_results.begin(), where)));
m_hotelsClassif.Add(result);
m_results.insert(where, move(result));
}
string DebugPrint(search::Results const & results)
{
return DebugPrintSequence(results.begin(), results.end());
}
} // namespace search
|