Libosmium  2.13.0
Fast and flexible C++ library for working with OpenStreetMap data
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 <cstddef>
39 #include <cstdint>
40 #include <tuple>
41 #include <type_traits>
42 #include <utility>
43 #include <vector>
44 
45 #include <osmium/osm/item_type.hpp>
46 #include <osmium/osm/relation.hpp>
47 #include <osmium/osm/types.hpp>
48 
49 namespace osmium {
50 
51  namespace index {
52 
53  namespace detail {
54 
55  template <typename TKey, typename TKeyInternal, typename TValue, typename TValueInternal>
56  class flat_map {
57 
58  public:
59 
60  using key_type = TKey;
61  using value_type = TValue;
62 
63  private:
64 
65  struct kv_pair {
66  TKeyInternal key;
67  TValueInternal value;
68 
69  explicit kv_pair(key_type key_id) :
70  key(static_cast<TKeyInternal>(key_id)),
71  value() {
72  }
73 
74  kv_pair(key_type key_id, value_type value_id) :
75  key(static_cast<TKeyInternal>(key_id)),
76  value(static_cast<TValueInternal>(value_id)) {
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 
83  bool operator==(const kv_pair& other) const noexcept {
84  return std::tie(key, value) == std::tie(other.key, other.value);
85  }
86  }; // struct kv_pair
87 
88  std::vector<kv_pair> m_map;
89 
90  public:
91 
92  using const_iterator = typename std::vector<kv_pair>::const_iterator;
93 
94  void set(key_type key, value_type value) {
95  m_map.emplace_back(key, value);
96  }
97 
98  typename std::enable_if<std::is_same<TKey, TValue>::value>::type flip_in_place() {
99  for (auto& p : m_map) {
100  using std::swap;
101  swap(p.key, p.value);
102  }
103  }
104 
105  flat_map<TValue, TValueInternal, TKey, TKeyInternal> flip_copy() {
106  flat_map<TValue, TValueInternal, TKey, TKeyInternal> map;
107  map.reserve(m_map.size());
108 
109  for (const auto& p : m_map) {
110  map.set(p.value, p.key);
111  }
112 
113  return map;
114  }
115 
116  void sort_unique() {
117  std::sort(m_map.begin(), m_map.end());
118  const auto last = std::unique(m_map.begin(), m_map.end());
119  m_map.erase(last, m_map.end());
120  }
121 
122  std::pair<const_iterator, const_iterator> get(key_type key) const noexcept {
123  return std::equal_range(m_map.begin(), m_map.end(), kv_pair{key}, [](const kv_pair& lhs, const kv_pair& rhs) {
124  return lhs.key < rhs.key;
125  });
126  }
127 
128  bool empty() const noexcept {
129  return m_map.empty();
130  }
131 
132  std::size_t size() const noexcept {
133  return m_map.size();
134  }
135 
136  void reserve(std::size_t size) {
137  m_map.reserve(size);
138  }
139 
140  }; // class flat_map
141 
142  } // namespace detail
143 
170 
171  friend class RelationsMapStash;
172  friend class RelationsMapIndexes;
173 
174  using map_type = detail::flat_map<osmium::unsigned_object_id_type, uint32_t,
176 
178 
179  explicit RelationsMapIndex(map_type&& map) :
180  m_map(std::move(map)) {
181  }
182 
183  public:
184 
185  RelationsMapIndex() = delete;
186 
187  RelationsMapIndex(const RelationsMapIndex&) = delete;
188  RelationsMapIndex& operator=(const RelationsMapIndex&) = delete;
189 
191  RelationsMapIndex& operator=(RelationsMapIndex&&) = default;
192 
209  template <typename TFunc>
210  void for_each_parent(osmium::unsigned_object_id_type member_id, TFunc&& func) const {
211  const auto parents = m_map.get(member_id);
212  for (auto it = parents.first; it != parents.second; ++it) {
213  std::forward<TFunc>(func)(it->value);
214  }
215  }
216 
231  template <typename TFunc>
232  void for_each(osmium::unsigned_object_id_type id, TFunc&& func) const {
233  const auto parents = m_map.get(id);
234  for (auto it = parents.first; it != parents.second; ++it) {
235  std::forward<TFunc>(func)(it->value);
236  }
237  }
238 
244  bool empty() const noexcept {
245  return m_map.empty();
246  }
247 
253  std::size_t size() const noexcept {
254  return m_map.size();
255  }
256 
257  }; // class RelationsMapIndex
258 
260 
261  friend class RelationsMapStash;
262 
265 
267  m_member_to_parent(std::move(map1)),
268  m_parent_to_member(std::move(map2)) {
269  }
270 
271  public:
272 
273  const RelationsMapIndex& member_to_parent() const noexcept {
274  return m_member_to_parent;
275  }
276 
277  const RelationsMapIndex& parent_to_member() const noexcept {
278  return m_parent_to_member;
279  }
280 
286  bool empty() const noexcept {
287  return m_member_to_parent.empty();
288  }
289 
295  std::size_t size() const noexcept {
296  return m_member_to_parent.size();
297  }
298 
299  }; // class RelationsMapIndexes
300 
307 
308  using map_type = detail::flat_map<osmium::unsigned_object_id_type, uint32_t,
310 
312 
313 #ifndef NDEBUG
314  bool m_valid = true;
315 #endif
316 
317  public:
318 
319  RelationsMapStash() = default;
320 
321  RelationsMapStash(const RelationsMapStash&) = delete;
322  RelationsMapStash& operator=(const RelationsMapStash&) = delete;
323 
325  RelationsMapStash& operator=(RelationsMapStash&&) = default;
326 
330  void add(osmium::unsigned_object_id_type member_id, osmium::unsigned_object_id_type relation_id) {
331  assert(m_valid && "You can't use the RelationsMap any more after calling build_index()");
332  m_map.set(member_id, relation_id);
333  }
334 
338  void add_members(const osmium::Relation& relation) {
339  assert(m_valid && "You can't use the RelationsMap any more after calling build_index()");
340  for (const auto& member : relation.members()) {
341  if (member.type() == osmium::item_type::relation) {
342  m_map.set(member.positive_ref(), relation.positive_id());
343  }
344  }
345  }
346 
352  bool empty() const noexcept {
353  assert(m_valid && "You can't use the RelationsMap any more after calling build_index()");
354  return m_map.empty();
355  }
356 
362  std::size_t size() const noexcept {
363  assert(m_valid && "You can't use the RelationsMap any more after calling build_index()");
364  return m_map.size();
365  }
366 
376  assert(m_valid && "You can't use the RelationsMap any more after calling build_index()");
377  m_map.sort_unique();
378 #ifndef NDEBUG
379  m_valid = false;
380 #endif
381  return RelationsMapIndex{std::move(m_map)};
382  }
383 
391  assert(m_valid && "You can't use the RelationsMap any more after calling build_member_to_parent_index()");
392  m_map.sort_unique();
393 #ifndef NDEBUG
394  m_valid = false;
395 #endif
396  return RelationsMapIndex{std::move(m_map)};
397  }
398 
406  assert(m_valid && "You can't use the RelationsMap any more after calling build_parent_to_member_index()");
407  m_map.flip_in_place();
408  m_map.sort_unique();
409 #ifndef NDEBUG
410  m_valid = false;
411 #endif
412  return RelationsMapIndex{std::move(m_map)};
413  }
414 
422  assert(m_valid && "You can't use the RelationsMap any more after calling build_indexes()");
423  auto reverse_map = m_map.flip_copy();
424  reverse_map.sort_unique();
425  m_map.sort_unique();
426 #ifndef NDEBUG
427  m_valid = false;
428 #endif
429  return RelationsMapIndexes{std::move(m_map), std::move(reverse_map)};
430  }
431 
432  }; // class RelationsMapStash
433 
434  } // namespace index
435 
436 } // namespace osmium
437 
438 #endif // OSMIUM_INDEX_RELATIONS_MAP_HPP
type
Definition: entity_bits.hpp:63
RelationMemberList & members()
Get a reference to the member list.
Definition: relation.hpp:186
const RelationsMapIndex & parent_to_member() const noexcept
Definition: relations_map.hpp:277
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:375
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
std::size_t size() const noexcept
Definition: relations_map.hpp:253
void for_each_parent(osmium::unsigned_object_id_type member_id, TFunc &&func) const
Definition: relations_map.hpp:210
Definition: reader_iterator.hpp:39
void swap(Buffer &lhs, Buffer &rhs)
Definition: buffer.hpp:762
Definition: relations_map.hpp:169
RelationsMapIndexes(RelationsMapIndex::map_type &&map1, RelationsMapIndex::map_type &&map2)
Definition: relations_map.hpp:266
Definition: relations_map.hpp:259
RelationsMapIndex build_parent_to_member_index()
Definition: relations_map.hpp:405
bool empty() const noexcept
Definition: relations_map.hpp:244
detail::flat_map< osmium::unsigned_object_id_type, uint32_t, osmium::unsigned_object_id_type, uint32_t > map_type
Definition: relations_map.hpp:175
RelationsMapIndexes build_indexes()
Definition: relations_map.hpp:421
RelationsMapIndex(map_type &&map)
Definition: relations_map.hpp:179
bool operator<(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:447
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
Definition: attr.hpp:333
RelationsMapIndex m_member_to_parent
Definition: relations_map.hpp:263
void add(osmium::unsigned_object_id_type member_id, osmium::unsigned_object_id_type relation_id)
Definition: relations_map.hpp:330
Definition: relations_map.hpp:306
const RelationsMapIndex & member_to_parent() const noexcept
Definition: relations_map.hpp:273
bool empty() const noexcept
Definition: relations_map.hpp:286
map_type m_map
Definition: relations_map.hpp:177
void add_members(const osmium::Relation &relation)
Definition: relations_map.hpp:338
detail::flat_map< osmium::unsigned_object_id_type, uint32_t, osmium::unsigned_object_id_type, uint32_t > map_type
Definition: relations_map.hpp:309
RelationsMapIndex build_member_to_parent_index()
Definition: relations_map.hpp:390
void for_each(osmium::unsigned_object_id_type id, TFunc &&func) const
Definition: relations_map.hpp:232
std::size_t size() const noexcept
Definition: relations_map.hpp:362
map_type m_map
Definition: relations_map.hpp:311
unsigned_object_id_type positive_id() const noexcept
Get absolute value of the ID of this object.
Definition: object.hpp:131
RelationsMapIndex m_parent_to_member
Definition: relations_map.hpp:264
bool empty() const noexcept
Definition: relations_map.hpp:352
std::size_t size() const noexcept
Definition: relations_map.hpp:295