Libosmium  2.9.0
Fast and flexible C++ library for working with OpenStreetMap data
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 (http://osmcode.org/libosmium).
7 
8 Copyright 2013-2016 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 <cstddef>
37 #include <utility>
38 
39 #include <osmium/index/index.hpp>
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  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 
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 
134  Hybrid() :
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  auto result_main = m_main.get_all(id);
163  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
size_t used_memory() const final
Definition: sparse_mem_multimap.hpp:120
void reserve(const size_t size)
Definition: hybrid.hpp:149
typename collection_type::iterator iterator
Definition: sparse_mem_multimap.hpp:67
size_t used_memory() const final
Definition: hybrid.hpp:145
VectorBasedSparseMultimap< TId, TValue, StdVectorWrap > SparseMemArray
Definition: sparse_mem_array.hpp:50
Hybrid()
Definition: hybrid.hpp:134
void unsorted_set(const TId id, const TValue value)
Definition: hybrid.hpp:153
void remove(const TId id, const TValue value)
Definition: sparse_mem_multimap.hpp:98
std::pair< iterator, iterator > get_all(const TId id)
Definition: sparse_mem_multimap.hpp:90
bool operator!=(const HybridIterator &rhs) const
Definition: hybrid.hpp:102
Namespace for everything in the Osmium library.
Definition: assembler.hpp:73
std::pair< iterator, iterator > get_all(const TId id)
Definition: hybrid.hpp:161
void consolidate()
Definition: hybrid.hpp:173
const element_type & operator*()
Definition: hybrid.hpp:106
HybridIterator< TId, TValue > operator++(int)
Definition: hybrid.hpp:89
extra_map_type m_extra
Definition: hybrid.hpp:127
HybridIterator & operator++()
Definition: hybrid.hpp:77
main_map_type::iterator m_end_main
Definition: hybrid.hpp:59
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
Definition: sparse_mem_multimap.hpp:56
void clear() final
Definition: sparse_mem_multimap.hpp:124
main_map_type m_main
Definition: hybrid.hpp:126
void set(const TId id, const TValue value) final
Set the field with id to value.
Definition: hybrid.hpp:157
extra_map_type::iterator m_begin_extra
Definition: hybrid.hpp:60
Definition: multimap.hpp:51
typename std::pair< TId, TValue > element_type
Definition: hybrid.hpp:56
Definition: hybrid.hpp:121
const element_type * operator->()
Definition: hybrid.hpp:114
main_map_type::iterator m_begin_main
Definition: hybrid.hpp:58
size_t size() const final
Definition: sparse_mem_multimap.hpp:116
void set(const TId id, const TValue value) final
Set the field with id to value.
Definition: sparse_mem_multimap.hpp:86
SparseMemArray< TId, TValue > main_map_type
Definition: hybrid.hpp:123
HybridIterator< TId, TValue > iterator
Definition: hybrid.hpp:131
extra_map_type::iterator m_end_extra
Definition: hybrid.hpp:61
SparseMemArray< TId, TValue > main_map_type
Definition: hybrid.hpp:53
bool operator==(const HybridIterator &rhs) const
Definition: hybrid.hpp:95
void dump_as_list(const int fd) final
Definition: hybrid.hpp:182
void clear() final
Definition: hybrid.hpp:187
void sort() final
Definition: hybrid.hpp:192
size_t size() const final
Definition: hybrid.hpp:141