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

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

#include "routing_common/transit_types.hpp"

#include "indexer/geometry_coding.hpp"

#include "geometry/point2d.hpp"

#include "coding/point_to_integer.hpp"
#include "coding/read_write_utils.hpp"
#include "coding/reader.hpp"
#include "coding/varint.hpp"
#include "coding/write_to_sink.hpp"

#include "base/assert.hpp"
#include "base/exception.hpp"
#include "base/macros.hpp"

#include <cmath>
#include <cstdint>
#include <limits>
#include <sstream>
#include <string>
#include <type_traits>
#include <vector>

namespace routing
{
namespace transit
{
// Note. For the time being double in transit section is used only for saving weight of edges (in seconds).
// Let us assume that it takes less than 10^7 seconds (115 days) to get from one station to a neighboring one.
double constexpr kMinDoubleAtTransitSection = kInvalidWeight;
double constexpr kMaxDoubleAtTransitSection = 10000000.0;
uint32_t constexpr kDoubleBits = 32;

template <typename Sink>
class Serializer
{
public:
  Serializer(Sink & sink) : m_sink(sink) {}

  template <typename T>
  typename std::enable_if<(std::is_integral<T>::value || std::is_enum<T>::value) &&
                          !std::is_same<T, uint32_t>::value && !std::is_same<T, uint64_t>::value &&
                          !std::is_same<T, int32_t>::value &&
                          !std::is_same<T, int64_t>::value>::type
  operator()(T const & t, char const * /* name */ = nullptr)
  {
    WriteToSink(m_sink, t);
  }

  template <typename T>
  typename std::enable_if<std::is_same<T, uint32_t>::value ||
                          std::is_same<T, uint64_t>::value>::type
  operator()(T t, char const * name = nullptr) const
  {
    WriteVarUint(m_sink, t);
  }

  template <typename T>
  typename std::enable_if<std::is_same<T, int32_t>::value || std::is_same<T, int64_t>::value>::type
  operator()(T t, char const * name = nullptr) const
  {
    WriteVarInt(m_sink, t);
  }

  template <typename T>
  typename std::enable_if<std::is_same<T, double>::value || std::is_same<T, float>::value>::type
  operator()(T d, char const * name = nullptr)
  {
    CHECK_GREATER_OR_EQUAL(d, kMinDoubleAtTransitSection, ());
    CHECK_LESS_OR_EQUAL(d, kMaxDoubleAtTransitSection, ());
    (*this)(DoubleToUint32(d, kMinDoubleAtTransitSection, kMaxDoubleAtTransitSection, kDoubleBits), name);
  }

  void operator()(std::string const & s, char const * /* name */ = nullptr)
  {
    rw::Write(m_sink, s);
  }

  void operator()(m2::PointD const & p, char const * /* name */ = nullptr)
  {
    WriteVarInt(m_sink, PointToInt64(p, POINT_COORD_BITS));
  }

  void operator()(std::vector<m2::PointD> const & vs, char const * /* name */ = nullptr)
  {
    CHECK_LESS_OR_EQUAL(vs.size(), std::numeric_limits<uint64_t>::max(), ());
    WriteVarUint(m_sink, static_cast<uint64_t>(vs.size()));
    m2::PointU lastEncodedPoint;
    for (auto const & p : vs)
    {
      m2::PointU const pointU = PointD2PointU(p, POINT_COORD_BITS);
      WriteVarUint(m_sink, EncodeDelta(pointU, lastEncodedPoint));
      lastEncodedPoint = pointU;
    }
  }

  void operator()(FeatureIdentifiers const & id, char const * name = nullptr)
  {
    (*this)(id.GetFeatureId(), name);
  }

  void operator()(StopIdRanges const & rs, char const * name = nullptr)
  {
    (*this)(rs.GetIds(), name);
  }

  template <typename T>
  void operator()(std::vector<T> const & vs, char const * /* name */ = nullptr)
  {
    CHECK_LESS_OR_EQUAL(vs.size(), std::numeric_limits<uint64_t>::max(), ());
    WriteVarUint(m_sink, static_cast<uint64_t>(vs.size()));
    for (auto const & v : vs)
      (*this)(v);
  }

  template<typename T>
  typename std::enable_if<std::is_class<T>::value>::type operator()(T const & t, char const * /* name */ = nullptr)
  {
    t.Visit(*this);
  }

private:
  Sink & m_sink;
};

template <typename Source>
class Deserializer
{
public:
  Deserializer(Source & source) : m_source(source) {}

  template <typename T>
  typename std::enable_if<(std::is_integral<T>::value || std::is_enum<T>::value) &&
                          !std::is_same<T, uint32_t>::value && !std::is_same<T, uint64_t>::value &&
                          !std::is_same<T, int32_t>::value &&
                          !std::is_same<T, int64_t>::value>::type
  operator()(T & t, char const * name = nullptr)
  {
    ReadPrimitiveFromSource(m_source, t);
  }

  template <typename T>
  typename std::enable_if<std::is_same<T, uint32_t>::value ||
                          std::is_same<T, uint64_t>::value>::type
  operator()(T & t, char const * name = nullptr)
  {
    t = ReadVarUint<T, Source>(m_source);
  }

  template <typename T>
  typename std::enable_if<std::is_same<T, int32_t>::value || std::is_same<T, int64_t>::value>::type
  operator()(T & t, char const * name = nullptr)
  {
    t = ReadVarInt<T, Source>(m_source);
  }

  template <typename T>
  typename std::enable_if<std::is_same<T, double>::value || std::is_same<T, float>::value>::type
  operator()(T & d, char const * name = nullptr)
  {
    uint32_t ui;
    (*this)(ui, name);
    d = Uint32ToDouble(ui, kMinDoubleAtTransitSection, kMaxDoubleAtTransitSection, kDoubleBits);
  }

  void operator()(std::string & s, char const * /* name */ = nullptr)
  {
    rw::Read(m_source, s);
  }

  void operator()(m2::PointD & p, char const * /* name */ = nullptr)
  {
    p = Int64ToPoint(ReadVarInt<int64_t, Source>(m_source), POINT_COORD_BITS);
  }

  void operator()(FeatureIdentifiers & id, char const * name = nullptr)
  {
    FeatureId featureId;
    operator()(featureId, name);
    id = FeatureIdentifiers(kInvalidOsmId, featureId);
  }

  void operator()(StopIdRanges & rs, char const * name = nullptr)
  {
    operator()(rs.m_ids, name);
  }

  void operator()(vector<m2::PointD> & vs, char const * /* name */ = nullptr)
  {
    auto const size = ReadVarUint<uint64_t, Source>(m_source);
    m2::PointU lastDecodedPoint;
    vs.resize(size);
    for (auto & p : vs)
    {
      m2::PointU const pointU = DecodeDelta(ReadVarUint<uint64_t, Source>(m_source), lastDecodedPoint);
      p = PointU2PointD(pointU, POINT_COORD_BITS);
      lastDecodedPoint = pointU;
    }
  }

  template <typename T>
  void operator()(vector<T> & vs, char const * /* name */ = nullptr)
  {
    auto const size = ReadVarUint<uint64_t, Source>(m_source);
    vs.resize(size);
    for (auto & v : vs)
      (*this)(v);
  }

  template<typename T>
  typename std::enable_if<std::is_class<T>::value>::type operator()(T & t, char const * /* name */ = nullptr)
  {
    t.Visit(*this);
  }

private:
  Source & m_source;
};
}  // namespace transit
}  // namespace routing