Libosmium  2.20.0
Fast and flexible C++ library for working with OpenStreetMap data
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
6This file is part of Osmium (https://osmcode.org/libosmium).
7
8Copyright 2013-2023 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
38
39#include <algorithm>
40#include <cassert>
41#include <cstddef>
42#include <utility>
43#include <vector>
44
45namespace osmium {
46
47 namespace relations {
48
49 class RelationHandle;
50
83
84 friend class RelationHandle;
85
86 struct element {
87
90
98 std::size_t members;
99
100 }; // struct element
101
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);
122 }
123
124 public:
125
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
209
210 friend class RelationsDatabase;
211
213 std::size_t m_pos;
214
217 m_pos(pos) {
218 }
219
220 public:
221
226 return m_relation_database;
227 }
228
238 std::size_t pos() const noexcept {
239 return m_pos;
240 }
241
247 }
248
252 const Relation& operator*() const {
254 }
255
261 }
262
266 const Relation* operator->() const {
268 }
269
274 void remove() {
276 }
277
281 void set_members(std::size_t value) noexcept {
283 }
284
288 void increment_members() noexcept {
290 }
291
297 void decrement_members() noexcept {
298 assert(m_relation_database->members(m_pos) > 0);
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
319 return {this, m_elements.size() - 1};
320 }
321
322 template <typename TFunc>
324 for (std::size_t pos = 0; pos < m_elements.size(); ++pos) {
325 if (m_elements[pos].handle.valid()) {
326 func(RelationHandle{this, pos});
327 }
328 }
329 }
330
331 } // namespace relations
332
333} // namespace osmium
334
335#endif // OSMIUM_RELATIONS_RELATIONS_DATABASE_HPP
Definition: item_stash.hpp:71
Definition: item_stash.hpp:57
T & get(handle_type handle) const
Definition: item_stash.hpp:294
void remove_item(handle_type handle)
Definition: item_stash.hpp:338
handle_type add_item(const osmium::memory::Item &item)
Definition: item_stash.hpp:251
Definition: relation.hpp:161
Definition: relations_database.hpp:208
void remove()
Definition: relations_database.hpp:274
void decrement_members() noexcept
Definition: relations_database.hpp:297
RelationsDatabase * m_relation_database
Definition: relations_database.hpp:212
RelationHandle(RelationsDatabase *relation_database, std::size_t pos)
Definition: relations_database.hpp:215
Relation * operator->()
Definition: relations_database.hpp:259
std::size_t m_pos
Definition: relations_database.hpp:213
std::size_t pos() const noexcept
Definition: relations_database.hpp:238
const Relation & operator*() const
Definition: relations_database.hpp:252
RelationsDatabase * relation_database() const noexcept
Definition: relations_database.hpp:225
Relation & operator*()
Definition: relations_database.hpp:245
void set_members(std::size_t value) noexcept
Definition: relations_database.hpp:281
bool has_all_members() const noexcept
Definition: relations_database.hpp:306
const Relation * operator->() const
Definition: relations_database.hpp:266
void increment_members() noexcept
Definition: relations_database.hpp:288
Definition: relations_database.hpp:82
void for_each_relation(TFunc &&func)
Definition: relations_database.hpp:323
RelationHandle operator[](std::size_t pos) noexcept
Definition: relations_database.hpp:312
std::size_t size() const noexcept
Definition: relations_database.hpp:155
osmium::ItemStash & m_stash
Definition: relations_database.hpp:102
std::size_t & members(std::size_t pos) noexcept
Definition: relations_database.hpp:114
std::size_t count_relations() const noexcept
Definition: relations_database.hpp:184
RelationHandle add(const osmium::Relation &relation)
Definition: relations_database.hpp:317
std::size_t used_memory() const noexcept
Definition: relations_database.hpp:144
void remove(std::size_t pos)
Definition: relations_database.hpp:118
osmium::Relation & get_relation(std::size_t pos)
Definition: relations_database.hpp:105
std::vector< element > m_elements
Definition: relations_database.hpp:103
RelationsDatabase(osmium::ItemStash &stash)
Definition: relations_database.hpp:133
@ relation
Definition: entity_bits.hpp:70
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
Definition: relations_database.hpp:86
std::size_t members
Definition: relations_database.hpp:98
osmium::ItemStash::handle_type handle
A handle to the relation in the ItemStash.
Definition: relations_database.hpp:89