From 99a54a0d2d3cf5a9bd522ed9e284ada8f2f43e8b Mon Sep 17 00:00:00 2001 From: Francisco Bischoff <984592+franzbischoff@users.noreply.github.com> Date: Mon, 4 Jan 2021 18:59:00 +0000 Subject: [PATCH] Update TinyThread.h Fix for Issue #142 This will guarantee that the last chunk will not be less than the grainSize. --- inst/include/RcppParallel/TinyThread.h | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/inst/include/RcppParallel/TinyThread.h b/inst/include/RcppParallel/TinyThread.h index 8ea48caf..dfa3dcb7 100644 --- a/inst/include/RcppParallel/TinyThread.h +++ b/inst/include/RcppParallel/TinyThread.h @@ -89,10 +89,15 @@ std::vector splitInputRange(const IndexRange& range, // allocate ranges std::vector ranges; std::size_t begin = range.begin(); + std::size_t end = begin; while (begin < range.end()) { - std::size_t end = std::min(begin + grainSize, range.end()); - ranges.push_back(IndexRange(begin, end)); - begin = end; + if ((range.end() - (begin + grainSize)) < grainSize) + end = range.end(); + else + end = std::min(begin + grainSize, range.end()); + + ranges.push_back(IndexRange(begin, end)); + begin = end; } // return ranges