Libosmium  2.13.1
Fast and flexible C++ library for working with OpenStreetMap data
id_set.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_INDEX_ID_SET_HPP
2 #define OSMIUM_INDEX_ID_SET_HPP
3 
4 /*
5 
6 This file is part of Osmium (http://osmcode.org/libosmium).
7 
8 Copyright 2013-2017 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 <algorithm>
37 #include <cassert>
38 #include <cstddef>
39 #include <cstring>
40 #include <iterator>
41 #include <memory>
42 #include <type_traits>
43 #include <vector>
44 
45 #include <osmium/osm/item_type.hpp>
46 #include <osmium/osm/types.hpp>
47 
48 namespace osmium {
49 
50  namespace index {
51 
56  template <typename T>
57  class IdSet {
58 
59  public:
60 
61  virtual ~IdSet() = default;
62 
66  virtual void set(T id) = 0;
67 
71  virtual bool get(T id) const noexcept = 0;
72 
76  virtual bool empty() const = 0;
77 
81  virtual void clear() = 0;
82 
86  virtual std::size_t used_memory() const noexcept = 0;
87 
88  }; // class IdSet
89 
90  template <typename T>
91  class IdSetDense;
92 
96  template <typename T>
98 
99 
103 
104  void next() noexcept {
105  while (m_value != m_last && !m_set->get(m_value)) {
106  const T cid = IdSetDense<T>::chunk_id(m_value);
107  assert(cid < m_set->m_data.size());
108  if (!m_set->m_data[cid]) {
109  m_value = (cid + 1) << (IdSetDense<T>::chunk_bits + 3);
110  } else {
111  const auto slot = m_set->m_data[cid][IdSetDense<T>::offset(m_value)];
112  if (slot == 0) {
113  m_value += 8;
114  m_value &= ~0x7;
115  } else {
116  ++m_value;
117  }
118  }
119  }
120  }
121 
122  public:
123 
124  using iterator_category = std::forward_iterator_tag;
125  using value_type = T;
126  using pointer = value_type*;
128 
129  IdSetDenseIterator(const IdSetDense<T>* set, T value, T last) noexcept :
130  m_set(set),
131  m_value(value),
132  m_last(last) {
133  next();
134  }
135 
137  if (m_value != m_last) {
138  ++m_value;
139  next();
140  }
141  return *this;
142  }
143 
145  IdSetDenseIterator<T> tmp{*this};
146  operator++();
147  return tmp;
148  }
149 
150  bool operator==(const IdSetDenseIterator<T>& rhs) const noexcept {
151  return m_set == rhs.m_set && m_value == rhs.m_value;
152  }
153 
154  bool operator!=(const IdSetDenseIterator<T>& rhs) const noexcept {
155  return ! (*this == rhs);
156  }
157 
158  T operator*() const noexcept {
159  assert(m_value < m_last);
160  return m_value;
161  }
162 
163  }; // class IdSetDenseIterator
164 
172  template <typename T>
173  class IdSetDense : public IdSet<T> {
174 
175 
176  friend class IdSetDenseIterator<T>;
177 
178  // This value is a compromise. For node Ids it could be bigger
179  // which would mean less (but larger) memory allocations. For
180  // relations Ids it could be smaller, because they would all fit
181  // into a smaller allocation.
182  constexpr static const std::size_t chunk_bits = 22;
183  constexpr static const std::size_t chunk_size = 1 << chunk_bits;
184 
185  std::vector<std::unique_ptr<unsigned char[]>> m_data;
186  T m_size = 0;
187 
188  static std::size_t chunk_id(T id) noexcept {
189  return id >> (chunk_bits + 3);
190  }
191 
192  static std::size_t offset(T id) noexcept {
193  return (id >> 3) & ((1 << chunk_bits) - 1);
194  }
195 
196  static unsigned char bitmask(T id) noexcept {
197  return 1 << (id & 0x7);
198  }
199 
200  T last() const noexcept {
201  return static_cast<T>(m_data.size()) * chunk_size * 8;
202  }
203 
204  unsigned char& get_element(T id) {
205  const auto cid = chunk_id(id);
206  if (cid >= m_data.size()) {
207  m_data.resize(cid + 1);
208  }
209 
210  auto& chunk = m_data[cid];
211  if (!chunk) {
212  chunk.reset(new unsigned char[chunk_size]);
213  ::memset(chunk.get(), 0, chunk_size);
214  }
215 
216  return chunk[offset(id)];
217  }
218 
219  public:
220 
222 
223  IdSetDense() = default;
224 
231  bool check_and_set(T id) {
232  auto& element = get_element(id);
233 
234  if ((element & bitmask(id)) == 0) {
235  element |= bitmask(id);
236  ++m_size;
237  return true;
238  }
239 
240  return false;
241  }
242 
248  void set(T id) final {
249  (void)check_and_set(id);
250  }
251 
257  void unset(T id) {
258  auto& element = get_element(id);
259 
260  if ((element & bitmask(id)) != 0) {
261  element &= ~bitmask(id);
262  --m_size;
263  }
264  }
265 
271  bool get(T id) const noexcept final {
272  if (chunk_id(id) >= m_data.size()) {
273  return false;
274  }
275  auto* r = m_data[chunk_id(id)].get();
276  if (!r) {
277  return false;
278  }
279  return (r[offset(id)] & bitmask(id)) != 0;
280  }
281 
285  bool empty() const noexcept final {
286  return m_size == 0;
287  }
288 
292  T size() const noexcept {
293  return m_size;
294  }
295 
299  void clear() final {
300  m_data.clear();
301  m_size = 0;
302  }
303 
304  std::size_t used_memory() const noexcept final {
305  return m_data.size() * chunk_size;
306  }
307 
309  return {this, 0, last()};
310  }
311 
313  return {this, last(), last()};
314  }
315 
316  }; // class IdSetDense
317 
322  template <typename T>
323  class IdSetSmall : public IdSet<T> {
324 
325  std::vector<T> m_data;
326 
327  public:
328 
332  void set(T id) final {
333  m_data.push_back(id);
334  }
335 
341  bool get(T id) const noexcept final {
342  const auto it = std::find(m_data.cbegin(), m_data.cend(), id);
343  return it != m_data.cend();
344  }
345 
356  bool get_binary_search(T id) const noexcept {
357  return std::binary_search(m_data.cbegin(), m_data.cend(), id);
358  }
359 
363  bool empty() const noexcept final {
364  return m_data.empty();
365  }
366 
370  void clear() final {
371  m_data.clear();
372  }
373 
378  void sort_unique() {
379  std::sort(m_data.begin(), m_data.end());
380  const auto last = std::unique(m_data.begin(), m_data.end());
381  m_data.erase(last, m_data.end());
382 
383  }
384 
391  std::size_t size() const noexcept {
392  return m_data.size();
393  }
394 
395  std::size_t used_memory() const noexcept final {
396  return m_data.capacity() * sizeof(T);
397  }
398 
400  using const_iterator = typename std::vector<T>::const_iterator;
401 
402  const_iterator begin() const noexcept {
403  return m_data.cbegin();
404  }
405 
406  const_iterator end() const noexcept {
407  return m_data.cend();
408  }
409 
410  const_iterator cbegin() const noexcept {
411  return m_data.cbegin();
412  }
413 
414  const_iterator cend() const noexcept {
415  return m_data.cend();
416  }
417 
418  }; // class IdSetSmall
419 
421  template <template<typename> class IdSetType>
422  class NWRIdSet {
423 
424  using id_set_type = IdSetType<osmium::unsigned_object_id_type>;
425 
426  id_set_type m_sets[3];
427 
428  public:
429 
431  return m_sets[osmium::item_type_to_nwr_index(type)];
432  }
433 
434  const id_set_type& operator()(osmium::item_type type) const noexcept {
435  return m_sets[osmium::item_type_to_nwr_index(type)];
436  }
437 
438  }; // class NWRIdSet
439 
440  } // namespace index
441 
442 } // namespace osmium
443 
444 #endif // OSMIUM_INDEX_ID_SET_HPP
void unset(T id)
Definition: id_set.hpp:257
std::size_t used_memory() const noexcept final
Definition: id_set.hpp:304
T m_last
Definition: id_set.hpp:102
type
Definition: entity_bits.hpp:63
IdSetType< osmium::unsigned_object_id_type > id_set_type
Definition: id_set.hpp:424
Definition: id_set.hpp:422
std::forward_iterator_tag iterator_category
Definition: id_set.hpp:124
Definition: id_set.hpp:57
static std::size_t offset(T id) noexcept
Definition: id_set.hpp:192
virtual void clear()=0
item_type
Definition: item_type.hpp:43
T last() const noexcept
Definition: id_set.hpp:200
value_type * pointer
Definition: id_set.hpp:126
void clear() final
Definition: id_set.hpp:370
std::vector< T > m_data
Definition: id_set.hpp:325
bool get(T id) const noexcept final
Definition: id_set.hpp:271
Definition: id_set.hpp:91
unsigned int item_type_to_nwr_index(item_type type) noexcept
Definition: item_type.hpp:82
const_iterator cend() const noexcept
Definition: id_set.hpp:414
std::size_t used_memory() const noexcept final
Definition: id_set.hpp:395
bool get_binary_search(T id) const noexcept
Definition: id_set.hpp:356
const_iterator end() const noexcept
Definition: id_set.hpp:406
IdSetDenseIterator< T > begin() const
Definition: id_set.hpp:308
bool operator==(const IdSetDenseIterator< T > &rhs) const noexcept
Definition: id_set.hpp:150
Definition: id_set.hpp:97
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
const_iterator begin() const noexcept
Definition: id_set.hpp:402
static unsigned char bitmask(T id) noexcept
Definition: id_set.hpp:196
const IdSetDense< T > * m_set
Definition: id_set.hpp:100
id_set_type & operator()(osmium::item_type type) noexcept
Definition: id_set.hpp:430
IdSetDenseIterator(const IdSetDense< T > *set, T value, T last) noexcept
Definition: id_set.hpp:129
T operator*() const noexcept
Definition: id_set.hpp:158
T m_value
Definition: id_set.hpp:101
bool check_and_set(T id)
Definition: id_set.hpp:231
const id_set_type & operator()(osmium::item_type type) const noexcept
Definition: id_set.hpp:434
bool empty() const noexcept final
Definition: id_set.hpp:285
std::vector< std::unique_ptr< unsigned char[]> > m_data
Definition: id_set.hpp:185
IdSetDenseIterator< T > operator++(int) noexcept
Definition: id_set.hpp:144
IdSetDenseIterator< T > & operator++() noexcept
Definition: id_set.hpp:136
T value_type
Definition: id_set.hpp:125
value_type & reference
Definition: id_set.hpp:127
IdSetDenseIterator< T > end() const
Definition: id_set.hpp:312
virtual std::size_t used_memory() const noexcept=0
Definition: id_set.hpp:323
std::size_t size() const noexcept
Definition: id_set.hpp:391
bool operator!=(const IdSetDenseIterator< T > &rhs) const noexcept
Definition: id_set.hpp:154
virtual ~IdSet()=default
const_iterator cbegin() const noexcept
Definition: id_set.hpp:410
typename std::vector< T >::const_iterator const_iterator
Iterator type. There is no non-const iterator.
Definition: id_set.hpp:400
void next() noexcept
Definition: id_set.hpp:104
bool empty() const noexcept final
Definition: id_set.hpp:363
void clear() final
Definition: id_set.hpp:299
T size() const noexcept
Definition: id_set.hpp:292
virtual bool empty() const =0
static std::size_t chunk_id(T id) noexcept
Definition: id_set.hpp:188
void sort_unique()
Definition: id_set.hpp:378
unsigned char & get_element(T id)
Definition: id_set.hpp:204