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

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

#include "coding/compressed_bit_vector.hpp"
#include "coding/read_write_utils.hpp"
#include "coding/write_to_sink.hpp"

#include "indexer/coding_params.hpp"
#include "indexer/geometry_serialization.hpp"

#include "base/assert.hpp"
#include "base/logging.hpp"

#include "std/algorithm.hpp"
#include "std/unique_ptr.hpp"
#include "std/utility.hpp"

/// Following classes are supposed to be used with StringsFile. They
/// allow to write/read them, compare or serialize to an in-memory
/// buffer. The reason to use these classes instead of
/// buffer_vector<uint8_t, N> is that in some specific cases, like
/// compressed search index construction, they allow to avoid
/// redundant serialization-deserialization or sorting.

// A wrapper around feature index.
struct FeatureIndexValue
{
  FeatureIndexValue() : m_featureId(0) {}

  FeatureIndexValue(uint64_t featureId) : m_featureId(featureId) {}

  bool operator<(FeatureIndexValue const & o) const { return m_featureId < o.m_featureId; }

  bool operator==(FeatureIndexValue const & o) const { return m_featureId == o.m_featureId; }

  void Swap(FeatureIndexValue & o) { swap(m_featureId, o.m_featureId); }

  uint64_t m_featureId;
};

struct FeatureWithRankAndCenter
{
  FeatureWithRankAndCenter() : m_pt(m2::PointD()), m_featureId(0), m_rank(0) {}

  FeatureWithRankAndCenter(m2::PointD pt, uint32_t featureId, uint8_t rank)
    : m_pt(pt), m_featureId(featureId), m_rank(rank)
  {
  }

  bool operator<(FeatureWithRankAndCenter const & o) const { return m_featureId < o.m_featureId; }

  bool operator==(FeatureWithRankAndCenter const & o) const { return m_featureId == o.m_featureId; }

  void Swap(FeatureWithRankAndCenter & o)
  {
    swap(m_pt, o.m_pt);
    swap(m_featureId, o.m_featureId);
    swap(m_rank, o.m_rank);
  }

  m2::PointD m_pt;       // Center point of the feature.
  uint32_t m_featureId;  // Feature identifier.
  uint8_t m_rank;        // Rank of the feature.
};

template <typename TValue>
class SingleValueSerializer;

template <>
class SingleValueSerializer<FeatureWithRankAndCenter>
{
public:
  using TValue = FeatureWithRankAndCenter;

  SingleValueSerializer(serial::CodingParams const & codingParams) : m_codingParams(codingParams) {}

  template <typename TWriter>
  void Serialize(TWriter & writer, TValue const & v) const
  {
    serial::SavePoint(writer, v.m_pt, m_codingParams);
    WriteToSink(writer, v.m_featureId);
    WriteToSink(writer, v.m_rank);
  }

  template <typename TReader>
  void Deserialize(TReader & reader, TValue & v) const
  {
    ReaderSource<TReader> src(reader);
    DeserializeFromSource(src, v);
  }

  template <typename TSource>
  void DeserializeFromSource(TSource & src, TValue & v) const
  {
    v.m_pt = serial::LoadPoint(src, m_codingParams);
    v.m_featureId = ReadPrimitiveFromSource<uint32_t>(src);
    v.m_rank = ReadPrimitiveFromSource<uint8_t>(src);
  }

private:
  serial::CodingParams m_codingParams;
};

template <>
class SingleValueSerializer<FeatureIndexValue>
{
public:
  using TValue = FeatureIndexValue;

  SingleValueSerializer() = default;

  // todo(@mpimenov). Remove.
  SingleValueSerializer(serial::CodingParams const & /* codingParams */) {}

  // The serialization and deserialization is needed for StringsFile.
  // Use ValueList for group serialization in CBVs.
  template <typename TWriter>
  void Serialize(TWriter & writer, TValue const & v) const
  {
    WriteToSink(writer, v.m_featureId);
  }

  template <typename TReader>
  void Deserialize(TReader & reader, TValue & v) const
  {
    ReaderSource<TReader> src(reader);
    DeserializeFromSource(src, v);
  }

  template <typename TSource>
  void DeserializeFromSource(TSource & src, TValue & v) const
  {
    v.m_featureId = ReadPrimitiveFromSource<uint64_t>(src);
  }
};

// This template is used to accumulate, serialize and deserialize
// a group of values of the same type.
template <typename TValue>
class ValueList;

// ValueList<FeatureIndexValue> serializes a group of feature
// indices as a compressed bit vector.
template <>
class ValueList<FeatureIndexValue>
{
public:
  using TValue = FeatureIndexValue;

  ValueList() = default;

  ValueList(ValueList<FeatureIndexValue> const & o)
  {
    if (o.m_cbv)
      m_cbv = o.m_cbv->Clone();
  }

  void Init(vector<FeatureIndexValue> const & values)
  {
    vector<uint64_t> ids(values.size());
    for (size_t i = 0; i < ids.size(); ++i)
      ids[i] = values[i].m_featureId;
    m_cbv = coding::CompressedBitVectorBuilder::FromBitPositions(move(ids));
  }

  // This method returns number of values in the current instance of
  // ValueList<FeatureIndexValue>, but as these values are actually
  // features indices and can be dumped as a single serialized
  // compressed bit vector, this method returns 1 when there're at
  // least one feature's index in the list - so, compressed bit
  // vector will be built and serialized - and 0 otherwise.
  size_t Size() const { return (m_cbv && m_cbv->PopCount() != 0) ? 1 : 0; }

  bool IsEmpty() const { return Size() == 0; }

  template <typename TSink>
  void Serialize(TSink & sink, SingleValueSerializer<TValue> const & /* serializer */) const
  {
    if (IsEmpty())
      return;
    vector<uint8_t> buf;
    MemWriter<decltype(buf)> writer(buf);
    m_cbv->Serialize(writer);
    sink.Write(buf.data(), buf.size());
  }

  // Note the valueCount parameter. It is here for compatibility with
  // an old data format that was serializing FeatureWithRankAndCenter`s.
  // They were put in a vector, this vector's size was encoded somehow
  // and then the vector was written with a method similar to Serialize above.
  // The deserialization code read the valueCount separately and then
  // read each FeatureWithRankAndCenter one by one.
  // A better approach is to make Serialize/Deserialize responsible for
  // every part of serialization and as such it should not need valueCount.
  template <typename TSource>
  void Deserialize(TSource & src, uint32_t valueCount,
                   SingleValueSerializer<TValue> const & /* serializer */)
  {
    if (valueCount > 0)
      m_cbv = coding::CompressedBitVectorBuilder::DeserializeFromSource(src);
    else
      m_cbv.reset();
  }

  template <typename TSource>
  void Deserialize(TSource & src, SingleValueSerializer<TValue> const & /* serializer */)
  {
    if (src.Size() > 0)
      m_cbv = coding::CompressedBitVectorBuilder::DeserializeFromSource(src);
    else
      m_cbv.reset();
  }

  template <typename TF>
  void ForEach(TF && f) const
  {
    if (IsEmpty())
      return;
    coding::CompressedBitVectorEnumerator::ForEach(*m_cbv, [&f](uint64_t const bitPosition)
                                                   {
                                                     f(TValue(bitPosition));
                                                   });
  }

private:
  unique_ptr<coding::CompressedBitVector> m_cbv;
};

/// ValueList<FeatureWithRankAndCenter> sequentially serializes
/// encoded features infos.
template <>
class ValueList<FeatureWithRankAndCenter>
{
public:
  using TValue = FeatureWithRankAndCenter;
  using TSerializer = SingleValueSerializer<TValue>;

  void Init(vector<TValue> const & values) { m_values = values; }

  size_t Size() const { return m_values.size(); }

  bool IsEmpty() const { return m_values.empty(); }

  template <typename TSink>
  void Serialize(TSink & sink, SingleValueSerializer<TValue> const & serializer) const
  {
    for (auto const & value : m_values)
      serializer.Serialize(sink, value);
  }

  template <typename TSource>
  void Deserialize(TSource & src, uint32_t valueCount,
                   SingleValueSerializer<TValue> const & serializer)
  {
    m_values.resize(valueCount);
    for (size_t i = 0; i < valueCount; ++i)
      serializer.DeserializeFromSource(src, m_values[i]);
  }

  // When valueCount is not known, Deserialize reads
  // until the source is exhausted.
  template <typename TSource>
  void Deserialize(TSource & src, SingleValueSerializer<TValue> const & serializer)
  {
    m_values.clear();
    while (src.Size() > 0)
    {
      m_values.push_back(TValue());
      serializer.DeserializeFromSource(src, m_values.back());
    }
  }

  template <typename TF>
  void ForEach(TF && f) const
  {
    for (auto const & value : m_values)
      f(value);
  }

private:
  vector<TValue> m_values;
};