Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Algorithm transformed_map_values #80

Merged
merged 1 commit into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions Documentation/algorithms.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Modifying algorithms
- <a href="#filter">filter</a>
- <a href="#transform">transform / transformed</a>
- <a href="#filtered_transformed">filtered_transformed</a>
- <a href="#transformed_map_values">transformed_map_values</a>
- <a href="#reverse">reverse</a>
- <a href="#sort">sort / sorted</a>
- <a href="#sort_by">sort_by / sorted_by</a>
Expand Down Expand Up @@ -199,6 +200,37 @@ auto result = kdalgorithms::filtered_transformed(structVec, &Struct::sumPairs,
auto result = kdalgorithms::filtered_transformed<std::deque>(intVector, squareItem, isOdd);
```

<a name="transformed_map_values">transformed_map_values</a>
-----------------------------------------------------------
Another special case of transforming is to only transform the values in a map, ie. not the keys.

```
std::map<int, int> map{{1, 2}, {2, 3}, {3, 4}};
auto toString = [](int i) { return std::to_string(i); };
auto result = kdalgorithms::transformed_map_values(map, toString);
// result is now std::map<int, std::string>{{1, "2"}, {2, "3"}, {3, "4"}};
```

A more involving example would be this:

```
struct TimeOnProjects
{
int projectID;
int hours;
};
using TimeList = QList<TimeOnProjects>;

TimeList timeOnProjects{{1, 10}, {2, 20}, {1, 30}, {3, 40}, {2, 12}};

auto map = kdalgorithms::multi_partitioned(timeOnProjects, &TimeOnProjects::projectID);
auto sumList = [](const TimeList &list) {
return kdalgorithms::sum(list, &TimeOnProjects::hours);
};
auto result = kdalgorithms::transformed_map_values(map, sumList);
// result is std::map<int, int>{{1, 40}, {2, 32}, {3, 40}};
```


<a name="reverse">reverse / reversed</a>
-----------------------------------------
Expand Down
44 changes: 44 additions & 0 deletions src/kdalgorithms_bits/transform.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,4 +239,48 @@ auto filtered_transformed(InputContainer &&input, Transform &&transform,
detail::to_function_object(std::forward<UnaryPredicate>(unaryPredicate)));
}

namespace detail {
template <typename ResultMap, typename Map, typename Transform>
auto transformed_map_values(Map &&input, Transform &&transform)
{
return transformed<ResultMap>(
std::forward<Map>(input),
[transform = std::forward<Transform>(transform)](auto &&pair) {
return std::make_pair(pair.first, transform(pair.second));
});
}

template <typename NewValue, template <typename...> class Map, typename Key, typename OldValue>
auto map_value(const Map<Key, OldValue> &) -> Map<remove_cvref_t<Key>, remove_cvref_t<NewValue>>
{
}
}

template <typename ResultMap, typename Map, typename Transform>
auto transformed_map_values(Map &&input, Transform &&transform)
{
return detail::transformed_map_values<ResultMap>(std::forward<Map>(input),
std::forward<Transform>(transform));
}

template <typename Map, typename Transform>
auto transformed_map_values(Map &&input, Transform &&transform)
{
using ValueType = detail::invoke_result_t<Transform, typename remove_cvref_t<Map>::mapped_type>;
using ResultMap = decltype(detail::map_value<ValueType>(input));
return detail::transformed_map_values<ResultMap>(std::forward<Map>(input),
std::forward<Transform>(transform));
}

template <template <typename...> class PartialResultMap, typename InputMap, typename Transform>
auto transformed_map_values(InputMap &&input, Transform &&transform)
{
using KeyType = typename remove_cvref_t<InputMap>::key_type;
using ValueType =
detail::invoke_result_t<Transform, typename remove_cvref_t<InputMap>::mapped_type>;
using ResultMap = PartialResultMap<KeyType, ValueType>;
return detail::transformed_map_values<ResultMap>(std::forward<InputMap>(input),
std::forward<Transform>(transform));
}

} // namespace kdalgorithms
79 changes: 77 additions & 2 deletions tests/tst_kdalgorithms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ private Q_SLOTS:
void transformedStaticFunctions();
void transform();
void filtered_transformed();
void transformed_map_values();
void anyOf();
void allOf();
void noneOf();
Expand Down Expand Up @@ -871,6 +872,80 @@ void TestAlgorithms::filtered_transformed()
}
}

void TestAlgorithms::transformed_map_values()
{
std::map<int, int> map{{1, 2}, {2, 3}, {3, 4}};

{ // Simple version specifying the result type
std::map<int, int> expected{{1, 4}, {2, 9}, {3, 16}};
auto result = kdalgorithms::transformed_map_values<std::map<int, int>>(map, squareItem);
QCOMPARE(result, expected);
}

{ // Simple version where result type is inferred.
std::map<int, int> expected{{1, 4}, {2, 9}, {3, 16}};
auto result = kdalgorithms::transformed_map_values(map, squareItem);
QCOMPARE(result, expected);
}

{ // Transformed result type
std::map<int, std::string> expected{{1, "2"}, {2, "3"}, {3, "4"}};
auto toString = [](int i) { return std::to_string(i); };
auto result = kdalgorithms::transformed_map_values(map, toString);
QCOMPARE(result, expected);
}

{ // Using QMap instead of std::map
QMap<int, int> map{{1, 2}, {2, 3}, {3, 4}};
QMap<int, QString> expected{{1, "2"}, {2, "3"}, {3, "4"}};
auto toString = [](int i) { return QString::number(i); };
auto result = kdalgorithms::transformed_map_values(std::move(map), toString);
QCOMPARE(result, expected);
}

{ // Using QHash instead of std::map
QHash<int, int> map{{1, 2}, {2, 3}, {3, 4}};
QHash<int, QString> expected{{1, "2"}, {2, "3"}, {3, "4"}};
auto toString = [](int i) { return QString::number(i); };
auto result = kdalgorithms::transformed_map_values(map, toString);
QCOMPARE(result, expected);
}

{ // Specifying a new return type
QMap<int, QString> expected{{1, "2"}, {2, "3"}, {3, "4"}};
auto toString = [](int i) { return QString::number(i); };
auto result = kdalgorithms::transformed_map_values<QMap<int, QString>>(map, toString);
QCOMPARE(result, expected);
}

{ // Specifying a new return type
QMap<int, QString> expected{{1, "2"}, {2, "3"}, {3, "4"}};
auto toString = [](int i) { return QString::number(i); };
auto result = kdalgorithms::transformed_map_values<QMap>(map, toString);
QCOMPARE(result, expected);
}

{ // Complex example
struct TimeOnProjects
{
int projectID;
int hours;
};
using TimeList = QList<TimeOnProjects>;

TimeList timeOnProjects{{1, 10}, {2, 20}, {1, 30}, {3, 40}, {2, 12}};
auto map = kdalgorithms::multi_partitioned(timeOnProjects, &TimeOnProjects::projectID);
auto sumList = [](const TimeList &list) {
return kdalgorithms::sum(list, &TimeOnProjects::hours);
};

auto time = kdalgorithms::transformed_map_values(map, sumList);

std::map<int, int> expected{{1, 40}, {2, 32}, {3, 40}};
QCOMPARE(time, expected);
}
}

void TestAlgorithms::anyOf()
{
bool res = kdalgorithms::any_of(intVector, greaterThan(10));
Expand Down Expand Up @@ -2909,8 +2984,8 @@ void TestAlgorithms::sub_range()
QCOMPARE(result, expected);
}

{ // With the sentinel being the end iterator it fails, and after changing multiple things, I
// gave up getting it to work.
{ // With the sentinel being the end iterator it fails, and after changing multiple things,
// I gave up getting it to work.
struct Sentinel
{
bool operator==(std::vector<int>::const_iterator Iter) const { return *Iter < 0; }
Expand Down
Loading