Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.62 KB | None | 0 0
  1. // https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
  2.         template <class T>
  3.         constexpr T fnv(LPCSTR _str, bool mode1a = false)
  4.         {
  5.             T hash, offset, prime;
  6.            
  7.             // Resolve the proper values for hashing
  8.             switch (sizeof(T))
  9.             {
  10.                 case 4:
  11.                     prime = 16777619ul;
  12.                     hash = offset = 2166136261ul;
  13.                     break;
  14.  
  15.                 case 8:
  16.                     prime = 1099511628211ull;
  17.                     hash = offset = 14695981039346656037ull;
  18.                     break;
  19.             }
  20.            
  21.             do      hash = mode1a ? ((hash ^ static_cast<T>(*_str)) * prime) : ((hash * prime) ^ static_cast<T>(*_str));
  22.             while (*_str++ != '\0');
  23.  
  24.             return hash;
  25.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement