Skip to content

Commit 06f3ed5

Browse files
authored
Merge pull request #2151 from joto/refactor-idlist
Refactor idlist
2 parents 96950d4 + bde21d8 commit 06f3ed5

18 files changed

+228
-117
lines changed

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ target_sources(osm2pgsql_lib PRIVATE
1919
geom-from-osm.cpp
2020
geom-functions.cpp
2121
geom-pole-of-inaccessibility.cpp
22+
idlist.cpp
2223
input.cpp
2324
logging.cpp
2425
middle.cpp

src/dependency-manager.cpp

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@
1515

1616
void full_dependency_manager_t::node_changed(osmid_t id)
1717
{
18-
m_changed_nodes.set(id);
18+
m_changed_nodes.push_back(id);
1919
}
2020

2121
void full_dependency_manager_t::way_changed(osmid_t id)
2222
{
23-
m_changed_ways.set(id);
23+
m_changed_ways.push_back(id);
2424
}
2525

2626
void full_dependency_manager_t::relation_changed(osmid_t id)
2727
{
28-
m_changed_relations.set(id);
28+
m_changed_relations.push_back(id);
2929
}
3030

3131
void full_dependency_manager_t::after_nodes()
@@ -39,29 +39,13 @@ void full_dependency_manager_t::after_nodes()
3939
m_changed_nodes.clear();
4040
}
4141

42-
static osmium::index::IdSetSmall<osmid_t>
43-
set_diff(osmium::index::IdSetSmall<osmid_t> const &set,
44-
osmium::index::IdSetSmall<osmid_t> const &to_be_removed)
45-
{
46-
osmium::index::IdSetSmall<osmid_t> new_set;
47-
48-
for (auto const id : set) {
49-
if (!to_be_removed.get_binary_search(id)) {
50-
new_set.set(id);
51-
}
52-
}
53-
54-
return new_set;
55-
}
56-
5742
void full_dependency_manager_t::after_ways()
5843
{
5944
if (!m_changed_ways.empty()) {
6045
if (!m_ways_pending_tracker.empty()) {
6146
// Remove ids from changed ways in the input data from
6247
// m_ways_pending_tracker, because they have already been processed.
63-
m_ways_pending_tracker =
64-
set_diff(m_ways_pending_tracker, m_changed_ways);
48+
m_ways_pending_tracker.remove_ids_if_in(m_changed_ways);
6549

6650
// Add the list of pending way ids to the list of changed ways,
6751
// because we need the parents for them, too.
@@ -85,14 +69,13 @@ void full_dependency_manager_t::after_relations()
8569
{
8670
// Remove ids from changed relations in the input data from
8771
// m_rels_pending_tracker, because they have already been processed.
88-
m_rels_pending_tracker =
89-
set_diff(m_rels_pending_tracker, m_changed_relations);
72+
m_rels_pending_tracker.remove_ids_if_in(m_changed_relations);
9073

9174
m_changed_relations.clear();
9275
}
9376

9477
void full_dependency_manager_t::mark_parent_relations_as_pending(
95-
osmium::index::IdSetSmall<osmid_t> const &way_ids)
78+
idlist_t const &way_ids)
9679
{
9780
assert(m_rels_pending_tracker.empty());
9881
m_object_store->get_way_parents(way_ids, &m_rels_pending_tracker);
@@ -102,18 +85,3 @@ bool full_dependency_manager_t::has_pending() const noexcept
10285
{
10386
return !m_ways_pending_tracker.empty() || !m_rels_pending_tracker.empty();
10487
}
105-
106-
idlist_t
107-
full_dependency_manager_t::get_ids(osmium::index::IdSetSmall<osmid_t> *tracker)
108-
{
109-
tracker->sort_unique();
110-
111-
idlist_t list;
112-
list.reserve(tracker->size());
113-
114-
std::copy(tracker->cbegin(), tracker->cend(), std::back_inserter(list));
115-
116-
tracker->clear();
117-
118-
return list;
119-
}

src/dependency-manager.hpp

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@
1010
* For a full list of authors see the git log.
1111
*/
1212

13+
#include "idlist.hpp"
1314
#include "osmtypes.hpp"
1415

15-
#include <osmium/index/id_set.hpp>
16-
1716
#include <cassert>
1817
#include <memory>
1918
#include <utility>
@@ -58,8 +57,7 @@ class dependency_manager_t
5857
virtual void after_ways() {}
5958
virtual void after_relations() {}
6059

61-
virtual void mark_parent_relations_as_pending(
62-
osmium::index::IdSetSmall<osmid_t> const & /*way_ids*/)
60+
virtual void mark_parent_relations_as_pending(idlist_t const & /*way_ids*/)
6361
{
6462
}
6563

@@ -111,24 +109,29 @@ class full_dependency_manager_t : public dependency_manager_t
111109
void after_ways() override;
112110
void after_relations() override;
113111

114-
void mark_parent_relations_as_pending(
115-
osmium::index::IdSetSmall<osmid_t> const &ids) override;
112+
void mark_parent_relations_as_pending(idlist_t const &ids) override;
116113

117114
bool has_pending() const noexcept override;
118115

119116
idlist_t get_pending_way_ids() override
120117
{
121-
return get_ids(&m_ways_pending_tracker);
118+
idlist_t list;
119+
using std::swap;
120+
swap(list, m_ways_pending_tracker);
121+
list.sort_unique();
122+
return list;
122123
}
123124

124125
idlist_t get_pending_relation_ids() override
125126
{
126-
return get_ids(&m_rels_pending_tracker);
127+
idlist_t list;
128+
using std::swap;
129+
swap(list, m_rels_pending_tracker);
130+
list.sort_unique();
131+
return list;
127132
}
128133

129134
private:
130-
static idlist_t get_ids(osmium::index::IdSetSmall<osmid_t> *tracker);
131-
132135
std::shared_ptr<middle_t> m_object_store;
133136

134137
/**
@@ -139,7 +142,7 @@ class full_dependency_manager_t : public dependency_manager_t
139142
* the change file, too, and so we don't have to find out which ones they
140143
* are.
141144
*/
142-
osmium::index::IdSetSmall<osmid_t> m_changed_nodes;
145+
idlist_t m_changed_nodes;
143146

144147
/**
145148
* In append mode all new and changed ways will be added to this. After
@@ -148,17 +151,17 @@ class full_dependency_manager_t : public dependency_manager_t
148151
* relations that referenced deleted ways must be in the change file, too,
149152
* and so we don't have to find out which ones they are.
150153
*/
151-
osmium::index::IdSetSmall<osmid_t> m_changed_ways;
154+
idlist_t m_changed_ways;
152155

153156
/**
154157
* In append mode all new and changed relations will be added to this.
155158
* This is then used to remove already processed relations from the
156159
* pending list.
157160
*/
158-
osmium::index::IdSetSmall<osmid_t> m_changed_relations;
161+
idlist_t m_changed_relations;
159162

160-
osmium::index::IdSetSmall<osmid_t> m_ways_pending_tracker;
161-
osmium::index::IdSetSmall<osmid_t> m_rels_pending_tracker;
163+
idlist_t m_ways_pending_tracker;
164+
idlist_t m_rels_pending_tracker;
162165
};
163166

164167
#endif // OSM2PGSQL_DEPENDENCY_MANAGER_HPP

src/idlist.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* SPDX-License-Identifier: GPL-2.0-or-later
3+
*
4+
* This file is part of osm2pgsql (https://osm2pgsql.org/).
5+
*
6+
* Copyright (C) 2006-2023 by the osm2pgsql developer community.
7+
* For a full list of authors see the git log.
8+
*/
9+
10+
#include "idlist.hpp"
11+
12+
#include <algorithm>
13+
#include <cassert>
14+
#include <iterator>
15+
#include <utility>
16+
17+
osmid_t idlist_t::pop_id()
18+
{
19+
assert(!m_list.empty());
20+
auto const id = m_list.back();
21+
m_list.pop_back();
22+
return id;
23+
}
24+
25+
void idlist_t::sort_unique()
26+
{
27+
std::sort(m_list.begin(), m_list.end());
28+
const auto last = std::unique(m_list.begin(), m_list.end());
29+
m_list.erase(last, m_list.end());
30+
}
31+
32+
void idlist_t::merge_sorted(idlist_t const &other)
33+
{
34+
std::vector<osmid_t> new_list;
35+
36+
new_list.reserve(m_list.size() + other.m_list.size());
37+
std::set_union(m_list.cbegin(), m_list.cend(), other.m_list.cbegin(),
38+
other.m_list.cend(), std::back_inserter(new_list));
39+
40+
using std::swap;
41+
swap(new_list, m_list);
42+
}
43+
44+
void idlist_t::remove_ids_if_in(idlist_t const &other)
45+
{
46+
std::vector<osmid_t> new_list;
47+
48+
new_list.reserve(m_list.size());
49+
std::set_difference(m_list.cbegin(), m_list.cend(), other.m_list.cbegin(),
50+
other.m_list.cend(), std::back_inserter(new_list));
51+
52+
using std::swap;
53+
swap(new_list, m_list);
54+
}

src/idlist.hpp

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#ifndef OSM2PGSQL_IDLIST_HPP
2+
#define OSM2PGSQL_IDLIST_HPP
3+
4+
/**
5+
* SPDX-License-Identifier: GPL-2.0-or-later
6+
*
7+
* This file is part of osm2pgsql (https://osm2pgsql.org/).
8+
*
9+
* Copyright (C) 2006-2023 by the osm2pgsql developer community.
10+
* For a full list of authors see the git log.
11+
*/
12+
13+
/**
14+
* \file
15+
*
16+
* This file contains the definition of the idlist_t class.
17+
*/
18+
19+
#include "osmtypes.hpp"
20+
21+
#include <vector>
22+
23+
/**
24+
* A list of OSM object ids. Internally this is a vector of ids.
25+
*
26+
* Some operations are only allowed when the list of ids is sorted and
27+
* without duplicates. Call sort_unique() to achieve this.
28+
*/
29+
class idlist_t
30+
{
31+
public:
32+
using value_type = osmid_t;
33+
34+
idlist_t() = default;
35+
~idlist_t() noexcept = default;
36+
37+
idlist_t(std::initializer_list<osmid_t> ids) : m_list(ids) {}
38+
39+
idlist_t(idlist_t const &) = delete;
40+
idlist_t &operator=(idlist_t const &) = delete;
41+
42+
idlist_t(idlist_t &&) = default;
43+
idlist_t &operator=(idlist_t &&) = default;
44+
45+
bool empty() const noexcept { return m_list.empty(); }
46+
47+
std::size_t size() const noexcept { return m_list.size(); }
48+
49+
auto begin() const noexcept { return m_list.begin(); }
50+
51+
auto end() const noexcept { return m_list.end(); }
52+
53+
auto cbegin() const noexcept { return m_list.cbegin(); }
54+
55+
auto cend() const noexcept { return m_list.cend(); }
56+
57+
osmid_t operator[](std::size_t n) const noexcept { return m_list[n]; }
58+
59+
void clear() noexcept { m_list.clear(); }
60+
61+
void push_back(osmid_t id) { m_list.push_back(id); }
62+
63+
void reserve(std::size_t size) { m_list.reserve(size); }
64+
65+
/**
66+
* Remove id at the end of the list and return it.
67+
*
68+
* \pre \code !m_list.empty()) \endcode
69+
*/
70+
osmid_t pop_id();
71+
72+
/// List are equal if they contain the same ids in the same order.
73+
friend bool operator==(idlist_t const &lhs, idlist_t const &rhs) noexcept
74+
{
75+
return lhs.m_list == rhs.m_list;
76+
}
77+
78+
friend bool operator!=(idlist_t const &lhs, idlist_t const &rhs) noexcept
79+
{
80+
return !(lhs == rhs);
81+
}
82+
83+
/**
84+
* Sort this list and remove duplicates.
85+
*/
86+
void sort_unique();
87+
88+
/**
89+
* Merge other list into this one.
90+
*
91+
* \pre Both lists must be sorted and without duplicates.
92+
*/
93+
void merge_sorted(idlist_t const &other);
94+
95+
/**
96+
* Remove all ids in this list that are also in the other list.
97+
*
98+
* \pre Both lists must be sorted and without duplicates.
99+
*/
100+
void remove_ids_if_in(idlist_t const &other);
101+
102+
private:
103+
std::vector<osmid_t> m_list;
104+
105+
}; // class idlist_t
106+
107+
#endif // OSM2PGSQL_IDLIST_HPP

0 commit comments

Comments
 (0)