2019-07-07 02:05:30 +02:00
|
|
|
//
|
2022-01-01 01:35:39 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
|
2019-07-07 02:05:30 +02: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"
|
|
|
|
|
|
|
|
#if TD_HAVE_ABSL
|
|
|
|
#include <absl/hash/hash.h>
|
|
|
|
#endif
|
|
|
|
|
2019-07-23 00:50:12 +02:00
|
|
|
#include <utility>
|
|
|
|
|
2019-07-07 02:05:30 +02:00
|
|
|
namespace td {
|
|
|
|
// A simple wrapper for absl::flat_hash_map, std::unordered_map and probably some our implementaion of hash map in
|
|
|
|
// the future
|
|
|
|
|
|
|
|
// We will introduce out own Hashing utility like an absl one.
|
|
|
|
class Hasher {
|
|
|
|
public:
|
|
|
|
Hasher() = default;
|
2019-07-21 20:07:07 +02:00
|
|
|
explicit Hasher(size_t init_value) : hash_(init_value) {
|
2019-07-07 02:05:30 +02:00
|
|
|
}
|
2019-07-21 20:07:07 +02:00
|
|
|
std::size_t finalize() const {
|
2019-07-07 02:05:30 +02:00
|
|
|
return hash_;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Hasher combine(Hasher hasher, size_t value) {
|
|
|
|
hasher.hash_ ^= value;
|
|
|
|
return hasher;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class A, class B>
|
|
|
|
static Hasher combine(Hasher hasher, const std::pair<A, B> &value) {
|
|
|
|
hasher = AbslHashValue(std::move(hasher), value.first);
|
2019-07-21 20:07:07 +02:00
|
|
|
hasher = AbslHashValue(std::move(hasher), value.second);
|
2019-07-07 02:05:30 +02:00
|
|
|
return hasher;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::size_t hash_{0};
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class IgnoreT>
|
|
|
|
class TdHash {
|
|
|
|
public:
|
|
|
|
template <class T>
|
|
|
|
std::size_t operator()(const T &value) const noexcept {
|
|
|
|
return AbslHashValue(Hasher(), value).finalize();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#if TD_HAVE_ABSL
|
|
|
|
template <class T>
|
|
|
|
using AbslHash = absl::Hash<T>;
|
2022-11-23 17:37:32 +01:00
|
|
|
#else
|
|
|
|
template <class T>
|
|
|
|
using AbslHash = TdHash<T>;
|
2019-07-07 02:05:30 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// default hash implementations
|
|
|
|
template <class H, class T>
|
|
|
|
decltype(H::combine(std::declval<H>(), std::declval<T>())) AbslHashValue(H hasher, const T &value) {
|
|
|
|
return H::combine(std::move(hasher), value);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|