summaryrefslogtreecommitdiff
path: root/modules/codec/c++/schema_hash.h
diff options
context:
space:
mode:
authorClaudius "keldu" Holeksa <mail@keldu.de>2024-01-18 13:03:38 +0100
committerClaudius "keldu" Holeksa <mail@keldu.de>2024-01-18 13:03:38 +0100
commitf528f24036698e2014f370f174abab286ef397b6 (patch)
tree8ac3cec9058b6a2d2392e79b43a7baf8c6c5f38f /modules/codec/c++/schema_hash.h
parentfee3e3bcf2fc1d84689b09483dc98a2e73db9915 (diff)
codec: Changed hashing to crc32 and made the calculation constexpr
Diffstat (limited to 'modules/codec/c++/schema_hash.h')
-rw-r--r--modules/codec/c++/schema_hash.h60
1 files changed, 37 insertions, 23 deletions
diff --git a/modules/codec/c++/schema_hash.h b/modules/codec/c++/schema_hash.h
index d4312c4..48e8d6c 100644
--- a/modules/codec/c++/schema_hash.h
+++ b/modules/codec/c++/schema_hash.h
@@ -2,22 +2,14 @@
#include <forstio/string_literal.h>
#include "schema.h"
+#include "crc32.h"
namespace saw {
-struct schema_hash_combine {
- static constexpr uint64_t apply(uint64_t seed, uint64_t v){
- return seed ^( std::hash<uint64_t>{}(v) + 0x9e3779b9 + (seed<<6) + (seed >> 2));
- }
-};
-
template<string_literal lit>
struct hash_literal {
- static constexpr uint64_t apply(uint64_t seed){
+ static constexpr uint32_t apply(uint32_t seed){
constexpr std::string_view view = lit.view();
- for(uint64_t i = 0; i < view.size(); ++i){
- seed = schema_hash_combine::apply(seed, static_cast<uint64_t>(view[i]));
- }
- return seed;
+ return hash<algs::crc32>::update(seed, view);
}
};
@@ -30,7 +22,7 @@ template<>
struct schema_hash_seed<schema::SignedInteger> {
using Schema = schema::SignedInteger;
- static constexpr uint64_t apply(uint64_t seed){
+ static constexpr uint32_t apply(uint32_t seed){
return hash_literal<Schema::name>::apply(seed);
}
};
@@ -39,7 +31,7 @@ template<>
struct schema_hash_seed<schema::UnsignedInteger> {
using Schema = schema::UnsignedInteger;
- static constexpr uint64_t apply(uint64_t seed){
+ static constexpr uint32_t apply(uint32_t seed){
return hash_literal<Schema::name>::apply(seed);
}
};
@@ -48,7 +40,7 @@ template<>
struct schema_hash_seed<schema::FloatingPoint> {
using Schema = schema::FloatingPoint;
- static constexpr uint64_t apply(uint64_t seed){
+ static constexpr uint32_t apply(uint32_t seed){
return hash_literal<Schema::name>::apply(seed);
}
};
@@ -57,7 +49,7 @@ template<>
struct schema_hash_seed<schema::String> {
using Schema = schema::String;
- static constexpr uint64_t apply(uint64_t seed){
+ static constexpr uint32_t apply(uint32_t seed){
return hash_literal<Schema::name>::apply(seed);
}
};
@@ -66,10 +58,21 @@ template<typename P, uint64_t N>
struct schema_hash_seed<schema::Primitive<P,N>> {
using Schema = schema::Primitive<P,N>;
- static constexpr uint64_t apply(uint64_t seed){
+ static constexpr uint32_t apply(uint32_t seed){
seed = hash_literal<Schema::name>::apply(seed);
seed = schema_hash_seed<P>::apply(seed);
- seed = schema_hash_combine::apply(seed, N);
+ uint64_t val = N;
+ std::array<uint8_t,sizeof(uint64_t)> dat{
+ static_cast<uint8_t>(val >> 0),
+ static_cast<uint8_t>(val >> 8),
+ static_cast<uint8_t>(val >> 16),
+ static_cast<uint8_t>(val >> 24),
+ static_cast<uint8_t>(val >> 32),
+ static_cast<uint8_t>(val >> 40),
+ static_cast<uint8_t>(val >> 48),
+ static_cast<uint8_t>(val >> 56)
+ };
+ seed = hash<algs::crc32>::update(seed, &dat[0], dat.size());
return seed;
}
};
@@ -78,10 +81,21 @@ template<typename T, uint64_t N>
struct schema_hash_seed<schema::Array<T,N>> {
using Schema = schema::Array<T,N>;
- static constexpr uint64_t apply(uint64_t seed){
+ static constexpr uint32_t apply(uint32_t seed){
seed = hash_literal<Schema::name>::apply(seed);
seed = schema_hash_seed<T>::apply(seed);
- seed = schema_hash_combine::apply(seed, N);
+ uint64_t val = N;
+ std::array<uint8_t,sizeof(uint64_t)> dat{
+ static_cast<uint8_t>(val >> 0),
+ static_cast<uint8_t>(val >> 8),
+ static_cast<uint8_t>(val >> 16),
+ static_cast<uint8_t>(val >> 24),
+ static_cast<uint8_t>(val >> 32),
+ static_cast<uint8_t>(val >> 40),
+ static_cast<uint8_t>(val >> 48),
+ static_cast<uint8_t>(val >> 56)
+ };
+ seed = hash<algs::crc32>::update(seed, &dat[0], dat.size());
return seed;
}
};
@@ -91,7 +105,7 @@ struct schema_hash_seed<schema::Struct<schema::Member<V,K>...>> {
using Schema = schema::Struct<schema::Member<V,K>...>;
template<uint64_t i>
- static constexpr uint64_t apply_ele(uint64_t seed){
+ static constexpr uint32_t apply_ele(uint32_t seed){
using Type = typename parameter_pack_type<i,V...>::type;
constexpr string_literal Lit = parameter_key_pack_type<i,K...>::literal;
using MemberT = typename parameter_pack_type<i,schema::Member<V,K>...>::type;
@@ -106,7 +120,7 @@ struct schema_hash_seed<schema::Struct<schema::Member<V,K>...>> {
return seed;
}
- static constexpr uint64_t apply(uint64_t seed){
+ static constexpr uint32_t apply(uint32_t seed){
seed = hash_literal<Schema::name>::apply(seed);
if constexpr (sizeof...(V) > 0){
seed = apply_ele<0>(seed);
@@ -117,8 +131,8 @@ struct schema_hash_seed<schema::Struct<schema::Member<V,K>...>> {
template<typename Schema>
struct schema_hash {
- static constexpr uint64_t apply() {
- constexpr uint64_t seed = 0;
+ static constexpr uint32_t apply() {
+ constexpr uint32_t seed = 0;
return schema_hash_seed<Schema>::apply(seed);
}
};