Skip to content

Commit

Permalink
Specialization of std::hash for TypeSafeIndex. (RobotLocomotion#7839)
Browse files Browse the repository at this point in the history
  • Loading branch information
amcastro-tri authored and jwnimmer-tri committed Jan 24, 2018
1 parent 7841c2a commit c732e3d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
1 change: 1 addition & 0 deletions common/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ drake_cc_library(
hdrs = ["type_safe_index.h"],
deps = [
":essential",
":hash",
":nice_type_name",
],
)
Expand Down
32 changes: 32 additions & 0 deletions common/test/type_safe_index_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
#include <limits>
#include <regex>
#include <sstream>
#include <string>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

Expand Down Expand Up @@ -487,6 +490,35 @@ GTEST_TEST(TypeSafeIndex, ConstructorAvailability) {
EXPECT_TRUE((has_constructor<AIndex, int64_t>()));
}

// Confirms that type safe indexes are configured to serve as key and/or values
// within std::unordered_map
GTEST_TEST(TypeSafeIndex, CompatibleWithUnorderedMap) {
std::unordered_map<AIndex, std::string> indexes;
AIndex a1(1), a2(2), a3(3);
std::string s1("hello"), s2("unordered"), s3("map");
indexes.emplace(a1, s1);
indexes.emplace(a2, s2);
EXPECT_EQ(indexes.find(a3), indexes.end());
EXPECT_NE(indexes.find(a2), indexes.end());
EXPECT_NE(indexes.find(a1), indexes.end());
indexes[a3] = s3;
EXPECT_NE(indexes.find(a3), indexes.end());
}

// Confirms that type safe indexes are configured to serve as values
// within std::unordered_set
GTEST_TEST(TypeSafeIndex, CompatibleWithUnorderedSet) {
std::unordered_set<AIndex> indexes;
AIndex a1(1), a2(2), a3(3);

indexes.emplace(a1);
indexes.insert(a2);
EXPECT_EQ(indexes.size(), 2);
EXPECT_EQ(indexes.find(a3), indexes.end());
EXPECT_NE(indexes.find(a1), indexes.end());
EXPECT_NE(indexes.find(a2), indexes.end());
}

} // namespace
} // namespace common
} // namespace drake
11 changes: 11 additions & 0 deletions common/type_safe_index.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <functional>
#include <limits>
#include <string>

Expand Down Expand Up @@ -446,3 +447,13 @@ class TypeSafeIndex {
};

} // namespace drake

namespace std {
/// Specialization of std::hash for drake::TypeSafeIndex<Tag>.
template <typename Tag>
struct hash<drake::TypeSafeIndex<Tag>> {
size_t operator()(const drake::TypeSafeIndex<Tag>& index) const {
return std::hash<int>()(index);
}
};
} // namespace std
2 changes: 1 addition & 1 deletion geometry/identifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ namespace std {
template <typename Tag>
struct hash<drake::geometry::Identifier<Tag>> {
size_t operator()(const drake::geometry::Identifier<Tag>& id) const {
return hash<int64_t>()(id.get_value());
return std::hash<int64_t>()(id.get_value());
}
};

Expand Down

0 comments on commit c732e3d

Please sign in to comment.