diff --git a/src/util/Batching.h b/src/util/Batching.h index 55cd644e6..f0931246a 100644 --- a/src/util/Batching.h +++ b/src/util/Batching.h @@ -22,7 +22,8 @@ #include "util/Assert.h" #include -#include +#include +#include #include 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"); - auto const totalSize = container.size(); - auto const batches = totalSize / batchSize + (totalSize % batchSize ? 1 : 0); + auto to = std::begin(container); + auto end = std::end(container); - for (auto i = 0u; i < batches; ++i) { - auto const start = i * batchSize; - auto const end = std::min(start + batchSize, totalSize); - fn(container.begin() + start, container.begin() + end); + while (to != end) { + auto from = to; + + auto cnt = batchSize; + while (to != end and cnt > 0) { + ++to; + --cnt; + } + + std::invoke(fn, from, to); } }