Libosmium  2.14.0
Fast and flexible C++ library for working with OpenStreetMap data
sparse_mem_table.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_INDEX_MAP_SPARSE_MEM_TABLE_HPP
2 #define OSMIUM_INDEX_MAP_SPARSE_MEM_TABLE_HPP
3 
4 /*
5 
6 This file is part of Osmium (http://osmcode.org/libosmium).
7 
8 Copyright 2013-2018 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 #ifdef OSMIUM_WITH_SPARSEHASH
37 
38 #include <osmium/index/index.hpp>
39 #include <osmium/index/map.hpp>
40 #include <osmium/io/detail/read_write.hpp>
41 
42 #include <google/sparsetable>
43 
44 #include <cstddef>
45 #include <utility>
46 #include <vector>
47 
48 #define OSMIUM_HAS_INDEX_MAP_SPARSE_MEM_TABLE
49 
50 namespace osmium {
51 
52  namespace index {
53 
54  namespace map {
55 
67  template <typename TId, typename TValue>
68  class SparseMemTable : public osmium::index::map::Map<TId, TValue> {
69 
70  TId m_grow_size;
71 
72  google::sparsetable<TValue> m_elements;
73 
74 
75  public:
76 
85  explicit SparseMemTable(const TId grow_size = 10000) :
86  m_grow_size(grow_size),
87  m_elements(grow_size) {
88  }
89 
90  void set(const TId id, const TValue value) final {
91  if (id >= m_elements.size()) {
92  m_elements.resize(id + m_grow_size);
93  }
94  m_elements[id] = value;
95  }
96 
97  TValue get(const TId id) const final {
98  if (id >= m_elements.size()) {
99  throw osmium::not_found{id};
100  }
101  const TValue value = m_elements[id];
102  if (value == osmium::index::empty_value<TValue>()) {
103  throw osmium::not_found{id};
104  }
105  return value;
106  }
107 
108  TValue get_noexcept(const TId id) const noexcept final {
109  if (id >= m_elements.size()) {
110  return osmium::index::empty_value<TValue>();
111  }
112  return m_elements[id];
113  }
114 
115  size_t size() const final {
116  return m_elements.size();
117  }
118 
119  size_t used_memory() const final {
120  // unused elements use 1 bit, used elements sizeof(TValue) bytes
121  // https://github.com/sparsehash/sparsehash/blob/master/doc/sparsetable.html
122  return (m_elements.size() / 8) + (m_elements.num_nonempty() * sizeof(TValue));
123  }
124 
125  void clear() final {
126  m_elements.clear();
127  }
128 
129  void dump_as_list(const int fd) final {
130  std::vector<std::pair<TId, TValue>> v;
131  v.reserve(m_elements.size());
132  int n = 0;
133  for (const TValue value : m_elements) {
134  if (value != osmium::index::empty_value<TValue>()) {
135  v.emplace_back(n, value);
136  }
137  ++n;
138  }
139  osmium::io::detail::reliable_write(fd, reinterpret_cast<const char*>(v.data()), sizeof(std::pair<TId, TValue>) * v.size());
140  }
141 
142  }; // class SparseMemTable
143 
144  } // namespace map
145 
146  } // namespace index
147 
148 } // namespace osmium
149 
150 #ifdef OSMIUM_WANT_NODE_LOCATION_MAPS
151  REGISTER_MAP(osmium::unsigned_object_id_type, osmium::Location, osmium::index::map::SparseMemTable, sparse_mem_table)
152 #endif
153 
154 #endif // OSMIUM_WITH_SPARSEHASH
155 
156 #endif // OSMIUM_INDEX_MAP_SPARSE_MEM_TABLE_HPP
#define REGISTER_MAP(id, value, klass, name)
Definition: map.hpp:285
uint64_t unsigned_object_id_type
Type for OSM object (node, way, or relation) IDs where we only allow positive IDs.
Definition: types.hpp:46
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
Definition: location.hpp:273
Definition: index.hpp:48
Definition: map.hpp:96