First Commit

This commit is contained in:
Hamza committed 2026-02-26 13:00:32 +01:00
commit 8a6c7106e3
1312 files changed
+425396

No files matched your search

@@ -0,0 +1,70 @@
#ifndef OSMIUM_INDEX_DETAIL_CREATE_MAP_WITH_FD_HPP
#define OSMIUM_INDEX_DETAIL_CREATE_MAP_WITH_FD_HPP
/*
This file is part of Osmium (https://osmcode.org/libosmium).
Copyright 2013-2023 Jochen Topf <jochen@topf.org> and others (see README).
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <cassert>
#include <cerrno>
#include <fcntl.h>
#include <stdexcept>
#include <string>
#include <system_error>
#include <vector>
namespace osmium {
namespace index {
namespace detail {
template <typename T>
inline T* create_map_with_fd(const std::vector<std::string>& config) {
if (config.size() == 1) {
return new T{};
}
assert(config.size() > 1);
const std::string& filename = config[1];
const int fd = ::open(filename.c_str(), O_CREAT | O_RDWR, 0644); // NOLINT(hicpp-signed-bitwise)
if (fd == -1) {
throw std::system_error{errno, std::system_category(), "can't open file '" + filename + "'"};
}
return new T{fd};
}
} // namespace detail
} // namespace index
} // namespace osmium
#endif // OSMIUM_INDEX_DETAIL_CREATE_MAP_WITH_FD_HPP
@@ -0,0 +1,65 @@
#ifndef OSMIUM_INDEX_DETAIL_MMAP_VECTOR_ANON_HPP
#define OSMIUM_INDEX_DETAIL_MMAP_VECTOR_ANON_HPP
/*
This file is part of Osmium (https://osmcode.org/libosmium).
Copyright 2013-2023 Jochen Topf <jochen@topf.org> and others (see README).
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#ifdef __linux__
#include <osmium/index/detail/mmap_vector_base.hpp>
namespace osmium {
namespace detail {
/**
* This class looks and behaves like STL vector, but uses mmap
* internally.
*/
template <typename T>
class mmap_vector_anon : public mmap_vector_base<T> {
public:
mmap_vector_anon() :
mmap_vector_base<T>() {
}
}; // class mmap_vector_anon
} // namespace detail
} // namespace osmium
#endif // __linux__
#endif // OSMIUM_INDEX_DETAIL_MMAP_VECTOR_ANON_HPP
@@ -0,0 +1,190 @@
#ifndef OSMIUM_INDEX_DETAIL_MMAP_VECTOR_BASE_HPP
#define OSMIUM_INDEX_DETAIL_MMAP_VECTOR_BASE_HPP
/*
This file is part of Osmium (https://osmcode.org/libosmium).
Copyright 2013-2023 Jochen Topf <jochen@topf.org> and others (see README).
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <osmium/index/index.hpp>
#include <osmium/util/memory_mapping.hpp>
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <new> // IWYU pragma: keep
#include <stdexcept>
namespace osmium {
namespace detail {
enum {
mmap_vector_size_increment = 1024UL * 1024UL
};
/**
* This is a base class for implementing classes that look like
* STL vector but use mmap internally. Do not use this class itself,
* use the derived classes mmap_vector_anon or mmap_vector_file.
*/
template <typename T>
class mmap_vector_base {
protected:
std::size_t m_size = 0;
osmium::TypedMemoryMapping<T> m_mapping;
public:
mmap_vector_base(const int fd, const std::size_t capacity, const std::size_t size = 0) :
m_size(size),
m_mapping(capacity, osmium::MemoryMapping::mapping_mode::write_shared, fd) {
assert(size <= capacity);
std::fill(data() + size, data() + capacity, osmium::index::empty_value<T>());
shrink_to_fit();
}
explicit mmap_vector_base(const std::size_t capacity = mmap_vector_size_increment) :
m_mapping(capacity) {
std::fill_n(data(), capacity, osmium::index::empty_value<T>());
}
using value_type = T;
using pointer = value_type*;
using const_pointer = const value_type*;
using reference = value_type&;
using const_reference = const value_type&;
using iterator = value_type*;
using const_iterator = const value_type*;
void close() {
m_mapping.unmap();
}
std::size_t capacity() const noexcept {
return m_mapping.size();
}
std::size_t size() const noexcept {
return m_size;
}
bool empty() const noexcept {
return m_size == 0;
}
const_pointer data() const {
return m_mapping.begin();
}
pointer data() {
return m_mapping.begin();
}
const_reference operator[](const std::size_t n) const {
assert(n < m_size);
return data()[n];
}
reference operator[](const std::size_t n) {
assert(n < m_size);
return data()[n];
}
value_type at(const std::size_t n) const {
if (n >= m_size) {
throw std::out_of_range{"out of range"};
}
return data()[n];
}
void clear() noexcept {
m_size = 0;
}
void shrink_to_fit() {
while (m_size > 0 && data()[m_size - 1] == osmium::index::empty_value<value_type>()) {
--m_size;
}
}
void push_back(const_reference value) {
resize(m_size + 1);
data()[m_size - 1] = value;
}
void reserve(const std::size_t new_capacity) {
if (new_capacity > capacity()) {
const std::size_t old_capacity = capacity();
m_mapping.resize(new_capacity);
std::fill(data() + old_capacity, data() + new_capacity, osmium::index::empty_value<value_type>());
}
}
void resize(const std::size_t new_size) {
if (new_size > capacity()) {
reserve(new_size + mmap_vector_size_increment);
}
m_size = new_size;
}
iterator begin() noexcept {
return data();
}
iterator end() noexcept {
return data() + m_size;
}
const_iterator begin() const noexcept {
return data();
}
const_iterator end() const noexcept {
return data() + m_size;
}
const_iterator cbegin() const noexcept {
return data();
}
const_iterator cend() const noexcept {
return data() + m_size;
}
}; // class mmap_vector_base
} // namespace detail
} // namespace osmium
#endif // OSMIUM_INDEX_DETAIL_MMAP_VECTOR_BASE_HPP
@@ -0,0 +1,87 @@
#ifndef OSMIUM_INDEX_DETAIL_MMAP_VECTOR_FILE_HPP
#define OSMIUM_INDEX_DETAIL_MMAP_VECTOR_FILE_HPP
/*
This file is part of Osmium (https://osmcode.org/libosmium).
Copyright 2013-2023 Jochen Topf <jochen@topf.org> and others (see README).
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <osmium/index/detail/mmap_vector_base.hpp>
#include <osmium/index/detail/tmpfile.hpp>
#include <osmium/util/file.hpp>
#include <algorithm>
#include <cstddef>
#include <stdexcept>
#include <string>
namespace osmium {
namespace detail {
/**
* This class looks and behaves like STL vector, but mmap's a file
* internally.
*/
template <typename T>
class mmap_vector_file : public mmap_vector_base<T> {
static std::size_t filesize(const int fd) {
const auto size = osmium::file_size(fd);
if (size % sizeof(T) != 0) {
throw std::runtime_error{"Index file has wrong size (must be multiple of " + std::to_string(sizeof(T)) + ")."};
}
return size / sizeof(T);
}
public:
mmap_vector_file() :
mmap_vector_base<T>(
osmium::detail::create_tmp_file(),
osmium::detail::mmap_vector_size_increment) {
}
explicit mmap_vector_file(const int fd) :
mmap_vector_base<T>(
fd,
std::max(static_cast<std::size_t>(mmap_vector_size_increment), filesize(fd)),
filesize(fd)) {
}
}; // class mmap_vector_file
} // namespace detail
} // namespace osmium
#endif // OSMIUM_INDEX_DETAIL_MMAP_VECTOR_FILE_HPP
@@ -0,0 +1,62 @@
#ifndef OSMIUM_INDEX_DETAIL_TMPFILE_HPP
#define OSMIUM_INDEX_DETAIL_TMPFILE_HPP
/*
This file is part of Osmium (https://osmcode.org/libosmium).
Copyright 2013-2023 Jochen Topf <jochen@topf.org> and others (see README).
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <cerrno>
#include <cstdio>
#include <system_error>
namespace osmium {
namespace detail {
/**
* Create and open a temporary file. It is removed after opening.
*
* @returns File descriptor of temporary file.
* @throws std::system_error if something went wrong.
*/
inline int create_tmp_file() {
FILE* file = ::tmpfile();
if (!file) {
throw std::system_error{errno, std::system_category(), "tempfile failed"};
}
return fileno(file);
}
} // namespace detail
} // namespace osmium
#endif // OSMIUM_INDEX_DETAIL_TMPFILE_HPP
@@ -0,0 +1,297 @@
#ifndef OSMIUM_INDEX_DETAIL_VECTOR_MAP_HPP
#define OSMIUM_INDEX_DETAIL_VECTOR_MAP_HPP
/*
This file is part of Osmium (https://osmcode.org/libosmium).
Copyright 2013-2023 Jochen Topf <jochen@topf.org> and others (see README).
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <osmium/index/index.hpp>
#include <osmium/index/map.hpp>
#include <osmium/io/detail/read_write.hpp>
#include <algorithm>
#include <cstddef>
#include <memory>
#include <utility>
namespace osmium {
namespace index {
namespace map {
template <typename TVector, typename TId, typename TValue>
class VectorBasedDenseMap : public Map<TId, TValue> {
TVector m_vector;
public:
using element_type = TValue;
using vector_type = TVector;
using iterator = typename vector_type::iterator;
using const_iterator = typename vector_type::const_iterator;
VectorBasedDenseMap() :
m_vector() {
}
VectorBasedDenseMap(const VectorBasedDenseMap&) = default;
VectorBasedDenseMap& operator=(const VectorBasedDenseMap&) = default;
VectorBasedDenseMap(VectorBasedDenseMap&&) noexcept = default;
VectorBasedDenseMap& operator=(VectorBasedDenseMap&&) noexcept = default;
~VectorBasedDenseMap() noexcept override = default;
explicit VectorBasedDenseMap(int fd) :
m_vector(fd) {
}
void reserve(const std::size_t size) final {
m_vector.reserve(size);
}
void set(const TId id, const TValue value) final {
if (size() <= id) {
m_vector.resize(id+1);
}
m_vector[id] = value;
}
TValue get(const TId id) const final {
if (id >= m_vector.size()) {
throw osmium::not_found{id};
}
const TValue value = m_vector[id];
if (value == osmium::index::empty_value<TValue>()) {
throw osmium::not_found{id};
}
return value;
}
TValue get_noexcept(const TId id) const noexcept final {
if (id >= m_vector.size()) {
return osmium::index::empty_value<TValue>();
}
return m_vector[id];
}
std::size_t size() const final {
return m_vector.size();
}
std::size_t byte_size() const {
return m_vector.size() * sizeof(element_type);
}
std::size_t used_memory() const final {
return sizeof(TValue) * size();
}
void clear() final {
m_vector.clear();
m_vector.shrink_to_fit();
}
void dump_as_array(const int fd) final {
osmium::io::detail::reliable_write(fd, reinterpret_cast<const char*>(m_vector.data()), byte_size());
}
iterator begin() {
return m_vector.begin();
}
iterator end() {
return m_vector.end();
}
const_iterator cbegin() const {
return m_vector.cbegin();
}
const_iterator cend() const {
return m_vector.cend();
}
const_iterator begin() const {
return m_vector.cbegin();
}
const_iterator end() const {
return m_vector.cend();
}
}; // class VectorBasedDenseMap
template <typename TId, typename TValue, template <typename...> class TVector>
class VectorBasedSparseMap : public Map<TId, TValue> {
public:
using element_type = typename std::pair<TId, TValue>;
using vector_type = TVector<element_type>;
using iterator = typename vector_type::iterator;
using const_iterator = typename vector_type::const_iterator;
private:
vector_type m_vector;
typename vector_type::const_iterator find_id(const TId id) const noexcept {
const element_type element{
id,
osmium::index::empty_value<TValue>()};
return std::lower_bound(m_vector.begin(), m_vector.end(), element, [](const element_type& a, const element_type& b) {
return a.first < b.first;
});
}
public:
VectorBasedSparseMap() :
m_vector() {
}
explicit VectorBasedSparseMap(int fd) :
m_vector(fd) {
}
VectorBasedSparseMap(const VectorBasedSparseMap&) = default;
VectorBasedSparseMap& operator=(const VectorBasedSparseMap&) = default;
VectorBasedSparseMap(VectorBasedSparseMap&&) noexcept = default;
VectorBasedSparseMap& operator=(VectorBasedSparseMap&&) noexcept = default;
~VectorBasedSparseMap() noexcept override = default;
void set(const TId id, const TValue value) final {
m_vector.push_back(element_type(id, value));
}
TValue get(const TId id) const final {
const auto result = find_id(id);
if (result == m_vector.end() || result->first != id) {
throw osmium::not_found{id};
}
return result->second;
}
TValue get_noexcept(const TId id) const noexcept final {
const auto result = find_id(id);
if (result == m_vector.end() || result->first != id) {
return osmium::index::empty_value<TValue>();
}
return result->second;
}
std::size_t size() const final {
return m_vector.size();
}
std::size_t byte_size() const {
return m_vector.size() * sizeof(element_type);
}
std::size_t used_memory() const final {
return sizeof(element_type) * size();
}
void clear() final {
m_vector.clear();
m_vector.shrink_to_fit();
}
void sort() final {
std::sort(m_vector.begin(), m_vector.end());
}
void dump_as_array(const int fd) final {
constexpr const size_t value_size = sizeof(TValue);
constexpr const size_t buffer_size = (10L * 1024L * 1024L) / value_size;
const std::unique_ptr<TValue[]> output_buffer{new TValue[buffer_size]};
size_t buffer_start_id = 0;
for (auto it = cbegin(); it != cend();) {
std::fill_n(output_buffer.get(), buffer_size, osmium::index::empty_value<TValue>());
size_t offset = 0;
for (; offset < buffer_size && it != end(); ++offset) {
if (buffer_start_id + offset == it->first) {
output_buffer[offset] = it->second;
++it;
}
}
osmium::io::detail::reliable_write(fd, reinterpret_cast<const unsigned char*>(output_buffer.get()), offset * value_size);
buffer_start_id += buffer_size;
}
}
void dump_as_list(const int fd) final {
osmium::io::detail::reliable_write(fd, reinterpret_cast<const char*>(m_vector.data()), byte_size());
}
iterator begin() {
return m_vector.begin();
}
iterator end() {
return m_vector.end();
}
const_iterator cbegin() const {
return m_vector.cbegin();
}
const_iterator cend() const {
return m_vector.cend();
}
const_iterator begin() const {
return m_vector.cbegin();
}
const_iterator end() const {
return m_vector.cend();
}
}; // class VectorBasedSparseMap
} // namespace map
} // namespace index
} // namespace osmium
#endif // OSMIUM_INDEX_DETAIL_VECTOR_MAP_HPP
@@ -0,0 +1,189 @@
#ifndef OSMIUM_INDEX_DETAIL_VECTOR_MULTIMAP_HPP
#define OSMIUM_INDEX_DETAIL_VECTOR_MULTIMAP_HPP
/*
This file is part of Osmium (https://osmcode.org/libosmium).
Copyright 2013-2023 Jochen Topf <jochen@topf.org> and others (see README).
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <osmium/index/index.hpp>
#include <osmium/index/multimap.hpp>
#include <osmium/io/detail/read_write.hpp>
#include <algorithm>
#include <cstddef>
#include <utility>
namespace osmium {
namespace index {
namespace multimap {
template <typename TId, typename TValue, template <typename...> class TVector>
class VectorBasedSparseMultimap : public Multimap<TId, TValue> {
public:
using element_type = typename std::pair<TId, TValue>;
using vector_type = TVector<element_type>;
using iterator = typename vector_type::iterator;
using const_iterator = typename vector_type::const_iterator;
private:
vector_type m_vector;
static bool is_removed(const element_type& element) {
return element.second == osmium::index::empty_value<TValue>();
}
public:
VectorBasedSparseMultimap() :
m_vector() {
}
explicit VectorBasedSparseMultimap(int fd) :
m_vector(fd) {
}
VectorBasedSparseMultimap(const VectorBasedSparseMultimap&) = default;
VectorBasedSparseMultimap& operator=(const VectorBasedSparseMultimap&) = default;
VectorBasedSparseMultimap(VectorBasedSparseMultimap&&) noexcept = default;
VectorBasedSparseMultimap& operator=(VectorBasedSparseMultimap&&) noexcept = default;
~VectorBasedSparseMultimap() noexcept override = default;
void set(const TId id, const TValue value) final {
m_vector.push_back(element_type(id, value));
}
void unsorted_set(const TId id, const TValue value) {
m_vector.push_back(element_type(id, value));
}
std::pair<iterator, iterator> get_all(const TId id) {
const element_type element{
id,
osmium::index::empty_value<TValue>()};
return std::equal_range(m_vector.begin(), m_vector.end(), element, [](const element_type& a, const element_type& b) {
return a.first < b.first;
});
}
std::pair<const_iterator, const_iterator> get_all(const TId id) const {
const element_type element{
id,
osmium::index::empty_value<TValue>()};
return std::equal_range(m_vector.cbegin(), m_vector.cend(), element, [](const element_type& a, const element_type& b) {
return a.first < b.first;
});
}
size_t size() const final {
return m_vector.size();
}
size_t byte_size() const {
return m_vector.size() * sizeof(element_type);
}
size_t used_memory() const final {
return sizeof(element_type) * size();
}
void clear() final {
m_vector.clear();
m_vector.shrink_to_fit();
}
void sort() final {
std::sort(m_vector.begin(), m_vector.end());
}
void remove(const TId id, const TValue value) {
const auto r = get_all(id);
for (auto it = r.first; it != r.second; ++it) {
if (it->second == value) {
it->second = 0;
return;
}
}
}
void consolidate() {
std::sort(m_vector.begin(), m_vector.end());
}
void erase_removed() {
m_vector.erase(
std::remove_if(m_vector.begin(), m_vector.end(), is_removed),
m_vector.end());
}
void dump_as_list(const int fd) final {
osmium::io::detail::reliable_write(fd, reinterpret_cast<const char*>(m_vector.data()), byte_size());
}
iterator begin() {
return m_vector.begin();
}
iterator end() {
return m_vector.end();
}
const_iterator cbegin() const {
return m_vector.cbegin();
}
const_iterator cend() const {
return m_vector.cend();
}
const_iterator begin() const {
return m_vector.cbegin();
}
const_iterator end() const {
return m_vector.cend();
}
}; // class VectorBasedSparseMultimap
} // namespace multimap
} // namespace index
} // namespace osmium
#endif // OSMIUM_INDEX_DETAIL_VECTOR_MULTIMAP_HPP
+510
View File
@@ -0,0 +1,510 @@
#ifndef OSMIUM_INDEX_ID_SET_HPP
#define OSMIUM_INDEX_ID_SET_HPP
/*
This file is part of Osmium (https://osmcode.org/libosmium).
Copyright 2013-2023 Jochen Topf <jochen@topf.org> and others (see README).
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <osmium/osm/item_type.hpp>
#include <osmium/osm/types.hpp>
#include <algorithm>
#include <array>
#include <cassert>
#include <cstddef>
#include <cstring>
#include <iterator>
#include <memory>
#include <type_traits>
#include <vector>
namespace osmium {
namespace index {
/**
* Virtual parent class for IdSets. Use one of the implementations
* provided.
*/
template <typename T>
class IdSet {
public:
IdSet() = default;
IdSet(const IdSet&) = default;
IdSet& operator=(const IdSet&) = default;
IdSet(IdSet&&) noexcept = default;
IdSet& operator=(IdSet&&) noexcept = default;
virtual ~IdSet() noexcept = default;
/**
* Add the given Id to the set.
*/
virtual void set(T id) = 0;
/**
* Is the Id in the set?
*/
virtual bool get(T id) const noexcept = 0;
/**
* Is the set empty?
*/
virtual bool empty() const = 0;
/**
* Clear the set.
*/
virtual void clear() = 0;
/**
* Get an estimate of the amount of memory used for the set.
*/
virtual std::size_t used_memory() const noexcept = 0;
}; // class IdSet
namespace detail {
// This value is a compromise. For node Ids it could be bigger
// which would mean less (but larger) memory allocations. For
// relations Ids it could be smaller, because they would all fit
// into a smaller allocation.
enum : std::size_t {
default_chunk_bits = 22U
};
} // namespace detail
template <typename T, std::size_t chunk_bits = detail::default_chunk_bits>
class IdSetDense;
/**
* Const_iterator for iterating over a IdSetDense.
*/
template <typename T, std::size_t chunk_bits>
class IdSetDenseIterator {
static_assert(std::is_unsigned<T>::value, "Needs unsigned type");
static_assert(sizeof(T) >= 4, "Needs at least 32bit type");
using id_set = IdSetDense<T, chunk_bits>;
const id_set* m_set;
T m_value;
T m_last;
void next() noexcept {
while (m_value != m_last && !m_set->get(m_value)) {
const T cid = id_set::chunk_id(m_value);
assert(cid < m_set->m_data.size());
if (!m_set->m_data[cid]) {
m_value = (cid + 1) << (chunk_bits + 3);
} else {
const auto slot = m_set->m_data[cid][id_set::offset(m_value)];
if (slot == 0) {
m_value += 8;
m_value &= ~0x7ULL;
} else {
++m_value;
}
}
}
}
public:
using iterator_category = std::forward_iterator_tag;
using value_type = T;
using pointer = value_type*;
using reference = value_type&;
IdSetDenseIterator(const id_set* set, T value, T last) noexcept :
m_set(set),
m_value(value),
m_last(last) {
next();
}
IdSetDenseIterator& operator++() noexcept {
if (m_value != m_last) {
++m_value;
next();
}
return *this;
}
IdSetDenseIterator operator++(int) noexcept {
IdSetDenseIterator tmp{*this};
operator++();
return tmp;
}
bool operator==(const IdSetDenseIterator& rhs) const noexcept {
return m_set == rhs.m_set && m_value == rhs.m_value;
}
bool operator!=(const IdSetDenseIterator& rhs) const noexcept {
return !(*this == rhs);
}
T operator*() const noexcept {
assert(m_value < m_last);
return m_value;
}
}; // class IdSetDenseIterator
/**
* A set of Ids of the given type. Internal storage is in chunks of
* arrays used as bit fields. Internally those chunks will be allocated
* as needed, so it works relatively efficiently with both smaller
* and larger Id sets. If it is not used, no memory is allocated at
* all.
*/
template <typename T, std::size_t chunk_bits>
class IdSetDense : public IdSet<T> {
static_assert(std::is_unsigned<T>::value, "Needs unsigned type");
static_assert(sizeof(T) >= 4, "Needs at least 32bit type");
friend class IdSetDenseIterator<T, chunk_bits>;
enum : std::size_t {
chunk_size = 1U << chunk_bits
};
std::vector<std::unique_ptr<unsigned char[]>> m_data;
T m_size = 0;
static std::size_t chunk_id(T id) noexcept {
return id >> (chunk_bits + 3U);
}
static std::size_t offset(T id) noexcept {
return (id >> 3U) & ((1U << chunk_bits) - 1U);
}
static unsigned int bitmask(T id) noexcept {
return 1U << (id & 0x7U);
}
T last() const noexcept {
return static_cast<T>(m_data.size()) * chunk_size * 8;
}
unsigned char& get_element(T id) {
const auto cid = chunk_id(id);
if (cid >= m_data.size()) {
m_data.resize(cid + 1);
}
auto& chunk = m_data[cid];
if (!chunk) {
chunk.reset(new unsigned char[chunk_size]);
::memset(chunk.get(), 0, chunk_size);
}
return chunk[offset(id)];
}
public:
using const_iterator = IdSetDenseIterator<T, chunk_bits>;
friend void swap(IdSetDense& first, IdSetDense& second) noexcept {
using std::swap;
swap(first.m_data, second.m_data);
swap(first.m_size, second.m_size);
}
IdSetDense() = default;
IdSetDense(const IdSetDense& other) :
IdSet<T>(other),
m_size(other.m_size) {
m_data.reserve(other.m_data.size());
for (const auto& ptr : other.m_data) {
if (ptr) {
m_data.emplace_back(new unsigned char[chunk_size]);
::memcpy(m_data.back().get(), ptr.get(), chunk_size);
} else {
m_data.emplace_back();
}
}
}
IdSetDense& operator=(IdSetDense other) {
swap(*this, other);
return *this;
}
IdSetDense(IdSetDense&&) noexcept = default;
// This should really be noexcept, but GCC 4.8 doesn't like it.
// NOLINTNEXTLINE(hicpp-noexcept-move, performance-noexcept-move-constructor)
IdSetDense& operator=(IdSetDense&&) = default;
~IdSetDense() noexcept override = default;
/**
* Add the Id to the set if it is not already in there.
*
* @param id The Id to set.
* @returns true if the Id was added, false if it was already set.
*/
bool check_and_set(T id) {
auto& element = get_element(id);
if ((element & bitmask(id)) == 0) {
element |= bitmask(id);
++m_size;
return true;
}
return false;
}
/**
* Add the given Id to the set.
*
* @param id The Id to set.
*/
void set(T id) final {
(void)check_and_set(id);
}
/**
* Remove the given Id from the set.
*
* @param id The Id to set.
*/
void unset(T id) {
auto& element = get_element(id);
if ((element & bitmask(id)) != 0) {
element &= ~bitmask(id);
--m_size;
}
}
/**
* Is the Id in the set?
*
* @param id The Id to check.
*/
bool get(T id) const noexcept final {
if (chunk_id(id) >= m_data.size()) {
return false;
}
const auto* r = m_data[chunk_id(id)].get();
if (!r) {
return false;
}
return (r[offset(id)] & bitmask(id)) != 0;
}
/**
* Is the set empty?
*/
bool empty() const noexcept final {
return m_size == 0;
}
/**
* The number of Ids stored in the set.
*/
T size() const noexcept {
return m_size;
}
/**
* Clear the set.
*/
void clear() final {
m_data.clear();
m_size = 0;
}
std::size_t used_memory() const noexcept final {
return m_data.size() * chunk_size;
}
const_iterator begin() const {
return {this, 0, last()};
}
const_iterator end() const {
return {this, last(), last()};
}
}; // class IdSetDense
/**
* IdSet implementation for small Id sets. It writes the Ids
* into a vector and uses linear search.
*/
template <typename T>
class IdSetSmall : public IdSet<T> {
std::vector<T> m_data;
public:
IdSetSmall() = default;
IdSetSmall(const IdSetSmall&) = default;
IdSetSmall& operator=(const IdSetSmall&) = default;
IdSetSmall(IdSetSmall&&) noexcept = default;
IdSetSmall& operator=(IdSetSmall&&) noexcept = default;
~IdSetSmall() noexcept override = default;
/**
* Add the given Id to the set.
*/
void set(T id) final {
if (m_data.empty() || m_data.back() != id) {
m_data.push_back(id);
}
}
/**
* Is the Id in the set? Uses linear search.
*
* @param id The Id to check.
*/
bool get(T id) const noexcept final {
const auto it = std::find(m_data.cbegin(), m_data.cend(), id);
return it != m_data.cend();
}
/**
* Is the Id in the set? Uses a binary search. For larger sets
* this might be more efficient than calling get(), the set
* must be sorted.
*
* @param id The Id to check.
* @pre You must have called sort_unique() before calling this
* or be sure there are no duplicates and the Ids have been
* set in order.
*/
bool get_binary_search(T id) const noexcept {
return std::binary_search(m_data.cbegin(), m_data.cend(), id);
}
/**
* Is the set empty?
*/
bool empty() const noexcept final {
return m_data.empty();
}
/**
* Clear the set.
*/
void clear() final {
m_data.clear();
}
/**
* Sort the internal vector and remove any duplicates. Call this
* before using size(), get_binary_search(), merge_sorted() or
* using an iterator.
*/
void sort_unique() {
std::sort(m_data.begin(), m_data.end());
const auto last = std::unique(m_data.begin(), m_data.end());
m_data.erase(last, m_data.end());
}
/**
* The number of Ids stored in the set.
*
* @pre You must have called sort_unique() before calling this
* or be sure there are no duplicates.
*/
std::size_t size() const noexcept {
return m_data.size();
}
std::size_t used_memory() const noexcept final {
return m_data.capacity() * sizeof(T);
}
/**
* Merge the other set into this one. The result is sorted.
*
* @pre Both sets must be sorted and must not contain any
* duplicates. Call sort_unique() if you are not sure.
*/
void merge_sorted(const IdSetSmall<T>& other) {
std::vector<T> new_data;
new_data.reserve(m_data.size() + other.m_data.size());
std::set_union(m_data.cbegin(), m_data.cend(),
other.m_data.cbegin(), other.m_data.cend(),
std::back_inserter(new_data));
using std::swap;
swap(new_data, m_data);
}
/// Iterator type. There is no non-const iterator.
using const_iterator = typename std::vector<T>::const_iterator;
const_iterator begin() const noexcept {
return m_data.cbegin();
}
const_iterator end() const noexcept {
return m_data.cend();
}
const_iterator cbegin() const noexcept {
return m_data.cbegin();
}
const_iterator cend() const noexcept {
return m_data.cend();
}
}; // class IdSetSmall
} // namespace index
} // namespace osmium
#endif // OSMIUM_INDEX_ID_SET_HPP
+97
View File
@@ -0,0 +1,97 @@
#ifndef OSMIUM_INDEX_INDEX_HPP
#define OSMIUM_INDEX_INDEX_HPP
/*
This file is part of Osmium (https://osmcode.org/libosmium).
Copyright 2013-2023 Jochen Topf <jochen@topf.org> and others (see README).
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <osmium/util/compatibility.hpp>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <stdexcept>
#include <string>
namespace osmium {
/**
* Exception signaling that an element could not be
* found in an index.
*/
struct OSMIUM_EXPORT not_found : public std::runtime_error {
explicit not_found(const std::string& what) :
std::runtime_error(what) {
}
explicit not_found(const char* what) :
std::runtime_error(what) {
}
explicit not_found(uint64_t id) :
std::runtime_error(std::string{"id "} + std::to_string(id) + " not found") {
}
}; // struct not_found
/**
* @brief Indexing of OSM data, Locations, etc.
*/
namespace index {
/**
* Some of the index classes need an "empty" value that can
* never appear in real data. This function must return this
* empty value for any class used as a value in an index.
* The default implementation returns a default constructed
* object, but it can be specialized.
*/
template <typename T>
inline constexpr T empty_value() {
return T{};
}
/**
* The size_t value in indexes is usually used for offsets
* into a buffer or file. It is unlikely that we ever need
* the full range, so the max value is a good "empty" value.
*/
template <>
inline constexpr size_t empty_value<size_t>() {
return std::numeric_limits<size_t>::max();
}
} // namespace index
} // namespace osmium
#endif // OSMIUM_INDEX_INDEX_HPP
+301
View File
@@ -0,0 +1,301 @@
#ifndef OSMIUM_INDEX_MAP_HPP
#define OSMIUM_INDEX_MAP_HPP
/*
This file is part of Osmium (https://osmcode.org/libosmium).
Copyright 2013-2023 Jochen Topf <jochen@topf.org> and others (see README).
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <osmium/util/compatibility.hpp>
#include <osmium/util/string.hpp>
#include <algorithm>
#include <cstddef>
#include <functional>
#include <map>
#include <memory>
#include <stdexcept>
#include <string>
#include <type_traits>
#include <vector>
namespace osmium {
struct OSMIUM_EXPORT map_factory_error : public std::runtime_error {
explicit map_factory_error(const char* message) :
std::runtime_error(message) {
}
explicit map_factory_error(const std::string& message) :
std::runtime_error(message) {
}
}; // struct map_factory_error
namespace index {
/**
* @brief Key-value containers with unique integer values for a key
*/
namespace map {
/**
* This abstract class defines an interface to storage classes
* intended for storing small pieces of data (such as coordinates)
* indexed by a positive integer (such as an object ID). The
* storage must be very space efficient and able to scale to billions
* of objects.
*
* Subclasses have different implementations that store the
* data in different ways in memory and/or on disk. Some storage
* classes are better suited when working with the whole planet,
* some are better for data extracts.
*
* Note that these classes are not required to track "empty" fields.
* When reading data you have to be sure you have put something in
* there before.
*
* A typical use for this and derived classes is storage of node
* locations indexed by node ID. These indexes will only work
* on 64 bit systems if used in this case. 32 bit systems just
* can't address that much memory!
*
* @tparam TId Id type, usually osmium::unsigned_object_id_type,
* must be an unsigned integral type.
* @tparam TValue Value type, usually osmium::Location or size_t.
* Copied by value, so should be "small" type.
*/
template <typename TId, typename TValue>
class Map {
static_assert(std::is_integral<TId>::value && std::is_unsigned<TId>::value,
"TId template parameter for class Map must be unsigned integral type");
protected:
Map(Map&&) noexcept = default;
Map& operator=(Map&&) noexcept = default;
public:
/// The "key" type, usually osmium::unsigned_object_id_type.
using key_type = TId;
/// The "value" type, usually a Location or size_t.
using value_type = TValue;
Map() noexcept = default;
Map(const Map&) = delete;
Map& operator=(const Map&) = delete;
virtual ~Map() noexcept = default;
virtual void reserve(const std::size_t /*size*/) {
// default implementation is empty
}
/// Set the field with id to value.
virtual void set(const TId id, const TValue value) = 0;
/**
* Retrieve value by id.
*
* @param id The id to look for.
* @returns Value.
* @throws osmium::not_found if the id could not be found.
*/
virtual TValue get(const TId id) const = 0;
/**
* Retrieve value by id.
*
* @param id The id to look for.
* @returns Value or, if not found, the empty value as defined
* by osmium::index::empty_value<TValue>() which is
* usually the default constructed value of type
* TValue.
*/
virtual TValue get_noexcept(const TId id) const noexcept = 0;
/**
* Get the approximate number of items in the storage. The storage
* might allocate memory in blocks, so this size might not be
* accurate. You can not use this to find out how much memory the
* storage uses. Use used_memory() for that.
*/
virtual std::size_t size() const = 0;
/**
* Get the memory used for this storage in bytes. Note that this
* is not necessarily entirely accurate but an approximation.
* For storage classes that store the data in memory, this is
* the main memory used, for storage classes storing data on disk
* this is the memory used on disk.
*/
virtual std::size_t used_memory() const = 0;
/**
* Clear memory used for this storage. After this you can not
* use the storage container any more.
*/
virtual void clear() = 0;
/**
* Sort data in map. Call this after writing all data and
* before reading. Not all implementations need this.
*/
virtual void sort() {
// default implementation is empty
}
// This function can usually be const in derived classes,
// but not always. It could, for instance, sort internal data.
// This is why it is not declared const here.
virtual void dump_as_list(const int /*fd*/) {
throw std::runtime_error{"can't dump as list"};
}
// This function can usually be const in derived classes,
// but not always. It could, for instance, sort internal data.
// This is why it is not declared const here.
virtual void dump_as_array(const int /*fd*/) {
throw std::runtime_error{"can't dump as array"};
}
}; // class Map
} // namespace map
template <typename TId, typename TValue>
class MapFactory {
public:
using id_type = TId;
using value_type = TValue;
using map_type = osmium::index::map::Map<id_type, value_type>;
using create_map_func = std::function<map_type*(const std::vector<std::string>&)>;
private:
std::map<const std::string, create_map_func> m_callbacks;
MapFactory() = default;
public:
MapFactory(const MapFactory&) = delete;
MapFactory& operator=(const MapFactory&) = delete;
MapFactory(MapFactory&&) = delete;
MapFactory& operator=(MapFactory&&) = delete;
~MapFactory() noexcept = default;
static MapFactory<id_type, value_type>& instance() {
static MapFactory<id_type, value_type> factory;
return factory;
}
bool register_map(const std::string& map_type_name, create_map_func func) {
return m_callbacks.emplace(map_type_name, func).second;
}
bool has_map_type(const std::string& map_type_name) const {
return m_callbacks.count(map_type_name) != 0;
}
std::vector<std::string> map_types() const {
std::vector<std::string> result;
result.reserve(m_callbacks.size());
for (const auto& cb : m_callbacks) {
result.push_back(cb.first);
}
std::sort(result.begin(), result.end());
return result;
}
std::unique_ptr<map_type> create_map(const std::string& config_string) const {
std::vector<std::string> config{osmium::split_string(config_string, ',')};
if (config.empty()) {
throw map_factory_error{"Need non-empty map type name"};
}
const auto it = m_callbacks.find(config[0]);
if (it != m_callbacks.end()) {
return std::unique_ptr<map_type>((it->second)(config));
}
throw map_factory_error{std::string{"Support for map type '"} + config[0] + "' not compiled into this binary"};
}
}; // class MapFactory
namespace map {
template <typename TId, typename TValue, template <typename, typename> class TMap>
struct create_map {
TMap<TId, TValue>* operator()(const std::vector<std::string>& /*config_string*/) {
return new TMap<TId, TValue>();
}
};
} // namespace map
template <typename TId, typename TValue, template <typename, typename> class TMap>
inline bool register_map(const std::string& name) {
return osmium::index::MapFactory<TId, TValue>::instance().register_map(name, [](const std::vector<std::string>& config) {
return map::create_map<TId, TValue, TMap>()(config);
});
}
#define OSMIUM_CONCATENATE_DETAIL_(x, y) x##y
#define OSMIUM_CONCATENATE_(x, y) OSMIUM_CONCATENATE_DETAIL_(x, y)
#define REGISTER_MAP(id, value, klass, name) \
namespace osmium { namespace index { namespace detail { \
namespace OSMIUM_CONCATENATE_(register_map_, __COUNTER__) { \
const bool registered = osmium::index::register_map<id, value, klass>(#name); \
inline bool get_registered() noexcept { \
return registered; \
} } \
} } }
} // namespace index
} // namespace osmium
#endif // OSMIUM_INDEX_MAP_HPP
+46
View File
@@ -0,0 +1,46 @@
#ifndef OSMIUM_INDEX_MAP_ALL_HPP
#define OSMIUM_INDEX_MAP_ALL_HPP
/*
This file is part of Osmium (https://osmcode.org/libosmium).
Copyright 2013-2023 Jochen Topf <jochen@topf.org> and others (see README).
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <osmium/index/map/dense_file_array.hpp> // IWYU pragma: keep
#include <osmium/index/map/dense_mem_array.hpp> // IWYU pragma: keep
#include <osmium/index/map/dense_mmap_array.hpp> // IWYU pragma: keep
#include <osmium/index/map/dummy.hpp> // IWYU pragma: keep
#include <osmium/index/map/flex_mem.hpp> // IWYU pragma: keep
#include <osmium/index/map/sparse_file_array.hpp> // IWYU pragma: keep
#include <osmium/index/map/sparse_mem_array.hpp> // IWYU pragma: keep
#include <osmium/index/map/sparse_mem_map.hpp> // IWYU pragma: keep
#include <osmium/index/map/sparse_mmap_array.hpp> // IWYU pragma: keep
#endif // OSMIUM_INDEX_MAP_ALL_HPP
@@ -0,0 +1,71 @@
#ifndef OSMIUM_INDEX_MAP_DENSE_FILE_ARRAY_HPP
#define OSMIUM_INDEX_MAP_DENSE_FILE_ARRAY_HPP
/*
This file is part of Osmium (https://osmcode.org/libosmium).
Copyright 2013-2023 Jochen Topf <jochen@topf.org> and others (see README).
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <osmium/index/detail/create_map_with_fd.hpp>
#include <osmium/index/detail/mmap_vector_file.hpp>
#include <osmium/index/detail/vector_map.hpp>
#include <string>
#include <vector>
#define OSMIUM_HAS_INDEX_MAP_DENSE_FILE_ARRAY
namespace osmium {
namespace index {
namespace map {
template <typename TId, typename TValue>
using DenseFileArray = VectorBasedDenseMap<osmium::detail::mmap_vector_file<TValue>, TId, TValue>;
template <typename TId, typename TValue>
struct create_map<TId, TValue, DenseFileArray> {
DenseFileArray<TId, TValue>* operator()(const std::vector<std::string>& config) {
return osmium::index::detail::create_map_with_fd<DenseFileArray<TId, TValue>>(config);
}
};
} // namespace map
} // namespace index
} // namespace osmium
#ifdef OSMIUM_WANT_NODE_LOCATION_MAPS
REGISTER_MAP(osmium::unsigned_object_id_type, osmium::Location, osmium::index::map::DenseFileArray, dense_file_array)
#endif
#endif // OSMIUM_INDEX_MAP_DENSE_FILE_ARRAY_HPP
@@ -0,0 +1,61 @@
#ifndef OSMIUM_INDEX_MAP_DENSE_MEM_ARRAY_HPP
#define OSMIUM_INDEX_MAP_DENSE_MEM_ARRAY_HPP
/*
This file is part of Osmium (https://osmcode.org/libosmium).
Copyright 2013-2023 Jochen Topf <jochen@topf.org> and others (see README).
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <osmium/index/detail/vector_map.hpp>
#include <vector>
#define OSMIUM_HAS_INDEX_MAP_DENSE_MEM_ARRAY
namespace osmium {
namespace index {
namespace map {
template <typename TId, typename TValue>
using DenseMemArray = VectorBasedDenseMap<std::vector<TValue>, TId, TValue>;
} // namespace map
} // namespace index
} // namespace osmium
#ifdef OSMIUM_WANT_NODE_LOCATION_MAPS
REGISTER_MAP(osmium::unsigned_object_id_type, osmium::Location, osmium::index::map::DenseMemArray, dense_mem_array)
#endif
#endif // OSMIUM_INDEX_MAP_DENSE_MEM_ARRAY_HPP
@@ -0,0 +1,64 @@
#ifndef OSMIUM_INDEX_MAP_DENSE_MMAP_ARRAY_HPP
#define OSMIUM_INDEX_MAP_DENSE_MMAP_ARRAY_HPP
/*
This file is part of Osmium (https://osmcode.org/libosmium).
Copyright 2013-2023 Jochen Topf <jochen@topf.org> and others (see README).
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#ifdef __linux__
#include <osmium/index/detail/mmap_vector_anon.hpp> // IWYU pragma: keep
#include <osmium/index/detail/vector_map.hpp>
#define OSMIUM_HAS_INDEX_MAP_DENSE_MMAP_ARRAY
namespace osmium {
namespace index {
namespace map {
template <typename TId, typename TValue>
using DenseMmapArray = VectorBasedDenseMap<osmium::detail::mmap_vector_anon<TValue>, TId, TValue>;
} // namespace map
} // namespace index
} // namespace osmium
#ifdef OSMIUM_WANT_NODE_LOCATION_MAPS
REGISTER_MAP(osmium::unsigned_object_id_type, osmium::Location, osmium::index::map::DenseMmapArray, dense_mmap_array)
#endif
#endif // __linux__
#endif // OSMIUM_INDEX_MAP_DENSE_MMAP_ARRAY_HPP
@@ -0,0 +1,98 @@
#ifndef OSMIUM_INDEX_MAP_DUMMY_HPP
#define OSMIUM_INDEX_MAP_DUMMY_HPP
/*
This file is part of Osmium (https://osmcode.org/libosmium).
Copyright 2013-2023 Jochen Topf <jochen@topf.org> and others (see README).
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <osmium/index/index.hpp>
#include <osmium/index/map.hpp>
#include <cstddef>
namespace osmium {
namespace index {
namespace map {
/**
* Pseudo map.
* Use this class if you don't need a map, but you
* need an object that behaves like one.
*/
template <typename TId, typename TValue>
class Dummy : public osmium::index::map::Map<TId, TValue> {
public:
Dummy() = default;
Dummy(const Dummy&) = default;
Dummy& operator=(const Dummy&) = default;
Dummy(Dummy&&) noexcept = default;
Dummy& operator=(Dummy&&) noexcept = default;
~Dummy() noexcept override = default;
void set(const TId /*id*/, const TValue /*value*/) final {
// intentionally left blank
}
TValue get(const TId id) const final {
throw osmium::not_found{id};
}
TValue get_noexcept(const TId /*id*/) const noexcept final {
return osmium::index::empty_value<TValue>();
}
size_t size() const final {
return 0;
}
size_t used_memory() const final {
return 0;
}
void clear() final {
}
}; // class Dummy
} // namespace map
} // namespace index
} // namespace osmium
#endif // OSMIUM_INDEX_MAP_DUMMY_HPP
@@ -0,0 +1,283 @@
#ifndef OSMIUM_INDEX_MAP_FLEX_MEM_HPP
#define OSMIUM_INDEX_MAP_FLEX_MEM_HPP
/*
This file is part of Osmium (https://osmcode.org/libosmium).
Copyright 2013-2023 Jochen Topf <jochen@topf.org> and others (see README).
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <osmium/index/index.hpp>
#include <osmium/index/map.hpp>
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <utility>
#include <vector>
#define OSMIUM_HAS_INDEX_MAP_FLEX_MEM
namespace osmium {
namespace index {
namespace map {
/**
* This is an autoscaling index that works well with small and
* large input data. All data will be held in memory. For small
* input data a sparse array will be used, if this becomes
* inefficient, the class will switch automatically to a dense
* index.
*/
template <typename TId, typename TValue>
class FlexMem : public osmium::index::map::Map<TId, TValue> {
// This value is based on benchmarks with a planet file and
// some smaller files.
enum {
bits = 16
};
enum : uint64_t {
block_size = 1ULL << bits
};
// Minimum number of entries in the sparse index before we
// are considering switching to a dense index.
enum : int64_t {
min_dense_entries = 0xffffff
};
// When more than a third of all Ids are in the index, we
// switch to the dense index. This is a compromise between
// the best memory efficiency (which we would get at a factor
// of 2) and the performance (dense index is much faster then
// the sparse index).
enum {
density_factor = 3
};
// An entry in the sparse index
struct entry {
uint64_t id;
TValue value;
entry(uint64_t i, TValue v) :
id(i),
value(std::move(v)) {
}
bool operator<(const entry other) const noexcept {
return id < other.id;
}
};
std::vector<entry> m_sparse_entries;
std::vector<std::vector<TValue>> m_dense_blocks;
// The maximum Id that was seen yet. Only set in sparse mode.
uint64_t m_max_id = 0;
// Set to false in sparse mode and to true in dense mode.
bool m_dense;
static uint64_t block(const uint64_t id) noexcept {
return id >> bits;
}
static uint64_t offset(const uint64_t id) noexcept {
return id & (block_size - 1);
}
// Assure that the block with the given number exists. Create
// it if needed.
void assure_block(const uint64_t num) {
if (num >= m_dense_blocks.size()) {
m_dense_blocks.resize(num + 1);
}
if (m_dense_blocks[num].empty()) {
m_dense_blocks[num].assign(block_size, osmium::index::empty_value<TValue>());
}
}
void set_sparse(const uint64_t id, const TValue value) {
m_sparse_entries.emplace_back(id, value);
if (id > m_max_id) {
m_max_id = id;
if (m_sparse_entries.size() >= min_dense_entries) {
if (m_max_id < m_sparse_entries.size() * density_factor) {
switch_to_dense();
}
}
}
}
TValue get_sparse(const uint64_t id) const noexcept {
const auto it = std::lower_bound(m_sparse_entries.begin(),
m_sparse_entries.end(),
entry{id, osmium::index::empty_value<TValue>()});
if (it == m_sparse_entries.end() || it->id != id) {
return osmium::index::empty_value<TValue>();
}
return it->value;
}
void set_dense(const uint64_t id, const TValue value) {
assure_block(block(id));
m_dense_blocks[block(id)][offset(id)] = value;
}
TValue get_dense(const uint64_t id) const noexcept {
if (m_dense_blocks.size() <= block(id) || m_dense_blocks[block(id)].empty()) {
return osmium::index::empty_value<TValue>();
}
return m_dense_blocks[block(id)][offset(id)];
}
public:
/**
* Create FlexMem index.
*
* @param use_dense Usually FlexMem indexes start out as sparse
* indexes and will switch to dense when they
* think it is better. Set this to force dense
* indexing from the start. This is usually
* only useful for testing.
*/
explicit FlexMem(bool use_dense = false) :
m_dense(use_dense) {
}
bool is_dense() const noexcept {
return m_dense;
}
std::size_t size() const noexcept final {
if (m_dense) {
return m_dense_blocks.size() * block_size;
}
return m_sparse_entries.size();
}
std::size_t used_memory() const noexcept final {
return sizeof(FlexMem) +
m_sparse_entries.size() * sizeof(entry) +
m_dense_blocks.size() * (block_size * sizeof(TValue) + sizeof(std::vector<TValue>));
}
void set(const TId id, const TValue value) final {
if (m_dense) {
set_dense(id, value);
} else {
set_sparse(id, value);
}
}
TValue get_noexcept(const TId id) const noexcept final {
if (m_dense) {
return get_dense(id);
}
return get_sparse(id);
}
TValue get(const TId id) const final {
const auto value = get_noexcept(id);
if (value == osmium::index::empty_value<TValue>()) {
throw osmium::not_found{id};
}
return value;
}
void clear() final {
m_sparse_entries.clear();
m_sparse_entries.shrink_to_fit();
m_dense_blocks.clear();
m_dense_blocks.shrink_to_fit();
m_max_id = 0;
m_dense = false;
}
void sort() final {
std::sort(m_sparse_entries.begin(), m_sparse_entries.end());
}
/**
* Switch from using a sparse to a dense index. Usually you
* do not need to call this, because the FlexMem class will
* do this automatically if it thinks the dense index is more
* efficient.
*
* Does nothing if the index is already in dense mode.
*/
void switch_to_dense() {
if (m_dense) {
return;
}
for (const auto& entry : m_sparse_entries) {
set_dense(entry.id, entry.value);
}
m_sparse_entries.clear();
m_sparse_entries.shrink_to_fit();
m_max_id = 0;
m_dense = true;
}
std::pair<std::size_t, std::size_t> stats() const noexcept {
std::size_t used_blocks = 0;
std::size_t empty_blocks = 0;
for (const auto& dense_block : m_dense_blocks) {
if (dense_block.empty()) {
++empty_blocks;
} else {
++used_blocks;
}
}
return std::make_pair(used_blocks, empty_blocks);
}
}; // class FlexMem
} // namespace map
} // namespace index
} // namespace osmium
#ifdef OSMIUM_WANT_NODE_LOCATION_MAPS
REGISTER_MAP(osmium::unsigned_object_id_type, osmium::Location, osmium::index::map::FlexMem, flex_mem)
#endif
#endif // OSMIUM_INDEX_MAP_FLEX_MEM_HPP
@@ -0,0 +1,71 @@
#ifndef OSMIUM_INDEX_MAP_SPARSE_FILE_ARRAY_HPP
#define OSMIUM_INDEX_MAP_SPARSE_FILE_ARRAY_HPP
/*
This file is part of Osmium (https://osmcode.org/libosmium).
Copyright 2013-2023 Jochen Topf <jochen@topf.org> and others (see README).
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <osmium/index/detail/create_map_with_fd.hpp>
#include <osmium/index/detail/mmap_vector_file.hpp>
#include <osmium/index/detail/vector_map.hpp>
#include <string>
#include <vector>
#define OSMIUM_HAS_INDEX_MAP_SPARSE_FILE_ARRAY
namespace osmium {
namespace index {
namespace map {
template <typename TId, typename TValue>
using SparseFileArray = VectorBasedSparseMap<TId, TValue, osmium::detail::mmap_vector_file>;
template <typename TId, typename TValue>
struct create_map<TId, TValue, SparseFileArray> {
SparseFileArray<TId, TValue>* operator()(const std::vector<std::string>& config) {
return osmium::index::detail::create_map_with_fd<SparseFileArray<TId, TValue>>(config);
}
};
} // namespace map
} // namespace index
} // namespace osmium
#ifdef OSMIUM_WANT_NODE_LOCATION_MAPS
REGISTER_MAP(osmium::unsigned_object_id_type, osmium::Location, osmium::index::map::SparseFileArray, sparse_file_array)
#endif
#endif // OSMIUM_INDEX_MAP_SPARSE_FILE_ARRAY_HPP
@@ -0,0 +1,64 @@
#ifndef OSMIUM_INDEX_MAP_SPARSE_MEM_ARRAY_HPP
#define OSMIUM_INDEX_MAP_SPARSE_MEM_ARRAY_HPP
/*
This file is part of Osmium (https://osmcode.org/libosmium).
Copyright 2013-2023 Jochen Topf <jochen@topf.org> and others (see README).
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <osmium/index/detail/vector_map.hpp>
#include <vector>
#define OSMIUM_HAS_INDEX_MAP_SPARSE_MEM_ARRAY
namespace osmium {
namespace index {
namespace map {
template <typename T>
using StdVectorWrap = std::vector<T>;
template <typename TId, typename TValue>
using SparseMemArray = VectorBasedSparseMap<TId, TValue, StdVectorWrap>;
} // namespace map
} // namespace index
} // namespace osmium
#ifdef OSMIUM_WANT_NODE_LOCATION_MAPS
REGISTER_MAP(osmium::unsigned_object_id_type, osmium::Location, osmium::index::map::SparseMemArray, sparse_mem_array)
#endif
#endif // OSMIUM_INDEX_MAP_SPARSE_MEM_ARRAY_HPP
@@ -0,0 +1,127 @@
#ifndef OSMIUM_INDEX_MAP_SPARSE_MEM_MAP_HPP
#define OSMIUM_INDEX_MAP_SPARSE_MEM_MAP_HPP
/*
This file is part of Osmium (https://osmcode.org/libosmium).
Copyright 2013-2023 Jochen Topf <jochen@topf.org> and others (see README).
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <osmium/index/index.hpp>
#include <osmium/index/map.hpp>
#include <osmium/io/detail/read_write.hpp>
#include <algorithm> // IWYU pragma: keep (for std::copy)
#include <cstddef>
#include <iterator>
#include <map>
#include <vector>
#define OSMIUM_HAS_INDEX_MAP_SPARSE_MEM_MAP
namespace osmium {
namespace index {
namespace map {
/**
* This implementation uses std::map internally. It uses rather a
* lot of memory, but might make sense for small maps.
*/
template <typename TId, typename TValue>
class SparseMemMap : public osmium::index::map::Map<TId, TValue> {
// This is a rough estimate for the memory needed for each
// element in the map (id + value + pointers to left, right,
// and parent plus some overhead for color of red-black-tree
// or similar).
enum {
element_size = sizeof(TId) + sizeof(TValue) + sizeof(void*) * 4U
};
std::map<TId, TValue> m_elements;
public:
SparseMemMap() = default;
void set(const TId id, const TValue value) final {
m_elements[id] = value;
}
TValue get(const TId id) const final {
const auto it = m_elements.find(id);
if (it == m_elements.end()) {
throw osmium::not_found{id};
}
return it->second;
}
TValue get_noexcept(const TId id) const noexcept final {
const auto it = m_elements.find(id);
if (it == m_elements.end()) {
return osmium::index::empty_value<TValue>();
}
return it->second;
}
size_t size() const noexcept final {
return m_elements.size();
}
size_t used_memory() const noexcept final {
return element_size * m_elements.size();
}
void clear() final {
m_elements.clear();
}
void dump_as_list(const int fd) final {
using t = typename std::map<TId, TValue>::value_type;
std::vector<t> v;
v.reserve(m_elements.size());
std::copy(m_elements.cbegin(), m_elements.cend(), std::back_inserter(v));
osmium::io::detail::reliable_write(fd, reinterpret_cast<const char*>(v.data()), sizeof(t) * v.size());
}
}; // class SparseMemMap
} // namespace map
} // namespace index
} // namespace osmium
#ifdef OSMIUM_WANT_NODE_LOCATION_MAPS
REGISTER_MAP(osmium::unsigned_object_id_type, osmium::Location, osmium::index::map::SparseMemMap, sparse_mem_map)
#endif
#endif // OSMIUM_INDEX_MAP_SPARSE_MEM_MAP_HPP
@@ -0,0 +1,64 @@
#ifndef OSMIUM_INDEX_MAP_SPARSE_MMAP_ARRAY_HPP
#define OSMIUM_INDEX_MAP_SPARSE_MMAP_ARRAY_HPP
/*
This file is part of Osmium (https://osmcode.org/libosmium).
Copyright 2013-2023 Jochen Topf <jochen@topf.org> and others (see README).
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#ifdef __linux__
#include <osmium/index/detail/mmap_vector_anon.hpp>
#include <osmium/index/detail/vector_map.hpp>
#define OSMIUM_HAS_INDEX_MAP_SPARSE_MMAP_ARRAY
namespace osmium {
namespace index {
namespace map {
template <typename TId, typename TValue>
using SparseMmapArray = VectorBasedSparseMap<TId, TValue, osmium::detail::mmap_vector_anon>;
} // namespace map
} // namespace index
} // namespace osmium
#ifdef OSMIUM_WANT_NODE_LOCATION_MAPS
REGISTER_MAP(osmium::unsigned_object_id_type, osmium::Location, osmium::index::map::SparseMmapArray, sparse_mmap_array)
#endif
#endif // __linux__
#endif // OSMIUM_INDEX_MAP_SPARSE_MMAP_ARRAY_HPP
+127
View File
@@ -0,0 +1,127 @@
#ifndef OSMIUM_INDEX_MULTIMAP_HPP
#define OSMIUM_INDEX_MULTIMAP_HPP
/*
This file is part of Osmium (https://osmcode.org/libosmium).
Copyright 2013-2023 Jochen Topf <jochen@topf.org> and others (see README).
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <cstddef>
#include <stdexcept>
#include <type_traits>
#include <utility>
namespace osmium {
namespace index {
/**
* @brief Key-value containers with multiple values for an integer key
*/
namespace multimap {
template <typename TId, typename TValue>
class Multimap {
static_assert(std::is_integral<TId>::value && std::is_unsigned<TId>::value, "TId template parameter for class Multimap must be unsigned integral type");
using element_type = typename std::pair<TId, TValue>;
protected:
Multimap(Multimap&&) noexcept = default;
Multimap& operator=(Multimap&&) noexcept = default;
public:
/// The "key" type, usually osmium::unsigned_object_id_type.
using key_type = TId;
/// The "value" type, usually a Location or size_t.
using value_type = TValue;
Multimap() = default;
Multimap(const Multimap&) = delete;
Multimap& operator=(const Multimap&) = delete;
virtual ~Multimap() noexcept = default;
/// Set the field with id to value.
virtual void set(const TId id, const TValue value) = 0;
using iterator = element_type*;
// virtual std::pair<iterator, iterator> get_all(const TId id) const = 0;
/**
* Get the approximate number of items in the storage. The storage
* might allocate memory in blocks, so this size might not be
* accurate. You can not use this to find out how much memory the
* storage uses. Use used_memory() for that.
*/
virtual size_t size() const = 0;
/**
* Get the memory used for this storage in bytes. Note that this
* is not necessarily entirely accurate but an approximation.
* For storage classes that store the data in memory, this is
* the main memory used, for storage classes storing data on disk
* this is the memory used on disk.
*/
virtual size_t used_memory() const = 0;
/**
* Clear memory used for this storage. After this you can not
* use the storage container any more.
*/
virtual void clear() = 0;
/**
* Sort data in map. Call this after writing all data and
* before reading. Not all implementations need this.
*/
virtual void sort() {
// default implementation is empty
}
virtual void dump_as_list(const int /*fd*/) {
throw std::runtime_error{"can't dump as list"};
}
}; // class Multimap
} // namespace multimap
} // namespace index
} // namespace osmium
#endif // OSMIUM_INDEX_MULTIMAP_HPP
@@ -0,0 +1,41 @@
#ifndef OSMIUM_INDEX_MULTIMAP_ALL_HPP
#define OSMIUM_INDEX_MULTIMAP_ALL_HPP
/*
This file is part of Osmium (https://osmcode.org/libosmium).
Copyright 2013-2023 Jochen Topf <jochen@topf.org> and others (see README).
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <osmium/index/multimap/sparse_file_array.hpp> // IWYU pragma: keep
#include <osmium/index/multimap/sparse_mem_array.hpp> // IWYU pragma: keep
#include <osmium/index/multimap/sparse_mem_multimap.hpp> // IWYU pragma: keep
#include <osmium/index/multimap/sparse_mmap_array.hpp> // IWYU pragma: keep
#endif // OSMIUM_INDEX_MULTIMAP_ALL_HPP
@@ -0,0 +1,203 @@
#ifndef OSMIUM_INDEX_MULTIMAP_HYBRID_HPP
#define OSMIUM_INDEX_MULTIMAP_HYBRID_HPP
/*
This file is part of Osmium (https://osmcode.org/libosmium).
Copyright 2013-2023 Jochen Topf <jochen@topf.org> and others (see README).
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <osmium/index/index.hpp>
#include <osmium/index/multimap.hpp>
#include <osmium/index/multimap/sparse_mem_array.hpp>
#include <osmium/index/multimap/sparse_mem_multimap.hpp>
#include <cstddef>
#include <utility>
namespace osmium {
namespace index {
namespace multimap {
template <typename TId, typename TValue>
class HybridIterator {
using main_map_type = SparseMemArray<TId, TValue>;
using extra_map_type = SparseMemMultimap<TId, TValue>;
using element_type = typename std::pair<TId, TValue>;
typename main_map_type::iterator m_begin_main;
typename main_map_type::iterator m_end_main;
typename extra_map_type::iterator m_begin_extra;
typename extra_map_type::iterator m_end_extra;
public:
HybridIterator(typename main_map_type::iterator begin_main,
typename main_map_type::iterator end_main,
typename extra_map_type::iterator begin_extra,
typename extra_map_type::iterator end_extra) :
m_begin_main(begin_main),
m_end_main(end_main),
m_begin_extra(begin_extra),
m_end_extra(end_extra) {
}
~HybridIterator() noexcept = default;
HybridIterator& operator++() {
if (m_begin_main == m_end_main) {
++m_begin_extra;
} else {
++m_begin_main;
while (m_begin_main != m_end_main && m_begin_main->second == osmium::index::empty_value<TValue>()) { // ignore removed elements
++m_begin_main;
}
}
return *this;
}
HybridIterator<TId, TValue> operator++(int) {
const auto tmp{*this};
operator++();
return tmp;
}
bool operator==(const HybridIterator& rhs) const {
return m_begin_main == rhs.m_begin_main &&
m_end_main == rhs.m_end_main &&
m_begin_extra == rhs.m_begin_extra &&
m_end_extra == rhs.m_end_extra;
}
bool operator!=(const HybridIterator& rhs) const {
return !operator==(rhs);
}
const element_type& operator*() {
if (m_begin_main == m_end_main) {
return *m_begin_extra;
}
return *m_begin_main;
}
const element_type* operator->() {
return &operator*();
}
}; // class HybridIterator
template <typename TId, typename TValue>
class Hybrid : public Multimap<TId, TValue> {
using main_map_type = SparseMemArray<TId, TValue>;
using extra_map_type = SparseMemMultimap<TId, TValue>;
main_map_type m_main;
extra_map_type m_extra;
public:
using iterator = HybridIterator<TId, TValue>;
using const_iterator = const HybridIterator<TId, TValue>;
Hybrid() :
m_main(),
m_extra() {
}
~Hybrid() noexcept = default;
size_t size() const final {
return m_main.size() + m_extra.size();
}
size_t used_memory() const final {
return m_main.used_memory() + m_extra.used_memory();
}
void reserve(const size_t size) {
m_main.reserve(size);
}
void unsorted_set(const TId id, const TValue value) {
m_main.set(id, value);
}
void set(const TId id, const TValue value) final {
m_extra.set(id, value);
}
std::pair<iterator, iterator> get_all(const TId id) {
const auto result_main = m_main.get_all(id);
const auto result_extra = m_extra.get_all(id);
return std::make_pair(iterator{result_main.first, result_main.second, result_extra.first, result_extra.second},
iterator{result_main.second, result_main.second, result_extra.second, result_extra.second});
}
void remove(const TId id, const TValue value) {
m_main.remove(id, value);
m_extra.remove(id, value);
}
void consolidate() {
m_main.erase_removed();
for (const auto& element : m_extra) {
m_main.set(element.first, element.second);
}
m_extra.clear();
m_main.sort();
}
void dump_as_list(const int fd) final {
consolidate();
m_main.dump_as_list(fd);
}
void clear() final {
m_main.clear();
m_extra.clear();
}
void sort() final {
m_main.sort();
}
}; // class Hybrid
} // namespace multimap
} // namespace index
} // namespace osmium
#endif // OSMIUM_INDEX_MULTIMAP_HYBRID_HPP
@@ -0,0 +1,54 @@
#ifndef OSMIUM_INDEX_MULTIMAP_SPARSE_FILE_ARRAY_HPP
#define OSMIUM_INDEX_MULTIMAP_SPARSE_FILE_ARRAY_HPP
/*
This file is part of Osmium (https://osmcode.org/libosmium).
Copyright 2013-2023 Jochen Topf <jochen@topf.org> and others (see README).
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <osmium/index/detail/mmap_vector_file.hpp>
#include <osmium/index/detail/vector_multimap.hpp>
namespace osmium {
namespace index {
namespace multimap {
template <typename TId, typename TValue>
using SparseFileArray = VectorBasedSparseMultimap<TId, TValue, osmium::detail::mmap_vector_file>;
} // namespace multimap
} // namespace index
} // namespace osmium
#endif // OSMIUM_INDEX_MULTIMAP_SPARSE_FILE_ARRAY_HPP
@@ -0,0 +1,58 @@
#ifndef OSMIUM_INDEX_MULTIMAP_SPARSE_MEM_ARRAY_HPP
#define OSMIUM_INDEX_MULTIMAP_SPARSE_MEM_ARRAY_HPP
/*
This file is part of Osmium (https://osmcode.org/libosmium).
Copyright 2013-2023 Jochen Topf <jochen@topf.org> and others (see README).
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <osmium/index/detail/vector_multimap.hpp>
#include <vector>
namespace osmium {
namespace index {
namespace multimap {
template <typename T>
using StdVectorWrap = std::vector<T>;
template <typename TId, typename TValue>
using SparseMemArray = VectorBasedSparseMultimap<TId, TValue, StdVectorWrap>;
} // namespace multimap
} // namespace index
} // namespace osmium
#endif // OSMIUM_INDEX_MULTIMAP_SPARSE_MEM_ARRAY_HPP
@@ -0,0 +1,152 @@
#ifndef OSMIUM_INDEX_MULTIMAP_SPARSE_MEM_MULTIMAP_HPP
#define OSMIUM_INDEX_MULTIMAP_SPARSE_MEM_MULTIMAP_HPP
/*
This file is part of Osmium (https://osmcode.org/libosmium).
Copyright 2013-2023 Jochen Topf <jochen@topf.org> and others (see README).
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <osmium/index/multimap.hpp>
#include <osmium/io/detail/read_write.hpp>
#include <algorithm>
#include <cstddef>
#include <map>
#include <utility>
#include <vector>
namespace osmium {
namespace index {
namespace multimap {
/**
* This implementation uses std::multimap internally. It uses rather a
* lot of memory, but might make sense for small maps.
*/
template <typename TId, typename TValue>
class SparseMemMultimap final : public osmium::index::multimap::Multimap<TId, TValue> {
// This is a rough estimate for the memory needed for each
// element in the map (id + value + pointers to left, right,
// and parent plus some overhead for color of red-black-tree
// or similar).
enum {
element_size = sizeof(TId) + sizeof(TValue) + sizeof(void*) * 4U
};
public:
using collection_type = typename std::multimap<const TId, TValue>;
using iterator = typename collection_type::iterator;
using const_iterator = typename collection_type::const_iterator;
using value_type = typename collection_type::value_type;
using element_type = typename std::pair<TId, TValue>;
private:
collection_type m_elements;
public:
SparseMemMultimap() = default;
~SparseMemMultimap() noexcept = default;
void unsorted_set(const TId id, const TValue value) {
m_elements.emplace(id, value);
}
void set(const TId id, const TValue value) override {
m_elements.emplace(id, value);
}
std::pair<iterator, iterator> get_all(const TId id) {
return m_elements.equal_range(id);
}
std::pair<const_iterator, const_iterator> get_all(const TId id) const {
return m_elements.equal_range(id);
}
void remove(const TId id, const TValue value) {
std::pair<iterator, iterator> r = get_all(id);
for (iterator it = r.first; it != r.second; ++it) {
if (it->second == value) {
m_elements.erase(it);
return;
}
}
}
iterator begin() {
return m_elements.begin();
}
iterator end() {
return m_elements.end();
}
size_t size() const override {
return m_elements.size();
}
size_t used_memory() const override {
return element_size * m_elements.size();
}
void clear() override {
m_elements.clear();
}
void consolidate() {
// intentionally left blank
}
void dump_as_list(const int fd) override {
std::vector<element_type> v;
v.reserve(m_elements.size());
for (const auto& element : m_elements) {
v.emplace_back(element.first, element.second);
}
std::sort(v.begin(), v.end());
osmium::io::detail::reliable_write(fd, reinterpret_cast<const char*>(v.data()), sizeof(element_type) * v.size());
}
}; // class SparseMemMultimap
} // namespace multimap
} // namespace index
} // namespace osmium
#endif // OSMIUM_INDEX_MULTIMAP_SPARSE_MEM_MULTIMAP_HPP
@@ -0,0 +1,58 @@
#ifndef OSMIUM_INDEX_MULTIMAP_SPARSE_MMAP_ARRAY_HPP
#define OSMIUM_INDEX_MULTIMAP_SPARSE_MMAP_ARRAY_HPP
/*
This file is part of Osmium (https://osmcode.org/libosmium).
Copyright 2013-2023 Jochen Topf <jochen@topf.org> and others (see README).
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#ifdef __linux__
#include <osmium/index/detail/mmap_vector_anon.hpp>
#include <osmium/index/detail/vector_multimap.hpp>
namespace osmium {
namespace index {
namespace multimap {
template <typename TId, typename TValue>
using SparseMmapArray = VectorBasedSparseMultimap<TId, TValue, osmium::detail::mmap_vector_anon>;
} // namespace multimap
} // namespace index
} // namespace osmium
#endif // __linux__
#endif // OSMIUM_INDEX_MULTIMAP_SPARSE_MMAP_ARRAY_HPP
@@ -0,0 +1,78 @@
#ifndef OSMIUM_INDEX_NODE_LOCATIONS_MAP_HPP
#define OSMIUM_INDEX_NODE_LOCATIONS_MAP_HPP
/*
This file is part of Osmium (https://osmcode.org/libosmium).
Copyright 2013-2023 Jochen Topf <jochen@topf.org> and others (see README).
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <osmium/index/map.hpp> // IWYU pragma: keep
#include <osmium/osm/location.hpp>
#include <osmium/osm/types.hpp>
#define OSMIUM_WANT_NODE_LOCATION_MAPS
#ifdef OSMIUM_HAS_INDEX_MAP_DENSE_FILE_ARRAY
REGISTER_MAP(osmium::unsigned_object_id_type, osmium::Location, osmium::index::map::DenseFileArray, dense_file_array)
#endif
#ifdef OSMIUM_HAS_INDEX_MAP_DENSE_MEM_ARRAY
REGISTER_MAP(osmium::unsigned_object_id_type, osmium::Location, osmium::index::map::DenseMemArray, dense_mem_array)
#endif
#ifdef OSMIUM_HAS_INDEX_MAP_DENSE_MMAP_ARRAY
REGISTER_MAP(osmium::unsigned_object_id_type, osmium::Location, osmium::index::map::DenseMmapArray, dense_mmap_array)
#endif
#ifdef OSMIUM_HAS_INDEX_MAP_SPARSE_FILE_ARRAY
REGISTER_MAP(osmium::unsigned_object_id_type, osmium::Location, osmium::index::map::SparseFileArray, sparse_file_array)
#endif
#ifdef OSMIUM_HAS_INDEX_MAP_SPARSE_MEM_ARRAY
REGISTER_MAP(osmium::unsigned_object_id_type, osmium::Location, osmium::index::map::SparseMemArray, sparse_mem_array)
#endif
#ifdef OSMIUM_HAS_INDEX_MAP_SPARSE_MEM_MAP
REGISTER_MAP(osmium::unsigned_object_id_type, osmium::Location, osmium::index::map::SparseMemMap, sparse_mem_map)
#endif
#ifdef OSMIUM_HAS_INDEX_MAP_SPARSE_MEM_TABLE
REGISTER_MAP(osmium::unsigned_object_id_type, osmium::Location, osmium::index::map::SparseMemTable, sparse_mem_table)
#endif
#ifdef OSMIUM_HAS_INDEX_MAP_SPARSE_MMAP_ARRAY
REGISTER_MAP(osmium::unsigned_object_id_type, osmium::Location, osmium::index::map::SparseMmapArray, sparse_mmap_array)
#endif
#ifdef OSMIUM_HAS_INDEX_MAP_FLEX_MEM
REGISTER_MAP(osmium::unsigned_object_id_type, osmium::Location, osmium::index::map::FlexMem, flex_mem)
#endif
#endif // OSMIUM_INDEX_NODE_LOCATIONS_MAP_HPP
+122
View File
@@ -0,0 +1,122 @@
#ifndef OSMIUM_INDEX_NWR_ARRAY_HPP
#define OSMIUM_INDEX_NWR_ARRAY_HPP
/*
This file is part of Osmium (https://osmcode.org/libosmium).
Copyright 2013-2023 Jochen Topf <jochen@topf.org> and others (see README).
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <osmium/osm/item_type.hpp>
#include <array>
namespace osmium {
/**
* Often some information must be kept separately for nodes, ways, and
* relations. Then this helper class becomes useful. It can keep three
* somethings (of type T) which can be accessed conveniently through
* the call operator.
*/
template <typename T>
class nwr_array {
std::array<T, 3> m_data;
public:
using iterator = typename std::array<T, 3>::iterator;
using const_iterator = typename std::array<T, 3>::const_iterator;
nwr_array() :
m_data() {
}
T& operator()(const osmium::item_type type) noexcept {
return m_data[osmium::item_type_to_nwr_index(type)];
}
const T& operator()(const osmium::item_type type) const noexcept {
return m_data[osmium::item_type_to_nwr_index(type)];
}
T& nodes() noexcept {
return m_data[0];
}
const T& nodes() const noexcept {
return m_data[0];
}
T& ways() noexcept {
return m_data[1];
}
const T& ways() const noexcept {
return m_data[1];
}
T& relations() noexcept {
return m_data[2];
}
const T& relations() const noexcept {
return m_data[2];
}
iterator begin() noexcept {
return m_data.begin();
}
iterator end() noexcept {
return m_data.end();
}
const_iterator begin() const noexcept {
return m_data.cbegin();
}
const_iterator end() const noexcept {
return m_data.cend();
}
const_iterator cbegin() const noexcept {
return m_data.cbegin();
}
const_iterator cend() const noexcept {
return m_data.cend();
}
}; // class nwr_array
} // namespace osmium
#endif // OSMIUM_INDEX_NWR_ARRAY_HPP
@@ -0,0 +1,452 @@
#ifndef OSMIUM_INDEX_RELATIONS_MAP_HPP
#define OSMIUM_INDEX_RELATIONS_MAP_HPP
/*
This file is part of Osmium (https://osmcode.org/libosmium).
Copyright 2013-2023 Jochen Topf <jochen@topf.org> and others (see README).
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <osmium/osm/item_type.hpp>
#include <osmium/osm/relation.hpp>
#include <osmium/osm/types.hpp>
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <tuple>
#include <type_traits>
#include <utility>
#include <vector>
namespace osmium {
namespace index {
namespace detail {
template <typename TKey, typename TKeyInternal, typename TValue, typename TValueInternal>
class flat_map {
public:
using key_type = TKey;
using value_type = TValue;
private:
struct kv_pair {
TKeyInternal key;
TValueInternal value;
explicit kv_pair(const key_type key_id) :
key(static_cast<TKeyInternal>(key_id)),
value() {
}
kv_pair(const key_type key_id, const value_type value_id) :
key(static_cast<TKeyInternal>(key_id)),
value(static_cast<TValueInternal>(value_id)) {
}
bool operator<(const kv_pair& other) const noexcept {
return std::tie(key, value) < std::tie(other.key, other.value);
}
bool operator==(const kv_pair& other) const noexcept {
return std::tie(key, value) == std::tie(other.key, other.value);
}
}; // struct kv_pair
std::vector<kv_pair> m_map;
public:
using const_iterator = typename std::vector<kv_pair>::const_iterator;
void set(const key_type key, const value_type value) {
m_map.emplace_back(key, value);
}
typename std::enable_if<std::is_same<TKey, TValue>::value>::type flip_in_place() {
for (auto& p : m_map) {
using std::swap;
swap(p.key, p.value);
}
}
flat_map<TValue, TValueInternal, TKey, TKeyInternal> flip_copy() {
flat_map<TValue, TValueInternal, TKey, TKeyInternal> map;
map.reserve(m_map.size());
for (const auto& p : m_map) {
map.set(p.value, p.key);
}
return map;
}
void sort_unique() {
std::sort(m_map.begin(), m_map.end());
const auto last = std::unique(m_map.begin(), m_map.end());
m_map.erase(last, m_map.end());
}
std::pair<const_iterator, const_iterator> get(const key_type key) const noexcept {
return std::equal_range(m_map.begin(), m_map.end(), kv_pair{key}, [](const kv_pair& lhs, const kv_pair& rhs) {
return lhs.key < rhs.key;
});
}
bool empty() const noexcept {
return m_map.empty();
}
std::size_t size() const noexcept {
return m_map.size();
}
void reserve(const std::size_t size) {
m_map.reserve(size);
}
}; // class flat_map
} // namespace detail
/**
* Index for looking up parent relation IDs given a member relation ID
* or the other way around.
*
* You can not instantiate such an index yourself, instead you need to
* instantiate a RelationsMapStash, fill it and then create an index
* from it:
*
* @code
* RelationsMapStash stash;
* ...
* for_each_relation(const osmium::Relation& relation) {
* stash.add_members(relation);
* }
* ...
* const auto index = stash.build_member_to_parent_index();
* ...
* osmium::unsigned_object_id_type member_id = ...;
* index.for_each(member_id, [](osmium::unsigned_object_id_type parent_id) {
* ...
* });
* ...
* @endcode
*
*/
class RelationsMapIndex {
friend class RelationsMapStash;
friend class RelationsMapIndexes;
using map_type = detail::flat_map<osmium::unsigned_object_id_type, uint32_t,
osmium::unsigned_object_id_type, uint32_t>;
map_type m_map;
explicit RelationsMapIndex(map_type&& map) :
m_map(std::move(map)) {
}
public:
RelationsMapIndex() = delete;
RelationsMapIndex(const RelationsMapIndex&) = delete;
RelationsMapIndex& operator=(const RelationsMapIndex&) = delete;
RelationsMapIndex(RelationsMapIndex&& /*other*/) noexcept(std::is_nothrow_move_constructible<map_type>::value);
RelationsMapIndex& operator=(RelationsMapIndex&& /*other*/) noexcept(std::is_nothrow_move_assignable<map_type>::value);
~RelationsMapIndex() noexcept = default;
/**
* Find the given relation id in the index and call the given
* function with all parent relation ids.
*
* @code
* osmium::unsigned_object_id_type member_id = 17;
* index.for_each_parent(member_id, [](osmium::unsigned_object_id_type id) {
* ...
* });
* @endcode
*
* @deprecated Use for_each() instead.
*
* Complexity: Logarithmic in the number of elements in the index.
* (Lookup uses binary search.)
*/
template <typename TFunc>
void for_each_parent(const osmium::unsigned_object_id_type member_id, TFunc&& func) const {
const auto parents = m_map.get(member_id);
for (auto it = parents.first; it != parents.second; ++it) {
func(it->value);
}
}
/**
* Find the given relation id in the index and call the given
* function with all related relation ids.
*
* @code
* osmium::unsigned_object_id_type id = 17;
* index.for_each(id, [](osmium::unsigned_object_id_type rid) {
* ...
* });
* @endcode
*
* Complexity: Logarithmic in the number of elements in the index.
* (Lookup uses binary search.)
*/
template <typename TFunc>
void for_each(const osmium::unsigned_object_id_type id, TFunc&& func) const {
const auto parents = m_map.get(id);
for (auto it = parents.first; it != parents.second; ++it) {
func(it->value);
}
}
/**
* Is this index empty?
*
* Complexity: Constant.
*/
bool empty() const noexcept {
return m_map.empty();
}
/**
* How many entries are in this index?
*
* Complexity: Constant.
*/
std::size_t size() const noexcept {
return m_map.size();
}
}; // class RelationsMapIndex
// defined outside the class on purpose
// see https://akrzemi1.wordpress.com/2015/09/11/declaring-the-move-constructor/
inline RelationsMapIndex::RelationsMapIndex(RelationsMapIndex&&) noexcept(std::is_nothrow_move_constructible<map_type>::value) = default;
inline RelationsMapIndex& RelationsMapIndex::operator=(RelationsMapIndex&&) noexcept(std::is_nothrow_move_assignable<map_type>::value) = default;
class RelationsMapIndexes {
friend class RelationsMapStash;
RelationsMapIndex m_member_to_parent;
RelationsMapIndex m_parent_to_member;
RelationsMapIndexes(RelationsMapIndex::map_type&& map1, RelationsMapIndex::map_type&& map2) :
m_member_to_parent(std::move(map1)),
m_parent_to_member(std::move(map2)) {
}
public:
const RelationsMapIndex& member_to_parent() const noexcept {
return m_member_to_parent;
}
const RelationsMapIndex& parent_to_member() const noexcept {
return m_parent_to_member;
}
/**
* Is this index empty?
*
* Complexity: Constant.
*/
bool empty() const noexcept {
return m_member_to_parent.empty();
}
/**
* How many entries are in this index?
*
* Complexity: Constant.
*/
std::size_t size() const noexcept {
return m_member_to_parent.size();
}
}; // class RelationsMapIndexes
/**
* The RelationsMapStash is used to build up the data needed to create
* an index of member relation ID to parent relation ID or the other
* way around. See the RelationsMapIndex class for more.
*/
class RelationsMapStash {
using map_type = detail::flat_map<osmium::unsigned_object_id_type, uint32_t,
osmium::unsigned_object_id_type, uint32_t>;
map_type m_map;
#ifndef NDEBUG
bool m_valid = true;
#endif
public:
RelationsMapStash() = default;
RelationsMapStash(const RelationsMapStash&) = delete;
RelationsMapStash& operator=(const RelationsMapStash&) = delete;
RelationsMapStash(RelationsMapStash&& /*other*/) noexcept(std::is_nothrow_move_constructible<map_type>::value);
RelationsMapStash& operator=(RelationsMapStash&& /*other*/) noexcept(std::is_nothrow_move_assignable<map_type>::value);
~RelationsMapStash() noexcept = default;
/**
* Add mapping from member to parent relation in the stash.
*/
void add(const osmium::unsigned_object_id_type member_id, const osmium::unsigned_object_id_type relation_id) {
assert(m_valid && "You can't use the RelationsMap any more after calling build_index()");
m_map.set(member_id, relation_id);
}
/**
* Add mapping from all members to given parent relation in the stash.
*/
void add_members(const osmium::Relation& relation) {
assert(m_valid && "You can't use the RelationsMap any more after calling build_index()");
for (const auto& member : relation.members()) {
if (member.type() == osmium::item_type::relation) {
m_map.set(member.positive_ref(), relation.positive_id());
}
}
}
/**
* Is this stash empty?
*
* Complexity: Constant.
*/
bool empty() const noexcept {
assert(m_valid && "You can't use the RelationsMap any more after calling build_index()");
return m_map.empty();
}
/**
* How many entries are in this stash?
*
* Complexity: Constant.
*/
std::size_t size() const noexcept {
assert(m_valid && "You can't use the RelationsMap any more after calling build_index()");
return m_map.size();
}
/**
* Build an index for member to parent lookups from the contents
* of this stash and return it.
*
* After you get the index you can not use the stash any more!
*
* @deprecated Use build_member_to_parent_index() instead.
*/
RelationsMapIndex build_index() {
assert(m_valid && "You can't use the RelationsMap any more after calling build_index()");
m_map.sort_unique();
#ifndef NDEBUG
m_valid = false;
#endif
return RelationsMapIndex{std::move(m_map)};
}
/**
* Build an index for member to parent lookups from the contents
* of this stash and return it.
*
* After you get the index you can not use the stash any more!
*/
RelationsMapIndex build_member_to_parent_index() {
assert(m_valid && "You can't use the RelationsMap any more after calling build_member_to_parent_index()");
m_map.sort_unique();
#ifndef NDEBUG
m_valid = false;
#endif
return RelationsMapIndex{std::move(m_map)};
}
/**
* Build an index for parent to member lookups from the contents
* of this stash and return it.
*
* After you get the index you can not use the stash any more!
*/
RelationsMapIndex build_parent_to_member_index() {
assert(m_valid && "You can't use the RelationsMap any more after calling build_parent_to_member_index()");
m_map.flip_in_place();
m_map.sort_unique();
#ifndef NDEBUG
m_valid = false;
#endif
return RelationsMapIndex{std::move(m_map)};
}
/**
* Build indexes for member-to-parent and parent-to-member lookups
* from the contents of this stash and return them.
*
* After you get the index you can not use the stash any more!
*/
RelationsMapIndexes build_indexes() {
assert(m_valid && "You can't use the RelationsMap any more after calling build_indexes()");
auto reverse_map = m_map.flip_copy();
reverse_map.sort_unique();
m_map.sort_unique();
#ifndef NDEBUG
m_valid = false;
#endif
return RelationsMapIndexes{std::move(m_map), std::move(reverse_map)};
}
}; // class RelationsMapStash
// defined outside the class on purpose
// see https://akrzemi1.wordpress.com/2015/09/11/declaring-the-move-constructor/
inline RelationsMapStash::RelationsMapStash(RelationsMapStash&&) noexcept(std::is_nothrow_move_constructible<map_type>::value) = default;
inline RelationsMapStash& RelationsMapStash::operator=(RelationsMapStash&&) noexcept(std::is_nothrow_move_assignable<map_type>::value) = default;
} // namespace index
} // namespace osmium
#endif // OSMIUM_INDEX_RELATIONS_MAP_HPP