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

Loading...
Searching...
No Matches
hybrid.hpp
Go to the documentation of this file.
1#ifndef OSMIUM_INDEX_MULTIMAP_HYBRID_HPP
2#define OSMIUM_INDEX_MULTIMAP_HYBRID_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/index/index.hpp>
37#include <osmium/index/multimap.hpp>
38#include <osmium/index/multimap/sparse_mem_array.hpp>
39#include <osmium/index/multimap/sparse_mem_multimap.hpp>
40
41#include <cstddef>
42#include <utility>
43
44 namespace osmium {
45
46 namespace index {
47
48 namespace multimap {
49
50 template <typename TId, typename TValue>
51 class HybridIterator {
52
53 using main_map_type = SparseMemArray<TId, TValue>;
54 using extra_map_type = SparseMemMultimap<TId, TValue>;
55
56 using element_type = typename std::pair<TId, TValue>;
57
58 typename main_map_type::iterator m_begin_main;
59 typename main_map_type::iterator m_end_main;
60 typename extra_map_type::iterator m_begin_extra;
61 typename extra_map_type::iterator m_end_extra;
62
63 public:
64
65 HybridIterator(typename main_map_type::iterator begin_main,
66 typename main_map_type::iterator end_main,
67 typename extra_map_type::iterator begin_extra,
68 typename extra_map_type::iterator end_extra) :
69 m_begin_main(begin_main),
70 m_end_main(end_main),
71 m_begin_extra(begin_extra),
72 m_end_extra(end_extra) {
73 }
74
75 ~HybridIterator() noexcept = default;
76
77 HybridIterator& operator++() {
78 if (m_begin_main == m_end_main) {
79 ++m_begin_extra;
80 } else {
81 ++m_begin_main;
82 while (m_begin_main != m_end_main && m_begin_main->second == osmium::index::empty_value<TValue>()) { // ignore removed elements
83 ++m_begin_main;
84 }
85 }
86 return *this;
87 }
88
89 HybridIterator<TId, TValue> operator++(int) {
90 const auto tmp{*this};
91 operator++();
92 return tmp;
93 }
94
95 bool operator==(const HybridIterator& rhs) const {
96 return m_begin_main == rhs.m_begin_main &&
97 m_end_main == rhs.m_end_main &&
98 m_begin_extra == rhs.m_begin_extra &&
99 m_end_extra == rhs.m_end_extra;
100 }
101
102 bool operator!=(const HybridIterator& rhs) const {
103 return !operator==(rhs);
104 }
105
106 const element_type& operator*() {
107 if (m_begin_main == m_end_main) {
108 return *m_begin_extra;
109 }
110 return *m_begin_main;
111 }
112
113 const element_type* operator->() {
114 return &operator*();
115 }
116
117 }; // class HybridIterator
118
119 template <typename TId, typename TValue>
120 class Hybrid : public Multimap<TId, TValue> {
121
122 using main_map_type = SparseMemArray<TId, TValue>;
123 using extra_map_type = SparseMemMultimap<TId, TValue>;
124
125 main_map_type m_main;
126 extra_map_type m_extra;
127
128 public:
129
130 using iterator = HybridIterator<TId, TValue>;
131 using const_iterator = const HybridIterator<TId, TValue>;
132
133 Hybrid() :
134 m_main(),
135 m_extra() {
136 }
137
138 ~Hybrid() noexcept = default;
139
140 size_t size() const final {
141 return m_main.size() + m_extra.size();
142 }
143
144 size_t used_memory() const final {
145 return m_main.used_memory() + m_extra.used_memory();
146 }
147
148 void reserve(const size_t size) {
149 m_main.reserve(size);
150 }
151
152 void unsorted_set(const TId id, const TValue value) {
153 m_main.set(id, value);
154 }
155
156 void set(const TId id, const TValue value) final {
157 m_extra.set(id, value);
158 }
159
160 std::pair<iterator, iterator> get_all(const TId id) {
161 const auto result_main = m_main.get_all(id);
162 const auto result_extra = m_extra.get_all(id);
163 return std::make_pair(iterator{result_main.first, result_main.second, result_extra.first, result_extra.second},
164 iterator{result_main.second, result_main.second, result_extra.second, result_extra.second});
165 }
166
167 void remove(const TId id, const TValue value) {
168 m_main.remove(id, value);
169 m_extra.remove(id, value);
170 }
171
172 void consolidate() {
173 m_main.erase_removed();
174 for (const auto& element : m_extra) {
175 m_main.set(element.first, element.second);
176 }
177 m_extra.clear();
178 m_main.sort();
179 }
180
181 void dump_as_list(const int fd) final {
182 consolidate();
183 m_main.dump_as_list(fd);
184 }
185
186 void clear() final {
187 m_main.clear();
188 m_extra.clear();
189 }
190
191 void sort() final {
192 m_main.sort();
193 }
194
195 }; // class Hybrid
196
197 } // namespace multimap
198
199 } // namespace index
200
201} // namespace osmium
202
203#endif // OSMIUM_INDEX_MULTIMAP_HYBRID_HPP
osmium::index::multimap::HybridIterator
Definition hybrid.hpp:51
osmium::index::multimap::HybridIterator::m_begin_main
main_map_type::iterator m_begin_main
Definition hybrid.hpp:58
osmium::index::multimap::HybridIterator::operator*
const element_type & operator*()
Definition hybrid.hpp:106
osmium::index::multimap::HybridIterator::main_map_type
SparseMemArray< TId, TValue > main_map_type
Definition hybrid.hpp:53
osmium::index::multimap::HybridIterator::element_type
typename std::pair< TId, TValue > element_type
Definition hybrid.hpp:56
osmium::index::multimap::HybridIterator::operator==
bool operator==(const HybridIterator &rhs) const
Definition hybrid.hpp:95
osmium::index::multimap::HybridIterator::HybridIterator
HybridIterator(typename main_map_type::iterator begin_main, typename main_map_type::iterator end_main, typename extra_map_type::iterator begin_extra, typename extra_map_type::iterator end_extra)
Definition hybrid.hpp:65
osmium::index::multimap::HybridIterator::operator++
HybridIterator< TId, TValue > operator++(int)
Definition hybrid.hpp:89
osmium::index::multimap::HybridIterator::m_begin_extra
extra_map_type::iterator m_begin_extra
Definition hybrid.hpp:60
osmium::index::multimap::HybridIterator::operator!=
bool operator!=(const HybridIterator &rhs) const
Definition hybrid.hpp:102
osmium::index::multimap::HybridIterator::operator++
HybridIterator & operator++()
Definition hybrid.hpp:77
osmium::index::multimap::HybridIterator::~HybridIterator
~HybridIterator() noexcept=default
osmium::index::multimap::HybridIterator::m_end_extra
extra_map_type::iterator m_end_extra
Definition hybrid.hpp:61
osmium::index::multimap::HybridIterator::operator->
const element_type * operator->()
Definition hybrid.hpp:113
osmium::index::multimap::HybridIterator::m_end_main
main_map_type::iterator m_end_main
Definition hybrid.hpp:59
osmium::index::multimap::Hybrid
Definition hybrid.hpp:120
osmium::index::multimap::Hybrid::reserve
void reserve(const size_t size)
Definition hybrid.hpp:148
osmium::index::multimap::Hybrid::used_memory
size_t used_memory() const final
Definition hybrid.hpp:144
osmium::index::multimap::Hybrid::sort
void sort() final
Definition hybrid.hpp:191
osmium::index::multimap::Hybrid::main_map_type
SparseMemArray< TId, TValue > main_map_type
Definition hybrid.hpp:122
osmium::index::multimap::Hybrid::m_extra
extra_map_type m_extra
Definition hybrid.hpp:126
osmium::index::multimap::Hybrid::consolidate
void consolidate()
Definition hybrid.hpp:172
osmium::index::multimap::Hybrid::unsorted_set
void unsorted_set(const TId id, const TValue value)
Definition hybrid.hpp:152
osmium::index::multimap::Hybrid::clear
void clear() final
Definition hybrid.hpp:186
osmium::index::multimap::Hybrid::Hybrid
Hybrid()
Definition hybrid.hpp:133
osmium::index::multimap::Hybrid::m_main
main_map_type m_main
Definition hybrid.hpp:125
osmium::index::multimap::Hybrid::size
size_t size() const final
Definition hybrid.hpp:140
osmium::index::multimap::Hybrid::set
void set(const TId id, const TValue value) final
Set the field with id to value.
Definition hybrid.hpp:156
osmium::index::multimap::Hybrid::get_all
std::pair< iterator, iterator > get_all(const TId id)
Definition hybrid.hpp:160
osmium::index::multimap::Hybrid::dump_as_list
void dump_as_list(const int fd) final
Definition hybrid.hpp:181
osmium::index::multimap::Hybrid::~Hybrid
~Hybrid() noexcept=default
osmium::index::multimap::Hybrid::remove
void remove(const TId id, const TValue value)
Definition hybrid.hpp:167
osmium::index::multimap::Multimap
Definition multimap.hpp:51
osmium::index::multimap::SparseMemMultimap
Definition sparse_mem_multimap.hpp:56
osmium::index::multimap::SparseMemMultimap::used_memory
size_t used_memory() const override
Definition sparse_mem_multimap.hpp:122
osmium::index::multimap::SparseMemMultimap::remove
void remove(const TId id, const TValue value)
Definition sparse_mem_multimap.hpp:100
osmium::index::multimap::SparseMemMultimap::size
size_t size() const override
Definition sparse_mem_multimap.hpp:118
osmium::index::multimap::SparseMemMultimap::iterator
typename collection_type::iterator iterator
Definition sparse_mem_multimap.hpp:69
osmium::index::multimap::SparseMemMultimap::set
void set(const TId id, const TValue value) override
Set the field with id to value.
Definition sparse_mem_multimap.hpp:88
osmium::index::multimap::SparseMemMultimap::get_all
std::pair< iterator, iterator > get_all(const TId id)
Definition sparse_mem_multimap.hpp:92
osmium::index::multimap::SparseMemMultimap::clear
void clear() override
Definition sparse_mem_multimap.hpp:126
index.hpp
sparse_mem_array.hpp
multimap.hpp
osmium::index::multimap::SparseMemArray
VectorBasedSparseMultimap< TId, TValue, StdVectorWrap > SparseMemArray
Definition sparse_mem_array.hpp:50
osmium::index::empty_value
constexpr T empty_value()
Definition index.hpp:79
osmium
Namespace for everything in the Osmium library.
Definition assembler.hpp:53
sparse_mem_multimap.hpp