From 070399c89abbe31eeb5a84965e0ee330662d5f96 Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 27 Jul 2023 19:05:15 +0300 Subject: [PATCH] Add combine_hashes function. --- td/telegram/Contact.h | 4 ++-- td/telegram/ContactsManager.h | 2 +- td/telegram/DialogDate.h | 2 +- td/telegram/FullMessageId.h | 4 ++-- td/telegram/MessageEntity.h | 6 +++--- td/telegram/StoryFullId.h | 2 +- tdutils/td/utils/HashTableUtils.h | 4 ++++ 7 files changed, 14 insertions(+), 10 deletions(-) diff --git a/td/telegram/Contact.h b/td/telegram/Contact.h index 227643c29..196c6cecd 100644 --- a/td/telegram/Contact.h +++ b/td/telegram/Contact.h @@ -136,8 +136,8 @@ struct ContactEqual { struct ContactHash { uint32 operator()(const Contact &contact) const { - return (Hash()(contact.phone_number_) * 2023654985u + Hash()(contact.first_name_)) * 2023654985u + - Hash()(contact.last_name_); + return combine_hashes(combine_hashes(Hash()(contact.phone_number_), Hash()(contact.first_name_)), + Hash()(contact.last_name_)); } }; diff --git a/td/telegram/ContactsManager.h b/td/telegram/ContactsManager.h index 1f90aa5a6..3e29f5977 100644 --- a/td/telegram/ContactsManager.h +++ b/td/telegram/ContactsManager.h @@ -1922,7 +1922,7 @@ class ContactsManager final : public Actor { WaitFreeHashMap, UserIdHash> pending_user_photos_; struct UserIdPhotoIdHash { uint32 operator()(const std::pair &pair) const { - return UserIdHash()(pair.first) * 2023654985u + Hash()(pair.second); + return combine_hashes(UserIdHash()(pair.first), Hash()(pair.second)); } }; WaitFreeHashMap, FileSourceId, UserIdPhotoIdHash> user_profile_photo_file_source_ids_; diff --git a/td/telegram/DialogDate.h b/td/telegram/DialogDate.h index 881be9f9a..cb70f455b 100644 --- a/td/telegram/DialogDate.h +++ b/td/telegram/DialogDate.h @@ -62,7 +62,7 @@ const int64 DEFAULT_ORDER = -1; struct DialogDateHash { uint32 operator()(const DialogDate &dialog_date) const { - return Hash()(dialog_date.get_order()) * 2023654985u + DialogIdHash()(dialog_date.get_dialog_id()); + return combine_hashes(Hash()(dialog_date.get_order()), DialogIdHash()(dialog_date.get_dialog_id())); } }; diff --git a/td/telegram/FullMessageId.h b/td/telegram/FullMessageId.h index 6bb1f5acd..414b4d68a 100644 --- a/td/telegram/FullMessageId.h +++ b/td/telegram/FullMessageId.h @@ -62,8 +62,8 @@ struct FullMessageId { struct FullMessageIdHash { uint32 operator()(FullMessageId full_message_id) const { - return DialogIdHash()(full_message_id.get_dialog_id()) * 2023654985u + - MessageIdHash()(full_message_id.get_message_id()); + return combine_hashes(DialogIdHash()(full_message_id.get_dialog_id()), + MessageIdHash()(full_message_id.get_message_id())); } }; diff --git a/td/telegram/MessageEntity.h b/td/telegram/MessageEntity.h index a0d8a4b7b..ea20d5e81 100644 --- a/td/telegram/MessageEntity.h +++ b/td/telegram/MessageEntity.h @@ -135,9 +135,9 @@ struct FormattedTextHash { uint32 operator()(const FormattedText &formatted_text) const { auto hash = Hash()(formatted_text.text); for (auto &entity : formatted_text.entities) { - hash = hash * 2023654985u + Hash()(static_cast(entity.type)); - hash = hash * 2023654985u + Hash()(entity.length); - hash = hash * 2023654985u + Hash()(entity.offset); + hash = combine_hashes(hash, Hash()(static_cast(entity.type))); + hash = combine_hashes(hash, Hash()(entity.length)); + hash = combine_hashes(hash, Hash()(entity.offset)); } return hash; } diff --git a/td/telegram/StoryFullId.h b/td/telegram/StoryFullId.h index ba9483a7c..a7e90ac1f 100644 --- a/td/telegram/StoryFullId.h +++ b/td/telegram/StoryFullId.h @@ -61,7 +61,7 @@ struct StoryFullId { struct StoryFullIdHash { uint32 operator()(StoryFullId story_full_id) const { - return DialogIdHash()(story_full_id.get_dialog_id()) * 2023654985u + StoryIdHash()(story_full_id.get_story_id()); + return combine_hashes(DialogIdHash()(story_full_id.get_dialog_id()), StoryIdHash()(story_full_id.get_story_id())); } }; diff --git a/tdutils/td/utils/HashTableUtils.h b/tdutils/td/utils/HashTableUtils.h index 8e11f9840..75eaecf7b 100644 --- a/tdutils/td/utils/HashTableUtils.h +++ b/tdutils/td/utils/HashTableUtils.h @@ -69,4 +69,8 @@ inline uint32 Hash::operator()(const string &value) const { return static_cast(std::hash()(value)); } +inline uint32 combine_hashes(uint32 first_hash, uint32 second_hash) { + return first_hash * 2023654985u + second_hash; +} + } // namespace td