Libosmium  2.11.4
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-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 #ifdef OSMIUM_WITH_SPARSEHASH
37 
38 #include <cstddef>
39 #include <utility>
40 #include <vector>
41 
42 #include <google/sparsetable>
43 
44 #include <osmium/index/index.hpp>
45 #include <osmium/index/map.hpp>
46 #include <osmium/io/detail/read_write.hpp>
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  ~SparseMemTable() noexcept final = default;
91 
92  void set(const TId id, const TValue value) final {
93  if (id >= m_elements.size()) {
94  m_elements.resize(id + m_grow_size);
95  }
96  m_elements[id] = value;
97  }
98 
99  TValue get(const TId id) const final {
100  if (id >= m_elements.size()) {
101  throw osmium::not_found{id};
102  }
103  const TValue value = m_elements[id];
104  if (value == osmium::index::empty_value<TValue>()) {
105  throw osmium::not_found{id};
106  }
107  return value;
108  }
109 
110  TValue get_noexcept(const TId id) const noexcept final {
111  if (id >= m_elements.size()) {
112  return osmium::index::empty_value<TValue>();
113  }
114  return m_elements[id];
115  }
116 
117  size_t size() const final {
118  return m_elements.size();
119  }
120 
121  size_t used_memory() const final {
122  // unused elements use 1 bit, used elements sizeof(TValue) bytes
123  // http://google-sparsehash.googlecode.com/svn/trunk/doc/sparsetable.html
124  return (m_elements.size() / 8) + (m_elements.num_nonempty() * sizeof(TValue));
125  }
126 
127  void clear() final {
128  m_elements.clear();
129  }
130 
131  void dump_as_list(const int fd) final {
132  std::vector<std::pair<TId, TValue>> v;
133  v.reserve(m_elements.size());
134  int n = 0;
135  for (const TValue value : m_elements) {
136  if (value != osmium::index::empty_value<TValue>()) {
137  v.emplace_back(n, value);
138  }
139  ++n;
140  }
141  osmium::io::detail::reliable_write(fd, reinterpret_cast<const char*>(v.data()), sizeof(std::pair<TId, TValue>) * v.size());
142  }
143 
144  }; // class SparseMemTable
145 
146  } // namespace map
147 
148  } // namespace index
149 
150 } // namespace osmium
151 
152 #endif // OSMIUM_WITH_SPARSEHASH
153 
154 #endif // OSMIUM_INDEX_BYID_SPARSE_MEM_TABLE_HPP
Namespace for everything in the Osmium library.
Definition: assembler.hpp:73
Definition: index.hpp:49
Definition: map.hpp:97