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

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

#include "indexer/features_offsets_table.hpp"

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

#include "platform/platform.hpp"

#include "base/scope_guard.hpp"

#include "std/limits.hpp"
#include "std/string.hpp"
#include "std/unordered_map.hpp"
#include "std/utility.hpp"
#include "std/vector.hpp"

#include "3party/succinct/rs_bit_vector.hpp"
#include "3party/succinct/elias_fano_compressed_list.hpp"

#include "defines.hpp"


namespace routing
{

typedef uint32_t TOsrmNodeId;
typedef vector<TOsrmNodeId> TNodesList;
constexpr TOsrmNodeId INVALID_NODE_ID = numeric_limits<TOsrmNodeId>::max();
constexpr uint32_t kInvalidFid = numeric_limits<uint32_t>::max();

namespace OsrmMappingTypes
{
#pragma pack (push, 1)
  struct FtSeg
  {
    uint32_t m_fid;
    uint16_t m_pointStart;
    uint16_t m_pointEnd;


    // No need to initialize something her (for vector<FtSeg>).
    FtSeg() {}
    FtSeg(uint32_t fid, uint32_t ps, uint32_t pe);

    explicit FtSeg(uint64_t x);
    uint64_t Store() const;

    bool Merge(FtSeg const & other);

    bool operator == (FtSeg const & other) const
    {
      return (other.m_fid == m_fid)
          && (other.m_pointEnd == m_pointEnd)
          && (other.m_pointStart == m_pointStart);
    }

    bool IsIntersect(FtSeg const & other) const;

    bool IsValid() const
    {
      return m_fid != kInvalidFid;
    }

    void Swap(FtSeg & other)
    {
      swap(m_fid, other.m_fid);
      swap(m_pointStart, other.m_pointStart);
      swap(m_pointEnd, other.m_pointEnd);
    }

    friend string DebugPrint(FtSeg const & seg);
  };

  struct SegOffset
  {
    TOsrmNodeId m_nodeId;
    uint32_t m_offset;

    SegOffset() : m_nodeId(0), m_offset(0) {}

    SegOffset(uint32_t nodeId, uint32_t offset)
      : m_nodeId(nodeId), m_offset(offset)
    {
    }

    friend string DebugPrint(SegOffset const & off);
  };

  struct FtSegLess
  {
    bool operator() (FtSeg const * a, FtSeg const * b) const
    {
      return a->Store() < b->Store();
    }
  };
#pragma pack (pop)
}

class OsrmFtSegMapping;

class OsrmFtSegBackwardIndex
{
  succinct::rs_bit_vector m_rankIndex;
  vector<TNodesList> m_nodeIds;
  unique_ptr<feature::FeaturesOffsetsTable> m_table;

  unique_ptr<MmapReader> m_mappedBits;

  bool m_oldFormat;

  void Save(string const & nodesFileName, string const & bitsFileName);
  bool Load(string const & nodesFileName, string const & bitsFileName);

public:
  void Construct(OsrmFtSegMapping & mapping, uint32_t maxNodeId,
                 FilesMappingContainer & routingFile,
                 platform::LocalCountryFile const & localFile);

  TNodesList const & GetNodeIdByFid(uint32_t fid) const;

  void Clear();
};

class OsrmFtSegMapping
{
public:
  typedef set<OsrmMappingTypes::FtSeg*, OsrmMappingTypes::FtSegLess> FtSegSetT;

  void Clear();
  void Load(FilesMappingContainer & cont, platform::LocalCountryFile const & localFile);

  void Map(FilesMappingContainer & cont);
  void Unmap();
  bool IsMapped() const;

  template <class ToDo> void ForEachFtSeg(TOsrmNodeId nodeId, ToDo toDo) const
  {
    pair<size_t, size_t> r = GetSegmentsRange(nodeId);
    while (r.first != r.second)
    {
      OsrmMappingTypes::FtSeg s(m_segments[r.first]);
      if (s.IsValid())
        toDo(s);
      ++r.first;
    }
  }

  typedef unordered_map<uint64_t, pair<TOsrmNodeId, TOsrmNodeId> > OsrmNodesT;
  void GetOsrmNodes(FtSegSetT & segments, OsrmNodesT & res) const;

  void GetSegmentByIndex(size_t idx, OsrmMappingTypes::FtSeg & seg) const;
  TNodesList const & GetNodeIdByFid(uint32_t fid) const
  {
    return m_backwardIndex.GetNodeIdByFid(fid);
  }

  /// @name For debug purpose only.
  //@{
  void DumpSegmentsByFID(uint32_t fID) const;
  void DumpSegmentByNode(TOsrmNodeId nodeId) const;
  //@}

  /// @name For unit test purpose only.
  //@{
  /// @return STL-like range [s, e) of segments indexies for passed node.
  pair<size_t, size_t> GetSegmentsRange(uint32_t nodeId) const;
  /// @return Node id for segment's index.
  TOsrmNodeId GetNodeId(size_t segInd) const;

  size_t GetSegmentsCount() const { return static_cast<size_t>(m_segments.size()); }
  //@}

protected:
  typedef vector<OsrmMappingTypes::SegOffset> SegOffsetsT;
  SegOffsetsT m_offsets;

private:
  succinct::elias_fano_compressed_list m_segments;
  FilesMappingContainer::Handle m_handle;
  OsrmFtSegBackwardIndex m_backwardIndex;
};

class OsrmFtSegMappingBuilder : public OsrmFtSegMapping
{
public:
  OsrmFtSegMappingBuilder();

  typedef vector<OsrmMappingTypes::FtSeg> FtSegVectorT;

  void Append(TOsrmNodeId nodeId, FtSegVectorT const & data);
  void Save(FilesContainerW & cont) const;

private:
  vector<uint64_t> m_buffer;
  uint64_t m_lastOffset;
};

}