Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions include/osmium/osm/node_ref.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ DEALINGS IN THE SOFTWARE.

#include <cstdint>
#include <cstdlib>
#include <functional>
#include <iosfwd>

namespace osmium {
Expand Down Expand Up @@ -234,4 +235,25 @@ namespace osmium {

} // namespace osmium

namespace std {

// This pragma is a workaround for a bug in an old libc implementation
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wmismatched-tags"
#endif
template <>
struct hash<osmium::NodeRef> {
using argument_type = osmium::NodeRef;
using result_type = size_t;
size_t operator()(const osmium::NodeRef& nr) const noexcept {
return hash<osmium::object_id_type>{}(nr.ref());
}
};
#ifdef __clang__
#pragma clang diagnostic pop
#endif

} // namespace std

#endif // OSMIUM_OSM_NODE_REF_HPP
30 changes: 29 additions & 1 deletion test/t/osm/test_node_ref.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include "catch.hpp"

#include <functional>
#include <unordered_set>

#include <osmium/builder/attr.hpp>
#include <osmium/memory/buffer.hpp>
#include <osmium/osm/node_ref.hpp>
Expand All @@ -16,7 +19,7 @@ TEST_CASE("Construct a NodeRef with an id") {
REQUIRE(node_ref.ref() == 7);
}

TEST_CASE("Equality comparison fo NodeRefs") {
TEST_CASE("Equality comparison of NodeRefs") {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix of an unrelated typo ;)

const osmium::NodeRef node_ref1{7, {1.2, 3.4}};
const osmium::NodeRef node_ref2{7, {1.4, 3.1}};
const osmium::NodeRef node_ref3{9, {1.2, 3.4}};
Expand All @@ -27,6 +30,31 @@ TEST_CASE("Equality comparison fo NodeRefs") {
REQUIRE( osmium::location_equal()(node_ref1, node_ref3));
}

TEST_CASE("Hash of NodeRefs") {
const osmium::NodeRef node_ref1{7, {1.2, 3.4}};
const osmium::NodeRef node_ref2{7, {1.4, 3.1}};
const osmium::NodeRef node_ref3{9, {1.2, 3.4}};

const std::hash<osmium::NodeRef> hasher;

SECTION("Equal NodeRefs have equal hashes") {
REQUIRE(hasher(node_ref1) == hasher(node_ref2));
}

SECTION("Different ref yields different hash") {
REQUIRE(hasher(node_ref1) != hasher(node_ref3));
}
Comment on lines +44 to +46

SECTION("Hash is usable in unordered containers") {
std::unordered_set<osmium::NodeRef> set;
set.insert(node_ref1);
set.insert(node_ref2);
REQUIRE(set.size() == 1);
set.insert(node_ref3);
REQUIRE(set.size() == 2);
}
}

TEST_CASE("Set location on a NodeRef") {
osmium::NodeRef node_ref{7};
REQUIRE_FALSE(node_ref.location().valid());
Expand Down
Loading