Libosmium: include/osmium/relations/members_database.hpp Source File

Libosmium Fast and flexible C++ library for working with OpenStreetMap data

Loading...
Searching...
No Matches
members_database.hpp
Go to the documentation of this file.
1#ifndef OSMIUM_RELATIONS_MEMBERS_DATABASE_HPP
2#define OSMIUM_RELATIONS_MEMBERS_DATABASE_HPP
3
4/*
5
6This file is part of Osmium (https://osmcode.org/libosmium).
7
8Copyright 2013-2026 Jochen Topf <jochen@topf.org> and others (see README).
9
10Boost Software License - Version 1.0 - August 17th, 2003
11
12Permission is hereby granted, free of charge, to any person or organization
13obtaining a copy of the software and accompanying documentation covered by
14this license (the "Software") to use, reproduce, display, distribute,
15execute, and transmit the Software, and to prepare derivative works of the
16Software, and to permit third-parties to whom the Software is furnished to
17do so, all subject to the following:
18
19The copyright notices in the Software and this entire statement, including
20the above license grant, this restriction and the following disclaimer,
21must be included in all copies of the Software, in whole or in part, and
22all derivative works of the Software, unless such copies or derivative
23works are solely in the form of machine-executable object code generated by
24a source language processor.
25
26THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32DEALINGS IN THE SOFTWARE.
33
34*/
35
36#include <osmium/osm/object.hpp>
37#include <osmium/osm/relation.hpp>
38#include <osmium/osm/types.hpp>
39#include <osmium/relations/relations_database.hpp>
40#include <osmium/storage/item_stash.hpp>
41#include <osmium/util/iterator.hpp>
42
43#include <algorithm>
44#include <cassert>
45#include <cstddef>
46#include <limits>
47#include <tuple>
48#include <type_traits>
49#include <utility>
50#include <vector>
51
52namespace osmium {
53
54 namespace relations {
55
63 class MembersDatabaseCommon {
64
65 struct element {
66
71 enum {
72 removed_value = std::numeric_limits<std::size_t>::max()
73 };
74
80 osmium::object_id_type member_id;
81
85 std::size_t member_num;
86
90 std::size_t relation_pos;
91
98 osmium::ItemStash::handle_type object_handle;
99
100 explicit element(std::size_t rel_pos, osmium::object_id_type memb_id, std::size_t memb_num) noexcept :
101 member_id(memb_id),
102 member_num(memb_num),
103 relation_pos(rel_pos) {
104 }
105
111 explicit element(osmium::object_id_type m_id) noexcept :
112 member_id(m_id),
113 member_num(0),
114 relation_pos(0) {
115 }
116
117 bool is_removed() const noexcept {
118 return member_num == removed_value;
119 }
120
121 void remove() noexcept {
122 member_num = removed_value;
123 }
124
125 bool operator<(const element& other) const noexcept {
126 return std::tie(member_id, member_num, relation_pos) <
127 std::tie(other.member_id, other.member_num, other.relation_pos);
128 }
129
130 }; // struct element
131
132 // comparison function only comparing member_id.
133 struct compare_member_id {
134 bool operator()(const element& a, const element& b) const noexcept {
135 return a.member_id < b.member_id;
136 }
137 };
138
139 std::vector<element> m_elements;
140
141 protected:
142
143 osmium::ItemStash& m_stash;
144 osmium::relations::RelationsDatabase& m_relations_db;
145
146#ifndef NDEBUG
147 // This is used only in debug builds to make sure the
148 // prepare_for_lookup() function is called at the right place.
149 bool m_init_phase = true;
150#endif
151
152 using iterator = std::vector<element>::iterator;
153 using const_iterator = std::vector<element>::const_iterator;
154
155 iterator_range<iterator> find(osmium::object_id_type id) {
156 return make_range(std::equal_range(m_elements.begin(), m_elements.end(), element{id}, compare_member_id{}));
157 }
158
159 iterator_range<const_iterator> find(osmium::object_id_type id) const {
160 return make_range(std::equal_range(m_elements.cbegin(), m_elements.cend(), element{id}, compare_member_id{}));
161 }
162
163 static typename std::iterator_traits<iterator>::difference_type count_not_removed(const iterator_range<iterator>& range) noexcept {
164 return std::count_if(range.begin(), range.end(), [](const element& elem) {
165 return !elem.is_removed();
166 });
167 }
168
169 void add_object(const osmium::OSMObject& object, iterator_range<iterator>& range) {
170 const auto handle = m_stash.add_item(object);
171 for (auto& elem : range) {
172 elem.object_handle = handle;
173 }
174 }
175
176 MembersDatabaseCommon(osmium::ItemStash& stash, osmium::relations::RelationsDatabase& relations_db) :
177 m_stash(stash),
178 m_relations_db(relations_db) {
179 }
180
181 public:
182
188 std::size_t used_memory() const noexcept {
189 return (sizeof(element) * m_elements.capacity()) +
190 sizeof(MembersDatabaseCommon);
191 }
192
200 std::size_t size() const noexcept {
201 return m_elements.size();
202 }
203
207 struct counts {
209 std::size_t tracked = 0;
211 std::size_t available = 0;
213 std::size_t removed = 0;
214 };
215
222 counts count() const noexcept {
223 counts c;
224
225 for (const auto& elem : m_elements) {
226 if (elem.is_removed()) {
227 ++c.removed;
228 } else if (elem.object_handle.valid()) {
229 ++c.available;
230 } else {
231 ++c.tracked;
232 }
233 }
234
235 return c;
236 }
237
247 void track(RelationHandle& rel_handle, osmium::object_id_type member_id, std::size_t member_num) {
248 assert(m_init_phase && "Can not call MembersDatabase::track() after MembersDatabase::prepare_for_lookup().");
249 assert(rel_handle.relation_database() == &m_relations_db);
250 m_elements.emplace_back(rel_handle.pos(), member_id, member_num);
251 rel_handle.increment_members();
252 }
253
260 void prepare_for_lookup() {
261 assert(m_init_phase && "Can not call MembersDatabase::prepare_for_lookup() twice.");
262 std::sort(m_elements.begin(), m_elements.end());
263#ifndef NDEBUG
264 m_init_phase = false;
265#endif
266 }
267
274 void remove(osmium::object_id_type member_id, osmium::object_id_type relation_id) {
275 assert(!m_init_phase && "Call MembersDatabase::prepare_for_lookup() before calling remove().");
276 const auto range = find(member_id);
277
278 if (range.empty()) {
279 return;
280 }
281
282 // If this is the last time this object was needed, remove it
283 // from the stash.
284 if (count_not_removed(range) == 1) {
285 m_stash.remove_item(range.begin()->object_handle);
286 }
287
288 for (auto& elem : range) {
289 if (!elem.is_removed() && relation_id == m_relations_db[elem.relation_pos]->id()) {
290 elem.remove();
291 break;
292 }
293 }
294 }
295
306 const osmium::OSMObject* get_object(osmium::object_id_type id) const {
307 assert(!m_init_phase && "Call MembersDatabase::prepare_for_lookup() before calling get_object().");
308 const auto range = find(id);
309 if (range.empty()) {
310 return nullptr;
311 }
312 const auto handle = range.begin()->object_handle;
313 if (handle.valid()) {
314 return &m_stash.get<osmium::OSMObject>(handle);
315 }
316 return nullptr;
317 }
318
319 }; // class MembersDatabaseCommon
320
333 template <typename TObject>
334 class MembersDatabase : public MembersDatabaseCommon {
335
336
337 public:
338
349 MembersDatabase(osmium::ItemStash& stash, osmium::relations::RelationsDatabase& relation_db) :
350 MembersDatabaseCommon(stash, relation_db) {
351 }
352
364 template <typename TFunc>
365 bool add(const TObject& object, TFunc&& func) {
366 assert(!m_init_phase && "Call MembersDatabase::prepare_for_lookup() before calling add().");
367 auto range = find(object.id());
368
369 if (range.empty()) {
370 // No relation needs this object.
371 return false;
372 }
373
374 // At least one relation needs this object. Store it and
375 // "tell" all relations.
376 add_object(object, range);
377
378 for (auto& elem : range) {
379 assert(!elem.is_removed());
380 assert(elem.member_id == object.id());
381
382 auto rel_handle = m_relations_db[elem.relation_pos];
383 assert(elem.member_num < rel_handle->members().size());
384 rel_handle.decrement_members();
385
386 if (rel_handle.has_all_members()) {
387 std::forward<TFunc>(func)(rel_handle);
388 }
389 }
390
391 return true;
392 }
393
404 const TObject* get(osmium::object_id_type id) const {
405 assert(!m_init_phase && "Call MembersDatabase::prepare_for_lookup() before calling get().");
406 return static_cast<const TObject*>(get_object(id));
407 }
408
409 }; // class MembersDatabase
410
411 } // namespace relations
412
413} // namespace osmium
414
415#endif // OSMIUM_RELATIONS_MEMBERS_DATABASE_HPP
osmium::ItemStash::handle_type
Definition item_stash.hpp:71
osmium::ItemStash
Definition item_stash.hpp:57
osmium::OSMObject
Definition object.hpp:64
osmium::relations::MembersDatabaseCommon::prepare_for_lookup
void prepare_for_lookup()
Definition members_database.hpp:260
osmium::relations::MembersDatabaseCommon::m_elements
std::vector< element > m_elements
Definition members_database.hpp:139
osmium::relations::MembersDatabaseCommon::size
std::size_t size() const noexcept
Definition members_database.hpp:200
osmium::relations::MembersDatabaseCommon::track
void track(RelationHandle &rel_handle, osmium::object_id_type member_id, std::size_t member_num)
Definition members_database.hpp:247
osmium::relations::MembersDatabaseCommon::const_iterator
std::vector< element >::const_iterator const_iterator
Definition members_database.hpp:153
osmium::relations::MembersDatabaseCommon::m_init_phase
bool m_init_phase
Definition members_database.hpp:149
osmium::relations::MembersDatabaseCommon::count_not_removed
static std::iterator_traits< iterator >::difference_type count_not_removed(const iterator_range< iterator > &range) noexcept
Definition members_database.hpp:163
osmium::relations::MembersDatabaseCommon::get_object
const osmium::OSMObject * get_object(osmium::object_id_type id) const
Definition members_database.hpp:306
osmium::relations::MembersDatabaseCommon::m_relations_db
osmium::relations::RelationsDatabase & m_relations_db
Definition members_database.hpp:144
osmium::relations::MembersDatabaseCommon::remove
void remove(osmium::object_id_type member_id, osmium::object_id_type relation_id)
Definition members_database.hpp:274
osmium::relations::MembersDatabaseCommon::m_stash
osmium::ItemStash & m_stash
Definition members_database.hpp:143
osmium::relations::MembersDatabaseCommon::iterator
std::vector< element >::iterator iterator
Definition members_database.hpp:152
osmium::relations::MembersDatabaseCommon::MembersDatabaseCommon
MembersDatabaseCommon(osmium::ItemStash &stash, osmium::relations::RelationsDatabase &relations_db)
Definition members_database.hpp:176
osmium::relations::MembersDatabaseCommon::count
counts count() const noexcept
Definition members_database.hpp:222
osmium::relations::MembersDatabaseCommon::used_memory
std::size_t used_memory() const noexcept
Definition members_database.hpp:188
osmium::relations::MembersDatabaseCommon::add_object
void add_object(const osmium::OSMObject &object, iterator_range< iterator > &range)
Definition members_database.hpp:169
osmium::relations::MembersDatabaseCommon::find
iterator_range< const_iterator > find(osmium::object_id_type id) const
Definition members_database.hpp:159
osmium::relations::MembersDatabaseCommon::find
iterator_range< iterator > find(osmium::object_id_type id)
Definition members_database.hpp:155
osmium::relations::MembersDatabase::MembersDatabase
MembersDatabase(osmium::ItemStash &stash, osmium::relations::RelationsDatabase &relation_db)
Definition members_database.hpp:349
osmium::relations::MembersDatabase::get
const TObject * get(osmium::object_id_type id) const
Definition members_database.hpp:404
osmium::relations::MembersDatabase::add
bool add(const TObject &object, TFunc &&func)
Definition members_database.hpp:365
osmium::relations::RelationHandle
Definition relations_database.hpp:208
osmium::relations::RelationHandle::pos
std::size_t pos() const noexcept
Definition relations_database.hpp:238
osmium::relations::RelationHandle::relation_database
RelationsDatabase * relation_database() const noexcept
Definition relations_database.hpp:225
osmium::relations::RelationHandle::increment_members
void increment_members() noexcept
Definition relations_database.hpp:288
osmium::relations::RelationsDatabase
Definition relations_database.hpp:82
item_stash.hpp
iterator.hpp
osmium::relations
Code related to the assembly of OSM relations.
Definition multipolygon_collector.hpp:53
osmium
Namespace for everything in the Osmium library.
Definition assembler.hpp:53
osmium::make_range
iterator_range< It > make_range(P &&p) noexcept
Definition iterator.hpp:68
osmium::object_id_type
int64_t object_id_type
Type for OSM object (node, way, or relation) IDs.
Definition types.hpp:45
object.hpp
relation.hpp
relations_database.hpp
osmium::iterator_range
Definition iterator.hpp:42
osmium::relations::MembersDatabaseCommon::compare_member_id
Definition members_database.hpp:133
osmium::relations::MembersDatabaseCommon::compare_member_id::operator()
bool operator()(const element &a, const element &b) const noexcept
Definition members_database.hpp:134
osmium::relations::MembersDatabaseCommon::counts
Definition members_database.hpp:207
osmium::relations::MembersDatabaseCommon::counts::available
std::size_t available
The number of members tracked and found already.
Definition members_database.hpp:211
osmium::relations::MembersDatabaseCommon::counts::tracked
std::size_t tracked
The number of members tracked and not found yet.
Definition members_database.hpp:209
osmium::relations::MembersDatabaseCommon::counts::removed
std::size_t removed
The number of members that were tracked, found and then removed because of a completed relation.
Definition members_database.hpp:213
osmium::relations::MembersDatabaseCommon::element
Definition members_database.hpp:65
osmium::relations::MembersDatabaseCommon::element::is_removed
bool is_removed() const noexcept
Definition members_database.hpp:117
osmium::relations::MembersDatabaseCommon::element::remove
void remove() noexcept
Definition members_database.hpp:121
osmium::relations::MembersDatabaseCommon::element::element
element(std::size_t rel_pos, osmium::object_id_type memb_id, std::size_t memb_num) noexcept
Definition members_database.hpp:100
osmium::relations::MembersDatabaseCommon::element::element
element(osmium::object_id_type m_id) noexcept
Definition members_database.hpp:111
osmium::relations::MembersDatabaseCommon::element::relation_pos
std::size_t relation_pos
Definition members_database.hpp:90
osmium::relations::MembersDatabaseCommon::element::member_id
osmium::object_id_type member_id
Definition members_database.hpp:80
osmium::relations::MembersDatabaseCommon::element::operator<
bool operator<(const element &other) const noexcept
Definition members_database.hpp:125
osmium::relations::MembersDatabaseCommon::element::member_num
std::size_t member_num
Definition members_database.hpp:85
osmium::relations::MembersDatabaseCommon::element::removed_value
@ removed_value
Definition members_database.hpp:72
osmium::relations::MembersDatabaseCommon::element::object_handle
osmium::ItemStash::handle_type object_handle
Definition members_database.hpp:98
types.hpp