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

index.hpp « indexer - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: dafe1bca8565c8d08bbf01d0ef38cc24a4cbb5f8 (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
340
341
342
343
344
345
346
347
348
#pragma once
#include "indexer/cell_id.hpp"
#include "indexer/data_factory.hpp"
#include "indexer/feature_covering.hpp"
#include "indexer/features_offsets_table.hpp"
#include "indexer/features_vector.hpp"
#include "indexer/mwm_set.hpp"
#include "indexer/scale_index.hpp"
#include "indexer/unique_index.hpp"

#include "coding/file_container.hpp"

#include "defines.hpp"

#include "base/macros.hpp"
#include "base/observer_list.hpp"

#include "std/algorithm.hpp"
#include "std/limits.hpp"
#include "std/utility.hpp"
#include "std/vector.hpp"


class MwmInfoEx : public MwmInfo
{
public:
  unique_ptr<feature::FeaturesOffsetsTable> m_table;
};

class MwmValue : public MwmSet::MwmValueBase
{
public:
  FilesContainerR const m_cont;
  IndexFactory m_factory;
  platform::LocalCountryFile const m_file;
  feature::FeaturesOffsetsTable const * m_table;

  explicit MwmValue(platform::LocalCountryFile const & localFile);
  void SetTable(MwmInfoEx & info);

  inline feature::DataHeader const & GetHeader() const { return m_factory.GetHeader(); }
  inline version::MwmVersion const & GetMwmVersion() const { return m_factory.GetMwmVersion(); }
  inline string const & GetCountryFileName() const { return m_file.GetCountryFile().GetNameWithoutExt(); }
};

class Index : public MwmSet
{
protected:
  /// @name MwmSet overrides.
  //@{
  unique_ptr<MwmInfo> CreateInfo(platform::LocalCountryFile const & localFile) const override;

  unique_ptr<MwmValueBase> CreateValue(MwmInfo & info) const override;

  void OnMwmDeregistered(platform::LocalCountryFile const & localFile) override;
  //@}

public:
  /// An Observer interface to MwmSet. Note that these functions can
  /// be called from *ANY* thread because most signals are sent when
  /// some thread releases its MwmHandle, so overrides must be as fast
  /// as possible and non-blocking when it's possible.
  class Observer
  {
  public:
    virtual ~Observer() = default;

    /// Called when a map is registered for a first time and can be
    /// used.
    virtual void OnMapRegistered(platform::LocalCountryFile const & localFile) {}

    /// Called when a map is deregistered and can not be used.
    virtual void OnMapDeregistered(platform::LocalCountryFile const & localFile) {}
  };

  /// Registers a new map.
  pair<MwmId, RegResult> RegisterMap(platform::LocalCountryFile const & localFile);

  /// Deregisters a map from internal records.
  ///
  /// \param countryFile A countryFile denoting a map to be deregistered.
  /// \return True if the map was successfully deregistered. If map is locked
  ///         now, returns false.
  bool DeregisterMap(platform::CountryFile const & countryFile);

  bool AddObserver(Observer & observer);

  bool RemoveObserver(Observer const & observer);

private:

  template <typename F> class ReadMWMFunctor
  {
    F & m_f;
  public:
    ReadMWMFunctor(F & f) : m_f(f) {}

    void operator()(MwmHandle const & handle, covering::CoveringGetter & cov, uint32_t scale) const
    {
      MwmValue const * pValue = handle.GetValue<MwmValue>();
      if (pValue)
      {
        feature::DataHeader const & header = pValue->GetHeader();

        // Prepare needed covering.
        uint32_t const lastScale = header.GetLastScale();

        // In case of WorldCoasts we should pass correct scale in ForEachInIntervalAndScale.
        if (scale > lastScale) scale = lastScale;

        // Use last coding scale for covering (see index_builder.cpp).
        covering::IntervalsT const & interval = cov.Get(lastScale);

        // prepare features reading
        FeaturesVector fv(pValue->m_cont, header, pValue->m_table);
        ScaleIndex<ModelReaderPtr> index(pValue->m_cont.GetReader(INDEX_FILE_TAG),
                                         pValue->m_factory);

        // iterate through intervals
        CheckUniqueIndexes checkUnique(header.GetFormat() >= version::v5);
        MwmId const mwmID = handle.GetId();

        for (auto const & i : interval)
        {
          index.ForEachInIntervalAndScale([&] (uint32_t index)
          {
            if (checkUnique(index))
            {
              FeatureType feature;

              fv.GetByIndex(index, feature);
              feature.SetID(FeatureID(mwmID, index));

              m_f(feature);
            }
          }, i.first, i.second, scale);
        }
      }
    }
  };

  template <typename F> class ReadFeatureIndexFunctor
  {
    F & m_f;
  public:
    ReadFeatureIndexFunctor(F & f) : m_f(f) {}

    void operator()(MwmHandle const & handle, covering::CoveringGetter & cov, uint32_t scale) const
    {
      MwmValue const * pValue = handle.GetValue<MwmValue>();
      if (pValue)
      {
        feature::DataHeader const & header = pValue->GetHeader();

        // Prepare needed covering.
        int const lastScale = header.GetLastScale();

        // In case of WorldCoasts we should pass correct scale in ForEachInIntervalAndScale.
        if (scale > lastScale) scale = lastScale;

        // Use last coding scale for covering (see index_builder.cpp).
        covering::IntervalsT const & interval = cov.Get(lastScale);
        ScaleIndex<ModelReaderPtr> index(pValue->m_cont.GetReader(INDEX_FILE_TAG),
                                         pValue->m_factory);

        // iterate through intervals
        CheckUniqueIndexes checkUnique(header.GetFormat() >= version::v5);
        MwmId const mwmID = handle.GetId();

        for (auto const & i : interval)
        {
          index.ForEachInIntervalAndScale([&] (uint32_t index)
          {
            if (checkUnique(index))
              m_f(FeatureID(mwmID, index));
          }, i.first, i.second, scale);
        }
      }
    }
  };

public:

  template <typename F>
  void ForEachInRect(F & f, m2::RectD const & rect, uint32_t scale) const
  {
    ReadMWMFunctor<F> implFunctor(f);
    ForEachInIntervals(implFunctor, covering::ViewportWithLowLevels, rect, scale);
  }

  template <typename F>
  void ForEachInRect_TileDrawing(F & f, m2::RectD const & rect, uint32_t scale) const
  {
    ReadMWMFunctor<F> implFunctor(f);
    ForEachInIntervals(implFunctor, covering::LowLevelsOnly, rect, scale);
  }

  template <typename F>
  void ForEachFeatureIDInRect(F & f, m2::RectD const & rect, uint32_t scale) const
  {
    ReadFeatureIndexFunctor<F> implFunctor(f);
    ForEachInIntervals(implFunctor, covering::LowLevelsOnly, rect, scale);
  }

  template <typename F>
  void ForEachInScale(F & f, uint32_t scale) const
  {
    ReadMWMFunctor<F> implFunctor(f);
    ForEachInIntervals(implFunctor, covering::FullCover, m2::RectD::GetInfiniteRect(), scale);
  }

  // "features" must be sorted using FeatureID::operator< as predicate
  template <typename F>
  void ReadFeatures(F & f, vector<FeatureID> const & features) const
  {
    size_t currentIndex = 0;
    while (currentIndex < features.size())
      currentIndex = ReadFeatureRange(f, features, currentIndex);
  }

  /// Guard for loading features from particular MWM by demand.
  class FeaturesLoaderGuard
  {
  public:
    FeaturesLoaderGuard(Index const & parent, MwmId id);

    inline MwmSet::MwmId GetId() const { return m_handle.GetId(); }
    string GetCountryFileName() const;
    bool IsWorld() const;
    void GetFeatureByIndex(uint32_t index, FeatureType & ft);

  private:
    MwmHandle m_handle;
    FeaturesVector m_vector;
  };

  template <typename F>
  void ForEachInRectForMWM(F & f, m2::RectD const & rect, uint32_t scale, MwmId const id) const
  {
    MwmHandle const handle = GetMwmHandleById(id);
    if (handle.IsAlive())
    {
      covering::CoveringGetter cov(rect, covering::ViewportWithLowLevels);
      ReadMWMFunctor<F> fn(f);
      fn(handle, cov, scale);
    }
  }

private:

  // "features" must be sorted using FeatureID::operator< as predicate
  template <typename F>
  size_t ReadFeatureRange(F & f, vector<FeatureID> const & features, size_t index) const
  {
    ASSERT_LESS(index, features.size(), ());
    size_t result = index;
    MwmId id = features[index].m_mwmId;

    if (!id.IsAlive())
    {
      while (features[result].m_mwmId == id)
        ++result;

      return result;
    }

    MwmHandle const handle = GetMwmHandleById(id);
    MwmValue const * pValue = handle.GetValue<MwmValue>();
    if (pValue)
    {
      FeaturesVector featureReader(pValue->m_cont, pValue->GetHeader(), pValue->m_table);
      while (result < features.size() && id == features[result].m_mwmId)
      {
        FeatureID const & featureId = features[result];
        FeatureType featureType;

        featureReader.GetByIndex(featureId.m_index, featureType);
        featureType.SetID(featureId);

        f(featureType);
        ++result;
      }
    }
    else
    {
      // Fake feature identifier which is used to determine the right
      // bound of all features in an mwm corresponding to id, because
      // it's greater than any other feature in the mwm in accordance
      // with FeatureID::operator<().
      FeatureID const fakeID(id, numeric_limits<uint32_t>::max());
      result = distance(features.cbegin(), upper_bound(features.cbegin(), features.cend(), fakeID));
    }

    return result;
  }

  template <typename F>
  void ForEachInIntervals(F & f, covering::CoveringMode mode, m2::RectD const & rect,
                          uint32_t scale) const
  {
    vector<shared_ptr<MwmInfo>> mwms;
    GetMwmsInfo(mwms);

    covering::CoveringGetter cov(rect, mode);

    MwmId worldID[2];

    for (shared_ptr<MwmInfo> const & info : mwms)
    {
      if (info->m_minScale <= scale && scale <= info->m_maxScale &&
          rect.IsIntersect(info->m_limitRect))
      {
        MwmId id(info);
        switch (info->GetType())
        {
          case MwmInfo::COUNTRY:
          {
            MwmHandle const handle = GetMwmHandleById(id);
            f(handle, cov, scale);
          }
          break;

          case MwmInfo::COASTS:
            worldID[0] = id;
            break;

          case MwmInfo::WORLD:
            worldID[1] = id;
            break;
        }
      }
    }

    if (worldID[0].IsAlive())
    {
      MwmHandle const handle = GetMwmHandleById(worldID[0]);
      f(handle, cov, scale);
    }

    if (worldID[1].IsAlive())
    {
      MwmHandle const handle = GetMwmHandleById(worldID[1]);
      f(handle, cov, scale);
    }
  }

  my::ObserverList<Observer> m_observers;
};