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
|
#include "search/result.hpp"
#include "search/common.hpp"
#include "search/geometry_utils.hpp"
namespace search
{
Result::Result(FeatureID const & id, m2::PointD const & pt, string const & str,
string const & address, string const & type, uint32_t featureType,
Metadata const & meta)
: m_id(id)
, m_center(pt)
, m_str(str.empty() ? type : str) //!< Features with empty names can be found after suggestion.
, m_address(address)
, m_type(type)
, m_featureType(featureType)
, m_metadata(meta)
{
}
Result::Result(m2::PointD const & pt, string const & latlon, string const & address)
: m_center(pt), m_str(latlon), m_address(address)
{
}
Result::Result(string const & str, string const & suggest)
: 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_type(res.m_type)
, m_featureType(res.m_featureType)
, m_suggestionStr(suggest)
, m_hightlightRanges(res.m_hightlightRanges)
{
}
Result::ResultType Result::GetResultType() const
{
bool const idValid = m_id.IsValid();
if (!m_suggestionStr.empty())
return (idValid ? RESULT_SUGGEST_FROM_FEATURE : RESULT_SUGGEST_PURE);
if (idValid)
return RESULT_FEATURE;
else
return RESULT_LATLON;
}
bool Result::IsSuggest() const
{
return !m_suggestionStr.empty();
}
bool Result::HasPoint() const
{
return (GetResultType() != RESULT_SUGGEST_PURE);
}
FeatureID const & Result::GetFeatureID() const
{
#if defined(DEBUG)
auto const type = GetResultType();
ASSERT(type == RESULT_FEATURE, (type));
#endif
return m_id;
}
m2::PointD Result::GetFeatureCenter() const
{
ASSERT(HasPoint(), ());
return m_center;
}
char const * Result::GetSuggestionString() const
{
ASSERT(IsSuggest(), ());
return m_suggestionStr.c_str();
}
bool Result::IsEqualSuggest(Result const & r) const
{
return (m_suggestionStr == r.m_suggestionStr);
}
bool Result::IsEqualFeature(Result const & r) const
{
ResultType const type = GetResultType();
if (type != r.GetResultType())
return false;
ASSERT_EQUAL(type, Result::RESULT_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::AppendCity(string const & name)
{
// Prepend only if city is absent in region (mwm) name.
if (m_address.find(name) == string::npos)
m_address = name + ", " + m_address;
}
string Result::ToStringForStats() const
{
string s;
s.append(GetString());
s.append("|");
s.append(GetFeatureType());
s.append("|");
s.append(IsSuggest() ? "1" : "0");
return s;
}
bool Results::AddResult(Result && res)
{
// Find first feature result.
auto it = find_if(m_vec.begin(), m_vec.end(), [](Result const & r)
{
switch (r.GetResultType())
{
case Result::RESULT_FEATURE:
return true;
default:
return false;
}
});
if (res.IsSuggest())
{
if (distance(m_vec.begin(), it) >= MAX_SUGGESTS_COUNT)
return false;
for (auto i = m_vec.begin(); i != it; ++i)
if (res.IsEqualSuggest(*i))
return false;
for (auto i = it; i != m_vec.end(); ++i)
{
auto & r = *i;
auto const oldPos = r.GetPositionInResults();
r.SetPositionInResults(oldPos + 1);
}
res.SetPositionInResults(distance(m_vec.begin(), it));
m_vec.insert(it, move(res));
}
else
{
for (; it != m_vec.end(); ++it)
if (res.IsEqualFeature(*it))
return false;
res.SetPositionInResults(m_vec.size());
m_vec.push_back(move(res));
}
return true;
}
size_t Results::GetSuggestsCount() const
{
size_t res = 0;
for (size_t i = 0; i < GetCount(); i++)
{
if (m_vec[i].IsSuggest())
++res;
else
{
// Suggests always go first, so we can exit here.
break;
}
}
return res;
}
////////////////////////////////////////////////////////////////////////////////////
// AddressInfo implementation
////////////////////////////////////////////////////////////////////////////////////
bool AddressInfo::IsEmptyName() const
{
return m_name.empty() && m_house.empty();
}
string AddressInfo::GetPinName() const
{
if (IsEmptyName() && !m_types.empty())
return m_types[0];
else
return m_name.empty() ? m_house : m_name;
}
string AddressInfo::GetPinType() const
{
return GetBestType();
}
string AddressInfo::FormatPinText() const
{
// select name or house if name is empty
string const & ret = (m_name.empty() ? m_house : m_name);
string const type = GetBestType();
if (type.empty())
return ret;
return (ret.empty() ? type : (ret + " (" + type + ')'));
}
string AddressInfo::FormatHouseAndStreet(AddressType type /* = DEFAULT */) const
{
// Check whether we can format address according to the query type and actual address distance.
/// @todo We can add "Near" prefix here in future according to the distance.
if (m_distanceMeters > 0.0)
{
if (type == SEARCH_RESULT && m_distanceMeters > 50.0)
return string();
if (m_distanceMeters > 200.0)
return string();
}
string result = m_street;
if (!m_house.empty())
{
if (!result.empty())
result += ", ";
result += m_house;
}
return result;
}
string AddressInfo::FormatAddress(AddressType type /* = DEFAULT */) const
{
string result = FormatHouseAndStreet(type);
if (!m_city.empty())
{
if (!result.empty())
result += ", ";
result += m_city;
}
if (!m_country.empty())
{
if (!result.empty())
result += ", ";
result += m_country;
}
return result;
}
string AddressInfo::FormatNameAndAddress(AddressType type /* = DEFAULT */) const
{
string const addr = FormatAddress(type);
return (m_name.empty() ? addr : m_name + ", " + addr);
}
string AddressInfo::FormatTypes() const
{
string result;
for (size_t i = 0; i < m_types.size(); ++i)
{
ASSERT ( !m_types.empty(), () );
if (!result.empty())
result += ' ';
result += m_types[i];
}
return result;
}
string AddressInfo::GetBestType() const
{
if (m_types.empty())
return string();
/// @todo Probably, we should skip some "common" types here like in TypesHolder::SortBySpec.
ASSERT(!m_types[0].empty(), ());
return m_types[0];
}
void AddressInfo::Clear()
{
m_country.clear();
m_city.clear();
m_street.clear();
m_house.clear();
m_name.clear();
m_types.clear();
}
string DebugPrint(AddressInfo const & info)
{
return info.FormatNameAndAddress();
}
string DebugPrint(Result const & r)
{
return "Result { Name: " + r.GetString() + "; Type: " + r.GetFeatureType() +
"; Info: " + DebugPrint(r.GetRankingInfo()) + " }";
}
} // namespace search
|