User Tools

Site Tools


cpp:hash

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

cpp:hash [2019/07/12 12:48] – created allspark_isecpp:hash [2019/07/12 12:48] (current) allspark_ise
Line 1: Line 1:
 ```cpp ```cpp
-#pragma once 
-#include <stdint.h> 
- 
 //fnv1a 32 and 64 bit hash functions //fnv1a 32 and 64 bit hash functions
 // key is the data to hash, len is the size of the data (or how much of it to hash against) // key is the data to hash, len is the size of the data (or how much of it to hash against)
 // code license: public domain or equivalent // code license: public domain or equivalent
 // post: https://notes.underscorediscovery.com/constexpr-fnv1a/ // post: https://notes.underscorediscovery.com/constexpr-fnv1a/
- +     
-inline const uint32_t hash_32_fnv1a(const void* key, const uint32_t len) { +constexpr const std::uint32_t hash_32_fnv1a(std::string_view str 
- +   
-    const char* data = (char*)key; +    std::uint32_t hash = 0x811c9dc5; 
-    uint32_t hash = 0x811c9dc5; +    constexpr std::uint32_t prime = 0x1000193; 
-    uint32_t prime = 0x1000193; +     
- +    for (std::uint8_t value : str) 
-    for(int i = 0; i < len; ++i) { +    {
-        uint8_t value = data[i];+
         hash = hash ^ value;         hash = hash ^ value;
         hash *= prime;         hash *= prime;
     }     }
- +     
-    return hash; +    return hash;    
 } //hash_32_fnv1a } //hash_32_fnv1a
- 
-inline const uint64_t hash_64_fnv1a(const void* key, const uint64_t len) { 
          
-    const char* data = (char*)key;+constexpr const uint64_t hash_64_fnv1a(std::string_view str 
 +{
     uint64_t hash = 0xcbf29ce484222325;     uint64_t hash = 0xcbf29ce484222325;
-    uint64_t prime = 0x100000001b3;+    constexpr uint64_t prime = 0x100000001b3;
          
-    for(int i = 0; i < len; ++i) { +    for (std::uint8_t value : str) 
-        uint8_t value = data[i];+    {
         hash = hash ^ value;         hash = hash ^ value;
         hash *= prime;         hash *= prime;
     }     }
          
-    return hash; +    return hash;   
 } //hash_64_fnv1a } //hash_64_fnv1a
 ``` ```
cpp/hash.1562928511.txt.gz · Last modified: by allspark_ise