-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add generic chunked fill and copy algorithms (#1564)
These are replacements for `std::ranges::fill` and `std::ranges::copy` that perform their respective loops in chunks of a dedicated size. After each chunk, a user-defined callback is invoked, which can be used e.g. for cancellation checks. The helpers are used within the `UNION` and `BIND` operations to add more cancellation checks there.
- Loading branch information
Showing
5 changed files
with
110 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,12 +2,14 @@ | |
// Chair of Algorithms and Data Structures. | ||
// Author: Robin Textor-Falconi <[email protected]> | ||
|
||
#include <gtest/gtest.h> | ||
#include <gmock/gmock.h> | ||
|
||
#include <atomic> | ||
|
||
#include "util/ChunkedForLoop.h" | ||
|
||
using ad_utility::chunkedCopy; | ||
using ad_utility::chunkedFill; | ||
using ad_utility::chunkedForLoop; | ||
|
||
TEST(ChunkedForLoop, testEmptyRange) { | ||
|
@@ -103,3 +105,42 @@ TEST(ChunkedForLoop, verifyBreakWorksAsExpected) { | |
EXPECT_EQ(counter, 4); | ||
EXPECT_EQ(chunkCounter, 1); | ||
} | ||
|
||
// _____________________________________________________________________________________________________________________ | ||
TEST(ChunkedForLoop, chunkedFillHandlesEmptyRange) { | ||
size_t chunkCounter = 0; | ||
chunkedFill(std::array<int, 0>{}, 0, 10, [&]() { chunkCounter++; }); | ||
|
||
EXPECT_EQ(chunkCounter, 1); | ||
} | ||
|
||
// _____________________________________________________________________________________________________________________ | ||
TEST(ChunkedForLoop, chunkedFillFillsCorrectly) { | ||
size_t chunkCounter = 0; | ||
std::array<int, 21> elements{}; | ||
chunkedFill(elements, 42, 10, [&]() { chunkCounter++; }); | ||
|
||
EXPECT_EQ(chunkCounter, 3); | ||
EXPECT_THAT(elements, ::testing::Each(::testing::Eq(42))); | ||
} | ||
|
||
// _____________________________________________________________________________________________________________________ | ||
TEST(ChunkedForLoop, chunkedCopyHandlesEmptyRange) { | ||
size_t chunkCounter = 0; | ||
std::array<int, 0> output{}; | ||
chunkedCopy(std::array<int, 0>{}, output.begin(), 2, | ||
[&]() { chunkCounter++; }); | ||
|
||
EXPECT_EQ(chunkCounter, 1); | ||
} | ||
|
||
// _____________________________________________________________________________________________________________________ | ||
TEST(ChunkedForLoop, chunkedCopyCopiesCorrectly) { | ||
size_t chunkCounter = 0; | ||
std::array<int, 5> input{5, 4, 3, 2, 1}; | ||
std::array<int, 5> output{}; | ||
chunkedCopy(input, output.begin(), 2, [&]() { chunkCounter++; }); | ||
|
||
EXPECT_EQ(chunkCounter, 3); | ||
EXPECT_EQ(input, output); | ||
} |