Move some functions from misc.h to algorithm.h.

This commit is contained in:
levlam 2021-01-01 15:59:53 +03:00
parent 09512926a6
commit 82f0386e73
60 changed files with 248 additions and 185 deletions

View File

@ -13,12 +13,12 @@
#include "td/mtproto/Transport.h"
#include "td/mtproto/utils.h"
#include "td/utils/algorithm.h"
#include "td/utils/as.h"
#include "td/utils/common.h"
#include "td/utils/format.h"
#include "td/utils/Gzip.h"
#include "td/utils/logging.h"
#include "td/utils/misc.h"
#include "td/utils/Random.h"
#include "td/utils/ScopeGuard.h"
#include "td/utils/Time.h"

View File

@ -29,6 +29,7 @@
#include "td/db/SqliteKeyValueAsync.h"
#include "td/utils/algorithm.h"
#include "td/utils/logging.h"
#include "td/utils/misc.h"
#include "td/utils/Random.h"

View File

@ -27,6 +27,7 @@
#include "td/db/SqliteKeyValueAsync.h"
#include "td/utils/algorithm.h"
#include "td/utils/buffer.h"
#include "td/utils/common.h"
#include "td/utils/format.h"

View File

@ -22,13 +22,13 @@
#include "td/telegram/Td.h"
#include "td/telegram/UpdatesManager.h"
#include "td/utils/algorithm.h"
#include "td/utils/as.h"
#include "td/utils/bits.h"
#include "td/utils/buffer.h"
#include "td/utils/common.h"
#include "td/utils/crypto.h"
#include "td/utils/logging.h"
#include "td/utils/misc.h"
#include "td/utils/Random.h"
#include <tuple>

View File

@ -12,6 +12,7 @@
#include "td/actor/actor.h"
#include "td/actor/ConcurrentScheduler.h"
#include "td/utils/algorithm.h"
#include "td/utils/common.h"
#include "td/utils/crypto.h"
#include "td/utils/ExitGuard.h"

View File

@ -39,6 +39,7 @@
#include "td/actor/actor.h"
#include "td/utils/algorithm.h"
#include "td/utils/base64.h"
#include "td/utils/buffer.h"
#include "td/utils/common.h"

View File

@ -47,6 +47,7 @@
#include "td/db/SqliteKeyValue.h"
#include "td/db/SqliteKeyValueAsync.h"
#include "td/utils/algorithm.h"
#include "td/utils/base64.h"
#include "td/utils/buffer.h"
#include "td/utils/format.h"

View File

@ -11,6 +11,7 @@
#include "td/telegram/Td.h"
#include "td/telegram/telegram_api.h"
#include "td/utils/algorithm.h"
#include "td/utils/base64.h"
#include "td/utils/buffer.h"
#include "td/utils/Gzip.h"

View File

@ -9,9 +9,9 @@
#include "td/telegram/DialogId.h"
#include "td/telegram/misc.h"
#include "td/utils/algorithm.h"
#include "td/utils/format.h"
#include "td/utils/logging.h"
#include "td/utils/misc.h"
#include <unordered_set>

View File

@ -14,7 +14,7 @@
#include "td/telegram/VideoNotesManager.h"
#include "td/telegram/VideosManager.h"
#include "td/utils/misc.h"
#include "td/utils/algorithm.h"
namespace td {

View File

@ -17,6 +17,7 @@
#include "td/telegram/Td.h"
#include "td/telegram/WebPagesManager.h"
#include "td/utils/algorithm.h"
#include "td/utils/common.h"
#include "td/utils/logging.h"
#include "td/utils/misc.h"

View File

@ -18,6 +18,7 @@
#include "td/telegram/Td.h"
#include "td/telegram/UpdatesManager.h"
#include "td/utils/algorithm.h"
#include "td/utils/buffer.h"
#include "td/utils/JsonBuilder.h"
#include "td/utils/logging.h"

View File

@ -40,6 +40,7 @@
#include "td/telegram/net/DcId.h"
#include "td/utils/algorithm.h"
#include "td/utils/base64.h"
#include "td/utils/buffer.h"
#include "td/utils/HttpUrl.h"

View File

@ -8,6 +8,7 @@
#include "td/telegram/misc.h"
#include "td/utils/algorithm.h"
#include "td/utils/JsonBuilder.h"
#include "td/utils/logging.h"
#include "td/utils/misc.h"

View File

@ -22,6 +22,7 @@
#include "td/db/SqliteDb.h"
#include "td/db/SqliteKeyValue.h"
#include "td/utils/algorithm.h"
#include "td/utils/ExitGuard.h"
#include "td/utils/logging.h"
#include "td/utils/misc.h"

View File

@ -29,6 +29,7 @@
#include "td/actor/actor.h"
#include "td/utils/algorithm.h"
#include "td/utils/ExitGuard.h"
#include "td/utils/FileLog.h"
#include "td/utils/logging.h"

View File

@ -71,6 +71,7 @@
#include "td/actor/MultiPromise.h"
#include "td/actor/PromiseFuture.h"
#include "td/utils/algorithm.h"
#include "td/utils/format.h"
#include "td/utils/HttpUrl.h"
#include "td/utils/logging.h"

View File

@ -10,6 +10,7 @@
#include "td/telegram/misc.h"
#include "td/telegram/SecretChatActor.h"
#include "td/utils/algorithm.h"
#include "td/utils/format.h"
#include "td/utils/logging.h"
#include "td/utils/misc.h"

View File

@ -10,6 +10,7 @@
#include "td/telegram/MessagesManager.h"
#include "td/telegram/ServerMessageId.h"
#include "td/utils/algorithm.h"
#include "td/utils/logging.h"
#include "td/utils/misc.h"

View File

@ -61,6 +61,7 @@
#include "td/db/SqliteKeyValue.h"
#include "td/db/SqliteKeyValueAsync.h"
#include "td/utils/algorithm.h"
#include "td/utils/format.h"
#include "td/utils/misc.h"
#include "td/utils/Random.h"

View File

@ -41,6 +41,7 @@
#include "td/actor/SleepActor.h"
#include "td/utils/algorithm.h"
#include "td/utils/as.h"
#include "td/utils/base64.h"
#include "td/utils/buffer.h"

View File

@ -16,12 +16,12 @@
#include "td/telegram/Td.h"
#include "td/telegram/UpdatesManager.h"
#include "td/utils/algorithm.h"
#include "td/utils/buffer.h"
#include "td/utils/common.h"
#include "td/utils/format.h"
#include "td/utils/JsonBuilder.h"
#include "td/utils/logging.h"
#include "td/utils/misc.h"
#include "td/utils/Status.h"
namespace td {

View File

@ -14,6 +14,7 @@
#include "td/telegram/files/FileManager.h"
#include "td/telegram/net/DcId.h"
#include "td/utils/algorithm.h"
#include "td/utils/base64.h"
#include "td/utils/common.h"
#include "td/utils/format.h"

View File

@ -31,6 +31,7 @@
#include "td/db/SqliteKeyValue.h"
#include "td/db/SqliteKeyValueAsync.h"
#include "td/utils/algorithm.h"
#include "td/utils/buffer.h"
#include "td/utils/format.h"
#include "td/utils/logging.h"

View File

@ -9,8 +9,8 @@
#include "td/telegram/PollManager.h"
#include "td/telegram/Version.h"
#include "td/utils/algorithm.h"
#include "td/utils/common.h"
#include "td/utils/misc.h"
#include "td/utils/tl_helpers.h"
namespace td {

View File

@ -19,8 +19,8 @@
#include "td/telegram/net/NetQueryDispatcher.h"
#include "td/telegram/Td.h"
#include "td/utils/algorithm.h"
#include "td/utils/logging.h"
#include "td/utils/misc.h"
#include <algorithm>
#include <iterator>

View File

@ -9,6 +9,7 @@
#include "td/telegram/ConfigShared.h"
#include "td/telegram/Global.h"
#include "td/utils/algorithm.h"
#include "td/utils/common.h"
#include "td/utils/misc.h"

View File

@ -17,6 +17,7 @@
#include "td/telegram/PasswordManager.h"
#include "td/telegram/Td.h"
#include "td/utils/algorithm.h"
#include "td/utils/buffer.h"
#include "td/utils/logging.h"
#include "td/utils/misc.h"

View File

@ -20,6 +20,7 @@
#include "td/telegram/telegram_api.h"
#include "td/telegram/telegram_api.hpp"
#include "td/utils/algorithm.h"
#include "td/utils/base64.h"
#include "td/utils/buffer.h"
#include "td/utils/crypto.h"

View File

@ -6,8 +6,8 @@
//
#pragma once
#include "td/utils/algorithm.h"
#include "td/utils/common.h"
#include "td/utils/misc.h"
#include <algorithm>
#include <set>

View File

@ -9,8 +9,8 @@
#include "td/actor/PromiseFuture.h"
#include "td/actor/SleepActor.h"
#include "td/utils/algorithm.h"
#include "td/utils/logging.h"
#include "td/utils/misc.h"
#include "td/utils/Time.h"
namespace td {

View File

@ -41,6 +41,7 @@
#include "td/db/SqliteKeyValue.h"
#include "td/db/SqliteKeyValueAsync.h"
#include "td/utils/algorithm.h"
#include "td/utils/format.h"
#include "td/utils/logging.h"
#include "td/utils/misc.h"

View File

@ -16,6 +16,7 @@
#include "td/db/SqliteDb.h"
#include "td/utils/algorithm.h"
#include "td/utils/logging.h"
#include "td/utils/misc.h"
#include "td/utils/port/Clocks.h"

View File

@ -116,6 +116,7 @@
#include "td/mtproto/RSA.h"
#include "td/mtproto/TransportType.h"
#include "td/utils/algorithm.h"
#include "td/utils/buffer.h"
#include "td/utils/filesystem.h"
#include "td/utils/format.h"

View File

@ -20,6 +20,7 @@
#include "td/telegram/Td.h"
#include "td/telegram/TdDb.h"
#include "td/utils/algorithm.h"
#include "td/utils/logging.h"
#include "td/utils/misc.h"
#include "td/utils/port/Clocks.h"

View File

@ -47,6 +47,7 @@
#include "td/actor/MultiPromise.h"
#include "td/utils/algorithm.h"
#include "td/utils/buffer.h"
#include "td/utils/format.h"
#include "td/utils/logging.h"

View File

@ -28,6 +28,7 @@
#include "td/telegram/VoiceNotesManager.hpp"
#include "td/telegram/WebPageId.h"
#include "td/utils/algorithm.h"
#include "td/utils/common.h"
#include "td/utils/logging.h"
#include "td/utils/misc.h"

View File

@ -38,6 +38,7 @@
#include "td/db/SqliteKeyValue.h"
#include "td/db/SqliteKeyValueAsync.h"
#include "td/utils/algorithm.h"
#include "td/utils/buffer.h"
#include "td/utils/common.h"
#include "td/utils/format.h"

View File

@ -17,6 +17,7 @@
#include "td/telegram/Td.h" // for VERBOSITY_NAME(td_requests)
#include "td/telegram/td_api_json.h"
#include "td/utils/algorithm.h"
#include "td/utils/base64.h"
#include "td/utils/buffer.h"
#include "td/utils/common.h"
@ -1078,7 +1079,7 @@ class CliClient final : public Actor {
}
td_api::object_ptr<td_api::userPrivacySettingRules> get_user_privacy_setting_rules(Slice allow, Slice ids) const {
td::vector<td_api::object_ptr<td_api::UserPrivacySettingRule>> rules;
vector<td_api::object_ptr<td_api::UserPrivacySettingRule>> rules;
if (allow == "c" || allow == "contacts") {
rules.push_back(td_api::make_object<td_api::userPrivacySettingRuleAllowContacts>());
} else if (allow == "users") {

View File

@ -11,6 +11,7 @@
#include "td/telegram/files/FileType.h"
#include "td/telegram/Global.h"
#include "td/utils/algorithm.h"
#include "td/utils/format.h"
#include "td/utils/logging.h"
#include "td/utils/misc.h"

View File

@ -24,6 +24,7 @@
#include "td/actor/SleepActor.h"
#include "td/utils/algorithm.h"
#include "td/utils/base64.h"
#include "td/utils/crypto.h"
#include "td/utils/filesystem.h"

View File

@ -6,6 +6,7 @@
//
#include "td/telegram/misc.h"
#include "td/utils/algorithm.h"
#include "td/utils/common.h"
#include "td/utils/HttpUrl.h"
#include "td/utils/logging.h"

View File

@ -9,9 +9,9 @@
#include "td/telegram/Global.h"
#include "td/telegram/TdDb.h"
#include "td/utils/algorithm.h"
#include "td/utils/format.h"
#include "td/utils/logging.h"
#include "td/utils/misc.h"
#include "td/utils/port/RwMutex.h"
#include "td/utils/tl_helpers.h"

View File

@ -28,6 +28,7 @@
#include "td/net/Socks5.h"
#include "td/net/TransparentProxy.h"
#include "td/utils/algorithm.h"
#include "td/utils/base64.h"
#include "td/utils/format.h"
#include "td/utils/logging.h"

View File

@ -11,9 +11,9 @@
#include "td/actor/actor.h"
#include "td/utils/algorithm.h"
#include "td/utils/format.h"
#include "td/utils/logging.h"
#include "td/utils/misc.h"
#include <algorithm>
#include <set>

View File

@ -16,6 +16,7 @@
#include "td/telegram/TdDb.h"
#include "td/telegram/Version.h"
#include "td/utils/algorithm.h"
#include "td/utils/common.h"
#include "td/utils/logging.h"
#include "td/utils/misc.h"

View File

@ -6,9 +6,9 @@
//
#include "td/telegram/net/PublicRsaKeyShared.h"
#include "td/utils/algorithm.h"
#include "td/utils/format.h"
#include "td/utils/logging.h"
#include "td/utils/misc.h"
#include "td/utils/Slice.h"
#include "td/utils/Status.h"

View File

@ -27,6 +27,7 @@
#include "td/mtproto/SessionConnection.h"
#include "td/mtproto/TransportType.h"
#include "td/utils/algorithm.h"
#include "td/utils/as.h"
#include "td/utils/format.h"
#include "td/utils/logging.h"

View File

@ -165,6 +165,7 @@ set(TDUTILS_SOURCE
td/utils/port/detail/WineventPoll.h
td/utils/AesCtrByteFlow.h
td/utils/algorithm.h
td/utils/as.h
td/utils/AtomicRead.h
td/utils/base64.h

View File

@ -6,6 +6,7 @@
//
#include "td/utils/Hints.h"
#include "td/utils/algorithm.h"
#include "td/utils/logging.h"
#include "td/utils/misc.h"
#include "td/utils/Slice.h"

View File

@ -89,9 +89,9 @@ struct MinMaxStat {
} // namespace detail
template <class T>
using MinStat = detail::MinMaxStat<T, std::less<>>;
using MinStat = detail::MinMaxStat<T, std::less<void>>;
template <class T>
using MaxStat = detail::MinMaxStat<T, std::greater<>>;
using MaxStat = detail::MinMaxStat<T, std::greater<void>>;
} // namespace td

View File

@ -0,0 +1,188 @@
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
//
// 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 <functional>
#include <type_traits>
#include <utility>
namespace td {
namespace detail {
template <typename V>
struct transform_helper {
template <class Func>
auto transform(const V &v, const Func &f) {
vector<decltype(f(*v.begin()))> result;
result.reserve(v.size());
for (auto &x : v) {
result.push_back(f(x));
}
return result;
}
template <class Func>
auto transform(V &&v, const Func &f) {
vector<decltype(f(std::move(*v.begin())))> result;
result.reserve(v.size());
for (auto &x : v) {
result.push_back(f(std::move(x)));
}
return result;
}
};
} // namespace detail
template <class V, class Func>
auto transform(V &&v, const Func &f) {
return detail::transform_helper<std::decay_t<V>>().transform(std::forward<V>(v), f);
}
template <class V, class Func>
bool remove_if(V &v, const Func &f) {
size_t i = 0;
while (i != v.size() && !f(v[i])) {
i++;
}
if (i == v.size()) {
return false;
}
size_t j = i;
while (++i != v.size()) {
if (!f(v[i])) {
v[j++] = std::move(v[i]);
}
}
v.erase(v.begin() + j, v.end());
return true;
}
template <class V, class T>
bool remove(V &v, const T &value) {
size_t i = 0;
while (i != v.size() && v[i] != value) {
i++;
}
if (i == v.size()) {
return false;
}
size_t j = i;
while (++i != v.size()) {
if (v[i] != value) {
v[j++] = std::move(v[i]);
}
}
v.erase(v.begin() + j, v.end());
return true;
}
template <class V>
void unique(V &v) {
if (v.empty()) {
return;
}
// use ADL to find std::sort
// caller will need to #include <algorithm>
sort(v.begin(), v.end(), std::less<void>());
size_t j = 1;
for (size_t i = 1; i < v.size(); i++) {
if (v[i] != v[i - 1]) {
if (i != j) {
v[j] = std::move(v[i]);
}
j++;
}
}
v.resize(j);
}
template <class V, class T>
bool contains(const V &v, const T &value) {
for (auto &x : v) {
if (x == value) {
return true;
}
}
return false;
}
template <class T>
void reset_to_empty(T &value) {
using std::swap;
std::decay_t<T> tmp;
swap(tmp, value);
}
template <class T>
void append(vector<T> &destination, const vector<T> &source) {
destination.insert(destination.end(), source.begin(), source.end());
}
template <class T>
void append(vector<T> &destination, vector<T> &&source) {
if (destination.empty()) {
destination.swap(source);
return;
}
destination.reserve(destination.size() + source.size());
for (auto &elem : source) {
destination.push_back(std::move(elem));
}
reset_to_empty(source);
}
template <class T>
void combine(vector<T> &destination, const vector<T> &source) {
append(destination, source);
}
template <class T>
void combine(vector<T> &destination, vector<T> &&source) {
if (destination.size() < source.size()) {
destination.swap(source);
}
if (source.empty()) {
return;
}
destination.reserve(destination.size() + source.size());
for (auto &elem : source) {
destination.push_back(std::move(elem));
}
reset_to_empty(source);
}
namespace detail {
template <typename T>
struct reversion_wrapper {
T &iterable;
};
template <typename T>
auto begin(reversion_wrapper<T> w) {
return w.iterable.rbegin();
}
template <typename T>
auto end(reversion_wrapper<T> w) {
return w.iterable.rend();
}
} // namespace detail
template <typename T>
detail::reversion_wrapper<T> reversed(T &iterable) {
return {iterable};
}
} // namespace td

View File

@ -52,153 +52,6 @@ vector<T> full_split(T s, char delimiter = ' ', size_t max_parts = std::numeric_
string implode(const vector<string> &v, char delimiter = ' ');
namespace detail {
template <typename V>
struct transform_helper {
template <class Func>
auto transform(const V &v, const Func &f) {
vector<decltype(f(*v.begin()))> result;
result.reserve(v.size());
for (auto &x : v) {
result.push_back(f(x));
}
return result;
}
template <class Func>
auto transform(V &&v, const Func &f) {
vector<decltype(f(std::move(*v.begin())))> result;
result.reserve(v.size());
for (auto &x : v) {
result.push_back(f(std::move(x)));
}
return result;
}
};
} // namespace detail
template <class V, class Func>
auto transform(V &&v, const Func &f) {
return detail::transform_helper<std::decay_t<V>>().transform(std::forward<V>(v), f);
}
template <class V, class Func>
bool remove_if(V &v, const Func &f) {
size_t i = 0;
while (i != v.size() && !f(v[i])) {
i++;
}
if (i == v.size()) {
return false;
}
size_t j = i;
while (++i != v.size()) {
if (!f(v[i])) {
v[j++] = std::move(v[i]);
}
}
v.erase(v.begin() + j, v.end());
return true;
}
template <class V, class T>
bool remove(V &v, const T &value) {
size_t i = 0;
while (i != v.size() && v[i] != value) {
i++;
}
if (i == v.size()) {
return false;
}
size_t j = i;
while (++i != v.size()) {
if (v[i] != value) {
v[j++] = std::move(v[i]);
}
}
v.erase(v.begin() + j, v.end());
return true;
}
template <class V>
void unique(V &v) {
if (v.empty()) {
return;
}
std::sort(v.begin(), v.end()); // caller will need to #include <algorithm>
size_t j = 1;
for (size_t i = 1; i < v.size(); i++) {
if (v[i] != v[i - 1]) {
if (i != j) {
v[j] = std::move(v[i]);
}
j++;
}
}
v.resize(j);
}
template <class V, class T>
bool contains(const V &v, const T &value) {
for (auto &x : v) {
if (x == value) {
return true;
}
}
return false;
}
template <class T>
void reset_to_empty(T &value) {
using std::swap;
std::decay_t<T> tmp;
swap(tmp, value);
}
template <class T>
void append(vector<T> &destination, const vector<T> &source) {
destination.insert(destination.end(), source.begin(), source.end());
}
template <class T>
void append(vector<T> &destination, vector<T> &&source) {
if (destination.empty()) {
destination.swap(source);
return;
}
destination.reserve(destination.size() + source.size());
for (auto &elem : source) {
destination.push_back(std::move(elem));
}
reset_to_empty(source);
}
template <class T>
void combine(vector<T> &destination, const vector<T> &source) {
append(destination, source);
}
template <class T>
void combine(vector<T> &destination, vector<T> &&source) {
if (destination.size() < source.size()) {
destination.swap(source);
}
if (source.empty()) {
return;
}
destination.reserve(destination.size() + source.size());
for (auto &elem : source) {
destination.push_back(std::move(elem));
}
reset_to_empty(source);
}
inline bool begins_with(Slice str, Slice prefix) {
return prefix.size() <= str.size() && prefix == Slice(str.data(), prefix.size());
}
@ -478,28 +331,6 @@ bool is_aligned_pointer(const T *pointer) {
return (reinterpret_cast<std::uintptr_t>(static_cast<const void *>(pointer)) & (Alignment - 1)) == 0;
}
namespace detail {
template <typename T>
struct reversion_wrapper {
T &iterable;
};
template <typename T>
auto begin(reversion_wrapper<T> w) {
return w.iterable.rbegin();
}
template <typename T>
auto end(reversion_wrapper<T> w) {
return w.iterable.rend();
}
} // namespace detail
template <typename T>
detail::reversion_wrapper<T> reversed(T &iterable) {
return {iterable};
}
string buffer_to_hex(Slice buffer);
string zero_encode(Slice data);

View File

@ -6,6 +6,7 @@
//
#include "td/utils/translit.h"
#include "td/utils/algorithm.h"
#include "td/utils/misc.h"
#include "td/utils/utf8.h"

View File

@ -4,6 +4,7 @@
// 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/algorithm.h"
#include "td/utils/as.h"
#include "td/utils/base64.h"
#include "td/utils/BigNum.h"

View File

@ -4,6 +4,7 @@
// 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/algorithm.h"
#include "td/utils/common.h"
#include "td/utils/logging.h"
#include "td/utils/misc.h"

View File

@ -6,12 +6,12 @@
//
#include "td/utils/tests.h"
#include "td/utils/algorithm.h"
#include "td/utils/BigNum.h"
#include "td/utils/common.h"
#include "td/utils/crypto.h"
#include "td/utils/format.h"
#include "td/utils/logging.h"
#include "td/utils/misc.h"
#include <algorithm>
#include <limits>

View File

@ -12,6 +12,7 @@
#include "td/net/HttpReader.h"
#include "td/utils/AesCtrByteFlow.h"
#include "td/utils/algorithm.h"
#include "td/utils/base64.h"
#include "td/utils/buffer.h"
#include "td/utils/BufferedFd.h"

View File

@ -28,6 +28,7 @@
#include "td/tl/tl_object_parse.h"
#include "td/tl/tl_object_store.h"
#include "td/utils/algorithm.h"
#include "td/utils/as.h"
#include "td/utils/base64.h"
#include "td/utils/buffer.h"

View File

@ -6,6 +6,7 @@
//
#include "td/telegram/SetWithPosition.h"
#include "td/utils/algorithm.h"
#include "td/utils/common.h"
#include "td/utils/misc.h"
#include "td/utils/Random.h"