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: ff3afbd85fef42b87d8322bd1fd8d82c53cdfb95 (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
#pragma once
#include "cell_id.hpp"
#include "data_factory.hpp"
#include "feature_covering.hpp"
#include "features_vector.hpp"
#include "scale_index.hpp"
#include "mwm_set.hpp"

#include "../coding/file_container.hpp"

#include "../../defines.hpp"

#include "../std/unordered_set.hpp"
#include "../std/vector.hpp"


class MwmValue : public MwmSet::MwmValueBase
{
public:
  FilesContainerR m_cont;
  string m_name;
  IndexFactory m_factory;

  explicit MwmValue(string const & name);

  inline feature::DataHeader const & GetHeader() const
  {
    return m_factory.GetHeader();
  }
};

class Index : public MwmSet
{
protected:
  /// @return mwm format version
  virtual int GetInfo(string const & name, MwmInfo & info) const;
  virtual MwmValue * CreateValue(string const & name) const;
  virtual void UpdateMwmInfo(MwmId id);

public:
  Index();
  ~Index();

  class MwmLock : public MwmSet::MwmLock
  {
  public:
    MwmLock(Index const & index, MwmId mwmId)
      : MwmSet::MwmLock(const_cast<Index &>(index), mwmId) {}

    inline MwmValue * GetValue() const
    {
      return static_cast<MwmValue *>(MwmSet::MwmLock::GetValue());
    }

    inline FilesContainerR const & GetContainer() const { return GetValue()->m_cont; }
    inline feature::DataHeader const & GetHeader() const { return GetValue()->GetHeader(); }
  };

  bool DeleteMap(string const & fileName);
  bool UpdateMap(string const & fileName, m2::RectD & rect);

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

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

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

private:

  template <typename F>
  class ReadFeatureFunctor
  {
    FeaturesVector const & m_V;
    F & m_F;
    unordered_set<uint32_t> & m_offsets;

  public:
    ReadFeatureFunctor(FeaturesVector const & v, F & f, unordered_set<uint32_t> & offsets)
      : m_V(v), m_F(f), m_offsets(offsets) {}
    void operator() (uint32_t offset) const
    {
      if (m_offsets.insert(offset).second)
      {
        FeatureType feature;
        m_V.Get(offset, feature);
        m_F(feature);
      }
    }
  };

  template <typename F>
  void ProcessMwm(F & f, MwmId id, covering::CoveringGetter & cov, uint32_t scale) const
  {
    MwmLock lock(*this, id);
    MwmValue * pValue = lock.GetValue();
    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);

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

      // iterate through intervals
      unordered_set<uint32_t> offsets;
      ReadFeatureFunctor<F> f1(fv, f, offsets);
      for (size_t i = 0; i < interval.size(); ++i)
      {
        index.ForEachInIntervalAndScale(f1, interval[i].first, interval[i].second, scale);
      }
    }
  }

  template <typename F>
  void ForEachInIntervals(F & f, int mode, m2::RectD const & rect, uint32_t scale) const
  {
    vector<MwmInfo> mwm;
    GetMwmInfo(mwm);

    covering::CoveringGetter cov(rect, mode);

    size_t const count = mwm.size();
    MwmId worldID[2] = { count, count };

    for (MwmId id = 0; id < count; ++id)
    {
      if ((mwm[id].m_minScale <= scale && scale <= mwm[id].m_maxScale) &&
          rect.IsIntersect(mwm[id].m_limitRect))
      {
        switch (mwm[id].GetType())
        {
        case MwmInfo::COUNTRY:
          ProcessMwm(f, id, cov, scale);
          break;

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

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

    if (worldID[0] < count)
      ProcessMwm(f, worldID[0], cov, scale);

    if (worldID[1] < count)
      ProcessMwm(f, worldID[1], cov, scale);
  }
};