Skip to content

Commit 3b8595f

Browse files
committed
Clarify documentation around ordering and add test
1 parent c047f96 commit 3b8595f

2 files changed

Lines changed: 18 additions & 3 deletions

File tree

include/osmium/osm/object_comparisons.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,15 +154,17 @@ namespace osmium {
154154
* (negative IDs first, then positive IDs, both in the order of their
155155
* absolute values), but later versions of an object are ordered before
156156
* earlier versions of the same object. This is useful when the last
157-
* version of an object needs to be used.
157+
* version of an object needs to be used. Object visibility is not taken
158+
* into account, so objects with different visibilities keep their order.
158159
*/
159160
struct object_order_type_id_reverse_version {
160161

161162
bool operator()(const osmium::OSMObject& lhs, const osmium::OSMObject& rhs) const noexcept {
163+
const bool tsvalid = lhs.timestamp().valid() && rhs.timestamp().valid();
162164
return const_tie(lhs.type(), lhs.id() > 0, lhs.positive_id(), rhs.version(),
163-
((lhs.timestamp().valid() && rhs.timestamp().valid()) ? rhs.timestamp() : osmium::Timestamp())) <
165+
(tsvalid ? rhs.timestamp() : osmium::Timestamp())) <
164166
const_tie(rhs.type(), rhs.id() > 0, rhs.positive_id(), lhs.version(),
165-
((lhs.timestamp().valid() && rhs.timestamp().valid()) ? lhs.timestamp() : osmium::Timestamp()));
167+
(tsvalid ? lhs.timestamp() : osmium::Timestamp()));
166168
}
167169

168170
/// @pre lhs and rhs must not be nullptr

test/t/osm/test_object_comparisons.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,19 @@ TEST_CASE("Node comparisons") {
195195
REQUIRE(std::is_sorted(nodes.cbegin(), nodes.cend(), osmium::object_order_type_id_reverse_version{}));
196196
}
197197

198+
SECTION("reverse version ordering should keep order for deleted objects") {
199+
nodes.emplace_back(buffer.get<osmium::Node>(osmium::builder::add_node(buffer, _id( 1), _version(2), _timestamp("2016-01-01T00:00:00Z"), _deleted(false))));
200+
nodes.emplace_back(buffer.get<osmium::Node>(osmium::builder::add_node(buffer, _id( 1), _version(2), _timestamp("2016-01-01T00:00:00Z"), _deleted(true))));
201+
202+
REQUIRE(std::is_sorted(nodes.cbegin(), nodes.cend(), osmium::object_order_type_id_reverse_version{}));
203+
204+
nodes.clear();
205+
206+
nodes.emplace_back(buffer.get<osmium::Node>(osmium::builder::add_node(buffer, _id( 1), _version(2), _timestamp("2016-01-01T00:00:00Z"), _deleted(true))));
207+
nodes.emplace_back(buffer.get<osmium::Node>(osmium::builder::add_node(buffer, _id( 1), _version(2), _timestamp("2016-01-01T00:00:00Z"), _deleted(false))));
208+
209+
REQUIRE(std::is_sorted(nodes.cbegin(), nodes.cend(), osmium::object_order_type_id_reverse_version{}));
210+
}
198211
}
199212

200213
TEST_CASE("Object comparisons: types are ordered nodes, then ways, then relations") {

0 commit comments

Comments
 (0)