Skip to content

Commit

Permalink
Add implementation for std::hash<MacAddress>
Browse files Browse the repository at this point in the history
Summary:
Add implementation for std::hash<MacAddress> so that it can be used in
maps.

Reviewed By: yfeldblum

Differential Revision: D10004544

fbshipit-source-id: 46cb177577123b1248cd8ff679f0fbe286596819
  • Loading branch information
ammubhave authored and facebook-github-bot committed Sep 24, 2018
1 parent b7ac6b7 commit 07f9076
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
12 changes: 12 additions & 0 deletions folly/MacAddress.h
Expand Up @@ -231,3 +231,15 @@ typename std::enable_if<IsSomeString<Tgt>::value>::type toAppend(
std::ostream& operator<<(std::ostream& os, MacAddress address);

} // namespace folly

namespace std {

// Provide an implementation for std::hash<MacAddress>
template <>
struct hash<folly::MacAddress> {
size_t operator()(const folly::MacAddress& address) const {
return std::hash<uint64_t>()(address.u64HBO());
}
};

} // namespace std
9 changes: 9 additions & 0 deletions folly/test/MacAddressTest.cpp
Expand Up @@ -167,3 +167,12 @@ TEST(MacAddress, ordering) {
testCmp("01:00:00:00:00:00", "02:00:00:00:00:00");
testCmp("00:00:00:00:00:01", "00:00:00:00:01:00");
}

TEST(MacAddress, hash) {
EXPECT_EQ(
std::hash<MacAddress>()(MacAddress("00:11:22:33:44:55")),
std::hash<MacAddress>()(MacAddress("00-11-22-33-44-55")));
EXPECT_NE(
std::hash<MacAddress>()(MacAddress("00:11:22:33:44:55")),
std::hash<MacAddress>()(MacAddress("00:11:22:33:44:56")));
}

0 comments on commit 07f9076

Please sign in to comment.