/** * \file src/core/include/megbrain/utils/hash.h * MegEngine is Licensed under the Apache License, Version 2.0 (the "License") * * Copyright (c) 2014-2021 Megvii Inc. All rights reserved. * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ #pragma once #include #include #include "megbrain/utils/thin/function.h" #include "megbrain_build_config.h" namespace mgb { /*! * \brief combine two hash values */ static constexpr inline size_t hash_pair_combine(size_t a, size_t b) { return a * 20141203 + b; } template struct HashTrait { static size_t eval(const T& val) { return std::hash()(val); } }; template static inline size_t hash(const T& val) { return HashTrait::eval(val); } template struct StdHashAdaptor { size_t operator()(const T& val) const { return hash(val); } }; /*! * \brief hash for std::pair */ struct pairhash { public: template size_t operator()(const std::pair& x) const { return hash_pair_combine(hash(x.first), hash(x.second)); } }; /*! * \brief wrapper of the xxHash algorithm */ class XXHash { long long m_state[11]; public: MGE_WIN_DECLSPEC_FUC XXHash(); MGE_WIN_DECLSPEC_FUC void reset(); //! update internal state, and return *this MGE_WIN_DECLSPEC_FUC XXHash& update(const void* data, size_t len); //! get hash value, guaranteed to be non-zero MGE_WIN_DECLSPEC_FUC uint64_t digest() const; }; /*! * \brief hash for enum class */ struct enumhash { public: template ::value>> size_t operator()(const E& e) const { return std::hash::type>()( static_cast::type>(e)); } }; } // namespace mgb // vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}}