Libosmium  2.2.0
Fast and flexible C++ library for working with OpenStreetMap data
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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-2015 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  typedef typename std::pair<TId, TValue> element_type;
57 
58  typename main_map_type::iterator m_begin_main;
59  typename main_map_type::iterator m_end_main;
62 
63  public:
64 
65  explicit 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 
76  if (m_begin_main == m_end_main) {
77  ++m_begin_extra;
78  } else {
79  ++m_begin_main;
80  while (m_begin_main != m_end_main && m_begin_main->second == osmium::index::empty_value<TValue>()) { // ignore removed elements
81  ++m_begin_main;
82  }
83  }
84  return *this;
85  }
86 
88  auto tmp(*this);
89  operator++();
90  return tmp;
91  }
92 
93  bool operator==(const HybridIterator& rhs) const {
94  return m_begin_main == rhs.m_begin_main &&
95  m_end_main == rhs.m_end_main &&
96  m_begin_extra == rhs.m_begin_extra &&
97  m_end_extra == rhs.m_end_extra;
98  }
99 
100  bool operator!=(const HybridIterator& rhs) const {
101  return ! operator==(rhs);
102  }
103 
104  const element_type& operator*() {
105  if (m_begin_main == m_end_main) {
106  return *m_begin_extra;
107  } else {
108  return *m_begin_main;
109  }
110  }
111 
112  const element_type* operator->() {
113  return &operator*();
114  }
115 
116  }; // class HybridIterator
117 
118  template <typename TId, typename TValue>
119  class Hybrid : public Multimap<TId, TValue> {
120 
123 
124  main_map_type m_main;
125  extra_map_type m_extra;
126 
127  public:
128 
131 
132  Hybrid() :
133  m_main(),
134  m_extra() {
135  }
136 
137  size_t size() const override final {
138  return m_main.size() + m_extra.size();
139  }
140 
141  size_t used_memory() const override final {
142  return m_main.used_memory() + m_extra.used_memory();
143  }
144 
145  void reserve(const size_t size) {
146  m_main.reserve(size);
147  }
148 
149  void unsorted_set(const TId id, const TValue value) {
150  m_main.set(id, value);
151  }
152 
153  void set(const TId id, const TValue value) override final {
154  m_extra.set(id, value);
155  }
156 
157  std::pair<iterator, iterator> get_all(const TId id) {
158  auto result_main = m_main.get_all(id);
159  auto result_extra = m_extra.get_all(id);
160  return std::make_pair(iterator(result_main.first, result_main.second, result_extra.first, result_extra.second),
161  iterator(result_main.second, result_main.second, result_extra.second, result_extra.second));
162  }
163 
164  void remove(const TId id, const TValue value) {
165  m_main.remove(id, value);
166  m_extra.remove(id, value);
167  }
168 
169  void consolidate() {
170  m_main.erase_removed();
171  for (const auto& element : m_extra) {
172  m_main.set(element.first, element.second);
173  }
174  m_extra.clear();
175  m_main.sort();
176  }
177 
178  void dump_as_list(const int fd) override final {
179  consolidate();
180  m_main.dump_as_list(fd);
181  }
182 
183  void clear() override final {
184  m_main.clear();
185  m_extra.clear();
186  }
187 
188  void sort() override final {
189  m_main.sort();
190  }
191 
192  }; // class Hybrid
193 
194  } // namespace multimap
195 
196  } // namespace index
197 
198 } // namespace osmium
199 
200 #endif // OSMIUM_INDEX_MULTIMAP_HYBRID_HPP
void set(const TId id, const TValue value) overridefinal
Set the field with id to value.
Definition: hybrid.hpp:153
SparseMemArray< TId, TValue > main_map_type
Definition: hybrid.hpp:121
void reserve(const size_t size)
Definition: hybrid.hpp:145
size_t size() const overridefinal
Definition: sparse_mem_multimap.hpp:117
HybridIterator< TId, TValue > iterator
Definition: hybrid.hpp:129
SparseMemArray< TId, TValue > main_map_type
Definition: hybrid.hpp:53
VectorBasedSparseMultimap< TId, TValue, StdVectorWrap > SparseMemArray
Definition: sparse_mem_array.hpp:50
Hybrid()
Definition: hybrid.hpp:132
void unsorted_set(const TId id, const TValue value)
Definition: hybrid.hpp:149
void remove(const TId id, const TValue value)
Definition: sparse_mem_multimap.hpp:99
size_t used_memory() const overridefinal
Definition: hybrid.hpp:141
std::pair< iterator, iterator > get_all(const TId id)
Definition: sparse_mem_multimap.hpp:91
bool operator!=(const HybridIterator &rhs) const
Definition: hybrid.hpp:100
Namespace for everything in the Osmium library.
Definition: assembler.hpp:55
std::pair< iterator, iterator > get_all(const TId id)
Definition: hybrid.hpp:157
void consolidate()
Definition: hybrid.hpp:169
size_t used_memory() const overridefinal
Definition: sparse_mem_multimap.hpp:121
const element_type & operator*()
Definition: hybrid.hpp:104
HybridIterator< TId, TValue > operator++(int)
Definition: hybrid.hpp:87
extra_map_type m_extra
Definition: hybrid.hpp:125
HybridIterator & operator++()
Definition: hybrid.hpp:75
collection_type::iterator iterator
Definition: sparse_mem_multimap.hpp:67
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
void set(const TId id, const TValue value) overridefinal
Set the field with id to value.
Definition: sparse_mem_multimap.hpp:87
Definition: sparse_mem_multimap.hpp:56
main_map_type m_main
Definition: hybrid.hpp:124
void clear() overridefinal
Definition: sparse_mem_multimap.hpp:125
extra_map_type::iterator m_begin_extra
Definition: hybrid.hpp:60
void clear() overridefinal
Definition: hybrid.hpp:183
void dump_as_list(const int fd) overridefinal
Definition: hybrid.hpp:178
Definition: multimap.hpp:51
Definition: hybrid.hpp:119
void sort() overridefinal
Definition: hybrid.hpp:188
const element_type * operator->()
Definition: hybrid.hpp:112
main_map_type::iterator m_begin_main
Definition: hybrid.hpp:58
extra_map_type::iterator m_end_extra
Definition: hybrid.hpp:61
bool operator==(const HybridIterator &rhs) const
Definition: hybrid.hpp:93
SparseMemMultimap< TId, TValue > extra_map_type
Definition: hybrid.hpp:122
SparseMemMultimap< TId, TValue > extra_map_type
Definition: hybrid.hpp:54
size_t size() const overridefinal
Definition: hybrid.hpp:137
const HybridIterator< TId, TValue > const_iterator
Definition: hybrid.hpp:130
std::pair< TId, TValue > element_type
Definition: hybrid.hpp:56