mirror of
https://github.com/XRPLF/clio.git
synced 2025-11-14 00:35:52 +00:00
@@ -22,7 +22,8 @@
|
|||||||
#include "util/Assert.h"
|
#include "util/Assert.h"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <cstdint>
|
#include <functional>
|
||||||
|
#include <iterator>
|
||||||
#include <ranges>
|
#include <ranges>
|
||||||
|
|
||||||
namespace util {
|
namespace util {
|
||||||
@@ -32,13 +33,19 @@ forEachBatch(std::ranges::forward_range auto&& container, std::size_t batchSize,
|
|||||||
{
|
{
|
||||||
ASSERT(batchSize > 0, "Batch size must be greater than 0");
|
ASSERT(batchSize > 0, "Batch size must be greater than 0");
|
||||||
|
|
||||||
auto const totalSize = container.size();
|
auto to = std::begin(container);
|
||||||
auto const batches = totalSize / batchSize + (totalSize % batchSize ? 1 : 0);
|
auto end = std::end(container);
|
||||||
|
|
||||||
for (auto i = 0u; i < batches; ++i) {
|
while (to != end) {
|
||||||
auto const start = i * batchSize;
|
auto from = to;
|
||||||
auto const end = std::min(start + batchSize, totalSize);
|
|
||||||
fn(container.begin() + start, container.begin() + end);
|
auto cnt = batchSize;
|
||||||
|
while (to != end and cnt > 0) {
|
||||||
|
++to;
|
||||||
|
--cnt;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::invoke(fn, from, to);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user