1717
1818#include < array>
1919#include < cstddef>
20+ #include < limits>
2021#include < string>
2122#include < utility>
2223
3536class node_locations_t
3637{
3738public:
39+ /* *
40+ * Construct a node locations store. Takes a single optional argument
41+ * which gives the maximum number of bytes this store should be allowed
42+ * to use. If this is not specified, the size is only limited by available
43+ * memory. The store will try to keep the memory used under what's
44+ * specified here.
45+ */
46+ explicit node_locations_t (
47+ std::size_t max_size = std::numeric_limits<std::size_t >::max())
48+ : m_max_size(max_size)
49+ {}
50+
3851 /* *
3952 * Store a node location.
4053 *
4154 * \pre id must be strictly larger than all ids stored before.
55+ * \return True if the entry was added, false if the index is full.
4256 */
43- void set (osmid_t id, osmium::Location location);
57+ bool set (osmid_t id, osmium::Location location);
4458
4559 /* *
4660 * Retrieve a node location. If the location wasn't stored before, an
@@ -69,6 +83,17 @@ class node_locations_t
6983 return m_count % block_size == 0 ;
7084 }
7185
86+ // / The maximum number of bytes an entry will need in storage.
87+ constexpr static std::size_t max_bytes_per_entry () noexcept {
88+ return 10U /* max varint length*/ * 3U /* id, x, y*/ ;
89+ }
90+
91+ bool will_resize () const noexcept
92+ {
93+ return m_index.will_resize () ||
94+ (m_data.size () + max_bytes_per_entry () >= m_data.capacity ());
95+ }
96+
7297 /* *
7398 * The block size used for internal blocks. The larger the block size
7499 * the less memory is consumed but the more expensive the access is.
@@ -78,6 +103,9 @@ class node_locations_t
78103 ordered_index_t m_index;
79104 std::string m_data;
80105
106+ // / Maximum size in bytes this object may allocate.
107+ std::size_t m_max_size;
108+
81109 // / The number of (id, location) pairs stored.
82110 std::size_t m_count = 0 ;
83111
0 commit comments