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

Loading...
Searching...
No Matches
relations_database.hpp
Go to the documentation of this file.
1#ifndef OSMIUM_RELATIONS_RELATIONS_DATABASE_HPP
2#define OSMIUM_RELATIONS_RELATIONS_DATABASE_HPP
3
4/*
5
6 This file is part of Osmium (https://osmcode.org/libosmium).
7
8 Copyright 2013-2025 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 <osmium/osm/relation.hpp>
37#include <osmium/storage/item_stash.hpp>
38
39#include <algorithm>
40#include <cassert>
41#include <cstddef>
42#include <utility>
43#include <vector>
44
45 namespace osmium {
46
47 namespace relations {
48
49 class RelationHandle;
50
82 class RelationsDatabase {
83
84 friend class RelationHandle;
85
86 struct element {
87
89 osmium::ItemStash::handle_type handle;
90
98 std::size_t members;
99
100 }; // struct element
101
102 osmium::ItemStash& m_stash;
103 std::vector<element> m_elements;
104
105 osmium::Relation& get_relation(std::size_t pos) {
106 assert(pos < m_elements.size());
107 return m_stash.get<osmium::Relation>(m_elements[pos].handle);
108 }
109
114 std::size_t& members(std::size_t pos) noexcept {
115 return m_elements[pos].members;
116 }
117
118 void remove(std::size_t pos) {
119 auto& elem = m_elements[pos];
120 m_stash.remove_item(elem.handle);
121 elem = element{osmium::ItemStash::handle_type{}, 0};
122 }
123
124 public:
125
133 explicit RelationsDatabase(osmium::ItemStash& stash) :
134 m_stash(stash) {
135 }
136
144 std::size_t used_memory() const noexcept {
145 return (sizeof(element) * m_elements.capacity()) +
146 sizeof(RelationsDatabase);
147 }
148
155 std::size_t size() const noexcept {
156 return m_elements.size();
157 }
158
168 RelationHandle add(const osmium::Relation& relation);
169
176 RelationHandle operator[](std::size_t pos) noexcept;
177
184 std::size_t count_relations() const noexcept {
185 return std::count_if(m_elements.cbegin(), m_elements.cend(), [&](const element& elem) {
186 return elem.handle.valid();
187 });
188 }
189
197 template <typename TFunc>
198 void for_each_relation(TFunc&& func);
199
200 }; // RelationsDatabase
201
208 class RelationHandle {
209
210 friend class RelationsDatabase;
211
212 RelationsDatabase* m_relation_database;
213 std::size_t m_pos;
214
215 RelationHandle(RelationsDatabase* relation_database, std::size_t pos) :
216 m_relation_database(relation_database),
217 m_pos(pos) {
218 }
219
220 public:
221
225 RelationsDatabase* relation_database() const noexcept {
226 return m_relation_database;
227 }
228
238 std::size_t pos() const noexcept {
239 return m_pos;
240 }
241
245 Relation& operator*() {
246 return m_relation_database->get_relation(m_pos);
247 }
248
252 const Relation& operator*() const {
253 return m_relation_database->get_relation(m_pos);
254 }
255
259 Relation* operator->() {
260 return &m_relation_database->get_relation(m_pos);
261 }
262
266 const Relation* operator->() const {
267 return &m_relation_database->get_relation(m_pos);
268 }
269
274 void remove() {
275 m_relation_database->remove(pos());
276 }
277
281 void set_members(std::size_t value) noexcept {
282 m_relation_database->members(m_pos) = value;
283 }
284
288 void increment_members() noexcept {
289 ++(m_relation_database->members(m_pos));
290 }
291
297 void decrement_members() noexcept {
298 assert(m_relation_database->members(m_pos) > 0);
299 --(m_relation_database->members(m_pos));
300 }
301
306 bool has_all_members() const noexcept {
307 return m_relation_database->members(m_pos) == 0;
308 }
309
310 }; // class RelationHandle
311
312 inline RelationHandle RelationsDatabase::operator[](std::size_t pos) noexcept {
313 assert(pos < m_elements.size());
314 return {this, pos};
315 }
316
317 inline RelationHandle RelationsDatabase::add(const osmium::Relation& relation) {
318 m_elements.push_back(element{m_stash.add_item(relation), 0});
319 return {this, m_elements.size() - 1};
320 }
321
322 template <typename TFunc>
323 void RelationsDatabase::for_each_relation(TFunc&& func) {
324 for (std::size_t pos = 0; pos < m_elements.size(); ++pos) {
325 if (m_elements[pos].handle.valid()) {
326 std::forward<TFunc>(func)(RelationHandle{this, pos});
327 }
328 }
329 }
330
331 } // namespace relations
332
333} // namespace osmium
334
335#endif // OSMIUM_RELATIONS_RELATIONS_DATABASE_HPP
osmium::ItemStash::handle_type
Definition item_stash.hpp:71
osmium::ItemStash
Definition item_stash.hpp:57
osmium::ItemStash::get
T & get(handle_type handle) const
Definition item_stash.hpp:294
osmium::ItemStash::remove_item
void remove_item(handle_type handle)
Definition item_stash.hpp:338
osmium::ItemStash::add_item
handle_type add_item(const osmium::memory::Item &item)
Definition item_stash.hpp:251
osmium::Relation
Definition relation.hpp:161
osmium::relations::RelationHandle
Definition relations_database.hpp:208
osmium::relations::RelationHandle::remove
void remove()
Definition relations_database.hpp:274
osmium::relations::RelationHandle::decrement_members
void decrement_members() noexcept
Definition relations_database.hpp:297
osmium::relations::RelationHandle::m_relation_database
RelationsDatabase * m_relation_database
Definition relations_database.hpp:212
osmium::relations::RelationHandle::RelationHandle
RelationHandle(RelationsDatabase *relation_database, std::size_t pos)
Definition relations_database.hpp:215
osmium::relations::RelationHandle::operator->
Relation * operator->()
Definition relations_database.hpp:259
osmium::relations::RelationHandle::m_pos
std::size_t m_pos
Definition relations_database.hpp:213
osmium::relations::RelationHandle::pos
std::size_t pos() const noexcept
Definition relations_database.hpp:238
osmium::relations::RelationHandle::operator*
const Relation & operator*() const
Definition relations_database.hpp:252
osmium::relations::RelationHandle::relation_database
RelationsDatabase * relation_database() const noexcept
Definition relations_database.hpp:225
osmium::relations::RelationHandle::operator*
Relation & operator*()
Definition relations_database.hpp:245
osmium::relations::RelationHandle::set_members
void set_members(std::size_t value) noexcept
Definition relations_database.hpp:281
osmium::relations::RelationHandle::has_all_members
bool has_all_members() const noexcept
Definition relations_database.hpp:306
osmium::relations::RelationHandle::operator->
const Relation * operator->() const
Definition relations_database.hpp:266
osmium::relations::RelationHandle::increment_members
void increment_members() noexcept
Definition relations_database.hpp:288
osmium::relations::RelationsDatabase
Definition relations_database.hpp:82
osmium::relations::RelationsDatabase::for_each_relation
void for_each_relation(TFunc &&func)
Definition relations_database.hpp:323
osmium::relations::RelationsDatabase::operator[]
RelationHandle operator[](std::size_t pos) noexcept
Definition relations_database.hpp:312
osmium::relations::RelationsDatabase::size
std::size_t size() const noexcept
Definition relations_database.hpp:155
osmium::relations::RelationsDatabase::m_stash
osmium::ItemStash & m_stash
Definition relations_database.hpp:102
osmium::relations::RelationsDatabase::members
std::size_t & members(std::size_t pos) noexcept
Definition relations_database.hpp:114
osmium::relations::RelationsDatabase::count_relations
std::size_t count_relations() const noexcept
Definition relations_database.hpp:184
osmium::relations::RelationsDatabase::add
RelationHandle add(const osmium::Relation &relation)
Definition relations_database.hpp:317
osmium::relations::RelationsDatabase::used_memory
std::size_t used_memory() const noexcept
Definition relations_database.hpp:144
osmium::relations::RelationsDatabase::remove
void remove(std::size_t pos)
Definition relations_database.hpp:118
osmium::relations::RelationsDatabase::get_relation
osmium::Relation & get_relation(std::size_t pos)
Definition relations_database.hpp:105
osmium::relations::RelationsDatabase::m_elements
std::vector< element > m_elements
Definition relations_database.hpp:103
osmium::relations::RelationsDatabase::RelationsDatabase
RelationsDatabase(osmium::ItemStash &stash)
Definition relations_database.hpp:133
item_stash.hpp
osmium
Namespace for everything in the Osmium library.
Definition assembler.hpp:53
osmium::item_type::relation
@ relation
relation.hpp
osmium::relations::RelationsDatabase::element
Definition relations_database.hpp:86
osmium::relations::RelationsDatabase::element::members
std::size_t members
Definition relations_database.hpp:98
osmium::relations::RelationsDatabase::element::handle
osmium::ItemStash::handle_type handle
A handle to the relation in the ItemStash.
Definition relations_database.hpp:89