Skip to content

Commit

Permalink
Use View instead of Iterator + size (#1250)
Browse files Browse the repository at this point in the history
  • Loading branch information
autoantwort authored Nov 3, 2023
1 parent df064e0 commit 0039471
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
32 changes: 16 additions & 16 deletions include/vcpkg/base/parallel-algorithms.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,55 +29,55 @@ namespace vcpkg
}
}

template<class RanIt, class F>
void parallel_for_each_n(RanIt begin, size_t work_count, F cb) noexcept
template<class Container, class F>
void parallel_for_each(Container&& c, F cb) noexcept
{
if (work_count == 0)
if (c.size() == 0)
{
return;
}
if (work_count == 1)
if (c.size() == 1)
{
cb(*begin);
cb(c[0]);
return;
}

std::atomic_size_t next{0};

execute_in_parallel(work_count, [&]() {
execute_in_parallel(c.size(), [&]() {
size_t i = 0;
while (i < work_count)
while (i < c.size())
{
if (next.compare_exchange_weak(i, i + 1, std::memory_order_relaxed))
{
cb(*(begin + i));
cb(c[i]);
}
}
});
}

template<class RanItSource, class RanItTarget, class F>
void parallel_transform(RanItSource begin, size_t work_count, RanItTarget out_begin, F&& cb) noexcept
template<class Container, class RanItTarget, class F>
void parallel_transform(const Container& c, RanItTarget out_begin, F&& cb) noexcept
{
if (work_count == 0)
if (c.size() == 0)
{
return;
}
if (work_count == 1)
if (c.size() == 1)
{
*out_begin = cb(*begin);
*out_begin = cb(c[0]);
return;
}

std::atomic_size_t next{0};

execute_in_parallel(work_count, [&]() {
execute_in_parallel(c.size(), [&]() {
size_t i = 0;
while (i < work_count)
while (i < c.size())
{
if (next.compare_exchange_weak(i, i + 1, std::memory_order_relaxed))
{
*(out_begin + i) = cb(*(begin + i));
*(out_begin + i) = cb(c[i]);
}
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/vcpkg/base/system.process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ namespace vcpkg
{
std::vector<ExpectedL<ExitCodeAndOutput>> res(cmd_lines.size(), LocalizedString{});

parallel_transform(cmd_lines.begin(), cmd_lines.size(), res.begin(), [&](const Command& cmd_line) {
parallel_transform(cmd_lines, res.begin(), [&](const Command& cmd_line) {
return cmd_execute_and_capture_output(cmd_line, wd, env);
});

Expand Down
2 changes: 1 addition & 1 deletion src/vcpkg/postbuildlint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1290,7 +1290,7 @@ namespace vcpkg
std::mutex mtx;
auto files = fs.get_regular_files_recursive(dir, IgnoreErrors{});

parallel_for_each_n(files.begin(), files.size(), [&](const Path& file) {
parallel_for_each(files, [&](const Path& file) {
if (file_contains_absolute_paths(fs, file, searcher_paths))
{
std::lock_guard lock{mtx};
Expand Down

0 comments on commit 0039471

Please sign in to comment.