Add td::Hash and use it everywhere instead of unusable std::hash.

This commit is contained in:
levlam 2022-11-23 19:37:32 +03:00
parent b1ddf9d3a7
commit af05db3f15
66 changed files with 285 additions and 232 deletions

View File

@ -27,7 +27,7 @@
#define CREATE_MAP_IMPL(num) \
int f_##num() { \
test_map<int, std::array<char, num>> m; \
test_map<td::int32, std::array<char, num>> m; \
m.emplace(1, std::array<char, num>{}); \
int sum = 0; \
for (auto &it : m) { \

View File

@ -12,6 +12,7 @@
#include "td/utils/FlatHashMap.h"
#include "td/utils/FlatHashMapChunks.h"
#include "td/utils/FlatHashTable.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/logging.h"
#include "td/utils/MapNode.h"
#include "td/utils/misc.h"
@ -77,9 +78,9 @@ class IntGenerator {
};
template <>
class Generator<td::uint32> final : public IntGenerator<td::uint32> {};
class Generator<td::int32> final : public IntGenerator<td::int32> {};
template <>
class Generator<td::uint64> final : public IntGenerator<td::uint64> {};
class Generator<td::int64> final : public IntGenerator<td::int64> {};
template <class T>
class Generator<td::unique_ptr<T>> {
@ -159,14 +160,14 @@ void print_memory_stats(td::Slice name) {
td::string big_buff(1 << 16, '\0');
td::StringBuilder sb(big_buff, false);
#define MEASURE(KeyT, ValueT) measure<T<KeyT, ValueT>, KeyT, ValueT>(sb, name, #KeyT, #ValueT);
MEASURE(td::uint32, td::uint32);
MEASURE(td::uint64, td::unique_ptr<Bytes<360>>);
MEASURE(td::int32, td::int32);
MEASURE(td::int64, td::unique_ptr<Bytes<360>>);
if (!sb.as_cslice().empty()) {
LOG(PLAIN) << '\n' << sb.as_cslice() << '\n';
}
}
template <class KeyT, class ValueT, class HashT = std::hash<KeyT>, class EqT = std::equal_to<KeyT>>
template <class KeyT, class ValueT, class HashT = td::Hash<KeyT>, class EqT = std::equal_to<KeyT>>
using FlatHashMapImpl = td::FlatHashTable<td::MapNode<KeyT, ValueT>, HashT, EqT>;
#define FOR_EACH_TABLE(F) \

View File

@ -7,10 +7,10 @@
#pragma once
#include "td/utils/common.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/StringBuilder.h"
#include "td/utils/tl_helpers.h"
#include <functional>
#include <type_traits>
namespace td {
@ -58,8 +58,8 @@ class BackgroundId {
};
struct BackgroundIdHash {
std::size_t operator()(BackgroundId background_id) const {
return std::hash<int64>()(background_id.get());
uint32 operator()(BackgroundId background_id) const {
return Hash<int64>()(background_id.get());
}
};

View File

@ -9,9 +9,9 @@
#include "td/telegram/td_api.h"
#include "td/utils/common.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/StringBuilder.h"
#include <functional>
#include <type_traits>
namespace td {
@ -47,8 +47,8 @@ class CallId {
};
struct CallIdHash {
std::size_t operator()(CallId call_id) const {
return std::hash<int32>()(call_id.get());
uint32 operator()(CallId call_id) const {
return Hash<int32>()(call_id.get());
}
};

View File

@ -15,8 +15,7 @@
#include "td/telegram/PollId.h"
#include "td/utils/common.h"
#include <functional>
#include "td/utils/HashTableUtils.h"
namespace td {
@ -47,7 +46,7 @@ class ChainId {
ChainId(PollId poll_id) : id(static_cast<uint64>(poll_id.get())) {
}
ChainId(const string &str) : id(std::hash<string>()(str)) {
ChainId(const string &str) : id(Hash<string>()(str)) {
}
uint64 get() const {

View File

@ -9,9 +9,9 @@
#include "td/telegram/Version.h"
#include "td/utils/common.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/StringBuilder.h"
#include <functional>
#include <type_traits>
namespace td {
@ -62,8 +62,8 @@ class ChannelId {
};
struct ChannelIdHash {
std::size_t operator()(ChannelId channel_id) const {
return std::hash<int64>()(channel_id.get());
uint32 operator()(ChannelId channel_id) const {
return Hash<int64>()(channel_id.get());
}
};

View File

@ -9,9 +9,9 @@
#include "td/telegram/Version.h"
#include "td/utils/common.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/StringBuilder.h"
#include <functional>
#include <type_traits>
namespace td {
@ -61,8 +61,8 @@ class ChatId {
};
struct ChatIdHash {
std::size_t operator()(ChatId chat_id) const {
return std::hash<int64>()(chat_id.get());
uint32 operator()(ChatId chat_id) const {
return Hash<int64>()(chat_id.get());
}
};

View File

@ -13,11 +13,11 @@
#include "td/telegram/Version.h"
#include "td/utils/common.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/Status.h"
#include "td/utils/StringBuilder.h"
#include "td/utils/tl_helpers.h"
#include <functional>
#include <tuple>
namespace td {
@ -128,18 +128,16 @@ bool operator!=(const Contact &lhs, const Contact &rhs);
StringBuilder &operator<<(StringBuilder &string_builder, const Contact &contact);
struct ContactEqual {
std::size_t operator()(const Contact &lhs, const Contact &rhs) const {
bool operator()(const Contact &lhs, const Contact &rhs) const {
return std::tie(lhs.phone_number_, lhs.first_name_, lhs.last_name_) ==
std::tie(rhs.phone_number_, rhs.first_name_, rhs.last_name_);
}
};
struct ContactHash {
std::size_t operator()(const Contact &contact) const {
return (std::hash<std::string>()(contact.phone_number_) * 2023654985u +
std::hash<std::string>()(contact.first_name_)) *
2023654985u +
std::hash<std::string>()(contact.last_name_);
uint32 operator()(const Contact &contact) const {
return (Hash<string>()(contact.phone_number_) * 2023654985u + Hash<string>()(contact.first_name_)) * 2023654985u +
Hash<string>()(contact.last_name_);
}
};

View File

@ -6094,7 +6094,7 @@ std::pair<vector<UserId>, vector<int32>> ContactsManager::change_imported_contac
vector<Contact> unique_new_contacts;
unique_new_contacts.reserve(contacts.size());
std::unordered_map<Contact, size_t, ContactHash, ContactEqual> different_new_contacts;
std::unordered_set<string> different_new_phone_numbers;
std::unordered_set<string, Hash<string>> different_new_phone_numbers;
size_t unique_size = 0;
for (size_t i = 0; i < contacts.size(); i++) {
auto it_success = different_new_contacts.emplace(std::move(contacts[i]), unique_size);
@ -8924,12 +8924,12 @@ void ContactsManager::on_import_contacts_finished(int64 random_id, vector<UserId
CHECK(unimported_contact_invites.size() == add_size);
CHECK(imported_contacts_unique_id_.size() == result_size);
std::unordered_map<size_t, int32> unique_id_to_unimported_contact_invites;
std::unordered_map<int64, int32, Hash<int64>> unique_id_to_unimported_contact_invites;
for (size_t i = 0; i < add_size; i++) {
auto unique_id = imported_contacts_pos_[i];
get_user_id_object(imported_contact_user_ids[i], "on_import_contacts_finished"); // to ensure updateUser
all_imported_contacts_[unique_id].set_user_id(imported_contact_user_ids[i]);
unique_id_to_unimported_contact_invites[unique_id] = unimported_contact_invites[i];
unique_id_to_unimported_contact_invites[narrow_cast<int64>(unique_id)] = unimported_contact_invites[i];
}
if (G()->parameters().use_chat_info_db) {
@ -8946,7 +8946,7 @@ void ContactsManager::on_import_contacts_finished(int64 random_id, vector<UserId
auto unique_id = imported_contacts_unique_id_[i];
CHECK(unique_id < unique_size);
imported_contact_user_ids_[i] = all_imported_contacts_[unique_id].get_user_id();
auto it = unique_id_to_unimported_contact_invites.find(unique_id);
auto it = unique_id_to_unimported_contact_invites.find(narrow_cast<int64>(unique_id));
if (it == unique_id_to_unimported_contact_invites.end()) {
unimported_contact_invites_[i] = 0;
} else {

View File

@ -48,6 +48,7 @@
#include "td/utils/common.h"
#include "td/utils/FlatHashMap.h"
#include "td/utils/FlatHashSet.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/Hints.h"
#include "td/utils/Promise.h"
#include "td/utils/Status.h"
@ -1767,8 +1768,8 @@ class ContactsManager final : public Actor {
mutable FlatHashSet<UserId, UserIdHash> unknown_users_;
WaitFreeHashMap<UserId, tl_object_ptr<telegram_api::UserProfilePhoto>, UserIdHash> pending_user_photos_;
struct UserIdPhotoIdHash {
std::size_t operator()(const std::pair<UserId, int64> &pair) const {
return UserIdHash()(pair.first) * 2023654985u + std::hash<int64>()(pair.second);
uint32 operator()(const std::pair<UserId, int64> &pair) const {
return UserIdHash()(pair.first) * 2023654985u + Hash<int64>()(pair.second);
}
};
WaitFreeHashMap<std::pair<UserId, int64>, FileSourceId, UserIdPhotoIdHash> user_profile_photo_file_source_ids_;

View File

@ -7,9 +7,9 @@
#pragma once
#include "td/utils/common.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/StringBuilder.h"
#include <functional>
#include <type_traits>
namespace td {
@ -53,8 +53,8 @@ class CustomEmojiId {
};
struct CustomEmojiIdHash {
std::size_t operator()(CustomEmojiId custom_emoji_id) const {
return std::hash<int64>()(custom_emoji_id.get());
uint32 operator()(CustomEmojiId custom_emoji_id) const {
return Hash<int64>()(custom_emoji_id.get());
}
};

View File

@ -10,9 +10,10 @@
#include "td/telegram/MessageId.h"
#include "td/telegram/ServerMessageId.h"
#include "td/utils/common.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/StringBuilder.h"
#include <functional>
#include <limits>
namespace td {
@ -60,8 +61,8 @@ const DialogDate MAX_DIALOG_DATE(0, DialogId());
const int64 DEFAULT_ORDER = -1;
struct DialogDateHash {
std::size_t operator()(const DialogDate &dialog_date) const {
return std::hash<int64>()(dialog_date.get_order()) * 2023654985u + DialogIdHash()(dialog_date.get_dialog_id());
uint32 operator()(const DialogDate &dialog_date) const {
return Hash<int64>()(dialog_date.get_order()) * 2023654985u + DialogIdHash()(dialog_date.get_dialog_id());
}
};

View File

@ -7,9 +7,9 @@
#pragma once
#include "td/utils/common.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/StringBuilder.h"
#include <functional>
#include <type_traits>
namespace td {
@ -61,8 +61,8 @@ class DialogFilterId {
};
struct DialogFilterIdHash {
std::size_t operator()(DialogFilterId dialog_filter_id) const {
return std::hash<int32>()(dialog_filter_id.get());
uint32 operator()(DialogFilterId dialog_filter_id) const {
return Hash<int32>()(dialog_filter_id.get());
}
};

View File

@ -13,9 +13,9 @@
#include "td/telegram/UserId.h"
#include "td/utils/common.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/StringBuilder.h"
#include <functional>
#include <type_traits>
namespace td {
@ -78,8 +78,8 @@ class DialogId {
};
struct DialogIdHash {
std::size_t operator()(DialogId dialog_id) const {
return std::hash<int64>()(dialog_id.get());
uint32 operator()(DialogId dialog_id) const {
return Hash<int64>()(dialog_id.get());
}
};

View File

@ -11,9 +11,9 @@
#include "td/telegram/td_api.h"
#include "td/utils/common.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/StringBuilder.h"
#include <functional>
#include <limits>
#include <type_traits>
@ -113,8 +113,8 @@ class DialogListId {
};
struct DialogListIdHash {
std::size_t operator()(DialogListId dialog_list_id) const {
return std::hash<int64>()(dialog_list_id.get());
uint32 operator()(DialogListId dialog_list_id) const {
return Hash<int64>()(dialog_list_id.get());
}
};

View File

@ -7,9 +7,9 @@
#pragma once
#include "td/utils/common.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/StringBuilder.h"
#include <functional>
#include <type_traits>
namespace td {
@ -56,8 +56,8 @@ class FolderId {
};
struct FolderIdHash {
std::size_t operator()(FolderId folder_id) const {
return std::hash<int32>()(folder_id.get());
uint32 operator()(FolderId folder_id) const {
return Hash<int32>()(folder_id.get());
}
};

View File

@ -55,7 +55,7 @@ struct FullMessageId {
};
struct FullMessageIdHash {
std::size_t operator()(FullMessageId full_message_id) const {
uint32 operator()(FullMessageId full_message_id) const {
return DialogIdHash()(full_message_id.get_dialog_id()) * 2023654985u +
MessageIdHash()(full_message_id.get_message_id());
}

View File

@ -7,9 +7,9 @@
#pragma once
#include "td/utils/common.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/StringBuilder.h"
#include <functional>
#include <type_traits>
namespace td {
@ -41,8 +41,8 @@ class GroupCallId {
};
struct GroupCallIdHash {
std::size_t operator()(GroupCallId group_call_id) const {
return std::hash<int32>()(group_call_id.get());
uint32 operator()(GroupCallId group_call_id) const {
return Hash<int32>()(group_call_id.get());
}
};

View File

@ -12,12 +12,11 @@
#include "td/db/SqliteKeyValueAsync.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/logging.h"
#include "td/utils/tl_helpers.h"
#include "td/utils/utf8.h"
#include <functional>
namespace td {
HashtagHints::HashtagHints(string mode, ActorShared<> parent) : mode_(std::move(mode)), parent_(std::move(parent)) {
@ -49,7 +48,7 @@ void HashtagHints::remove_hashtag(string hashtag, Promise<> promise) {
if (hashtag[0] == '#') {
hashtag = hashtag.substr(1);
}
auto key = std::hash<std::string>()(hashtag);
auto key = Hash<string>()(hashtag);
if (hints_.has_key(key)) {
hints_.remove(key);
G()->td_db()->get_sqlite_pmc()->set(get_key(), serialize(keys_to_strings(hints_.search_empty(101).second)),
@ -80,8 +79,7 @@ void HashtagHints::hashtag_used_impl(const string &hashtag) {
return;
}
// TODO: may be it should be optimized a little
auto key = std::hash<std::string>()(hashtag);
auto key = Hash<string>()(hashtag);
hints_.add(key, hashtag);
hints_.set_rating(key, -++counter_);
}

View File

@ -46,6 +46,7 @@
#include "td/utils/algorithm.h"
#include "td/utils/base64.h"
#include "td/utils/buffer.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/HttpUrl.h"
#include "td/utils/logging.h"
#include "td/utils/misc.h"
@ -57,7 +58,6 @@
#include "td/utils/tl_parsers.h"
#include <algorithm>
#include <functional>
namespace td {
@ -981,10 +981,10 @@ uint64 InlineQueriesManager::send_inline_query(UserId bot_user_id, DialogId dial
}
}();
uint64 query_hash = std::hash<std::string>()(trim(query));
uint64 query_hash = Hash<string>()(trim(query));
query_hash = query_hash * 2023654985u + bot_user_id.get();
query_hash = query_hash * 2023654985u + static_cast<uint64>(peer_type);
query_hash = query_hash * 2023654985u + std::hash<std::string>()(offset);
query_hash = query_hash * 2023654985u + Hash<string>()(offset);
if (r_bot_data.ok().need_location) {
query_hash = query_hash * 2023654985u + static_cast<uint64>(user_location.get_latitude() * 1e4);
query_hash = query_hash * 2023654985u + static_cast<uint64>(user_location.get_longitude() * 1e4);

View File

@ -9,10 +9,9 @@
#include "td/telegram/telegram_api.h"
#include "td/utils/common.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/StringBuilder.h"
#include <functional>
namespace td {
class InputGroupCallId {
@ -43,8 +42,8 @@ class InputGroupCallId {
return group_call_id != 0;
}
std::size_t get_hash() const {
return std::hash<int64>()(group_call_id);
uint32 get_hash() const {
return Hash<int64>()(group_call_id);
}
tl_object_ptr<telegram_api::inputGroupCall> get_input_group_call() const;
@ -65,7 +64,7 @@ class InputGroupCallId {
};
struct InputGroupCallIdHash {
std::size_t operator()(InputGroupCallId input_group_call_id) const {
uint32 operator()(InputGroupCallId input_group_call_id) const {
return input_group_call_id.get_hash();
}
};

View File

@ -1914,6 +1914,7 @@ void LanguagePackManager::hangup() {
int32 LanguagePackManager::manager_count_ = 0;
std::mutex LanguagePackManager::language_database_mutex_;
std::unordered_map<string, unique_ptr<LanguagePackManager::LanguageDatabase>> LanguagePackManager::language_databases_;
std::unordered_map<string, unique_ptr<LanguagePackManager::LanguageDatabase>, Hash<string>>
LanguagePackManager::language_databases_;
} // namespace td

View File

@ -15,6 +15,7 @@
#include "td/utils/common.h"
#include "td/utils/Container.h"
#include "td/utils/FlatHashMap.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/Promise.h"
#include "td/utils/Slice.h"
#include "td/utils/Status.h"
@ -105,7 +106,7 @@ class LanguagePackManager final : public NetQueryCallback {
static int32 manager_count_;
static std::mutex language_database_mutex_;
static std::unordered_map<string, unique_ptr<LanguageDatabase>> language_databases_;
static std::unordered_map<string, unique_ptr<LanguageDatabase>, Hash<string>> language_databases_;
static LanguageDatabase *add_language_database(string path);

View File

@ -7,10 +7,9 @@
#pragma once
#include "td/utils/common.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/StringBuilder.h"
#include <functional>
namespace td {
// increase MessageUnsupported::CURRENT_VERSION each time a new message content type is added
@ -84,8 +83,8 @@ bool can_have_message_content_caption(MessageContentType content_type);
uint64 get_message_content_chain_id(MessageContentType content_type);
struct MessageContentTypeHash {
std::size_t operator()(MessageContentType content_type) const {
return std::hash<int32>()(static_cast<int32>(content_type));
uint32 operator()(MessageContentType content_type) const {
return Hash<int32>()(static_cast<int32>(content_type));
}
};

View File

@ -19,6 +19,7 @@
#include "td/utils/algorithm.h"
#include "td/utils/format.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/logging.h"
#include "td/utils/misc.h"
#include "td/utils/Promise.h"
@ -2455,7 +2456,7 @@ static FormattedText parse_text_url_entities_v3(Slice text, const vector<Message
}
static vector<MessageEntity> find_splittable_entities_v3(Slice text, const vector<MessageEntity> &entities) {
std::unordered_set<size_t> unallowed_boundaries;
std::unordered_set<int32, Hash<int32>> unallowed_boundaries;
for (auto &entity : entities) {
unallowed_boundaries.insert(entity.offset);
unallowed_boundaries.insert(entity.offset + entity.length);
@ -2489,7 +2490,8 @@ static vector<MessageEntity> find_splittable_entities_v3(Slice text, const vecto
if ((c == '_' || c == '*' || c == '~' || c == '|') && text[i] == text[i + 1] &&
unallowed_boundaries.count(utf16_offset) == 0) {
auto j = i + 2;
while (j != text.size() && text[j] == text[i] && unallowed_boundaries.count(utf16_offset + j - i - 1) == 0) {
while (j != text.size() && text[j] == text[i] &&
unallowed_boundaries.count(utf16_offset + static_cast<int32>(j - i - 1)) == 0) {
j++;
}
if (j == i + 2) {

View File

@ -10,9 +10,9 @@
#include "td/telegram/ServerMessageId.h"
#include "td/utils/common.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/StringBuilder.h"
#include <functional>
#include <limits>
#include <type_traits>
@ -182,8 +182,8 @@ class MessageId {
};
struct MessageIdHash {
std::size_t operator()(MessageId message_id) const {
return std::hash<int64>()(message_id.get());
uint32 operator()(MessageId message_id) const {
return Hash<int64>()(message_id.get());
}
};

View File

@ -29188,8 +29188,8 @@ Result<MessagesManager::ForwardedMessages> MessagesManager::get_forwarded_messag
result.drop_author = can_use_server_forward && copy_options[0].send_copy;
result.drop_media_captions = can_use_server_forward && copy_options[0].replace_caption;
std::unordered_map<int64, std::pair<int64, int32>> new_copied_media_album_ids;
std::unordered_map<int64, std::pair<int64, int32>> new_forwarded_media_album_ids;
std::unordered_map<int64, std::pair<int64, int32>, Hash<int64>> new_copied_media_album_ids;
std::unordered_map<int64, std::pair<int64, int32>, Hash<int64>> new_forwarded_media_album_ids;
for (size_t i = 0; i < message_ids.size(); i++) {
MessageId message_id = get_persistent_message_id(from_dialog, message_ids[i]);
@ -29497,7 +29497,7 @@ Result<vector<MessageId>> MessagesManager::resend_messages(DialogId dialog_id, v
}
vector<unique_ptr<MessageContent>> new_contents(message_ids.size());
std::unordered_map<int64, std::pair<int64, int32>> new_media_album_ids;
std::unordered_map<int64, std::pair<int64, int32>, Hash<int64>> new_media_album_ids;
for (size_t i = 0; i < message_ids.size(); i++) {
MessageId message_id = message_ids[i];
const Message *m = get_message(d, message_id);

View File

@ -73,6 +73,7 @@
#include "td/utils/common.h"
#include "td/utils/FlatHashMap.h"
#include "td/utils/FlatHashSet.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/Heap.h"
#include "td/utils/Hints.h"
#include "td/utils/logging.h"
@ -1300,7 +1301,7 @@ class MessagesManager final : public Actor {
unique_ptr<DialogActionBar> action_bar;
LogEventIdWithGeneration save_draft_message_log_event_id;
LogEventIdWithGeneration save_notification_settings_log_event_id;
std::unordered_map<int64, LogEventIdWithGeneration> read_history_log_event_ids;
std::unordered_map<int64, LogEventIdWithGeneration, Hash<int64>> read_history_log_event_ids;
std::unordered_set<MessageId, MessageIdHash> updated_read_history_message_ids;
LogEventIdWithGeneration set_folder_id_log_event_id;
InputGroupCallId active_group_call_id;
@ -1403,7 +1404,8 @@ class MessagesManager final : public Actor {
int32 pending_read_channel_inbox_pts = 0; // for channels only
int32 pending_read_channel_inbox_server_unread_count = 0; // for channels only
MessageId pending_read_channel_inbox_max_message_id; // for channels only
std::unordered_map<int64, MessageId> random_id_to_message_id; // for secret chats and yet unsent messages only
std::unordered_map<int64, MessageId, Hash<int64>>
random_id_to_message_id; // for secret chats and yet unsent messages only
MessageId last_assigned_message_id; // identifier of the last local or yet unsent message, assigned after
// application start, used to guarantee that all assigned message identifiers
@ -3439,8 +3441,8 @@ class MessagesManager final : public Actor {
}
};
struct TtlNodeHash {
std::size_t operator()(const TtlNode &ttl_node) const {
return FullMessageIdHash()(ttl_node.full_message_id_) * 2 + static_cast<size_t>(ttl_node.by_ttl_period_);
uint32 operator()(const TtlNode &ttl_node) const {
return FullMessageIdHash()(ttl_node.full_message_id_) * 2 + static_cast<uint32>(ttl_node.by_ttl_period_);
}
};
std::unordered_set<TtlNode, TtlNodeHash> ttl_nodes_;

View File

@ -7,9 +7,9 @@
#pragma once
#include "td/utils/common.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/StringBuilder.h"
#include <functional>
#include <type_traits>
namespace td {
@ -55,8 +55,8 @@ class NotificationGroupId {
};
struct NotificationGroupIdHash {
std::size_t operator()(NotificationGroupId group_id) const {
return std::hash<int32>()(group_id.get());
uint32 operator()(NotificationGroupId group_id) const {
return Hash<int32>()(group_id.get());
}
};

View File

@ -7,9 +7,9 @@
#pragma once
#include "td/utils/common.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/StringBuilder.h"
#include <functional>
#include <limits>
#include <type_traits>
@ -60,8 +60,8 @@ class NotificationId {
};
struct NotificationIdHash {
std::size_t operator()(NotificationId notification_id) const {
return std::hash<int32>()(notification_id.get());
uint32 operator()(NotificationId notification_id) const {
return Hash<int32>()(notification_id.get());
}
};

View File

@ -7,9 +7,9 @@
#pragma once
#include "td/utils/common.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/StringBuilder.h"
#include <functional>
#include <type_traits>
namespace td {
@ -43,8 +43,8 @@ class PollId {
};
struct PollIdHash {
std::size_t operator()(PollId poll_id) const {
return std::hash<int64>()(poll_id.get());
uint32 operator()(PollId poll_id) const {
return Hash<int64>()(poll_id.get());
}
};

View File

@ -7,9 +7,9 @@
#pragma once
#include "td/utils/common.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/StringBuilder.h"
#include <functional>
#include <type_traits>
namespace td {
@ -53,8 +53,8 @@ class ScheduledServerMessageId {
};
struct ScheduledServerMessageIdHash {
std::size_t operator()(ScheduledServerMessageId message_id) const {
return std::hash<int32>()(message_id.get());
uint32 operator()(ScheduledServerMessageId message_id) const {
return Hash<int32>()(message_id.get());
}
};

View File

@ -6,9 +6,10 @@
//
#pragma once
#include "td/utils/common.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/StringBuilder.h"
#include <functional>
#include <type_traits>
namespace td {
@ -54,8 +55,8 @@ class SecretChatId {
};
struct SecretChatIdHash {
std::size_t operator()(SecretChatId secret_chat_id) const {
return std::hash<int32>()(secret_chat_id.get());
uint32 operator()(SecretChatId secret_chat_id) const {
return Hash<int32>()(secret_chat_id.get());
}
};

View File

@ -9,8 +9,7 @@
#include "td/telegram/telegram_api.h"
#include "td/utils/common.h"
#include <functional>
#include "td/utils/HashTableUtils.h"
namespace td {
@ -59,8 +58,8 @@ inline bool operator!=(const SpecialStickerSetType &lhs, const SpecialStickerSet
}
struct SpecialStickerSetTypeHash {
std::size_t operator()(SpecialStickerSetType type) const {
return std::hash<string>()(type.type_);
uint32 operator()(SpecialStickerSetType type) const {
return Hash<string>()(type.type_);
}
};

View File

@ -7,9 +7,9 @@
#pragma once
#include "td/utils/common.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/StringBuilder.h"
#include <functional>
#include <type_traits>
namespace td {
@ -43,8 +43,8 @@ class StickerSetId {
};
struct StickerSetIdHash {
std::size_t operator()(StickerSetId sticker_set_id) const {
return std::hash<int64>()(sticker_set_id.get());
uint32 operator()(StickerSetId sticker_set_id) const {
return Hash<int64>()(sticker_set_id.get());
}
};

View File

@ -31,6 +31,7 @@
#include "td/utils/common.h"
#include "td/utils/FlatHashMap.h"
#include "td/utils/FlatHashSet.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/Hints.h"
#include "td/utils/Promise.h"
#include "td/utils/Slice.h"
@ -1018,8 +1019,8 @@ class StickersManager final : public Actor {
FlatHashMap<string, FoundStickers> found_stickers_;
FlatHashMap<string, vector<std::pair<int32, Promise<td_api::object_ptr<td_api::stickers>>>>> search_stickers_queries_;
std::unordered_map<string, vector<StickerSetId>> found_sticker_sets_;
std::unordered_map<string, vector<Promise<Unit>>> search_sticker_sets_queries_;
std::unordered_map<string, vector<StickerSetId>, Hash<string>> found_sticker_sets_;
std::unordered_map<string, vector<Promise<Unit>>, Hash<string>> search_sticker_sets_queries_;
FlatHashSet<StickerSetId, StickerSetIdHash> pending_viewed_featured_sticker_set_ids_;
Timeout pending_featured_sticker_set_views_timeout_;

View File

@ -9,9 +9,9 @@
#include "td/telegram/Version.h"
#include "td/utils/common.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/StringBuilder.h"
#include <functional>
#include <type_traits>
namespace td {
@ -79,8 +79,8 @@ class UserId {
};
struct UserIdHash {
std::size_t operator()(UserId user_id) const {
return std::hash<int64>()(user_id.get());
uint32 operator()(UserId user_id) const {
return Hash<int64>()(user_id.get());
}
};

View File

@ -7,10 +7,10 @@
#pragma once
#include "td/utils/common.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/StringBuilder.h"
#include "td/utils/tl_helpers.h"
#include <functional>
#include <type_traits>
namespace td {
@ -54,8 +54,8 @@ class WebPageId {
};
struct WebPageIdHash {
std::size_t operator()(WebPageId web_page_id) const {
return std::hash<int64>()(web_page_id.get());
uint32 operator()(WebPageId web_page_id) const {
return Hash<int64>()(web_page_id.get());
}
};

View File

@ -7,9 +7,9 @@
#pragma once
#include "td/utils/common.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/StringBuilder.h"
#include <functional>
#include <type_traits>
namespace td {
@ -56,8 +56,8 @@ class FileId {
};
struct FileIdHash {
std::size_t operator()(FileId file_id) const {
return std::hash<int32>()(file_id.get());
uint32 operator()(FileId file_id) const {
return Hash<int32>()(file_id.get());
}
};

View File

@ -7,9 +7,9 @@
#pragma once
#include "td/utils/common.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/StringBuilder.h"
#include <functional>
#include <type_traits>
namespace td {
@ -47,8 +47,8 @@ class FileSourceId {
};
struct FileSourceIdHash {
std::size_t operator()(FileSourceId file_source_id) const {
return std::hash<int32>()(file_source_id.get());
uint32 operator()(FileSourceId file_source_id) const {
return Hash<int32>()(file_source_id.get());
}
};

View File

@ -20,6 +20,7 @@
#include "td/db/SqliteKeyValue.h"
#include "td/utils/format.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/logging.h"
#include "td/utils/misc.h"
#include "td/utils/PathView.h"
@ -30,7 +31,6 @@
#include "td/utils/Time.h"
#include "td/utils/tl_parsers.h"
#include <functional>
#include <unordered_map>
#include <unordered_set>
@ -102,7 +102,7 @@ struct FsFileInfo {
template <class CallbackT>
void scan_fs(CancellationToken &token, CallbackT &&callback) {
std::unordered_set<string> scanned_file_dirs;
std::unordered_set<string, Hash<string>> scanned_file_dirs;
auto scan_dir = [&](FileType file_type, const string &file_dir) {
LOG(INFO) << "Trying to scan directory " << file_dir;
if (!scanned_file_dirs.insert(file_dir).second) {
@ -188,17 +188,17 @@ void FileStatsWorker::get_stats(bool need_all_files, bool split_by_owner_dialog_
return promise.set_error(Global::request_aborted_error());
}
std::unordered_map<size_t, size_t> hash_to_pos;
std::unordered_map<int64, size_t, Hash<int64>> hash_to_pos;
size_t pos = 0;
for (auto &full_info : full_infos) {
hash_to_pos[std::hash<std::string>()(full_info.path)] = pos;
hash_to_pos[Hash<string>()(full_info.path)] = pos;
pos++;
if (token_) {
return promise.set_error(Global::request_aborted_error());
}
}
scan_db(token_, [&](DbFileInfo &db_info) {
auto it = hash_to_pos.find(std::hash<std::string>()(db_info.path));
auto it = hash_to_pos.find(Hash<string>()(db_info.path));
if (it == hash_to_pos.end()) {
return;
}

View File

@ -50,7 +50,7 @@ namespace detail {
class StatsCallback final : public mtproto::RawConnection::StatsCallback {
public:
StatsCallback(std::shared_ptr<NetStatsCallback> net_stats_callback, ActorId<ConnectionCreator> connection_creator,
size_t hash, DcOptionsSet::Stat *option_stat)
uint32 hash, DcOptionsSet::Stat *option_stat)
: net_stats_callback_(std::move(net_stats_callback))
, connection_creator_(std::move(connection_creator))
, hash_(hash)
@ -84,7 +84,7 @@ class StatsCallback final : public mtproto::RawConnection::StatsCallback {
private:
std::shared_ptr<NetStatsCallback> net_stats_callback_;
ActorId<ConnectionCreator> connection_creator_;
size_t hash_;
uint32 hash_;
DcOptionsSet::Stat *option_stat_;
};
@ -595,7 +595,7 @@ void ConnectionCreator::on_logging_out(bool is_logging_out) {
}
}
void ConnectionCreator::on_pong(size_t hash) {
void ConnectionCreator::on_pong(uint32 hash) {
G()->save_server_time();
if (active_proxy_id_ != 0) {
auto now = G()->unix_time();
@ -607,14 +607,14 @@ void ConnectionCreator::on_pong(size_t hash) {
}
}
void ConnectionCreator::on_mtproto_error(size_t hash) {
void ConnectionCreator::on_mtproto_error(uint32 hash) {
auto &client = clients_[hash];
client.hash = hash;
client.mtproto_error_flood_control.add_event(Time::now_cached());
}
void ConnectionCreator::request_raw_connection(DcId dc_id, bool allow_media_only, bool is_media,
Promise<unique_ptr<mtproto::RawConnection>> promise, size_t hash,
Promise<unique_ptr<mtproto::RawConnection>> promise, uint32 hash,
unique_ptr<mtproto::AuthData> auth_data) {
auto &client = clients_[hash];
if (!client.inited) {
@ -957,7 +957,7 @@ void ConnectionCreator::client_loop(ClientInfo &client) {
}
void ConnectionCreator::client_create_raw_connection(Result<ConnectionData> r_connection_data, bool check_mode,
mtproto::TransportType transport_type, size_t hash,
mtproto::TransportType transport_type, uint32 hash,
string debug_str, uint32 network_generation) {
unique_ptr<mtproto::AuthData> auth_data;
uint64 auth_data_generation{0};
@ -1018,7 +1018,7 @@ void ConnectionCreator::client_set_timeout_at(ClientInfo &client, double wakeup_
<< wakeup_at - Time::now_cached();
}
void ConnectionCreator::client_add_connection(size_t hash, Result<unique_ptr<mtproto::RawConnection>> r_raw_connection,
void ConnectionCreator::client_add_connection(uint32 hash, Result<unique_ptr<mtproto::RawConnection>> r_raw_connection,
bool check_flag, uint64 auth_data_generation, int64 session_id) {
auto &client = clients_[hash];
client.add_session_id(session_id);
@ -1044,7 +1044,7 @@ void ConnectionCreator::client_add_connection(size_t hash, Result<unique_ptr<mtp
client_loop(client);
}
void ConnectionCreator::client_wakeup(size_t hash) {
void ConnectionCreator::client_wakeup(uint32 hash) {
VLOG(connections) << tag("hash", format::as_hex(hash)) << " wakeup";
G()->save_server_time();
client_loop(clients_[hash]);

View File

@ -59,10 +59,10 @@ class ConnectionCreator final : public NetQueryCallback {
void on_dc_options(DcOptions new_dc_options);
void on_dc_update(DcId dc_id, string ip_port, Promise<> promise);
void on_pong(size_t hash);
void on_mtproto_error(size_t hash);
void on_pong(uint32 hash);
void on_mtproto_error(uint32 hash);
void request_raw_connection(DcId dc_id, bool allow_media_only, bool is_media,
Promise<unique_ptr<mtproto::RawConnection>> promise, size_t hash = 0,
Promise<unique_ptr<mtproto::RawConnection>> promise, uint32 hash = 0,
unique_ptr<mtproto::AuthData> auth_data = {});
void request_raw_connection_by_ip(IPAddress ip_address, mtproto::TransportType transport_type,
Promise<unique_ptr<mtproto::RawConnection>> promise);
@ -159,7 +159,7 @@ class ConnectionCreator final : public NetQueryCallback {
static constexpr double READY_CONNECTIONS_TIMEOUT = 10;
bool inited{false};
size_t hash{0};
uint32 hash{0};
DcId dc_id;
bool allow_media_only{false};
bool is_media{false};
@ -167,7 +167,7 @@ class ConnectionCreator final : public NetQueryCallback {
unique_ptr<mtproto::AuthData> auth_data;
uint64 auth_data_generation{0};
};
std::map<size_t, ClientInfo> clients_;
std::map<uint32, ClientInfo> clients_;
std::shared_ptr<NetStatsCallback> media_net_stats_callback_;
std::shared_ptr<NetStatsCallback> common_net_stats_callback_;
@ -209,7 +209,7 @@ class ConnectionCreator final : public NetQueryCallback {
Result<SocketFd> do_request_connection(DcId dc_id, bool allow_media_only);
Result<std::pair<unique_ptr<mtproto::RawConnection>, bool>> do_request_raw_connection(DcId dc_id,
bool allow_media_only,
bool is_media, size_t hash);
bool is_media, uint32 hash);
void on_network(bool network_flag, uint32 network_generation);
void on_online(bool online_flag);
@ -217,12 +217,12 @@ class ConnectionCreator final : public NetQueryCallback {
static void update_mtproto_header(const Proxy &proxy);
void client_wakeup(size_t hash);
void client_wakeup(uint32 hash);
void client_loop(ClientInfo &client);
void client_create_raw_connection(Result<ConnectionData> r_connection_data, bool check_mode,
mtproto::TransportType transport_type, size_t hash, string debug_str,
mtproto::TransportType transport_type, uint32 hash, string debug_str,
uint32 network_generation);
void client_add_connection(size_t hash, Result<unique_ptr<mtproto::RawConnection>> r_raw_connection, bool check_flag,
void client_add_connection(uint32 hash, Result<unique_ptr<mtproto::RawConnection>> r_raw_connection, bool check_flag,
uint64 auth_data_generation, int64 session_id);
void client_set_timeout_at(ClientInfo &client, double wakeup_at);

View File

@ -16,13 +16,12 @@
#include "td/utils/buffer.h"
#include "td/utils/common.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/logging.h"
#include "td/utils/Promise.h"
#include "td/utils/Slice.h"
#include "td/utils/SliceBuilder.h"
#include <functional>
namespace td {
namespace mtproto {
@ -31,7 +30,7 @@ class RawConnection;
class SessionCallback final : public Session::Callback {
public:
SessionCallback(ActorShared<SessionProxy> parent, DcId dc_id, bool allow_media_only, bool is_media, size_t hash)
SessionCallback(ActorShared<SessionProxy> parent, DcId dc_id, bool allow_media_only, bool is_media, uint32 hash)
: parent_(std::move(parent))
, dc_id_(dc_id)
, allow_media_only_(allow_media_only)
@ -75,7 +74,7 @@ class SessionCallback final : public Session::Callback {
DcId dc_id_;
bool allow_media_only_ = false;
bool is_media_ = false;
size_t hash_ = 0;
uint32 hash_ = 0;
};
SessionProxy::SessionProxy(unique_ptr<Callback> callback, std::shared_ptr<AuthDataShared> shared_auth_data,
@ -201,7 +200,7 @@ void SessionProxy::open_session(bool force) {
auto dc_id = auth_data_->dc_id();
string name = PSTRING() << "Session" << get_name().substr(Slice("SessionProxy").size());
string hash_string = PSTRING() << name << " " << dc_id.get_raw_id() << " " << allow_media_only_;
auto hash = std::hash<std::string>()(hash_string);
auto hash = Hash<string>()(hash_string);
int32 raw_dc_id = dc_id.get_raw_id();
int32 int_dc_id = raw_dc_id;
if (G()->is_test_dc()) {

View File

@ -14,6 +14,7 @@
#include "td/utils/algorithm.h"
#include "td/utils/buffer.h"
#include "td/utils/common.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/logging.h"
#include "td/utils/misc.h"
#include "td/utils/port/RwMutex.h"
@ -192,9 +193,9 @@ class BinlogKeyValue final : public KeyValueSyncInterface {
binlog_->lazy_sync(std::move(promise));
}
std::unordered_map<string, string> prefix_get(Slice prefix) final {
std::unordered_map<string, string, Hash<string>> prefix_get(Slice prefix) final {
auto lock = rw_mutex_.lock_write().move_as_ok();
std::unordered_map<string, string> res;
std::unordered_map<string, string, Hash<string>> res;
for (const auto &kv : map_) {
if (begins_with(kv.first, prefix)) {
res.emplace(kv.first.substr(prefix.size()), kv.second.first);
@ -203,9 +204,9 @@ class BinlogKeyValue final : public KeyValueSyncInterface {
return res;
}
std::unordered_map<string, string> get_all() final {
std::unordered_map<string, string, Hash<string>> get_all() final {
auto lock = rw_mutex_.lock_write().move_as_ok();
std::unordered_map<string, string> res;
std::unordered_map<string, string, Hash<string>> res;
for (const auto &kv : map_) {
res.emplace(kv.first, kv.second.first);
}
@ -238,7 +239,7 @@ class BinlogKeyValue final : public KeyValueSyncInterface {
}
private:
std::unordered_map<string, std::pair<string, uint64>> map_;
std::unordered_map<string, std::pair<string, uint64>, Hash<string>> map_;
std::shared_ptr<BinlogT> binlog_;
RwMutex rw_mutex_;
int32 magic_ = MAGIC;

View File

@ -7,6 +7,7 @@
#pragma once
#include "td/utils/common.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/Promise.h"
#include "td/utils/Slice.h"
@ -33,9 +34,9 @@ class KeyValueSyncInterface {
virtual string get(const string &key) = 0;
virtual std::unordered_map<string, string> prefix_get(Slice prefix) = 0;
virtual std::unordered_map<string, string, Hash<string>> prefix_get(Slice prefix) = 0;
virtual std::unordered_map<string, string> get_all() = 0;
virtual std::unordered_map<string, string, Hash<string>> get_all() = 0;
virtual SeqNo erase(const string &key) = 0;

View File

@ -6,6 +6,7 @@
//
#pragma once
#include "td/utils/HashTableUtils.h"
#include "td/utils/Slice.h"
#include <unordered_map>
@ -66,12 +67,12 @@ class SeqKeyValue {
return map_.size();
}
std::unordered_map<string, string> get_all() const {
std::unordered_map<string, string, Hash<string>> get_all() const {
return map_;
}
private:
std::unordered_map<string, string> map_;
std::unordered_map<string, string, Hash<string>> map_;
SeqNo current_id_ = 0;
SeqNo next_seq_no() {
return ++current_id_;

View File

@ -8,6 +8,7 @@
#include "td/db/SeqKeyValue.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/port/RwMutex.h"
#include "td/utils/Slice.h"
@ -63,7 +64,7 @@ class TsSeqKeyValue {
return kv_.size();
}
std::unordered_map<string, string> get_all() const {
std::unordered_map<string, string, Hash<string>> get_all() const {
auto lock = rw_mutex_.lock_write().move_as_ok();
return kv_.get_all();
}

View File

@ -10,6 +10,7 @@
#include "td/utils/common.h"
#include "td/utils/Container.h"
#include "td/utils/FlatHashSet.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/List.h"
#include "td/utils/logging.h"
#include "td/utils/optional.h"
@ -140,8 +141,8 @@ class ChainScheduler final : public ChainSchedulerBase {
vector<TaskChainInfo> chains;
ExtraT extra;
};
std::unordered_map<ChainId, ChainInfo> chains_;
std::unordered_map<ChainId, TaskId> limited_tasks_;
std::unordered_map<ChainId, ChainInfo, Hash<ChainId>> chains_;
std::unordered_map<ChainId, TaskId, Hash<ChainId>> limited_tasks_;
Container<Task> tasks_;
VectorQueue<TaskId> pending_tasks_;

View File

@ -8,6 +8,7 @@
//#include "td/utils/FlatHashMapChunks.h"
#include "td/utils/FlatHashTable.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/MapNode.h"
#include <functional>
@ -15,7 +16,7 @@
namespace td {
template <class KeyT, class ValueT, class HashT = std::hash<KeyT>, class EqT = std::equal_to<KeyT>>
template <class KeyT, class ValueT, class HashT = Hash<KeyT>, class EqT = std::equal_to<KeyT>>
using FlatHashMap = FlatHashTable<MapNode<KeyT, ValueT>, HashT, EqT>;
//using FlatHashMap = FlatHashMapChunks<KeyT, ValueT, HashT, EqT>;
//using FlatHashMap = std::unordered_map<KeyT, ValueT, HashT, EqT>;

View File

@ -487,7 +487,7 @@ class FlatHashTableChunks {
}
HashInfo calc_hash(const KeyT &key) {
auto h = randomize_hash(HashT()(key));
auto h = HashT()(key);
return {(h >> 8) % chunks_.size(), static_cast<uint8>(0x80 | h)};
}
@ -561,9 +561,10 @@ class FlatHashTableChunks {
}
};
template <class KeyT, class ValueT, class HashT = std::hash<KeyT>, class EqT = std::equal_to<KeyT>>
template <class KeyT, class ValueT, class HashT = Hash<KeyT>, class EqT = std::equal_to<KeyT>>
using FlatHashMapChunks = FlatHashTableChunks<MapNode<KeyT, ValueT>, HashT, EqT>;
template <class KeyT, class HashT = std::hash<KeyT>, class EqT = std::equal_to<KeyT>>
template <class KeyT, class HashT = Hash<KeyT>, class EqT = std::equal_to<KeyT>>
using FlatHashSetChunks = FlatHashTableChunks<SetNode<KeyT>, HashT, EqT>;
template <class NodeT, class HashT, class EqT, class FuncT>

View File

@ -8,6 +8,7 @@
//#include "td/utils/FlatHashMapChunks.h"
#include "td/utils/FlatHashTable.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/SetNode.h"
#include <functional>
@ -15,7 +16,7 @@
namespace td {
template <class KeyT, class HashT = std::hash<KeyT>, class EqT = std::equal_to<KeyT>>
template <class KeyT, class HashT = Hash<KeyT>, class EqT = std::equal_to<KeyT>>
using FlatHashSet = FlatHashTable<SetNode<KeyT>, HashT, EqT>;
//using FlatHashSet = FlatHashSetChunks<KeyT, HashT, EqT>;
//using FlatHashSet = std::unordered_set<KeyT, HashT, EqT>;

View File

@ -465,7 +465,7 @@ class FlatHashTable {
}
uint32 calc_bucket(const KeyT &key) const {
return randomize_hash(HashT()(key)) & bucket_count_mask_;
return HashT()(key) & bucket_count_mask_;
}
inline void next_bucket(uint32 &bucket) const {

View File

@ -56,6 +56,9 @@ class TdHash {
#if TD_HAVE_ABSL
template <class T>
using AbslHash = absl::Hash<T>;
#else
template <class T>
using AbslHash = TdHash<T>;
#endif
// default hash implementations
@ -64,12 +67,4 @@ decltype(H::combine(std::declval<H>(), std::declval<T>())) AbslHashValue(H hashe
return H::combine(std::move(hasher), value);
}
#if TD_HAVE_ABSL
template <class T>
using Hash = AbslHash<T>;
#else
template <class T>
using Hash = TdHash<T>;
#endif
} // namespace td

View File

@ -17,10 +17,10 @@
namespace td {
#if TD_HAVE_ABSL
template <class Key, class Value, class H = Hash<Key>>
template <class Key, class Value, class H = AbslHash<Key>>
using HashMap = absl::flat_hash_map<Key, Value, H>;
#else
template <class Key, class Value, class H = Hash<Key>>
template <class Key, class Value, class H = AbslHash<Key>>
using HashMap = std::unordered_map<Key, Value, H>;
#endif

View File

@ -17,10 +17,10 @@
namespace td {
#if TD_HAVE_ABSL
template <class Key, class H = Hash<Key>>
template <class Key, class H = AbslHash<Key>>
using HashSet = absl::flat_hash_set<Key, H>;
#else
template <class Key, class H = Hash<Key>>
template <class Key, class H = AbslHash<Key>>
using HashSet = std::unordered_set<Key, H>;
#endif

View File

@ -8,6 +8,9 @@
#include "td/utils/common.h"
#include <cstdint>
#include <functional>
namespace td {
template <class KeyT>
@ -15,14 +18,55 @@ bool is_hash_table_key_empty(const KeyT &key) {
return key == KeyT();
}
inline uint32 randomize_hash(size_t h) {
auto result = static_cast<uint32>(h & 0xFFFFFFFF);
result ^= result >> 16;
result *= 0x85ebca6b;
result ^= result >> 13;
result *= 0xc2b2ae35;
result ^= result >> 16;
return result;
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));
}
} // namespace td

View File

@ -7,6 +7,7 @@
#pragma once
#include "td/utils/common.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/Slice.h"
#include <map>
@ -46,8 +47,8 @@ class Hints {
private:
std::map<string, vector<KeyT>> word_to_keys_;
std::map<string, vector<KeyT>> translit_word_to_keys_;
std::unordered_map<KeyT, string> key_to_name_;
std::unordered_map<KeyT, RatingT> key_to_rating_;
std::unordered_map<KeyT, string, Hash<KeyT>> key_to_name_;
std::unordered_map<KeyT, RatingT, Hash<KeyT>> key_to_rating_;
static void add_word(const string &word, KeyT key, std::map<string, vector<KeyT>> &word_to_keys);
static void delete_word(const string &word, KeyT key, std::map<string, vector<KeyT>> &word_to_keys);
@ -60,7 +61,7 @@ class Hints {
vector<KeyT> search_word(const string &word) const;
class CompareByRating {
const std::unordered_map<KeyT, RatingT> &key_to_rating_;
const std::unordered_map<KeyT, RatingT, Hash<KeyT>> &key_to_rating_;
RatingT get_rating(const KeyT &key) const {
auto it = key_to_rating_.find(key);
@ -71,7 +72,8 @@ class Hints {
}
public:
explicit CompareByRating(const std::unordered_map<KeyT, RatingT> &key_to_rating) : key_to_rating_(key_to_rating) {
explicit CompareByRating(const std::unordered_map<KeyT, RatingT, Hash<KeyT>> &key_to_rating)
: key_to_rating_(key_to_rating) {
}
bool operator()(const KeyT &lhs, const KeyT &rhs) const {

View File

@ -206,7 +206,7 @@ class CSlice : public Slice {
};
struct SliceHash {
std::size_t operator()(Slice slice) const;
uint32 operator()(Slice slice) const;
};
} // namespace td

View File

@ -290,10 +290,10 @@ inline CSlice::CSlice(const char *s, const char *t) : Slice(s, t) {
CHECK(*t == '\0');
}
inline std::size_t SliceHash::operator()(Slice slice) const {
inline uint32 SliceHash::operator()(Slice slice) const {
// simple string hash
std::size_t result = 0;
constexpr std::size_t MUL = 123456789;
uint32 result = 0;
constexpr uint32 MUL = 123456789;
for (auto c : slice) {
result = result * MUL + c;
}

View File

@ -14,7 +14,7 @@
namespace td {
template <class KeyT, class ValueT, class HashT = std::hash<KeyT>, class EqT = std::equal_to<KeyT>>
template <class KeyT, class ValueT, class HashT = Hash<KeyT>, class EqT = std::equal_to<KeyT>>
class WaitFreeHashMap {
static constexpr size_t MAX_STORAGE_COUNT = 1 << 8;
static_assert((MAX_STORAGE_COUNT & (MAX_STORAGE_COUNT - 1)) == 0, "");
@ -29,7 +29,7 @@ class WaitFreeHashMap {
uint32 max_storage_size_ = DEFAULT_STORAGE_SIZE;
uint32 get_wait_free_index(const KeyT &key) const {
return randomize_hash(HashT()(key) * hash_mult_) & (MAX_STORAGE_COUNT - 1);
return (HashT()(key) * hash_mult_) & (MAX_STORAGE_COUNT - 1);
}
WaitFreeHashMap &get_wait_free_storage(const KeyT &key) {

View File

@ -14,7 +14,7 @@
namespace td {
template <class KeyT, class HashT = std::hash<KeyT>, class EqT = std::equal_to<KeyT>>
template <class KeyT, class HashT = Hash<KeyT>, class EqT = std::equal_to<KeyT>>
class WaitFreeHashSet {
static constexpr size_t MAX_STORAGE_COUNT = 1 << 8;
static_assert((MAX_STORAGE_COUNT & (MAX_STORAGE_COUNT - 1)) == 0, "");
@ -29,7 +29,7 @@ class WaitFreeHashSet {
uint32 max_storage_size_ = DEFAULT_STORAGE_SIZE;
uint32 get_wait_free_index(const KeyT &key) const {
return randomize_hash(HashT()(key) * hash_mult_) & (MAX_STORAGE_COUNT - 1);
return (HashT()(key) * hash_mult_) & (MAX_STORAGE_COUNT - 1);
}
WaitFreeHashSet &get_wait_free_storage(const KeyT &key) {

View File

@ -7,6 +7,7 @@
#include "td/utils/benchmark.h"
#include "td/utils/common.h"
#include "td/utils/ConcurrentHashTable.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/misc.h"
#include "td/utils/port/Mutex.h"
#include "td/utils/port/thread.h"
@ -88,7 +89,7 @@ class ConcurrentHashMapMutex {
#if TD_HAVE_ABSL
absl::flat_hash_map<KeyT, ValueT> hash_map_;
#else
std::unordered_map<KeyT, ValueT> hash_map_;
std::unordered_map<KeyT, ValueT, td::Hash<KeyT>> hash_map_;
#endif
};
@ -118,7 +119,7 @@ class ConcurrentHashMapSpinlock {
#if TD_HAVE_ABSL
absl::flat_hash_map<KeyT, ValueT> hash_map_;
#else
std::unordered_map<KeyT, ValueT> hash_map_;
std::unordered_map<KeyT, ValueT, td::Hash<KeyT>> hash_map_;
#endif
};
@ -236,15 +237,15 @@ static void bench_hash_map() {
}
TEST(ConcurrentHashMap, Benchmark) {
bench_hash_map<td::ConcurrentHashMap<int, int>>();
bench_hash_map<ArrayHashMap<int, int>>();
bench_hash_map<ConcurrentHashMapSpinlock<int, int>>();
bench_hash_map<ConcurrentHashMapMutex<int, int>>();
bench_hash_map<td::ConcurrentHashMap<td::int32, td::int32>>();
bench_hash_map<ArrayHashMap<td::int32, td::int32>>();
bench_hash_map<ConcurrentHashMapSpinlock<td::int32, td::int32>>();
bench_hash_map<ConcurrentHashMapMutex<td::int32, td::int32>>();
#if TD_WITH_LIBCUCKOO
bench_hash_map<ConcurrentHashMapLibcuckoo<int, int>>();
bench_hash_map<ConcurrentHashMapLibcuckoo<td::int32, td::int32>>();
#endif
#if TD_WITH_JUNCTION
bench_hash_map<ConcurrentHashMapJunction<int, int>>();
bench_hash_map<ConcurrentHashMapJunction<td::int32, td::int32>>();
#endif
}

View File

@ -9,6 +9,7 @@
#include "td/utils/FlatHashMap.h"
#include "td/utils/FlatHashMapChunks.h"
#include "td/utils/FlatHashSet.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/logging.h"
#include "td/utils/Random.h"
#include "td/utils/Slice.h"
@ -16,7 +17,6 @@
#include <algorithm>
#include <array>
#include <functional>
#include <random>
#include <unordered_map>
#include <unordered_set>
@ -83,8 +83,8 @@ struct A {
};
struct AHash {
std::size_t operator()(A a) const {
return std::hash<int>()(a.a);
td::uint32 operator()(A a) const {
return td::Hash<int>()(a.a);
}
};
@ -213,7 +213,7 @@ TEST(FlatHashMap, remove_if_basic) {
constexpr int TESTS_N = 1000;
constexpr int MAX_TABLE_SIZE = 1000;
for (int test_i = 0; test_i < TESTS_N; test_i++) {
std::unordered_map<td::uint64, td::uint64> reference;
std::unordered_map<td::uint64, td::uint64, td::Hash<td::uint64>> reference;
td::FlatHashMap<td::uint64, td::uint64> table;
int N = rnd.fast(1, MAX_TABLE_SIZE);
for (int i = 0; i < N; i++) {
@ -241,7 +241,7 @@ static constexpr size_t MAX_TABLE_SIZE = 1000;
TEST(FlatHashMap, stress_test) {
td::Random::Xorshift128plus rnd(123);
size_t max_table_size = MAX_TABLE_SIZE; // dynamic value
std::unordered_map<td::uint64, td::uint64> ref;
std::unordered_map<td::uint64, td::uint64, td::Hash<td::uint64>> ref;
td::FlatHashMap<td::uint64, td::uint64> tbl;
auto validate = [&] {
@ -363,7 +363,7 @@ TEST(FlatHashSet, stress_test) {
td::Random::Xorshift128plus rnd(123);
size_t max_table_size = MAX_TABLE_SIZE; // dynamic value
std::unordered_set<td::uint64> ref;
std::unordered_set<td::uint64, td::Hash<td::uint64>> ref;
td::FlatHashSet<td::uint64> tbl;
auto validate = [&] {

View File

@ -10,7 +10,7 @@
#include "td/utils/FlatHashMapChunks.h"
#include "td/utils/FlatHashTable.h"
#include "td/utils/format.h"
#include "td/utils/Hash.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/logging.h"
#include "td/utils/MapNode.h"
#include "td/utils/Random.h"
@ -588,7 +588,7 @@ BENCHMARK_TEMPLATE(BM_mask, td::MaskNeon);
BENCHMARK_TEMPLATE(BM_mask, td::MaskSse2);
#endif
template <class KeyT, class ValueT, class HashT = std::hash<KeyT>, class EqT = std::equal_to<KeyT>>
template <class KeyT, class ValueT, class HashT = td::Hash<KeyT>, class EqT = std::equal_to<KeyT>>
using FlatHashMapImpl = td::FlatHashTable<td::MapNode<KeyT, ValueT>, HashT, EqT>;
#define FOR_EACH_TABLE(F) \

View File

@ -16,6 +16,7 @@
#include "td/utils/Hash.h"
#include "td/utils/HashMap.h"
#include "td/utils/HashSet.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/invoke.h"
#include "td/utils/logging.h"
#include "td/utils/misc.h"