Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class fnv {
- // Compile-time FNV-1a string hashing
- // kWEZEE, 2017.07
- // Usage:
- // Find hash : fnv{ }( TEXT )
- // Find hash and compare : fnv{ }( TEXT, HASH )
- template< class _Ty, size_t _Size >
- constexpr auto get_hash_of( const _Ty ( &element )[ _Size ] ) {
- #if defined(_WIN64) || defined(__x86_64__)
- constexpr auto fnv_offset_basis = 14695981039346656037ull;
- constexpr auto fnv_prime = 1099511628211ull;
- #else
- constexpr auto fnv_offset_basis = 2166136261u;
- constexpr auto fnv_prime = 16777619u;
- #endif
- auto hash = fnv_offset_basis;
- for( size_t i{ }; i < _Size * sizeof _Ty; ++i ) {
- hash ^= element[ i ];
- hash *= fnv_prime;
- }
- return hash;
- }
- public:
- template< class _Ty, size_t _Size >
- constexpr auto operator()( const _Ty ( &element )[ _Size ] ) {
- return this->get_hash_of( element );
- }
- template< class _Ty, size_t _Size >
- constexpr auto operator()( const _Ty ( &element )[ _Size ], const size_t &hash ) {
- return this->get_hash_of( element ) == hash;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement