CoolProp 8.0.0
An open-source fluid property and humid air property database
Hash.h
Go to the documentation of this file.
1#ifndef COOLPROP_HASH_H
2#define COOLPROP_HASH_H
3
4#include <cstddef>
5#include <cstdint>
6#include <string>
7#include <string_view>
8
9namespace CoolProp {
10
11// FNV-1a 64-bit hash over a raw byte buffer.
12//
13// Same hash family that CoolProp already uses for stamping
14// `source_eos_hash` on superancillaries (see the `Superancillary
15// source_eos_hash matches current EOS at bit level` test in
16// src/Tests/CoolProp-Tests.cpp). Deterministic across compilers
17// and platforms. Used here for short cache-key prefixes computed
18// over canonical-JSON option blobs.
19//
20// References:
21// - Fowler / Noll / Vo 1991 — http://www.isthe.com/chongo/tech/comp/fnv/
22// - constants per the canonical specification (offset basis +
23// prime are 64-bit)
24inline std::uint64_t fnv1a_64(const void* data, std::size_t n) noexcept {
25 constexpr std::uint64_t kFnv64OffsetBasis = 0xCBF29CE484222325ULL;
26 constexpr std::uint64_t kFnv64Prime = 0x100000001B3ULL;
27 std::uint64_t h = kFnv64OffsetBasis;
28 const auto* p = static_cast<const std::uint8_t*>(data);
29 for (std::size_t i = 0; i < n; ++i) {
30 h ^= static_cast<std::uint64_t>(p[i]);
31 h *= kFnv64Prime;
32 }
33 return h;
34}
35
36inline std::uint64_t fnv1a_64(std::string_view s) noexcept {
37 return fnv1a_64(s.data(), s.size());
38}
39
40// Lowercase 16-char hex representation of a 64-bit hash. No "0x"
41// prefix. Suitable as a filename-safe cache-key fragment.
42inline std::string to_hex16(std::uint64_t h) {
43 static constexpr char kHex[] = "0123456789abcdef";
44 std::string out(16, '0');
45 for (int i = 15; i >= 0; --i) {
46 out[i] = kHex[h & 0xF];
47 h >>= 4;
48 }
49 return out;
50}
51
52} // namespace CoolProp
53
54#endif // COOLPROP_HASH_H