Make generic version of batcher function (#3822)

This commit is contained in:
Kasia
2022-06-17 20:52:20 +02:00
committed by GitHub
parent a83c139154
commit f3f340335f
10 changed files with 316 additions and 259 deletions
+30
View File
@@ -3,6 +3,8 @@
#include <QColor>
#include <QString>
#include <cmath>
namespace chatterino {
/**
@@ -37,4 +39,32 @@ QColor getRandomColor(const QString &userId);
QString formatUserMention(const QString &userName, bool isFirstWord,
bool mentionUsersWithComma);
template <typename T>
std::vector<T> splitListIntoBatches(const T &list, int batchSize = 100)
{
std::vector<T> batches;
int batchCount = std::ceil(static_cast<double>(list.size()) / batchSize);
batches.reserve(batchCount);
auto it = list.cbegin();
for (int j = 0; j < batchCount; j++)
{
T batch;
for (int i = 0; i < batchSize && it != list.end(); i++)
{
batch.append(*it);
it++;
}
if (batch.empty())
{
break;
}
batches.emplace_back(std::move(batch));
}
return batches;
}
} // namespace chatterino