First Commit
This commit is contained in:
commit
8a6c7106e3
1312 files changed
+425396
No files matched your search
@@ -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
|
||||
Reference in new issue
Block a user