mirror of
https://github.com/mfontanini/libtins
synced 2026-01-23 02:35:57 +01:00
Don't convert IPv6Address to string when hashing
This commit is contained in:
@@ -32,10 +32,10 @@
|
||||
|
||||
#include <string>
|
||||
#include <iosfwd>
|
||||
#include <functional>
|
||||
#include <stdint.h>
|
||||
#include <tins/cxxstd.h>
|
||||
#include <tins/macros.h>
|
||||
#include <functional>
|
||||
|
||||
namespace Tins {
|
||||
|
||||
@@ -224,9 +224,14 @@ namespace std {
|
||||
|
||||
template<>
|
||||
struct hash<Tins::IPv6Address> {
|
||||
size_t operator()(const Tins::IPv6Address& addr) const
|
||||
{
|
||||
return std::hash<string>()(addr.to_string());
|
||||
// Implementation taken from boost.functional
|
||||
size_t operator()(const Tins::IPv6Address& addr) const {
|
||||
std::size_t output = Tins::IPv6Address::address_size;
|
||||
Tins::IPv6Address::const_iterator iter = addr.begin();
|
||||
for (; iter != addr.end(); ++iter) {
|
||||
output ^= *iter + 0x9e3779b9 + (output << 6) + (output >> 2);
|
||||
}
|
||||
return output;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -5,10 +5,10 @@
|
||||
#include <sstream>
|
||||
#include <stdint.h>
|
||||
#include <tins/ipv6_address.h>
|
||||
#include <tins/macros.h>
|
||||
|
||||
using namespace Tins;
|
||||
|
||||
|
||||
const uint8_t empty_addr[IPv6Address::address_size] = { 0 };
|
||||
|
||||
void test_to_string(const std::string& str) {
|
||||
@@ -123,3 +123,17 @@ TEST(IPv6AddressTest, MaskAddress) {
|
||||
IPv6Address("deaf:beef:adad:beef::") & IPv6Address("ffff:e000::")
|
||||
);
|
||||
}
|
||||
|
||||
#if TINS_IS_CXX11
|
||||
|
||||
TEST(IPv6AddressTest, HashTest) {
|
||||
using std::hash;
|
||||
const auto hasher = [](const IPv6Address& address) {
|
||||
return hash<IPv6Address>()(address);
|
||||
};
|
||||
EXPECT_NE(hasher("dead:beef::"), hasher("dead:beef::1"));
|
||||
EXPECT_NE(hasher("dead:beef::"), hasher("feed:dead::1"));
|
||||
EXPECT_EQ(hasher("dead:beef::"), hasher("dead:beef::"));
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user