Libosmium  2.3.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-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 #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() override final = default;
91 
92  void set(const TId id, const TValue value) override final {
93  if (id >= m_elements.size()) {
94  m_elements.resize(id + m_grow_size);
95  }
96  m_elements[id] = value;
97  }
98 
99  const TValue get(const TId id) const override final {
100  if (id >= m_elements.size()) {
101  not_found_error(id);
102  }
103  if (m_elements[id] == osmium::index::empty_value<TValue>()) {
104  not_found_error(id);
105  }
106  return m_elements[id];
107  }
108 
109  size_t size() const override final {
110  return m_elements.size();
111  }
112 
113  size_t used_memory() const override final {
114  // unused elements use 1 bit, used elements sizeof(TValue) bytes
115  // http://google-sparsehash.googlecode.com/svn/trunk/doc/sparsetable.html
116  return (m_elements.size() / 8) + (m_elements.num_nonempty() * sizeof(TValue));
117  }
118 
119  void clear() override final {
120  m_elements.clear();
121  }
122 
123  void dump_as_list(const int fd) override final {
124  std::vector<std::pair<TId, TValue>> v;
125  int n = 0;
126  for (const TValue value : m_elements) {
127  if (value != osmium::index::empty_value<TValue>()) {
128  v.emplace_back(n, value);
129  }
130  ++n;
131  }
132  osmium::io::detail::reliable_write(fd, reinterpret_cast<const char*>(v.data()), sizeof(std::pair<TId, TValue>) * v.size());
133  }
134 
135  }; // class SparseMemTable
136 
137  } // namespace map
138 
139  } // namespace index
140 
141 } // namespace osmium
142 
143 #endif // OSMIUM_WITH_SPARSEHASH
144 
145 #endif // OSMIUM_INDEX_BYID_SPARSE_MEM_TABLE_HPP
Namespace for everything in the Osmium library.
Definition: assembler.hpp:55
OSMIUM_NORETURN void not_found_error(TKey key)
Definition: index.hpp:68
Definition: map.hpp:85