diff options
Diffstat (limited to 'modules/codec/c++/schema_hash.h')
-rw-r--r-- | modules/codec/c++/schema_hash.h | 104 |
1 files changed, 21 insertions, 83 deletions
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 <forstio/string_literal.h> #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<uint64_t>{}(v) + 0x9e3779b9 + (seed<<6) + (seed >> 2)); } }; +template<string_literal lit> +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<uint64_t>(view[i])); + } + return seed; + } +}; + template<typename Schema> struct schema_hash { static_assert(always_false<Schema>, "Not schema_hashable"); }; template<> -struct schema_hash<schema::Primitive<schema::SignedInteger,1>> { - static constexpr uint64_t base_value = 0u; -}; - -template<> -struct schema_hash<schema::Primitive<schema::SignedInteger,2>> { - static constexpr uint64_t base_value = 1u; -}; - -template<> -struct schema_hash<schema::Primitive<schema::SignedInteger,4>> { - static constexpr uint64_t base_value = 2u; -}; - -template<> -struct schema_hash<schema::Primitive<schema::SignedInteger,8>> { - static constexpr uint64_t base_value = 3u; -}; - -template<> -struct schema_hash<schema::Primitive<schema::UnsignedInteger,1>> { - static constexpr uint64_t base_value = 4u; -}; - -template<> -struct schema_hash<schema::Primitive<schema::UnsignedInteger,2>> { - static constexpr uint64_t base_value = 5u; -}; - -template<> -struct schema_hash<schema::Primitive<schema::UnsignedInteger,4>> { - static constexpr uint64_t base_value = 6u; -}; - -template<> -struct schema_hash<schema::Primitive<schema::UnsignedInteger,8>> { - static constexpr uint64_t base_value = 7u; -}; - -template<> -struct schema_hash<schema::Primitive<schema::FloatingPoint,4>> { - static constexpr uint64_t base_value = 8u; -}; - -template<> -struct schema_hash<schema::Primitive<schema::FloatingPoint,8>> { - static constexpr uint64_t base_value = 9u; -}; - -template<typename... T> -struct schema_hash<schema::Tuple<T...>> { - static constexpr uint64_t base_value = 10u; -}; - -template<typename... T, string_literal Literal> -struct schema_hash<schema::Struct<schema::Member<T,Literal>...>> { - static constexpr uint64_t base_value = 11u; -}; - -template<typename... T, string_literal Literal> -struct schema_hash<schema::Union<schema::Member<T,Literal>...>> { - static constexpr uint64_t base_value = 12u; -}; - -template<typename T, size_t Dim> -struct schema_hash<schema::Array<T,Dim>> { - static constexpr uint64_t base_value = 13u; -}; - -template<> struct schema_hash<schema::String> { - static constexpr uint64_t base_value = 14u; -}; + using Schema = schema::String; -template<typename T, typename N> -struct schema_hash<schema::Wrapper<T,N>> { - static constexpr uint64_t base_value = 15u; -}; - -template<typename T, size_t... Dims> -struct schema_hash<schema::FixedArray<T,Dims...>> { - static constexpr uint64_t base_value = 16u; + static constexpr uint64_t apply(uint64_t seed, const data<Schema>& val){ + seed = schema_hash_literal<Schema::name>::apply(seed); + for(size_t i = 0; i < val.size(); ++i){ + seed = schema_hash_combine::apply(seed, static_cast<uint64_t>(val.at(i))); + } + return seed; + } }; } |