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
|
#include "search/pre_ranker.hpp"
#include "search/dummy_rank_table.hpp"
#include "search/lazy_centers_table.hpp"
#include "search/pre_ranking_info.hpp"
#include "indexer/mwm_set.hpp"
#include "indexer/rank_table.hpp"
#include "indexer/scales.hpp"
#include "geometry/nearby_points_sweeper.hpp"
#include "base/random.hpp"
#include "base/stl_helpers.hpp"
#include "std/iterator.hpp"
namespace search
{
namespace
{
struct LessFeatureID
{
using TValue = PreResult1;
inline bool operator()(TValue const & lhs, TValue const & rhs) const
{
return lhs.GetId() < rhs.GetId();
}
};
// Orders PreResult1 by following criterion:
// 1. Feature Id (increasing), if same...
// 2. Number of matched tokens from the query (decreasing), if same...
// 3. Index of the first matched token from the query (increasing).
struct ComparePreResult1
{
bool operator()(PreResult1 const & lhs, PreResult1 const & rhs) const
{
if (lhs.GetId() != rhs.GetId())
return lhs.GetId() < rhs.GetId();
auto const & linfo = lhs.GetInfo();
auto const & rinfo = rhs.GetInfo();
if (linfo.GetNumTokens() != rinfo.GetNumTokens())
return linfo.GetNumTokens() > rinfo.GetNumTokens();
return linfo.InnermostTokenRange().Begin() < rinfo.InnermostTokenRange().Begin();
}
};
void SweepNearbyResults(double eps, vector<PreResult1> & results)
{
m2::NearbyPointsSweeper sweeper(eps);
for (size_t i = 0; i < results.size(); ++i)
{
auto const & p = results[i].GetInfo().m_center;
sweeper.Add(p.x, p.y, i);
}
vector<PreResult1> filtered;
sweeper.Sweep([&filtered, &results](size_t i)
{
filtered.push_back(results[i]);
});
results.swap(filtered);
}
} // namespace
PreRanker::PreRanker(Index const & index, Ranker & ranker, size_t limit)
: m_index(index), m_ranker(ranker), m_limit(limit), m_pivotFeatures(index)
{
}
void PreRanker::Init(Params const & params)
{
m_numSentResults = 0;
m_results.clear();
m_params = params;
m_currEmit.clear();
}
void PreRanker::FillMissingFieldsInPreResults()
{
MwmSet::MwmId mwmId;
MwmSet::MwmHandle mwmHandle;
unique_ptr<RankTable> ranks = make_unique<DummyRankTable>();
unique_ptr<LazyCentersTable> centers;
bool const fillCenters = (Size() > BatchSize());
if (fillCenters)
m_pivotFeatures.SetPosition(m_params.m_accuratePivotCenter, m_params.m_scale);
ForEach([&](PreResult1 & r) {
FeatureID const & id = r.GetId();
PreRankingInfo & info = r.GetInfo();
if (id.m_mwmId != mwmId)
{
mwmId = id.m_mwmId;
mwmHandle = m_index.GetMwmHandleById(mwmId);
ranks.reset();
centers.reset();
if (mwmHandle.IsAlive())
{
ranks = RankTable::Load(mwmHandle.GetValue<MwmValue>()->m_cont);
centers = make_unique<LazyCentersTable>(*mwmHandle.GetValue<MwmValue>());
}
if (!ranks)
ranks = make_unique<DummyRankTable>();
}
info.m_rank = ranks->Get(id.m_index);
if (fillCenters)
{
m2::PointD center;
if (centers && centers->Get(id.m_index, center))
{
info.m_distanceToPivot =
MercatorBounds::DistanceOnEarth(m_params.m_accuratePivotCenter, center);
info.m_center = center;
info.m_centerLoaded = true;
}
else
{
info.m_distanceToPivot = m_pivotFeatures.GetDistanceToFeatureMeters(id);
}
}
});
}
void PreRanker::Filter(bool viewportSearch)
{
using TSet = set<PreResult1, LessFeatureID>;
TSet filtered;
sort(m_results.begin(), m_results.end(), ComparePreResult1());
m_results.erase(unique(m_results.begin(), m_results.end(), my::EqualsBy(&PreResult1::GetId)),
m_results.end());
if (m_results.size() > BatchSize())
{
bool const centersLoaded =
all_of(m_results.begin(), m_results.end(),
[](PreResult1 const & result) { return result.GetInfo().m_centerLoaded; });
if (viewportSearch && centersLoaded)
{
FilterForViewportSearch();
ASSERT_LESS_OR_EQUAL(m_results.size(), BatchSize(), ());
for (auto const & result : m_results)
m_currEmit.insert(result.GetId());
}
else
{
sort(m_results.begin(), m_results.end(), &PreResult1::LessDistance);
// Priority is some kind of distance from the viewport or
// position, therefore if we have a bunch of results with the same
// priority, we have no idea here which results are relevant. To
// prevent bias from previous search routines (like sorting by
// feature id) this code randomly selects tail of the
// sorted-by-priority list of pre-results.
double const last = m_results[BatchSize()].GetDistance();
auto b = m_results.begin() + BatchSize();
for (; b != m_results.begin() && b->GetDistance() == last; --b)
;
if (b->GetDistance() != last)
++b;
auto e = m_results.begin() + BatchSize();
for (; e != m_results.end() && e->GetDistance() == last; ++e)
;
// The main reason of shuffling here is to select a random subset
// from the low-priority results. We're using a linear
// congruential method with default seed because it is fast,
// simple and doesn't need an external entropy source.
//
// TODO (@y, @m, @vng): consider to take some kind of hash from
// features and then select a subset with smallest values of this
// hash. In this case this subset of results will be persistent
// to small changes in the original set.
shuffle(b, e, m_rng);
}
}
filtered.insert(m_results.begin(), m_results.begin() + min(m_results.size(), BatchSize()));
if (!viewportSearch)
{
size_t n = min(m_results.size(), BatchSize());
nth_element(m_results.begin(), m_results.begin() + n, m_results.end(), &PreResult1::LessRank);
filtered.insert(m_results.begin(), m_results.begin() + n);
}
m_results.assign(filtered.begin(), filtered.end());
}
void PreRanker::UpdateResults(bool lastUpdate)
{
FillMissingFieldsInPreResults();
Filter(m_viewportSearch);
m_numSentResults += m_results.size();
m_ranker.SetPreResults1(move(m_results));
m_results.clear();
m_ranker.UpdateResults(lastUpdate);
if (lastUpdate)
m_currEmit.swap(m_prevEmit);
}
void PreRanker::ClearCaches()
{
m_pivotFeatures.Clear();
m_prevEmit.clear();
m_currEmit.clear();
}
void PreRanker::FilterForViewportSearch()
{
auto const & viewport = m_params.m_viewport;
my::EraseIf(m_results, [&viewport](PreResult1 const & result) {
auto const & info = result.GetInfo();
return !viewport.IsPointInside(info.m_center);
});
SweepNearbyResults(m_params.m_minDistanceOnMapBetweenResults, m_results);
size_t const n = m_results.size();
if (n <= BatchSize())
return;
size_t const kNumXSlots = 5;
size_t const kNumYSlots = 5;
size_t const kNumBuckets = kNumXSlots * kNumYSlots;
vector<size_t> buckets[kNumBuckets];
double const sizeX = viewport.SizeX();
double const sizeY = viewport.SizeY();
for (size_t i = 0; i < m_results.size(); ++i)
{
auto const & p = m_results[i].GetInfo().m_center;
int dx = static_cast<int>((p.x - viewport.minX()) / sizeX * kNumXSlots);
dx = my::clamp(dx, 0, static_cast<int>(kNumXSlots) - 1);
int dy = static_cast<int>((p.y - viewport.minY()) / sizeY * kNumYSlots);
dy = my::clamp(dy, 0, static_cast<int>(kNumYSlots) - 1);
buckets[dx * kNumYSlots + dy].push_back(i);
}
vector<PreResult1> results;
double const density = static_cast<double>(BatchSize()) / static_cast<double>(n);
for (auto & bucket : buckets)
{
size_t const m = std::min(static_cast<size_t>(ceil(density * bucket.size())), bucket.size());
size_t const old =
partition(bucket.begin(), bucket.end(),
[this](size_t i) { return m_prevEmit.count(m_results[i].GetId()) != 0; }) -
bucket.begin();
if (m <= old)
{
for (size_t i : base::RandomSample(old, m, m_rng))
results.push_back(m_results[bucket[i]]);
}
else
{
for (size_t i = 0; i < old; ++i)
results.push_back(m_results[bucket[i]]);
for (size_t i : base::RandomSample(bucket.size() - old, m - old, m_rng))
results.push_back(m_results[bucket[old + i]]);
}
}
if (results.size() <= BatchSize())
{
m_results.swap(results);
}
else
{
m_results.clear();
for (size_t i : base::RandomSample(results.size(), BatchSize(), m_rng))
m_results.push_back(results[i]);
}
}
} // namespace search
|