Libosmium  2.20.0
Fast and flexible C++ library for working with OpenStreetMap data
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
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
40
41#include <cstddef>
42#include <utility>
43
44namespace osmium {
45
46 namespace index {
47
48 namespace multimap {
49
50 template <typename TId, typename TValue>
52
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;
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) {
80 } else {
82 while (m_begin_main != m_end_main && m_begin_main->second == osmium::index::empty_value<TValue>()) { // ignore removed elements
84 }
85 }
86 return *this;
87 }
88
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 &&
100 }
101
102 bool operator!=(const HybridIterator& rhs) const {
103 return !operator==(rhs);
104 }
105
107 if (m_begin_main == m_end_main) {
108 return *m_begin_extra;
109 }
110 return *m_begin_main;
111 }
112
114 return &operator*();
115 }
116
117 }; // class HybridIterator
118
119 template <typename TId, typename TValue>
120 class Hybrid : public Multimap<TId, TValue> {
121
124
127
128 public:
129
132
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
main_map_type::iterator m_begin_main
Definition: hybrid.hpp:58
const element_type & operator*()
Definition: hybrid.hpp:106
SparseMemArray< TId, TValue > main_map_type
Definition: hybrid.hpp:53
typename std::pair< TId, TValue > element_type
Definition: hybrid.hpp:56
bool operator==(const HybridIterator &rhs) const
Definition: hybrid.hpp:95
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
HybridIterator< TId, TValue > operator++(int)
Definition: hybrid.hpp:89
extra_map_type::iterator m_begin_extra
Definition: hybrid.hpp:60
bool operator!=(const HybridIterator &rhs) const
Definition: hybrid.hpp:102
HybridIterator & operator++()
Definition: hybrid.hpp:77
extra_map_type::iterator m_end_extra
Definition: hybrid.hpp:61
const element_type * operator->()
Definition: hybrid.hpp:113
main_map_type::iterator m_end_main
Definition: hybrid.hpp:59
Definition: hybrid.hpp:120
void reserve(const size_t size)
Definition: hybrid.hpp:148
size_t used_memory() const final
Definition: hybrid.hpp:144
void sort() final
Definition: hybrid.hpp:191
SparseMemArray< TId, TValue > main_map_type
Definition: hybrid.hpp:122
extra_map_type m_extra
Definition: hybrid.hpp:126
void consolidate()
Definition: hybrid.hpp:172
void unsorted_set(const TId id, const TValue value)
Definition: hybrid.hpp:152
void clear() final
Definition: hybrid.hpp:186
Hybrid()
Definition: hybrid.hpp:133
main_map_type m_main
Definition: hybrid.hpp:125
size_t size() const final
Definition: hybrid.hpp:140
void set(const TId id, const TValue value) final
Set the field with id to value.
Definition: hybrid.hpp:156
std::pair< iterator, iterator > get_all(const TId id)
Definition: hybrid.hpp:160
void dump_as_list(const int fd) final
Definition: hybrid.hpp:181
void remove(const TId id, const TValue value)
Definition: hybrid.hpp:167
Definition: multimap.hpp:51
element_type * iterator
Definition: multimap.hpp:79
Definition: sparse_mem_multimap.hpp:56
size_t used_memory() const override
Definition: sparse_mem_multimap.hpp:122
void remove(const TId id, const TValue value)
Definition: sparse_mem_multimap.hpp:100
size_t size() const override
Definition: sparse_mem_multimap.hpp:118
typename collection_type::iterator iterator
Definition: sparse_mem_multimap.hpp:69
void set(const TId id, const TValue value) override
Set the field with id to value.
Definition: sparse_mem_multimap.hpp:88
std::pair< iterator, iterator > get_all(const TId id)
Definition: sparse_mem_multimap.hpp:92
void clear() override
Definition: sparse_mem_multimap.hpp:126
VectorBasedSparseMultimap< TId, TValue, StdVectorWrap > SparseMemArray
Definition: sparse_mem_array.hpp:50
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53