From b62ced6d9570054e255876057a02434542355178 Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 8 Feb 2022 00:42:53 +0300 Subject: [PATCH] Minor improvements. --- td/telegram/ContactsManager.cpp | 2 +- td/telegram/PollManager.cpp | 2 +- tdutils/CMakeLists.txt | 1 + tdutils/td/utils/ChainScheduler.h | 2 +- tdutils/td/utils/FlatHashMap.h | 33 ++++++++++++++++++++----------- tdutils/test/HashSet.cpp | 13 +++++++++--- 6 files changed, 36 insertions(+), 17 deletions(-) diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index f01ae0c28..4cf82168d 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -5493,7 +5493,7 @@ std::pair, vector> ContactsManager::change_imported_contac vector new_contacts_unique_id(contacts.size()); vector unique_new_contacts; unique_new_contacts.reserve(contacts.size()); - FlatHashMap different_new_contacts; + std::unordered_map different_new_contacts; std::unordered_set different_new_phone_numbers; size_t unique_size = 0; for (size_t i = 0; i < contacts.size(); i++) { diff --git a/td/telegram/PollManager.cpp b/td/telegram/PollManager.cpp index ed5f27616..4508438da 100644 --- a/td/telegram/PollManager.cpp +++ b/td/telegram/PollManager.cpp @@ -167,7 +167,7 @@ class SendVoteQuery final : public Td::ResultHandler { } auto result = result_ptr.move_as_ok(); - LOG(INFO) << "Receive SendVoteQuery result: " << to_string(result); + LOG(INFO) << "Receive result for SendVoteQuery: " << to_string(result); promise_.set_value(std::move(result)); } diff --git a/tdutils/CMakeLists.txt b/tdutils/CMakeLists.txt index 18f42d5d4..30228d012 100644 --- a/tdutils/CMakeLists.txt +++ b/tdutils/CMakeLists.txt @@ -203,6 +203,7 @@ set(TDUTILS_SOURCE td/utils/FileLog.h td/utils/filesystem.h td/utils/find_boundary.h + td/utils/FlatHashMap.h td/utils/FloodControlFast.h td/utils/FloodControlStrict.h td/utils/format.h diff --git a/tdutils/td/utils/ChainScheduler.h b/tdutils/td/utils/ChainScheduler.h index a01c044ca..08b8408cd 100644 --- a/tdutils/td/utils/ChainScheduler.h +++ b/tdutils/td/utils/ChainScheduler.h @@ -237,7 +237,7 @@ class ChainScheduler final : public ChainSchedulerBase { vector to_start_; void try_start_task_later(TaskId task_id) { - LOG(DEBUG) << "Try to start later " << task_id; + LOG(DEBUG) << "Start later " << task_id; to_start_.push_back(task_id); } diff --git a/tdutils/td/utils/FlatHashMap.h b/tdutils/td/utils/FlatHashMap.h index 50466fd3f..a29585df4 100644 --- a/tdutils/td/utils/FlatHashMap.h +++ b/tdutils/td/utils/FlatHashMap.h @@ -1,8 +1,18 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022 +// +// 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" + +#include +#include +#include #include #include -#include namespace td { @@ -27,10 +37,10 @@ class FlatHashMapImpl { second.~ValueT(); } } - Node(Node &&other) { + Node(Node &&other) noexcept { *this = std::move(other); } - Node &operator=(Node &&other) { + Node &operator=(Node &&other) noexcept { DCHECK(empty()); DCHECK(!other.empty()); first = std::move(other.first); @@ -62,7 +72,6 @@ class FlatHashMapImpl { public: struct Iterator { - public: using iterator_category = std::bidirectional_iterator_tag; using difference_type = std::ptrdiff_t; using value_type = Node; @@ -110,7 +119,6 @@ class FlatHashMapImpl { Self *map_; }; struct ConstIterator { - public: using iterator_category = std::bidirectional_iterator_tag; using difference_type = std::ptrdiff_t; using value_type = Node; @@ -165,10 +173,10 @@ class FlatHashMapImpl { using value_type = std::pair; FlatHashMapImpl() = default; - FlatHashMapImpl(FlatHashMapImpl &&other) : nodes_(std::move(other.nodes_)), used_nodes_(other.used_nodes_) { + FlatHashMapImpl(FlatHashMapImpl &&other) noexcept : nodes_(std::move(other.nodes_)), used_nodes_(other.used_nodes_) { other.used_nodes_ = 0; } - FlatHashMapImpl &operator=(FlatHashMapImpl &&other) { + FlatHashMapImpl &operator=(FlatHashMapImpl &&other) noexcept { nodes_ = std::move(other.nodes_); used_nodes_ = other.used_nodes_; other.used_nodes_ = 0; @@ -225,7 +233,7 @@ class FlatHashMapImpl { } auto it = nodes_.begin(); while (it->empty()) { - it++; + ++it; } return Iterator(it, this); } @@ -238,7 +246,7 @@ class FlatHashMapImpl { } auto it = nodes_.begin(); while (it->empty()) { - it++; + ++it; } return ConstIterator(it, this); } @@ -297,7 +305,7 @@ class FlatHashMapImpl { DCHECK(!is_key_empty(it->key())); size_t empty_i = it.it_ - nodes_.begin(); auto empty_bucket = empty_i; - DCHECK(0 <= empty_i < nodes_.size()); + DCHECK(0 <= empty_i && empty_i < nodes_.size()); nodes_[empty_bucket].clear(); used_nodes_--; @@ -364,12 +372,15 @@ class FlatHashMapImpl { if (is_key_empty(node.key())) { continue; } - *find_bucket_for_insert(node.key()) = std::move(node); + auto new_node = find_bucket_for_insert(node.key()); + *new_node = std::move(node); } } }; + //template , class EqualT = std::equal_to> //using FlatHashMap = FlatHashMapImpl; + template , class EqualT = std::equal_to> using FlatHashMap = std::unordered_map; diff --git a/tdutils/test/HashSet.cpp b/tdutils/test/HashSet.cpp index 2859f4019..59132f99c 100644 --- a/tdutils/test/HashSet.cpp +++ b/tdutils/test/HashSet.cpp @@ -1,7 +1,14 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022 +// +// 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) +// +#include "td/utils/common.h" #include "td/utils/FlatHashMap.h" #include "td/utils/tests.h" + #include -#include TEST(FlatHashMap, basic) { td::FlatHashMap map; @@ -18,8 +25,8 @@ TEST(FlatHashMap, basic) { map.erase(map.find(1)); auto map_copy = map; - td::FlatHashMap, 20>> x; + td::FlatHashMap, 20>> x; auto y = std::move(x); x[12]; x.erase(x.find(12)); -} \ No newline at end of file +}