Libosmium  2.17.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
6This file is part of Osmium (https://osmcode.org/libosmium).
7
8Copyright 2013-2021 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 } else {
110 return *m_begin_main;
111 }
112 }
113
115 return &operator*();
116 }
117
118 }; // class HybridIterator
119
120 template <typename TId, typename TValue>
121 class Hybrid : public Multimap<TId, TValue> {
122
125
128
129 public:
130
133
135 m_main(),
136 m_extra() {
137 }
138
139 ~Hybrid() noexcept = default;
140
141 size_t size() const final {
142 return m_main.size() + m_extra.size();
143 }
144
145 size_t used_memory() const final {
146 return m_main.used_memory() + m_extra.used_memory();
147 }
148
149 void reserve(const size_t size) {
150 m_main.reserve(size);
151 }
152
153 void unsorted_set(const TId id, const TValue value) {
154 m_main.set(id, value);
155 }
156
157 void set(const TId id, const TValue value) final {
158 m_extra.set(id, value);
159 }
160
161 std::pair<iterator, iterator> get_all(const TId id) {
162 const auto result_main = m_main.get_all(id);
163 const auto result_extra = m_extra.get_all(id);
164 return std::make_pair(iterator{result_main.first, result_main.second, result_extra.first, result_extra.second},
165 iterator{result_main.second, result_main.second, result_extra.second, result_extra.second});
166 }
167
168 void remove(const TId id, const TValue value) {
169 m_main.remove(id, value);
170 m_extra.remove(id, value);
171 }
172
173 void consolidate() {
174 m_main.erase_removed();
175 for (const auto& element : m_extra) {
176 m_main.set(element.first, element.second);
177 }
178 m_extra.clear();
179 m_main.sort();
180 }
181
182 void dump_as_list(const int fd) final {
183 consolidate();
184 m_main.dump_as_list(fd);
185 }
186
187 void clear() final {
188 m_main.clear();
189 m_extra.clear();
190 }
191
192 void sort() final {
193 m_main.sort();
194 }
195
196 }; // class Hybrid
197
198 } // namespace multimap
199
200 } // namespace index
201
202} // namespace osmium
203
204#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:114
main_map_type::iterator m_end_main
Definition: hybrid.hpp:59
Definition: hybrid.hpp:121
void reserve(const size_t size)
Definition: hybrid.hpp:149
size_t used_memory() const final
Definition: hybrid.hpp:145
void sort() final
Definition: hybrid.hpp:192
SparseMemArray< TId, TValue > main_map_type
Definition: hybrid.hpp:123
extra_map_type m_extra
Definition: hybrid.hpp:127
void consolidate()
Definition: hybrid.hpp:173
void unsorted_set(const TId id, const TValue value)
Definition: hybrid.hpp:153
void clear() final
Definition: hybrid.hpp:187
Hybrid()
Definition: hybrid.hpp:134
main_map_type m_main
Definition: hybrid.hpp:126
size_t size() const final
Definition: hybrid.hpp:141
void set(const TId id, const TValue value) final
Set the field with id to value.
Definition: hybrid.hpp:157
std::pair< iterator, iterator > get_all(const TId id)
Definition: hybrid.hpp:161
void dump_as_list(const int fd) final
Definition: hybrid.hpp:182
void remove(const TId id, const TValue value)
Definition: hybrid.hpp:168
Definition: multimap.hpp:51
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