Libosmium  2.17.2
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 
6 This file is part of Osmium (https://osmcode.org/libosmium).
7 
8 Copyright 2013-2021 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>
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>
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) {
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 
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 &&
99  m_end_extra == rhs.m_end_extra;
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 
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
main_map_type::iterator m_begin_main
Definition: hybrid.hpp:58
SparseMemArray< TId, TValue > main_map_type
Definition: hybrid.hpp:53
const element_type & operator*()
Definition: hybrid.hpp:106
typename std::pair< TId, TValue > element_type
Definition: hybrid.hpp:56
bool operator==(const HybridIterator &rhs) const
Definition: hybrid.hpp:95
const element_type * operator->()
Definition: hybrid.hpp:113
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
extra_map_type::iterator m_begin_extra
Definition: hybrid.hpp:60
bool operator!=(const HybridIterator &rhs) const
Definition: hybrid.hpp:102
HybridIterator< TId, TValue > operator++(int)
Definition: hybrid.hpp:89
extra_map_type::iterator m_end_extra
Definition: hybrid.hpp:61
main_map_type::iterator m_end_main
Definition: hybrid.hpp:59
HybridIterator & operator++()
Definition: hybrid.hpp:77
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
Definition: sparse_mem_multimap.hpp:56
size_t used_memory() const override
Definition: sparse_mem_multimap.hpp:122
std::pair< iterator, iterator > get_all(const TId id)
Definition: sparse_mem_multimap.hpp:92
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
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