Skip to content

Commit 4472dfb

Browse files
committed
Round out ObjectPointerCollection implementation and test it.
1 parent 28cb35d commit 4472dfb

File tree

4 files changed

+123
-8
lines changed

4 files changed

+123
-8
lines changed

include/osmium/object_pointer_collection.hpp

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,24 @@ namespace osmium {
6161
* osmium::memory::Buffer buffer = reader.read();
6262
* osmium::apply(buffer, objects);
6363
*
64+
* It is not possible to remove pointers from the collection except by
65+
* clearing the whole collection.
66+
*
6467
*/
6568
class ObjectPointerCollection : public osmium::handler::Handler {
6669

67-
std::vector<osmium::OSMObject*> m_objects;
70+
std::vector<osmium::OSMObject*> m_objects{};
6871

6972
public:
7073

7174
using iterator = boost::indirect_iterator<std::vector<osmium::OSMObject*>::iterator, osmium::OSMObject>;
7275
using const_iterator = boost::indirect_iterator<std::vector<osmium::OSMObject*>::const_iterator, const osmium::OSMObject>;
7376

74-
ObjectPointerCollection() noexcept :
75-
m_objects() {
76-
}
77+
ObjectPointerCollection() noexcept = default;
7778

79+
/**
80+
* Add a pointer to an object to the collection.
81+
*/
7882
void osm_object(osmium::OSMObject& object) {
7983
m_objects.push_back(&object);
8084
}
@@ -87,20 +91,35 @@ namespace osmium {
8791
std::sort(m_objects.begin(), m_objects.end(), std::forward<TCompare>(compare));
8892
}
8993

94+
/// Is the collection empty?
95+
bool empty() const noexcept {
96+
return m_objects.empty();
97+
}
98+
99+
/// Return size of the collection.
100+
size_t size() const noexcept {
101+
return m_objects.size();
102+
}
103+
104+
/// Clear the collection,
105+
void clear() {
106+
m_objects.clear();
107+
}
108+
90109
iterator begin() {
91-
return iterator { m_objects.begin() };
110+
return iterator{m_objects.begin()};
92111
}
93112

94113
iterator end() {
95-
return iterator { m_objects.end() };
114+
return iterator{m_objects.end()};
96115
}
97116

98117
const_iterator cbegin() const {
99-
return const_iterator { m_objects.cbegin() };
118+
return const_iterator{m_objects.cbegin()};
100119
}
101120

102121
const_iterator cend() const {
103-
return const_iterator { m_objects.cend() };
122+
return const_iterator{m_objects.cend()};
104123
}
105124

106125
}; // class ObjectPointerCollection

include/osmium/osm/object_comparisons.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ DEALINGS IN THE SOFTWARE.
3333
3434
*/
3535

36+
#include <cassert>
3637
#include <tuple>
3738

3839
#include <osmium/osm/object.hpp>
@@ -51,7 +52,9 @@ namespace osmium {
5152
return lhs == rhs;
5253
}
5354

55+
/// @pre lhs and rhs must not be nullptr
5456
bool operator()(const osmium::OSMObject* lhs, const osmium::OSMObject* rhs) const noexcept {
57+
assert(lhs && rhs);
5558
return *lhs == *rhs;
5659
}
5760

@@ -68,7 +71,9 @@ namespace osmium {
6871
lhs.id() == rhs.id();
6972
}
7073

74+
/// @pre lhs and rhs must not be nullptr
7175
bool operator()(const osmium::OSMObject* lhs, const osmium::OSMObject* rhs) const noexcept {
76+
assert(lhs && rhs);
7277
return operator()(*lhs, *rhs);
7378
}
7479

@@ -84,7 +89,9 @@ namespace osmium {
8489
return lhs < rhs;
8590
}
8691

92+
/// @pre lhs and rhs must not be nullptr
8793
bool operator()(const osmium::OSMObject* lhs, const osmium::OSMObject* rhs) const noexcept {
94+
assert(lhs && rhs);
8895
return *lhs < *rhs;
8996
}
9097

@@ -104,7 +111,9 @@ namespace osmium {
104111
const_tie(rhs.type(), rhs.id() < 0, rhs.positive_id(), lhs.version(), lhs.timestamp());
105112
}
106113

114+
/// @pre lhs and rhs must not be nullptr
107115
bool operator()(const osmium::OSMObject* lhs, const osmium::OSMObject* rhs) const noexcept {
116+
assert(lhs && rhs);
108117
return operator()(*lhs, *rhs);
109118
}
110119

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ add_unit_test(geom test_wkt)
158158
add_unit_test(index test_id_set)
159159
add_unit_test(index test_id_to_location ENABLE_IF ${SPARSEHASH_FOUND})
160160
add_unit_test(index test_file_based_index)
161+
add_unit_test(index test_object_pointer_collection)
161162

162163
add_unit_test(io test_compression_factory)
163164
add_unit_test(io test_bzip2 ENABLE_IF ${BZIP2_FOUND} LIBS ${BZIP2_LIBRARIES})
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
2+
#include "catch.hpp"
3+
4+
#include <osmium/builder/attr.hpp>
5+
#include <osmium/memory/buffer.hpp>
6+
#include <osmium/object_pointer_collection.hpp>
7+
#include <osmium/osm/object_comparisons.hpp>
8+
#include <osmium/visitor.hpp>
9+
10+
using namespace osmium::builder::attr;
11+
12+
TEST_CASE("Create ObjectPointerCollection") {
13+
osmium::memory::Buffer buffer{1024, osmium::memory::Buffer::auto_grow::yes};
14+
15+
osmium::builder::add_node(buffer,
16+
_id(3),
17+
_version(3)
18+
);
19+
20+
osmium::builder::add_node(buffer,
21+
_id(1),
22+
_version(2)
23+
);
24+
25+
osmium::builder::add_node(buffer,
26+
_id(1),
27+
_version(4)
28+
);
29+
30+
osmium::ObjectPointerCollection collection;
31+
REQUIRE(collection.empty());
32+
REQUIRE(collection.size() == 0);
33+
34+
osmium::apply(buffer, collection);
35+
36+
REQUIRE_FALSE(collection.empty());
37+
REQUIRE(collection.size() == 3);
38+
39+
auto it = collection.cbegin();
40+
REQUIRE(it->id() == 3);
41+
REQUIRE(it->version() == 3);
42+
++it;
43+
REQUIRE(it->id() == 1);
44+
REQUIRE(it->version() == 2);
45+
++it;
46+
REQUIRE(it->id() == 1);
47+
REQUIRE(it->version() == 4);
48+
++it;
49+
REQUIRE(it == collection.cend());
50+
51+
collection.sort(osmium::object_order_type_id_version{});
52+
53+
REQUIRE(collection.size() == 3);
54+
55+
it = collection.cbegin();
56+
REQUIRE(it->id() == 1);
57+
REQUIRE(it->version() == 2);
58+
++it;
59+
REQUIRE(it->id() == 1);
60+
REQUIRE(it->version() == 4);
61+
++it;
62+
REQUIRE(it->id() == 3);
63+
REQUIRE(it->version() == 3);
64+
++it;
65+
REQUIRE(it == collection.cend());
66+
67+
collection.sort(osmium::object_order_type_id_reverse_version{});
68+
69+
it = collection.cbegin();
70+
REQUIRE(it->id() == 1);
71+
REQUIRE(it->version() == 4);
72+
++it;
73+
REQUIRE(it->id() == 1);
74+
REQUIRE(it->version() == 2);
75+
++it;
76+
REQUIRE(it->id() == 3);
77+
REQUIRE(it->version() == 3);
78+
++it;
79+
REQUIRE(it == collection.cend());
80+
81+
collection.clear();
82+
83+
REQUIRE(collection.empty());
84+
REQUIRE(collection.size() == 0);
85+
}
86+

0 commit comments

Comments
 (0)