1 #ifndef OSMIUM_INDEX_RELATIONS_MAP_HPP 2 #define OSMIUM_INDEX_RELATIONS_MAP_HPP 51 template <
typename TKey,
typename TKeyInternal,
typename TValue,
typename TValueInternal>
56 using key_type = TKey;
57 using value_type = TValue;
65 explicit kv_pair(key_type key_id) :
66 key(static_cast<TKeyInternal>(key_id)),
70 kv_pair(key_type key_id, value_type value_id) :
71 key(static_cast<TKeyInternal>(key_id)),
72 value(static_cast<TValueInternal>(value_id)) {
75 bool operator<(
const kv_pair& other)
const noexcept {
76 return std::tie(key, value) < std::tie(other.key, other.value);
79 bool operator==(
const kv_pair& other)
const noexcept {
80 return std::tie(key, value) == std::tie(other.key, other.value);
84 std::vector<kv_pair> m_map;
88 using const_iterator =
typename std::vector<kv_pair>::const_iterator;
90 void set(key_type key, value_type value) {
91 m_map.emplace_back(key, value);
94 typename std::enable_if<std::is_same<TKey, TValue>::value>
::type flip_in_place() {
95 for (
auto& p : m_map) {
101 flat_map<TValue, TValueInternal, TKey, TKeyInternal> flip_copy() {
102 flat_map<TValue, TValueInternal, TKey, TKeyInternal> map;
103 map.reserve(m_map.size());
105 for (
const auto& p : m_map) {
106 map.set(p.value, p.key);
113 std::sort(m_map.begin(), m_map.end());
114 const auto last = std::unique(m_map.begin(), m_map.end());
115 m_map.erase(last, m_map.end());
118 std::pair<const_iterator, const_iterator>
get(key_type key)
const noexcept {
119 return std::equal_range(m_map.begin(), m_map.end(), kv_pair{key}, [](
const kv_pair& lhs,
const kv_pair& rhs) {
120 return lhs.key < rhs.key;
124 bool empty() const noexcept {
125 return m_map.empty();
128 size_t size() const noexcept {
132 void reserve(
size_t size) {
176 m_map(
std::move(map)) {
205 template <
typename TFunc>
207 const auto parents = m_map.get(member_id);
208 for (
auto it = parents.first; it != parents.second; ++it) {
209 std::forward<TFunc>(func)(it->value);
227 template <
typename TFunc>
228 void for_each(osmium::unsigned_object_id_type
id, TFunc&& func)
const {
229 const auto parents = m_map.get(
id);
230 for (
auto it = parents.first; it != parents.second; ++it) {
231 std::forward<TFunc>(func)(it->value);
241 return m_map.empty();
263 m_member_to_parent(
std::move(map1)),
264 m_parent_to_member(
std::move(map2)) {
270 return m_member_to_parent;
274 return m_parent_to_member;
283 return m_member_to_parent.
empty();
292 return m_member_to_parent.
size();
326 void add(osmium::unsigned_object_id_type member_id, osmium::unsigned_object_id_type relation_id) {
327 assert(m_valid &&
"You can't use the RelationsMap any more after calling build_index()");
328 m_map.set(member_id, relation_id);
335 assert(m_valid &&
"You can't use the RelationsMap any more after calling build_index()");
336 for (
const auto& member : relation.
members()) {
338 m_map.set(member.positive_ref(), relation.
positive_id());
349 assert(m_valid &&
"You can't use the RelationsMap any more after calling build_index()");
350 return m_map.empty();
359 assert(m_valid &&
"You can't use the RelationsMap any more after calling build_index()");
372 assert(m_valid &&
"You can't use the RelationsMap any more after calling build_index()");
387 assert(m_valid &&
"You can't use the RelationsMap any more after calling build_member_to_parent_index()");
402 assert(m_valid &&
"You can't use the RelationsMap any more after calling build_parent_to_member_index()");
403 m_map.flip_in_place();
418 assert(m_valid &&
"You can't use the RelationsMap any more after calling build_indexes()");
419 auto reverse_map = m_map.flip_copy();
420 reverse_map.sort_unique();
434 #endif // OSMIUM_INDEX_RELATIONS_MAP_HPP type
Definition: entity_bits.hpp:63
RelationMemberList & members()
Definition: relation.hpp:185
const RelationsMapIndex & parent_to_member() const noexcept
Definition: relations_map.hpp:273
constexpr bool operator==(const Box &lhs, const Box &rhs) noexcept
Definition: box.hpp:221
Definition: relation.hpp:168
RelationsMapIndex build_index()
Definition: relations_map.hpp:371
uint64_t unsigned_object_id_type
Type for OSM object (node, way, or relation) IDs where we only allow positive IDs.
Definition: types.hpp:46
void for_each_parent(osmium::unsigned_object_id_type member_id, TFunc &&func) const
Definition: relations_map.hpp:206
Definition: reader_iterator.hpp:39
void swap(Buffer &lhs, Buffer &rhs)
Definition: buffer.hpp:765
Definition: relations_map.hpp:165
RelationsMapIndexes(RelationsMapIndex::map_type &&map1, RelationsMapIndex::map_type &&map2)
Definition: relations_map.hpp:262
Definition: relations_map.hpp:255
RelationsMapIndex build_parent_to_member_index()
Definition: relations_map.hpp:401
size_t size() const noexcept
Definition: relations_map.hpp:358
bool empty() const noexcept
Definition: relations_map.hpp:240
detail::flat_map< osmium::unsigned_object_id_type, uint32_t, osmium::unsigned_object_id_type, uint32_t > map_type
Definition: relations_map.hpp:171
RelationsMapIndexes build_indexes()
Definition: relations_map.hpp:417
RelationsMapIndex(map_type &&map)
Definition: relations_map.hpp:175
bool operator<(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:447
Namespace for everything in the Osmium library.
Definition: assembler.hpp:63
RelationsMapIndex m_member_to_parent
Definition: relations_map.hpp:259
void add(osmium::unsigned_object_id_type member_id, osmium::unsigned_object_id_type relation_id)
Definition: relations_map.hpp:326
Definition: relations_map.hpp:302
const RelationsMapIndex & member_to_parent() const noexcept
Definition: relations_map.hpp:269
bool empty() const noexcept
Definition: relations_map.hpp:282
map_type m_map
Definition: relations_map.hpp:173
size_t size() const noexcept
Definition: relations_map.hpp:291
void add_members(const osmium::Relation &relation)
Definition: relations_map.hpp:334
detail::flat_map< osmium::unsigned_object_id_type, uint32_t, osmium::unsigned_object_id_type, uint32_t > map_type
Definition: relations_map.hpp:305
RelationsMapIndex build_member_to_parent_index()
Definition: relations_map.hpp:386
void for_each(osmium::unsigned_object_id_type id, TFunc &&func) const
Definition: relations_map.hpp:228
map_type m_map
Definition: relations_map.hpp:307
size_t size() const noexcept
Definition: relations_map.hpp:249
unsigned_object_id_type positive_id() const noexcept
Get absolute value of the ID of this object.
Definition: object.hpp:131
RelationsMapIndex m_parent_to_member
Definition: relations_map.hpp:260
bool empty() const noexcept
Definition: relations_map.hpp:348