From 4194a609c2b1ae60a25880cb59b800e6d299ed06 Mon Sep 17 00:00:00 2001 From: Nathan Hourt Date: Fri, 6 Mar 2015 16:41:52 -0500 Subject: [PATCH] Allow hashing of uint128 --- include/fc/crypto/city.hpp | 6 ++++-- include/fc/uint128.hpp | 15 +++++++++------ src/crypto/city.cpp | 2 ++ 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/include/fc/crypto/city.hpp b/include/fc/crypto/city.hpp index 74b8884..7e42f37 100644 --- a/include/fc/crypto/city.hpp +++ b/include/fc/crypto/city.hpp @@ -44,11 +44,13 @@ #include // for size_t. #include #include -#include -#include namespace fc { +class uint128; +template +class array; + // Hash function for a byte array. uint64_t city_hash64(const char *buf, size_t len); diff --git a/include/fc/uint128.hpp b/include/fc/uint128.hpp index cbee51b..6071f64 100644 --- a/include/fc/uint128.hpp +++ b/include/fc/uint128.hpp @@ -4,25 +4,26 @@ #include #include +#include #ifdef _MSC_VER #pragma warning (push) #pragma warning (disable : 4244) #endif //// _MSC_VER -namespace fc +namespace fc { class bigint; /** * @brief an implementation of 128 bit unsigned integer * */ - class uint128 + class uint128 { public: - uint128():hi(0),lo(0){}; + uint128():hi(0),lo(0){} uint128( uint32_t l ):hi(0),lo(l){} uint128( int32_t l ):hi( -(l<0) ),lo(l){} uint128( int64_t l ):hi( -(l<0) ),lo(l){} @@ -44,7 +45,7 @@ namespace fc uint128& operator++() { hi += (++lo == 0); return *this; } uint128& operator--() { hi -= (lo-- == 0); return *this; } - uint128 operator++(int) { auto tmp = *this; ++(*this); return tmp; } + uint128 operator++(int) { auto tmp = *this; ++(*this); return tmp; } uint128 operator--(int) { auto tmp = *this; --(*this); return tmp; } uint128& operator |= ( const uint128& u ) { hi |= u.hi; lo |= u.lo; return *this; } @@ -76,6 +77,8 @@ namespace fc friend bool operator >= ( const uint128& l, const uint128& r ) { return l == r || l > r; } friend bool operator <= ( const uint128& l, const uint128& r ) { return l == r || l < r; } + friend std::size_t hash_value( const uint128& v ) { return city_hash_size_t((const char*)&v, sizeof(v)); } + uint32_t to_integer()const { FC_ASSERT( hi == 0 ); @@ -102,7 +105,7 @@ namespace fc private: uint64_t hi; uint64_t lo; - + }; static_assert( sizeof(uint128) == 2*sizeof(uint64_t), "validate packing assumptions" ); @@ -113,7 +116,7 @@ namespace fc void to_variant( const uint128& var, variant& vo ); void from_variant( const variant& var, uint128& vo ); - namespace raw + namespace raw { template inline void pack( Stream& s, const uint128& u ) { s.write( (char*)&u, sizeof(u) ); } diff --git a/src/crypto/city.cpp b/src/crypto/city.cpp index d90d79c..18367fb 100644 --- a/src/crypto/city.cpp +++ b/src/crypto/city.cpp @@ -33,6 +33,8 @@ #include #include // for memcpy and memset #include +#include +#include #if defined(__SSE4_2__) && defined(__x86_64__) #include