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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
|
#include "indexer/classificator.hpp"
#include "indexer/tree_structure.hpp"
#include "base/macros.hpp"
#include "base/logging.hpp"
#include "std/bind.hpp"
#include "std/algorithm.hpp"
#include "std/iterator.hpp"
/////////////////////////////////////////////////////////////////////////////////////////
// ClassifObject implementation
/////////////////////////////////////////////////////////////////////////////////////////
ClassifObject * ClassifObject::AddImpl(string const & s)
{
if (m_objs.empty()) m_objs.reserve(30);
m_objs.push_back(ClassifObject(s));
return &(m_objs.back());
}
ClassifObject * ClassifObject::Add(string const & s)
{
ClassifObject * p = Find(s);
return (p ? p : AddImpl(s));
}
ClassifObject * ClassifObject::Find(string const & s)
{
for (iter_t i = m_objs.begin(); i != m_objs.end(); ++i)
if ((*i).m_name == s)
return &(*i);
return 0;
}
void ClassifObject::AddDrawRule(drule::Key const & k)
{
for (size_t i = 0; i < m_drawRule.size(); ++i)
if (k == m_drawRule[i]) return;
m_drawRule.push_back(k);
}
ClassifObjectPtr ClassifObject::BinaryFind(string const & s) const
{
const_iter_t i = lower_bound(m_objs.begin(), m_objs.end(), s, less_name_t());
if ((i == m_objs.end()) || ((*i).m_name != s))
return ClassifObjectPtr(0, 0);
else
return ClassifObjectPtr(&(*i), distance(m_objs.begin(), i));
}
void ClassifObject::LoadPolicy::Start(size_t i)
{
ClassifObject * p = Current();
p->m_objs.push_back(ClassifObject());
base_type::Start(i);
}
namespace
{
struct less_scales
{
bool operator() (drule::Key const & l, int r) const { return l.m_scale < r; }
bool operator() (int l, drule::Key const & r) const { return l < r.m_scale; }
bool operator() (drule::Key const & l, drule::Key const & r) const { return l.m_scale < r.m_scale; }
};
}
void ClassifObject::LoadPolicy::EndChilds()
{
ClassifObject * p = Current();
ASSERT ( p->m_objs.back().m_name.empty(), () );
p->m_objs.pop_back();
}
void ClassifObject::Sort()
{
sort(m_drawRule.begin(), m_drawRule.end(), less_scales());
sort(m_objs.begin(), m_objs.end(), less_name_t());
for_each(m_objs.begin(), m_objs.end(), bind(&ClassifObject::Sort, _1));
}
void ClassifObject::Swap(ClassifObject & r)
{
swap(m_name, r.m_name);
swap(m_drawRule, r.m_drawRule);
swap(m_objs, r.m_objs);
swap(m_visibility, r.m_visibility);
}
ClassifObject const * ClassifObject::GetObject(size_t i) const
{
if (i < m_objs.size())
return &(m_objs[i]);
else
{
LOG(LINFO, ("Map contains object that has no classificator entry", i, m_name));
return 0;
}
}
void ClassifObject::ConcatChildNames(string & s) const
{
s.clear();
size_t const count = m_objs.size();
for (size_t i = 0; i < count; ++i)
{
s += m_objs[i].GetName();
if (i != count-1) s += '|';
}
}
/////////////////////////////////////////////////////////////////////////////////////////
// Classificator implementation
/////////////////////////////////////////////////////////////////////////////////////////
Classificator & classif()
{
static Classificator c;
return c;
}
namespace ftype
{
uint8_t const bits_count = 6;
uint8_t const levels_count = 5;
uint8_t const max_value = (1 << bits_count) - 1;
void set_value(uint32_t & type, uint8_t level, uint8_t value)
{
level *= bits_count; // level to bits
uint32_t const m1 = uint32_t(max_value) << level;
type &= (~m1); // zero bits
uint32_t const m2 = uint32_t(value) << level;
type |= m2; // set bits
}
uint8_t get_value(uint32_t type, uint8_t level)
{
level *= bits_count; // level to bits;
uint32_t const m = uint32_t(max_value) << level;
type &= m; // leave only our bits
type = type >> level; // move to start
ASSERT ( type <= max_value, ("invalid output value", type) );
return uint8_t(type); // conversion
}
uint8_t get_control_level(uint32_t type)
{
uint8_t count = 0;
while (type > 1)
{
type = type >> bits_count;
++count;
}
return count;
}
void PushValue(uint32_t & type, uint8_t value)
{
ASSERT ( value <= max_value, ("invalid input value", value) );
uint8_t const cl = get_control_level(type);
// to avoid warning in release
#ifdef RELEASE
UNUSED_VALUE(levels_count);
#endif
ASSERT ( cl < levels_count, (cl) );
set_value(type, cl, value);
// set control level
set_value(type, cl+1, 1);
}
bool GetValue(uint32_t type, uint8_t level, uint8_t & value)
{
ASSERT ( level < levels_count, ("invalid input level", level) );
if (level < get_control_level(type))
{
value = get_value(type, level);
return true;
}
return false;
}
void PopValue(uint32_t & type)
{
uint8_t const cl = get_control_level(type);
ASSERT_GREATER ( cl, 0, () );
// remove control level
set_value(type, cl, 0);
// set control level
set_value(type, cl-1, 1);
}
void TruncValue(uint32_t & type, uint8_t level)
{
ASSERT_GREATER ( level, 0, () );
uint8_t cl = get_control_level(type);
while (cl > level)
{
// remove control level
set_value(type, cl, 0);
--cl;
// set control level
set_value(type, cl, 1);
}
}
uint8_t GetLevel(uint32_t type)
{
return get_control_level(type);
}
}
namespace
{
class suitable_getter
{
typedef vector<drule::Key> vec_t;
typedef vec_t::const_iterator iter_t;
vec_t const & m_rules;
drule::KeysT & m_keys;
bool m_added;
void add_rule(int ft, iter_t i)
{
static const int visible[3][drule::count_of_rules] = {
{ 0, 0, 1, 1, 1, 0, 0 }, // fpoint
{ 1, 0, 0, 0, 0, 1, 0 }, // fline
{ 1, 1, 1, 1, 1, 0, 0 } // farea
};
if (visible[ft][i->m_type] == 1)
{
m_keys.push_back(*i);
m_added = true;
}
}
public:
suitable_getter(vec_t const & rules, drule::KeysT & keys)
: m_rules(rules), m_keys(keys)
{
}
void find(int ft, int scale)
{
iter_t i = lower_bound(m_rules.begin(), m_rules.end(), scale, less_scales());
while (i != m_rules.end() && i->m_scale == scale)
add_rule(ft, i++);
}
};
}
void ClassifObject::GetSuitable(int scale, feature::EGeomType ft, drule::KeysT & keys) const
{
ASSERT(ft >= 0 && ft <= 2, ());
// 2. Check visibility criterion for scale first.
if (!m_visibility[scale])
return;
// find rules for 'scale'
suitable_getter rulesGetter(m_drawRule, keys);
rulesGetter.find(ft, scale);
}
bool ClassifObject::IsDrawable(int scale) const
{
return (m_visibility[scale] && IsDrawableAny());
}
bool ClassifObject::IsDrawableAny() const
{
return (m_visibility != visible_mask_t() && !m_drawRule.empty());
}
bool ClassifObject::IsDrawableLike(feature::EGeomType ft) const
{
ASSERT(ft >= 0 && ft <= 2, ());
// check the very common criterion first
if (!IsDrawableAny())
return false;
static const int visible[3][drule::count_of_rules] = {
{0, 0, 1, 1, 1, 0, 0}, // fpoint
{1, 0, 0, 0, 0, 1, 0}, // fline
{0, 1, 0, 0, 0, 0, 0} // farea (!!! key difference with GetSuitable !!!)
};
for (size_t i = 0; i < m_drawRule.size(); ++i)
{
ASSERT ( m_drawRule[i].m_type < drule::count_of_rules, () );
if (visible[ft][m_drawRule[i].m_type] == 1)
{
/// @todo Check if rule's scale is reachable according to m_visibility (see GetSuitable algorithm).
return true;
}
}
return false;
}
pair<int, int> ClassifObject::GetDrawScaleRange() const
{
if (!IsDrawableAny())
return make_pair(-1, -1);
int const count = static_cast<int>(m_visibility.size());
int left = -1;
for (int i = 0; i < count; ++i)
if (m_visibility[i])
{
left = i;
break;
}
ASSERT_NOT_EQUAL ( left, -1, () );
int right = left;
for (int i = count-1; i > left; --i)
if (m_visibility[i])
{
right = i;
break;
}
return make_pair(left, right);
}
void Classificator::ReadClassificator(istream & s)
{
ClassifObject::LoadPolicy policy(&m_root);
tree::LoadTreeAsText(s, policy);
m_root.Sort();
m_coastType = GetTypeByPath({ "natural", "coastline" });
}
void Classificator::SortClassificator()
{
GetMutableRoot()->Sort();
}
template <class IterT> uint32_t Classificator::GetTypeByPathImpl(IterT beg, IterT end) const
{
ClassifObject const * p = GetRoot();
uint32_t type = ftype::GetEmptyValue();
while (beg != end)
{
ClassifObjectPtr ptr = p->BinaryFind(*beg++);
if (!ptr)
return 0;
ftype::PushValue(type, ptr.GetIndex());
p = ptr.get();
}
return type;
}
uint32_t Classificator::GetTypeByPathSafe(vector<string> const & path) const
{
return GetTypeByPathImpl(path.begin(), path.end());
}
uint32_t Classificator::GetTypeByPath(vector<string> const & path) const
{
uint32_t const type = GetTypeByPathImpl(path.begin(), path.end());
ASSERT_NOT_EQUAL(type, 0, (path));
return type;
}
uint32_t Classificator::GetTypeByPath(initializer_list<char const *> const & lst) const
{
uint32_t const type = GetTypeByPathImpl(lst.begin(), lst.end());
ASSERT_NOT_EQUAL(type, 0, (lst));
return type;
}
void Classificator::ReadTypesMapping(istream & s)
{
m_mapping.Load(s);
}
void Classificator::Clear()
{
ClassifObject("world").Swap(m_root);
m_mapping.Clear();
}
string Classificator::GetReadableObjectName(uint32_t type) const
{
string s = classif().GetFullObjectName(type);
// remove ending dummy symbol
ASSERT ( !s.empty(), () );
s.resize(s.size()-1);
// replace separator
replace(s.begin(), s.end(), '|', '-');
return s;
}
|