Welcome to mirror list, hosted at ThFree Co, Russian Federation.

feature_data.hpp « indexer - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1f3e57f3e2cb310d2f01e78fb6d721405776917c (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
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
#pragma once

#include "indexer/feature_decl.hpp"
#include "indexer/feature_meta.hpp"

#include "geometry/point2d.hpp"

#include "coding/reader.hpp"
#include "coding/string_utf8_multilang.hpp"
#include "coding/value_opt_string.hpp"

#include <algorithm>
#include <array>
#include <cstdint>
#include <iterator>
#include <string>
#include <utility>
#include <vector>

struct FeatureParamsBase;
class FeatureType;

namespace feature
{
  enum HeaderMask
  {
    HEADER_MASK_TYPE = 7U,
    HEADER_MASK_HAS_NAME = 1U << 3,
    HEADER_MASK_HAS_LAYER = 1U << 4,
    HEADER_MASK_GEOMTYPE = 3U << 5,
    HEADER_MASK_HAS_ADDINFO = 1U << 7
  };

  enum class HeaderGeomType : uint8_t
  {
    /// Coding geometry feature type in 2 bits.
    Point = 0,         /// point feature (addinfo = rank)
    Line = 1U << 5,    /// linear feature (addinfo = ref)
    Area = 1U << 6,    /// area feature (addinfo = house)
    PointEx = 3U << 5  /// point feature (addinfo = house)
  };

  static constexpr int kMaxTypesCount = HEADER_MASK_TYPE + 1;

  enum Layer
  {
    LAYER_LOW = -11,

    LAYER_EMPTY = 0,
    LAYER_TRANSPARENT_TUNNEL = 11,

    LAYER_HIGH = 12
  };

  class TypesHolder
  {
  public:
    using Types = std::array<uint32_t, kMaxTypesCount>;

    TypesHolder() = default;
    explicit TypesHolder(GeomType geomType) : m_geomType(geomType) {}
    explicit TypesHolder(FeatureType & f);

    void Assign(uint32_t type)
    {
      m_types[0] = type;
      m_size = 1;
    }

    /// Accumulation function.
    void Add(uint32_t type)
    {
      ASSERT_LESS(m_size, kMaxTypesCount, ());
      if (m_size < kMaxTypesCount)
        m_types[m_size++] = type;
    }

    GeomType GetGeomType() const { return m_geomType; }

    size_t Size() const { return m_size; }
    bool Empty() const { return (m_size == 0); }
    Types::const_iterator begin() const { return m_types.cbegin(); }
    Types::const_iterator end() const { return m_types.cbegin() + m_size; }
    Types::iterator begin() { return m_types.begin(); }
    Types::iterator end() { return m_types.begin() + m_size; }

    /// Assume that m_types is already sorted by SortBySpec function.
    uint32_t GetBestType() const
    {
      // 0 - is an empty type.
      return (m_size > 0 ? m_types[0] : 0);
    }

    bool Has(uint32_t t) const { return std::find(begin(), end(), t) != end(); }

    template <typename Fn>
    bool RemoveIf(Fn && fn)
    {
      size_t const oldSize = m_size;

      auto const e = std::remove_if(begin(), end(), std::forward<Fn>(fn));
      m_size = std::distance(begin(), e);

      return (m_size != oldSize);
    }

    void Remove(uint32_t type);

    /// Sort types by it's specification (more detailed type goes first).
    void SortBySpec();

    /// Returns true if this->m_types and other.m_types contain same values
    /// in any order. Works in O(n log n).
    bool Equals(TypesHolder const & other) const;

    std::vector<std::string> ToObjectNames() const;

  private:
    Types m_types;
    size_t m_size = 0;

    GeomType m_geomType = GeomType::Undefined;
  };

  std::string DebugPrint(TypesHolder const & holder);

  uint8_t CalculateHeader(size_t const typesCount, HeaderGeomType const headerGeomType,
                          FeatureParamsBase const & params);
}  // namespace feature

/// Feature description struct.
struct FeatureParamsBase
{
  StringUtf8Multilang name;
  StringNumericOptimal house;
  std::string ref;
  int8_t layer;
  uint8_t rank;

  FeatureParamsBase() : layer(0), rank(0) {}

  void MakeZero();

  bool operator == (FeatureParamsBase const & rhs) const;

  bool CheckValid() const;
  std::string DebugString() const;

  /// @return true if feature doesn't have any drawable strings (names, houses, etc).
  bool IsEmptyNames() const;

  template <class TSink>
  void Write(TSink & sink, uint8_t header) const
  {
    using namespace feature;

    if (header & HEADER_MASK_HAS_NAME)
      name.Write(sink);

    if (header & HEADER_MASK_HAS_LAYER)
      WriteToSink(sink, layer);

    if (header & HEADER_MASK_HAS_ADDINFO)
    {
      auto const headerGeomType = static_cast<HeaderGeomType>(header & HEADER_MASK_GEOMTYPE);
      switch (headerGeomType)
      {
      case HeaderGeomType::Point:
        WriteToSink(sink, rank);
        break;
      case HeaderGeomType::Line:
        utils::WriteString(sink, ref);
        break;
      case HeaderGeomType::Area:
      case HeaderGeomType::PointEx:
        house.Write(sink);
        break;
      }
    }
  }

  template <class TSrc>
  void Read(TSrc & src, uint8_t header)
  {
    using namespace feature;

    if (header & HEADER_MASK_HAS_NAME)
      name.Read(src);

    if (header & HEADER_MASK_HAS_LAYER)
      layer = ReadPrimitiveFromSource<int8_t>(src);

    if (header & HEADER_MASK_HAS_ADDINFO)
    {
      auto const headerGeomType = static_cast<HeaderGeomType>(header & HEADER_MASK_GEOMTYPE);
      switch (headerGeomType)
      {
      case HeaderGeomType::Point:
        rank = ReadPrimitiveFromSource<uint8_t>(src);
        break;
      case HeaderGeomType::Line:
        utils::ReadString(src, ref);
        break;
      case HeaderGeomType::Area:
      case HeaderGeomType::PointEx:
        house.Read(src);
        break;
      }
    }
  }
};

class FeatureParams : public FeatureParamsBase
{
  using Base = FeatureParamsBase;

  feature::HeaderGeomType m_geomType = feature::HeaderGeomType::Point;

  feature::Metadata m_metadata;
  feature::AddressData m_addrTags;

public:
  using Types = std::vector<uint32_t>;
  Types m_types;

  bool m_reverseGeometry;

  FeatureParams() : m_reverseGeometry(false) {}

  void ClearName();

  bool AddName(std::string const & lang, std::string const & s);
  bool AddHouseName(std::string const & s);
  bool AddHouseNumber(std::string houseNumber);

  /// @name Used in storing full street address only.
  //@{
  void AddStreet(std::string s);
  void AddPlace(std::string const & s);
  void AddPostcode(std::string const & s);
  void AddAddress(std::string const & s);

  bool FormatFullAddress(m2::PointD const & pt, std::string & res) const;
  //@}

  /// Used for testing purposes now.
  std::string GetStreet() const;
  feature::AddressData const & GetAddressData() const { return m_addrTags; }

  /// Assign parameters except geometry type.
  /// Geometry is independent state and it's set by FeatureType's geometry functions.
  void SetParams(FeatureParams const & rhs)
  {
    Base::operator=(rhs);

    m_types = rhs.m_types;
    m_addrTags = rhs.m_addrTags;
    m_metadata = rhs.m_metadata;
    m_reverseGeometry = rhs.m_reverseGeometry;
  }

  bool IsValid() const { return !m_types.empty(); }
  void SetGeomType(feature::GeomType t);
  void SetGeomTypePointEx();
  feature::GeomType GetGeomType() const;

  void AddType(uint32_t t) { m_types.push_back(t); }

  /// Special function to replace a regular railway station type with
  /// the special subway type for the correspondent city.
  void SetRwSubwayType(char const * cityName);

  /// @param skipType2  Do not accumulate this type if skipType2 != 0.
  /// '2' means 2-level type in classificator tree (also skip child types).
  void AddTypes(FeatureParams const & rhs, uint32_t skipType2);

  bool FinishAddingTypes();

  void SetType(uint32_t t);
  bool PopAnyType(uint32_t & t);
  bool PopExactType(uint32_t t);
  bool IsTypeExist(uint32_t t) const;

  /// Find type that matches "comp" with "level" in classificator hierarchy.
  uint32_t FindType(uint32_t comp, uint8_t level) const;

  bool CheckValid() const;

  uint8_t GetHeader() const;

  feature::Metadata const & GetMetadata() const { return m_metadata; }
  feature::Metadata & GetMetadata() { return m_metadata; }

  /// @param[in] fullStoring \n
  /// - true when saving in temporary files after first generation step \n
  /// - false when final mwm saving
  template <class TSink> void Write(TSink & sink, bool fullStoring) const
  {
    uint8_t const header = GetHeader();

    WriteToSink(sink, header);

    for (size_t i = 0; i < m_types.size(); ++i)
      WriteVarUint(sink, GetIndexForType(m_types[i]));

    if (fullStoring)
    {
      m_metadata.Serialize(sink);
      m_addrTags.Serialize(sink);
    }

    Base::Write(sink, header);
  }

  template <class TSource> void Read(TSource & src)
  {
    using namespace feature;

    uint8_t const header = ReadPrimitiveFromSource<uint8_t>(src);
    m_geomType = static_cast<feature::HeaderGeomType>(header & HEADER_MASK_GEOMTYPE);

    size_t const count = (header & HEADER_MASK_TYPE) + 1;
    for (size_t i = 0; i < count; ++i)
      m_types.push_back(GetTypeForIndex(ReadVarUint<uint32_t>(src)));

    m_metadata.Deserialize(src);
    m_addrTags.Deserialize(src);

    Base::Read(src, header);
  }

private:
  feature::HeaderGeomType GetHeaderGeomType() const;

  static uint32_t GetIndexForType(uint32_t t);
  static uint32_t GetTypeForIndex(uint32_t i);
};

std::string DebugPrint(FeatureParams const & p);