Libosmium  2.20.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 (https://osmcode.org/libosmium).
7 
8 Copyright 2013-2023 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 <osmium/index/index.hpp>
37 #include <osmium/index/map.hpp>
38 #include <osmium/io/detail/read_write.hpp>
39 
40 #include <algorithm> // IWYU pragma: keep (for std::copy)
41 #include <cstddef>
42 #include <iterator>
43 #include <map>
44 #include <vector>
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  enum {
66  element_size = sizeof(TId) + sizeof(TValue) + sizeof(void*) * 4U
67  };
68 
69  std::map<TId, TValue> m_elements;
70 
71  public:
72 
73  SparseMemMap() = default;
74 
75  void set(const TId id, const TValue value) final {
76  m_elements[id] = value;
77  }
78 
79  TValue get(const TId id) const final {
80  const auto it = m_elements.find(id);
81  if (it == m_elements.end()) {
82  throw osmium::not_found{id};
83  }
84  return it->second;
85  }
86 
87  TValue get_noexcept(const TId id) const noexcept final {
88  const auto it = m_elements.find(id);
89  if (it == m_elements.end()) {
90  return osmium::index::empty_value<TValue>();
91  }
92  return it->second;
93  }
94 
95  size_t size() const noexcept final {
96  return m_elements.size();
97  }
98 
99  size_t used_memory() const noexcept final {
100  return element_size * m_elements.size();
101  }
102 
103  void clear() final {
104  m_elements.clear();
105  }
106 
107  void dump_as_list(const int fd) final {
108  using t = typename std::map<TId, TValue>::value_type;
109  std::vector<t> v;
110  v.reserve(m_elements.size());
111  std::copy(m_elements.cbegin(), m_elements.cend(), std::back_inserter(v));
112  osmium::io::detail::reliable_write(fd, reinterpret_cast<const char*>(v.data()), sizeof(t) * v.size());
113  }
114 
115  }; // class SparseMemMap
116 
117  } // namespace map
118 
119  } // namespace index
120 
121 } // namespace osmium
122 
123 #ifdef OSMIUM_WANT_NODE_LOCATION_MAPS
125 #endif
126 
127 #endif // OSMIUM_INDEX_MAP_SPARSE_MEM_MAP_HPP
Definition: location.hpp:271
Definition: map.hpp:97
Definition: sparse_mem_map.hpp:59
@ element_size
Definition: sparse_mem_map.hpp:66
TValue get(const TId id) const final
Definition: sparse_mem_map.hpp:79
size_t used_memory() const noexcept final
Definition: sparse_mem_map.hpp:99
TValue get_noexcept(const TId id) const noexcept final
Definition: sparse_mem_map.hpp:87
void dump_as_list(const int fd) final
Definition: sparse_mem_map.hpp:107
void set(const TId id, const TValue value) final
Set the field with id to value.
Definition: sparse_mem_map.hpp:75
void clear() final
Definition: sparse_mem_map.hpp:103
size_t size() const noexcept final
Definition: sparse_mem_map.hpp:95
std::map< TId, TValue > m_elements
Definition: sparse_mem_map.hpp:69
#define REGISTER_MAP(id, value, klass, name)
Definition: map.hpp:287
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