Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Batching.hpp
1#pragma once
2
3#include "util/Assert.hpp"
4
5#include <cstddef>
6#include <functional>
7#include <iterator>
8
9namespace util {
10
18void
19forEachBatch(std::ranges::forward_range auto&& container, std::size_t batchSize, auto&& fn)
20{
21 ASSERT(batchSize > 0, "Batch size must be greater than 0");
22
23 auto to = std::begin(container);
24 auto end = std::end(container);
25
26 while (to != end) {
27 auto from = to;
28
29 auto cnt = batchSize;
30 while (to != end and cnt > 0) {
31 ++to;
32 --cnt;
33 }
34
35 std::invoke(fn, from, to);
36 }
37}
38
39} // namespace util
This namespace contains various utilities.
Definition AccountUtils.hpp:11
void forEachBatch(std::ranges::forward_range auto &&container, std::size_t batchSize, auto &&fn)
Iterate over a container in batches.
Definition Batching.hpp:19