Libosmium  2.3.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-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 #include <algorithm> // IWYU pragma: keep (for std::copy)
37 #include <cstddef>
38 #include <iterator>
39 #include <map>
40 #include <stdexcept>
41 #include <vector>
42 
43 #include <osmium/index/map.hpp>
44 #include <osmium/index/index.hpp>
45 #include <osmium/io/detail/read_write.hpp>
46 
47 #define OSMIUM_HAS_INDEX_MAP_SPARSE_MEM_MAP
48 
49 namespace osmium {
50 
51  namespace index {
52 
53  namespace map {
54 
59  template <typename TId, typename TValue>
60  class SparseMemMap : public osmium::index::map::Map<TId, TValue> {
61 
62  // This is a rough estimate for the memory needed for each
63  // element in the map (id + value + pointers to left, right,
64  // and parent plus some overhead for color of red-black-tree
65  // or similar).
66  static constexpr size_t element_size = sizeof(TId) + sizeof(TValue) + sizeof(void*) * 4;
67 
68  std::map<TId, TValue> m_elements;
69 
70  public:
71 
72  SparseMemMap() = default;
73 
74  ~SparseMemMap() override final = default;
75 
76  void set(const TId id, const TValue value) override final {
77  m_elements[id] = value;
78  }
79 
80  const TValue get(const TId id) const override final {
81  try {
82  return m_elements.at(id);
83  } catch (std::out_of_range&) {
84  not_found_error(id);
85  }
86  }
87 
88  size_t size() const override final {
89  return m_elements.size();
90  }
91 
92  size_t used_memory() const override final {
93  return element_size * m_elements.size();
94  }
95 
96  void clear() override final {
97  m_elements.clear();
98  }
99 
100  void dump_as_list(const int fd) override final {
101  typedef typename std::map<TId, TValue>::value_type t;
102  std::vector<t> v;
103  std::copy(m_elements.begin(), m_elements.end(), 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:60
static constexpr size_t element_size
Definition: sparse_mem_map.hpp:66
void dump_as_list(const int fd) overridefinal
Definition: sparse_mem_map.hpp:100
Namespace for everything in the Osmium library.
Definition: assembler.hpp:55
OSMIUM_NORETURN void not_found_error(TKey key)
Definition: index.hpp:68
~SparseMemMap() overridefinal=default
size_t used_memory() const overridefinal
Definition: sparse_mem_map.hpp:92
void clear() overridefinal
Definition: sparse_mem_map.hpp:96
size_t size() const overridefinal
Definition: sparse_mem_map.hpp:88
void set(const TId id, const TValue value) overridefinal
Set the field with id to value.
Definition: sparse_mem_map.hpp:76
Definition: map.hpp:85
std::map< TId, TValue > m_elements
Definition: sparse_mem_map.hpp:68