From d94c31fee53c3c7df981cf44a54dd722d7fc122c Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Mon, 15 Jan 2024 17:04:16 +0100 Subject: codec: hash functionality added --- modules/codec/c++/schema.h | 59 +++++++++++++++++------ modules/codec/c++/schema_hash.h | 104 ++++++++-------------------------------- 2 files changed, 66 insertions(+), 97 deletions(-) (limited to 'modules/codec/c++') diff --git a/modules/codec/c++/schema.h b/modules/codec/c++/schema.h index 576f378..4549916 100644 --- a/modules/codec/c++/schema.h +++ b/modules/codec/c++/schema.h @@ -6,7 +6,9 @@ namespace saw { namespace schema { // NOLINTBEGIN -template struct Member {}; +template struct Member { + static constexpr string_literal name = "Member"; +}; template struct Struct { static_assert( @@ -15,7 +17,9 @@ template struct Struct { }; template -struct Struct...> {}; +struct Struct...> { + static constexpr string_literal name = "Struct"; +}; template struct Union { static_assert( @@ -24,13 +28,21 @@ template struct Union { }; template -struct Union...> {}; +struct Union...> { + static constexpr string_literal name = "Union"; +}; -template struct Array {}; +template struct Array { + static constexpr string_literal name = "Array"; +}; -template struct FixedArray {}; +template struct FixedArray { + static constexpr string_literal name = "FixedArray"; +}; -template struct Tuple {}; +template struct Tuple { + static constexpr string_literal name = "Tuple"; +}; /** * This acts as a separator of different encodings being mashed together @@ -43,14 +55,24 @@ template struct Tuple {}; * data ex_data; */ template -class Wrapper {}; +class Wrapper { + static constexpr string_literal name = "Wrapper"; +}; -struct String {}; +struct String { + static constexpr string_literal name = "String"; +}; -struct SignedInteger {}; -struct UnsignedInteger {}; -struct FloatingPoint {}; +struct SignedInteger { + static constexpr string_literal name = "SignedInteger"; +}; +struct UnsignedInteger { + static constexpr string_literal name = "UnsignedInteger"; +}; +struct FloatingPoint { + static constexpr string_literal name = "FloatingPoint"; +}; template struct Primitive { static_assert(((std::is_same_v || @@ -58,6 +80,7 @@ template struct Primitive { N == 4 || N == 8)) || (std::is_same_v && (N == 4 || N == 8)), "Primitive Type is not supported"); + static constexpr string_literal name = "Primitive"; }; using Int8 = Primitive; @@ -77,7 +100,9 @@ using Float64 = Primitive; * Classes allowing to distinguish Ints from VarInts */ template -struct VariableLengthPrimitive {}; +struct VariableLengthPrimitive { + static constexpr string_literal name = "VariableLengthPrimitive"; +}; using VarInt = VariableLengthPrimitive; using VarLong = VariableLengthPrimitive; @@ -86,16 +111,22 @@ using VarLong = VariableLengthPrimitive; * Classes enabling Rpc calls */ template -struct Function {}; +struct Function { + static constexpr string_literal name = "Function"; +}; template struct Interface { static_assert( always_false, "This schema template doesn't support this type of template argument"); + + static constexpr string_literal name = "Interface"; }; template -struct Interface,Names>...> {}; +struct Interface,Names>...> { + static constexpr string_literal name = "Interface"; +}; // NOLINTEND } // namespace schema diff --git a/modules/codec/c++/schema_hash.h b/modules/codec/c++/schema_hash.h index 5690166..a7f482f 100644 --- a/modules/codec/c++/schema_hash.h +++ b/modules/codec/c++/schema_hash.h @@ -1,105 +1,43 @@ #pragma once +#include #include "schema.h" +#include "data.h" namespace saw { struct schema_hash_combine { static constexpr uint64_t apply(uint64_t seed, uint64_t v){ - return (seed ^ v) * 1099511628211u; - - return seed ^( std::hash{}(v) + 0x9e3779b9 + (seed<<6) + (seed >> 2)); } }; +template +struct schema_hash_literal { + static constexpr uint64_t apply(uint64_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(view[i])); + } + return seed; + } +}; + template struct schema_hash { static_assert(always_false, "Not schema_hashable"); }; -template<> -struct schema_hash> { - static constexpr uint64_t base_value = 0u; -}; - -template<> -struct schema_hash> { - static constexpr uint64_t base_value = 1u; -}; - -template<> -struct schema_hash> { - static constexpr uint64_t base_value = 2u; -}; - -template<> -struct schema_hash> { - static constexpr uint64_t base_value = 3u; -}; - -template<> -struct schema_hash> { - static constexpr uint64_t base_value = 4u; -}; - -template<> -struct schema_hash> { - static constexpr uint64_t base_value = 5u; -}; - -template<> -struct schema_hash> { - static constexpr uint64_t base_value = 6u; -}; - -template<> -struct schema_hash> { - static constexpr uint64_t base_value = 7u; -}; - -template<> -struct schema_hash> { - static constexpr uint64_t base_value = 8u; -}; - -template<> -struct schema_hash> { - static constexpr uint64_t base_value = 9u; -}; - -template -struct schema_hash> { - static constexpr uint64_t base_value = 10u; -}; - -template -struct schema_hash...>> { - static constexpr uint64_t base_value = 11u; -}; - -template -struct schema_hash...>> { - static constexpr uint64_t base_value = 12u; -}; - -template -struct schema_hash> { - static constexpr uint64_t base_value = 13u; -}; - template<> struct schema_hash { - static constexpr uint64_t base_value = 14u; -}; + using Schema = schema::String; -template -struct schema_hash> { - static constexpr uint64_t base_value = 15u; -}; - -template -struct schema_hash> { - static constexpr uint64_t base_value = 16u; + static constexpr uint64_t apply(uint64_t seed, const data& val){ + seed = schema_hash_literal::apply(seed); + for(size_t i = 0; i < val.size(); ++i){ + seed = schema_hash_combine::apply(seed, static_cast(val.at(i))); + } + return seed; + } }; } -- cgit v1.2.3