-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Move
Hash.hpp
into core codebase
- Loading branch information
Showing
3 changed files
with
35 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// | ||
// Hash.hpp | ||
// Pods | ||
// | ||
// Created by Marc Rousavy on 14.07.24. | ||
// | ||
|
||
#pragma once | ||
|
||
#include <cstddef> | ||
#include <cstdint> | ||
|
||
namespace margelo { | ||
|
||
/** | ||
* Hashes the given C-String using the FNV-1a hashing algorithm. | ||
* | ||
* This function can be used at compile time as a constexpr to build | ||
* statically optimized switch statements. | ||
*/ | ||
constexpr uint64_t hashString(const char* str, size_t length) { | ||
uint64_t hash = 14695981039346656037ull; // FNV offset basis | ||
const uint64_t fnv_prime = 1099511628211ull; | ||
|
||
for (size_t i = 0; i < length; ++i) { | ||
hash ^= static_cast<uint64_t>(str[i]); | ||
hash *= fnv_prime; | ||
} | ||
|
||
return hash; | ||
} | ||
|
||
} |