Libosmium  2.17.3
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 (https://osmcode.org/libosmium).
7 
8 Copyright 2013-2022 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>
42 
43 #include <google/sparsetable>
44 
45 #include <cstddef>
46 #include <utility>
47 #include <vector>
48 
49 #define OSMIUM_HAS_INDEX_MAP_SPARSE_MEM_TABLE
50 
51 namespace osmium {
52 
53  namespace index {
54 
55  namespace map {
56 
71  template <typename TId, typename TValue>
72  class OSMIUM_DEPRECATED SparseMemTable : public osmium::index::map::Map<TId, TValue> {
73 
74  TId m_grow_size;
75 
76  google::sparsetable<TValue> m_elements;
77 
78 
79  public:
80 
89  explicit SparseMemTable(const TId grow_size = 10000) :
90  m_grow_size(grow_size),
91  m_elements(grow_size) {
92  }
93 
94  void set(const TId id, const TValue value) final {
95  if (id >= m_elements.size()) {
96  m_elements.resize(id + m_grow_size);
97  }
98  m_elements[id] = value;
99  }
100 
101  TValue get(const TId id) const final {
102  if (id >= m_elements.size()) {
103  throw osmium::not_found{id};
104  }
105  const TValue value = m_elements[id];
106  if (value == osmium::index::empty_value<TValue>()) {
107  throw osmium::not_found{id};
108  }
109  return value;
110  }
111 
112  TValue get_noexcept(const TId id) const noexcept final {
113  if (id >= m_elements.size()) {
114  return osmium::index::empty_value<TValue>();
115  }
116  return m_elements[id];
117  }
118 
119  size_t size() const final {
120  return m_elements.size();
121  }
122 
123  size_t used_memory() const final {
124  // unused elements use 1 bit, used elements sizeof(TValue) bytes
125  // https://github.com/sparsehash/sparsehash/blob/master/doc/sparsetable.html
126  return (m_elements.size() / 8) + (m_elements.num_nonempty() * sizeof(TValue));
127  }
128 
129  void clear() final {
130  m_elements.clear();
131  }
132 
133  void dump_as_list(const int fd) final {
134  std::vector<std::pair<TId, TValue>> v;
135  v.reserve(m_elements.size());
136  int n = 0;
137  for (const TValue value : m_elements) {
138  if (value != osmium::index::empty_value<TValue>()) {
139  v.emplace_back(n, value);
140  }
141  ++n;
142  }
143  osmium::io::detail::reliable_write(fd, reinterpret_cast<const char*>(v.data()), sizeof(std::pair<TId, TValue>) * v.size());
144  }
145 
146  }; // class SparseMemTable
147 
148  } // namespace map
149 
150  } // namespace index
151 
152 } // namespace osmium
153 
154 #ifdef OSMIUM_WANT_NODE_LOCATION_MAPS
155  REGISTER_MAP(osmium::unsigned_object_id_type, osmium::Location, osmium::index::map::SparseMemTable, sparse_mem_table)
156 #endif
157 
158 #endif // OSMIUM_WITH_SPARSEHASH
159 
160 #endif // OSMIUM_INDEX_MAP_SPARSE_MEM_TABLE_HPP
Definition: location.hpp:271
Definition: map.hpp:97
#define OSMIUM_DEPRECATED
Definition: compatibility.hpp:51
#define REGISTER_MAP(id, value, klass, name)
Definition: map.hpp:286
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
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
Definition: index.hpp:50