2022-03-09 14:29:47 +01:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
2022-03-09 14:29:47 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
|
|
|
|
2022-11-23 17:37:32 +01:00
|
|
|
#include <cstdint>
|
|
|
|
#include <functional>
|
|
|
|
|
2022-03-09 14:29:47 +01:00
|
|
|
namespace td {
|
|
|
|
|
2024-03-21 16:43:47 +01:00
|
|
|
template <class EqT, class KeyT>
|
2022-03-09 14:40:50 +01:00
|
|
|
bool is_hash_table_key_empty(const KeyT &key) {
|
2024-03-21 16:43:47 +01:00
|
|
|
return EqT()(key, KeyT());
|
2022-03-09 14:29:47 +01:00
|
|
|
}
|
|
|
|
|
2022-11-23 17:37:32 +01:00
|
|
|
inline uint32 randomize_hash(uint32 h) {
|
|
|
|
h ^= h >> 16;
|
|
|
|
h *= 0x85ebca6b;
|
|
|
|
h ^= h >> 13;
|
|
|
|
h *= 0xc2b2ae35;
|
|
|
|
h ^= h >> 16;
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Type>
|
|
|
|
struct Hash {
|
|
|
|
uint32 operator()(const Type &value) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class Type>
|
|
|
|
struct Hash<Type *> {
|
|
|
|
uint32 operator()(Type *pointer) const {
|
|
|
|
return Hash<uint64>()(reinterpret_cast<std::uintptr_t>(pointer));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
inline uint32 Hash<char>::operator()(const char &value) const {
|
|
|
|
return randomize_hash(static_cast<uint32>(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
inline uint32 Hash<int32>::operator()(const int32 &value) const {
|
|
|
|
return randomize_hash(static_cast<uint32>(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
inline uint32 Hash<uint32>::operator()(const uint32 &value) const {
|
|
|
|
return randomize_hash(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
inline uint32 Hash<int64>::operator()(const int64 &value) const {
|
|
|
|
return randomize_hash(static_cast<uint32>(value + (value >> 32)));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
inline uint32 Hash<uint64>::operator()(const uint64 &value) const {
|
|
|
|
return randomize_hash(static_cast<uint32>(value + (value >> 32)));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
inline uint32 Hash<string>::operator()(const string &value) const {
|
|
|
|
return static_cast<uint32>(std::hash<string>()(value));
|
2022-03-09 14:29:47 +01:00
|
|
|
}
|
|
|
|
|
2023-07-27 18:05:15 +02:00
|
|
|
inline uint32 combine_hashes(uint32 first_hash, uint32 second_hash) {
|
|
|
|
return first_hash * 2023654985u + second_hash;
|
|
|
|
}
|
|
|
|
|
2022-03-09 14:29:47 +01:00
|
|
|
} // namespace td
|