Loading...
Searching...
No Matches
relations_database.hpp
Go to the documentation of this file.
#ifndef OSMIUM_RELATIONS_RELATIONS_DATABASE_HPP
#define OSMIUM_RELATIONS_RELATIONS_DATABASE_HPP
/*
This file is part of Osmium (https://osmcode.org/libosmium).
Copyright 2013-2025 Jochen Topf <jochen@topf.org> and others (see README).
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <osmium/osm/relation.hpp>
#include <osmium/storage/item_stash.hpp>
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <utility>
#include <vector>
namespace osmium {
namespace relations {
class RelationHandle;
class RelationsDatabase {
friend class RelationHandle;
struct element {
osmium::ItemStash::handle_type handle;
std::size_t members;
}; // struct element
osmium::ItemStash& m_stash;
std::vector<element> m_elements;
osmium::Relation& get_relation(std::size_t pos) {
assert(pos < m_elements.size());
return m_stash.get<osmium::Relation>(m_elements[pos].handle);
}
std::size_t& members(std::size_t pos) noexcept {
return m_elements[pos].members;
}
void remove(std::size_t pos) {
auto& elem = m_elements[pos];
m_stash.remove_item(elem.handle);
elem = element{osmium::ItemStash::handle_type{}, 0};
}
public:
explicit RelationsDatabase(osmium::ItemStash& stash) :
m_stash(stash) {
}
std::size_t used_memory() const noexcept {
return (sizeof(element) * m_elements.capacity()) +
sizeof(RelationsDatabase);
}
std::size_t size() const noexcept {
return m_elements.size();
}
RelationHandle add(const osmium::Relation& relation);
RelationHandle operator[](std::size_t pos) noexcept;
std::size_t count_relations() const noexcept {
return std::count_if(m_elements.cbegin(), m_elements.cend(), [&](const element& elem) {
return elem.handle.valid();
});
}
template <typename TFunc>
void for_each_relation(TFunc&& func);
}; // RelationsDatabase
class RelationHandle {
friend class RelationsDatabase;
RelationsDatabase* m_relation_database;
std::size_t m_pos;
RelationHandle(RelationsDatabase* relation_database, std::size_t pos) :
m_relation_database(relation_database),
m_pos(pos) {
}
public:
RelationsDatabase* relation_database() const noexcept {
return m_relation_database;
}
std::size_t pos() const noexcept {
return m_pos;
}
Relation& operator*() {
return m_relation_database->get_relation(m_pos);
}
const Relation& operator*() const {
return m_relation_database->get_relation(m_pos);
}
Relation* operator->() {
return &m_relation_database->get_relation(m_pos);
}
const Relation* operator->() const {
return &m_relation_database->get_relation(m_pos);
}
void remove() {
m_relation_database->remove(pos());
}
void set_members(std::size_t value) noexcept {
m_relation_database->members(m_pos) = value;
}
void increment_members() noexcept {
++(m_relation_database->members(m_pos));
}
void decrement_members() noexcept {
assert(m_relation_database->members(m_pos) > 0);
--(m_relation_database->members(m_pos));
}
bool has_all_members() const noexcept {
return m_relation_database->members(m_pos) == 0;
}
}; // class RelationHandle
inline RelationHandle RelationsDatabase::operator[](std::size_t pos) noexcept {
assert(pos < m_elements.size());
return {this, pos};
}
inline RelationHandle RelationsDatabase::add(const osmium::Relation& relation) {
m_elements.push_back(element{m_stash.add_item(relation), 0});
return {this, m_elements.size() - 1};
}
template <typename TFunc>
void RelationsDatabase::for_each_relation(TFunc&& func) {
for (std::size_t pos = 0; pos < m_elements.size(); ++pos) {
if (m_elements[pos].handle.valid()) {
std::forward<TFunc>(func)(RelationHandle{this, pos});
}
}
}
} // namespace relations
} // namespace osmium
#endif // OSMIUM_RELATIONS_RELATIONS_DATABASE_HPP
osmium::ItemStash::handle_type
Definition item_stash.hpp:71
osmium::ItemStash
Definition item_stash.hpp:57
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::RelationsDatabase
friend class RelationsDatabase
Definition relations_database.hpp:210
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::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::RelationHandle
friend class RelationHandle
Definition relations_database.hpp:84
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::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::item_type::relation
@ relation
Definition item_type.hpp:50
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