Libosmium  2.9.0
Fast and flexible C++ library for working with OpenStreetMap data
sparse_mem_map.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_INDEX_MAP_SPARSE_MEM_MAP_HPP
2 #define OSMIUM_INDEX_MAP_SPARSE_MEM_MAP_HPP
3 
4 /*
5 
6 This file is part of Osmium (http://osmcode.org/libosmium).
7 
8 Copyright 2013-2016 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> // IWYU pragma: keep (for std::copy)
37 #include <cstddef>
38 #include <iterator>
39 #include <map>
40 #include <vector>
41 
42 #include <osmium/index/map.hpp>
43 #include <osmium/index/index.hpp>
44 #include <osmium/io/detail/read_write.hpp>
45 
46 #define OSMIUM_HAS_INDEX_MAP_SPARSE_MEM_MAP
47 
48 namespace osmium {
49 
50  namespace index {
51 
52  namespace map {
53 
58  template <typename TId, typename TValue>
59  class SparseMemMap : public osmium::index::map::Map<TId, TValue> {
60 
61  // This is a rough estimate for the memory needed for each
62  // element in the map (id + value + pointers to left, right,
63  // and parent plus some overhead for color of red-black-tree
64  // or similar).
65  static constexpr size_t element_size = sizeof(TId) + sizeof(TValue) + sizeof(void*) * 4;
66 
67  std::map<TId, TValue> m_elements;
68 
69  public:
70 
71  SparseMemMap() = default;
72 
73  ~SparseMemMap() noexcept final = default;
74 
75  void set(const TId id, const TValue value) final {
76  m_elements[id] = value;
77  }
78 
79  const TValue get(const TId id) const final {
80  auto it = m_elements.find(id);
81  if (it == m_elements.end()) {
82  not_found_error(id);
83  }
84  return it->second;
85  }
86 
87  size_t size() const noexcept final {
88  return m_elements.size();
89  }
90 
91  size_t used_memory() const noexcept final {
92  return element_size * m_elements.size();
93  }
94 
95  void clear() final {
96  m_elements.clear();
97  }
98 
99  void dump_as_list(const int fd) final {
100  using t = typename std::map<TId, TValue>::value_type;
101  std::vector<t> v;
102  v.reserve(m_elements.size());
103  std::copy(m_elements.cbegin(), m_elements.cend(), std::back_inserter(v));
104  osmium::io::detail::reliable_write(fd, reinterpret_cast<const char*>(v.data()), sizeof(t) * v.size());
105  }
106 
107  }; // class SparseMemMap
108 
109  } // namespace map
110 
111  } // namespace index
112 
113 } // namespace osmium
114 
115 #endif // OSMIUM_INDEX_MAP_SPARSE_MEM_MAP_HPP
Definition: sparse_mem_map.hpp:59
static constexpr size_t element_size
Definition: sparse_mem_map.hpp:65
~SparseMemMap() noexcept final=default
size_t size() const noexcept final
Definition: sparse_mem_map.hpp:87
void set(const TId id, const TValue value) final
Set the field with id to value.
Definition: sparse_mem_map.hpp:75
Namespace for everything in the Osmium library.
Definition: assembler.hpp:73
OSMIUM_NORETURN void not_found_error(TKey key)
Definition: index.hpp:68
void clear() final
Definition: sparse_mem_map.hpp:95
size_t used_memory() const noexcept final
Definition: sparse_mem_map.hpp:91
void dump_as_list(const int fd) final
Definition: sparse_mem_map.hpp:99
Definition: map.hpp:85
std::map< TId, TValue > m_elements
Definition: sparse_mem_map.hpp:67