Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. class fnv {
  2.  
  3. // Compile-time FNV-1a string hashing
  4. // kWEZEE, 2017.07
  5.  
  6. // Usage:
  7. // Find hash : fnv{ }( TEXT )
  8. // Find hash and compare : fnv{ }( TEXT, HASH )
  9.  
  10. template< class _Ty, size_t _Size >
  11. constexpr auto get_hash_of( const _Ty ( &element )[ _Size ] ) {
  12. #if defined(_WIN64) || defined(__x86_64__)
  13. constexpr auto fnv_offset_basis = 14695981039346656037ull;
  14. constexpr auto fnv_prime = 1099511628211ull;
  15. #else
  16. constexpr auto fnv_offset_basis = 2166136261u;
  17. constexpr auto fnv_prime = 16777619u;
  18. #endif
  19.  
  20. auto hash = fnv_offset_basis;
  21.  
  22. for( size_t i{ }; i < _Size * sizeof _Ty; ++i ) {
  23. hash ^= element[ i ];
  24. hash *= fnv_prime;
  25. }
  26.  
  27. return hash;
  28. }
  29.  
  30. public:
  31. template< class _Ty, size_t _Size >
  32. constexpr auto operator()( const _Ty ( &element )[ _Size ] ) {
  33. return this->get_hash_of( element );
  34. }
  35.  
  36. template< class _Ty, size_t _Size >
  37. constexpr auto operator()( const _Ty ( &element )[ _Size ], const size_t &hash ) {
  38. return this->get_hash_of( element ) == hash;
  39. }
  40.  
  41. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement