Libosmium  2.11.1
Fast and flexible C++ library for working with OpenStreetMap data
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
relations_map.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_INDEX_RELATIONS_MAP_HPP
2 #define OSMIUM_INDEX_RELATIONS_MAP_HPP
3 
4 /*
5 
6 This file is part of Osmium (http://osmcode.org/libosmium).
7 
8 Copyright 2013-2017 Jochen Topf <jochen@topf.org> and others (see README).
9 
10 Boost Software License - Version 1.0 - August 17th, 2003
11 
12 Permission is hereby granted, free of charge, to any person or organization
13 obtaining a copy of the software and accompanying documentation covered by
14 this license (the "Software") to use, reproduce, display, distribute,
15 execute, and transmit the Software, and to prepare derivative works of the
16 Software, and to permit third-parties to whom the Software is furnished to
17 do so, all subject to the following:
18 
19 The copyright notices in the Software and this entire statement, including
20 the above license grant, this restriction and the following disclaimer,
21 must be included in all copies of the Software, in whole or in part, and
22 all derivative works of the Software, unless such copies or derivative
23 works are solely in the form of machine-executable object code generated by
24 a source language processor.
25 
26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32 DEALINGS IN THE SOFTWARE.
33 
34 */
35 
36 #include <algorithm>
37 #include <cassert>
38 #include <cstdint>
39 #include <tuple>
40 #include <vector>
41 
42 #include <osmium/osm/relation.hpp>
43 #include <osmium/osm/types.hpp>
44 
45 namespace osmium {
46 
47  namespace index {
48 
49  namespace detail {
50 
51  template <typename TKey, typename TKeyInternal, typename TValue, typename TValueInternal>
52  class flat_map {
53 
54  public:
55 
56  using key_type = TKey;
57  using value_type = TValue;
58 
59  private:
60 
61  struct kv_pair {
62  TKeyInternal key;
63  TValueInternal value;
64 
65  explicit kv_pair(key_type key_id) :
66  key(static_cast<TKeyInternal>(key_id)),
67  value() {
68  }
69 
70  kv_pair(key_type key_id, value_type value_id) :
71  key(static_cast<TKeyInternal>(key_id)),
72  value(static_cast<TValueInternal>(value_id)) {
73  }
74 
75  bool operator<(const kv_pair& other) const noexcept {
76  return std::tie(key, value) < std::tie(other.key, other.value);
77  }
78 
79  bool operator==(const kv_pair& other) const noexcept {
80  return std::tie(key, value) == std::tie(other.key, other.value);
81  }
82  }; // struct kv_pair
83 
84  std::vector<kv_pair> m_map;
85 
86  public:
87 
88  using const_iterator = typename std::vector<kv_pair>::const_iterator;
89 
90  void set(key_type key, value_type value) {
91  m_map.emplace_back(key, value);
92  }
93 
94  void sort_unique() {
95  std::sort(m_map.begin(), m_map.end());
96  const auto last = std::unique(m_map.begin(), m_map.end());
97  m_map.erase(last, m_map.end());
98  }
99 
100  std::pair<const_iterator, const_iterator> get(key_type key) const noexcept {
101  return std::equal_range(m_map.begin(), m_map.end(), kv_pair{key}, [](const kv_pair& lhs, const kv_pair& rhs) {
102  return lhs.key < rhs.key;
103  });
104  }
105 
106  bool empty() const noexcept {
107  return m_map.empty();
108  }
109 
110  size_t size() const noexcept {
111  return m_map.size();
112  }
113 
114  }; // class flat_map
115 
116  } // namespace detail
117 
142 
143  friend class RelationsMapStash;
144 
145  using map_type = detail::flat_map<osmium::unsigned_object_id_type, uint32_t,
147 
149 
151  m_map(std::move(map)) {
152  }
153 
154  public:
155 
156  RelationsMapIndex() = delete;
157 
158  RelationsMapIndex(const RelationsMapIndex&) = delete;
160 
163 
178  template <typename Func>
179  void for_each_parent(osmium::unsigned_object_id_type member_id, Func&& func) const {
180  const auto parents = m_map.get(member_id);
181  for (auto it = parents.first; it != parents.second; ++it) {
182  std::forward<Func>(func)(it->value);
183  }
184  }
185 
191  bool empty() const noexcept {
192  return m_map.empty();
193  }
194 
200  size_t size() const noexcept {
201  return m_map.size();
202  }
203 
204  }; // RelationsMapIndex
205 
212 
213  using map_type = detail::flat_map<osmium::unsigned_object_id_type, uint32_t,
215 
217 
218 #ifndef NDEBUG
219  bool m_valid = true;
220 #endif
221 
222  public:
223 
224  RelationsMapStash() = default;
225 
226  RelationsMapStash(const RelationsMapStash&) = delete;
228 
231 
235  void add(osmium::unsigned_object_id_type member_id, osmium::unsigned_object_id_type relation_id) {
236  assert(m_valid && "You can't use the RelationsMap any more after calling build_index()");
237  m_map.set(member_id, relation_id);
238  }
239 
243  void add_members(const osmium::Relation& relation) {
244  assert(m_valid && "You can't use the RelationsMap any more after calling build_index()");
245  for (const auto& member : relation.members()) {
246  if (member.type() == osmium::item_type::relation) {
247  m_map.set(member.positive_ref(), relation.positive_id());
248  }
249  }
250  }
251 
257  bool empty() const noexcept {
258  assert(m_valid && "You can't use the RelationsMap any more after calling build_index()");
259  return m_map.empty();
260  }
261 
267  size_t size() const noexcept {
268  assert(m_valid && "You can't use the RelationsMap any more after calling build_index()");
269  return m_map.size();
270  }
271 
278  assert(m_valid && "You can't use the RelationsMap any more after calling build_index()");
279  m_map.sort_unique();
280 #ifndef NDEBUG
281  m_valid = false;
282 #endif
283  return RelationsMapIndex{std::move(m_map)};
284  }
285 
286  }; // class RelationsMapStash
287 
288  } // namespace index
289 
290 } // namespace osmium
291 
292 #endif // OSMIUM_INDEX_RELATIONS_MAP_HPP
RelationMemberList & members()
Definition: relation.hpp:185
RelationsMapStash & operator=(const RelationsMapStash &)=delete
bool m_valid
Definition: relations_map.hpp:219
constexpr bool operator==(const Box &lhs, const Box &rhs) noexcept
Definition: box.hpp:221
Definition: relation.hpp:168
RelationsMapIndex build_index()
Definition: relations_map.hpp:277
uint64_t unsigned_object_id_type
Type for OSM object (node, way, or relation) IDs where we only allow positive IDs.
Definition: types.hpp:46
Definition: reader_iterator.hpp:39
Definition: relations_map.hpp:141
size_t size() const noexcept
Definition: relations_map.hpp:267
bool empty() const noexcept
Definition: relations_map.hpp:191
detail::flat_map< osmium::unsigned_object_id_type, uint32_t, osmium::unsigned_object_id_type, uint32_t > map_type
Definition: relations_map.hpp:146
RelationsMapIndex(map_type &&map)
Definition: relations_map.hpp:150
bool operator<(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:447
Namespace for everything in the Osmium library.
Definition: assembler.hpp:73
Definition: attr.hpp:333
void add(osmium::unsigned_object_id_type member_id, osmium::unsigned_object_id_type relation_id)
Definition: relations_map.hpp:235
Definition: relations_map.hpp:211
map_type m_map
Definition: relations_map.hpp:148
void add_members(const osmium::Relation &relation)
Definition: relations_map.hpp:243
detail::flat_map< osmium::unsigned_object_id_type, uint32_t, osmium::unsigned_object_id_type, uint32_t > map_type
Definition: relations_map.hpp:214
RelationsMapIndex & operator=(const RelationsMapIndex &)=delete
map_type m_map
Definition: relations_map.hpp:216
size_t size() const noexcept
Definition: relations_map.hpp:200
void for_each_parent(osmium::unsigned_object_id_type member_id, Func &&func) const
Definition: relations_map.hpp:179
unsigned_object_id_type positive_id() const noexcept
Get absolute value of the ID of this object.
Definition: object.hpp:131
bool empty() const noexcept
Definition: relations_map.hpp:257