This shows you the differences between two versions of the page.
| cpp:hash [2019/07/12 12:48] – created allspark_ise | cpp:hash [2019/07/12 12:48] (current) – allspark_ise | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ```cpp | ```cpp | ||
| - | #pragma once | ||
| - | #include < | ||
| - | |||
| //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:// | // post: https:// | ||
| - | + | | |
| - | inline | + | constexpr |
| - | + | { | |
| - | const char* data = (char*)key; | + | |
| - | uint32_t hash = 0x811c9dc5; | + | |
| - | uint32_t prime = 0x1000193; | + | |
| - | + | for (std:: | |
| - | 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; |
| } // | } // | ||
| - | |||
| - | inline const uint64_t hash_64_fnv1a(const void* key, const uint64_t len) { | ||
| | | ||
| - | | + | constexpr |
| + | { | ||
| uint64_t hash = 0xcbf29ce484222325; | uint64_t hash = 0xcbf29ce484222325; | ||
| - | uint64_t prime = 0x100000001b3; | + | |
| | | ||
| - | for(int i = 0; i < len; ++i) { | + | for (std::uint8_t value : str) |
| - | | + | { |
| hash = hash ^ value; | hash = hash ^ value; | ||
| hash *= prime; | hash *= prime; | ||
| } | } | ||
| | | ||
| - | return hash; | + | return hash; |
| } // | } // | ||
| ``` | ``` | ||