-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzip.cpp
39 lines (32 loc) · 1.28 KB
/
zip.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include "pch.h"
#include "DbItemDatabase.h"
namespace WhereThereIsAlLoop
{
std::vector<InsertionResult> AddItemsToDatabase(DbItemDatabase & index, std::vector<Guid> const & ids, std::vector<ItemMetadata> const & metadata)
{
std::vector<InsertionResult> results(ids.size());
// original
{
for (size_t i = 0; i < ids.size(); ++i)
{
results.push_back(index.AddItems(ids[i], metadata[i]));
}
}
// 1st attempt
{ // use zip, transform, and copy
auto resultsView = std::views::zip /*C++23*/ (ids, metadata)
| std::views::transform([&index](auto const & pair)
{ return index.AddItems(std::get<0>(pair), std::get<1>(pair)); });
std::ranges::copy(resultsView, std::back_inserter(results));
}
// 2nd attempt
{ // use transform
std::ranges::transform(
ids,
metadata, // YAY! transform can take two ranges
std::back_inserter(results),
[&index](Guid const & id, ItemMetadata const & meta) { return index.AddItems(id, meta); });
}
return results;
}
} // namespace WhereThereIsAlLoop