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 algorithms cartesian_product #71

Merged
merged 1 commit into from
Jul 31, 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
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ if(BUILD_TESTING AND ${KDALGORITHMS_BUILD_TEST})
src/kdalgorithms_bits/zip.h
src/kdalgorithms_bits/tuple_utils.h
src/kdalgorithms_bits/invoke.h
src/kdalgorithms_bits/cartesian_product.h

tests/tst_kdalgorithms.cpp
tests/tst_constraints.cpp
Expand Down
32 changes: 32 additions & 0 deletions Documentation/algorithms.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Other
- <a href="#partitioned">partitioned</a>
- <a href="#multi_partitioned">multi_partitioned</a>
- <a href="#zip">zip</a>
- <a href="#cartesian_product">product</a>



Expand Down Expand Up @@ -953,3 +954,34 @@ auto result = kdalgorithms::zip<std::deque>(v1, v2);
```

See [boost::compine](https://www.boost.org/doc/libs/1_81_0/libs/range/doc/html/range/reference/utilities/combine.html) for similar algorithm in boost, and [std::ranges::views::zip](https://en.cppreference.com/w/cpp/ranges/zip_view) for the C++23 version.


<a name="cartesian_product">cartesian_product</a>
-----------------------------
cartesian_product takes a number of containers and returns a cartesian product of the items.

```
const std::array<char, 2> x = {'A', 'B'};
const std::vector<int> y = {1, 2, 3};
const std::list<std::string> z = {"α", "β", "γ", "δ"};

auto result = kdalgorithms::product(x, y, z);
// result is:
// std::vector<std::tuple<char, int, std::string>>{
// {'A', 1, "α"}, {'A', 1, "β"}, {'A', 1, "γ"}, {'A', 1, "δ"},
// {'A', 2, "α"}, {'A', 2, "β"}, {'A', 2, "γ"}, {'A', 2, "δ"},
// {'A', 3, "α"}, {'A', 3, "β"}, {'A', 3, "γ"}, {'A', 3, "δ"},
// {'B', 1, "α"}, {'B', 1, "β"}, {'B', 1, "γ"}, {'B', 1, "δ"},
// {'B', 2, "α"}, {'B', 2, "β"}, {'B', 2, "γ"}, {'B', 2, "δ"},
// {'B', 3, "α"}, {'B', 3, "β"}, {'B', 3, "γ"}, {'B', 3, "δ"},
// };
```

It is also possible to specify the return type:
```
auto result = kdalgorithms::cartesian_product<std::deque>( x, y, z );
// result is:
// std::deque<std::tuple<char, int, std::string>>{ ... }
```

See [std::cartesian_product](https://en.cppreference.com/w/cpp/ranges/cartesian_product_view)
1 change: 1 addition & 0 deletions src/kdalgorithms.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#pragma once

#include "kdalgorithms_bits/cartesian_product.h"
#include "kdalgorithms_bits/filter.h"
#include "kdalgorithms_bits/find_if.h"
#include "kdalgorithms_bits/generate.h"
Expand Down
51 changes: 51 additions & 0 deletions src/kdalgorithms_bits/cartesian_product.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/****************************************************************************
**
** This file is part of KDAlgorithms
**
** SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
**
** SPDX-License-Identifier: MIT
**
****************************************************************************/

#pragma once

#include "transform.h"
#include "tuple_utils.h"
#include <tuple>
#include <vector>

namespace kdalgorithms {
namespace detail {
template <template <typename...> class ResultContainerClass, typename Container>
auto cartesian_product(Container &&arg)
{
return kdalgorithms::transformed<ResultContainerClass>(
std::forward<Container>(arg), [](auto &&elm) { return std::make_tuple(elm); });
}

template <template <typename...> class ResultContainerClass, typename Container,
typename... REST>
auto cartesian_product(Container &&container, REST... args)
{
auto partialResult = cartesian_product<ResultContainerClass>(args...);

using TupleType =
detail::merge_tuple_types_t<ValueType<Container>, ValueType<decltype(partialResult)>>;
ResultContainerClass<TupleType> result;
for (auto &&item : container) {
for (auto &&partialItem : partialResult) {
result.push_back(std::tuple_cat(std::make_tuple(item), partialItem));
}
}
return result;
}
} // namespace detail

template <template <typename...> class ResultContainerClass = std::vector, typename... ARGS>
auto cartesian_product(ARGS... args)
{
return detail::cartesian_product<ResultContainerClass>(std::forward<ARGS>(args)...);
}

} // namespace kdalgorithms
1 change: 1 addition & 0 deletions src/kdalgorithms_bits/transform.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#pragma once

#include "insert_wrapper.h"
#include "read_iterator_wrapper.h"
#include "reserve_helper.h"
#include "shared.h"
#include "to_function_object.h"
Expand Down
11 changes: 11 additions & 0 deletions src/kdalgorithms_bits/tuple_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,16 @@ namespace detail {
fn);
}

template <typename T1, typename T2>
struct merge_tuple_types;

template <typename T, typename... TupleItems>
struct merge_tuple_types<T, std::tuple<TupleItems...>>
{
using type = std::tuple<T, TupleItems...>;
};

template <typename T, typename Tuple>
using merge_tuple_types_t = typename merge_tuple_types<T, Tuple>::type;
} // namespace detail
} // namespace kdalgorithms
64 changes: 64 additions & 0 deletions tests/tst_kdalgorithms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <QTest>
#include <QVector>
#include <algorithm>
#include <array>
#include <deque>
#include <forward_list>
#include <iostream>
Expand Down Expand Up @@ -182,6 +183,7 @@ private Q_SLOTS:
void multi_partitioned();
void multi_partitioned_with_function_taking_a_value();
void sub_range();
void product();
};

void TestAlgorithms::copy()
Expand Down Expand Up @@ -2929,6 +2931,68 @@ void TestAlgorithms::sub_range()
#endif
}

void TestAlgorithms::product()
{
{ // Base case
std::vector<int> v{1, 2, 3};
std::vector<std::tuple<int>> expected{{1}, {2}, {3}};
auto result = kdalgorithms::cartesian_product(v);
QCOMPARE(result, expected);
}

{ // Two lists
std::vector<int> v1{1, 2};
std::vector<int> v2{3, 4};
std::vector<std::tuple<int, int>> expected{{1, 3}, {1, 4}, {2, 3}, {2, 4}};
auto result = kdalgorithms::cartesian_product(v1, v2);
QCOMPARE(result, expected);
}

{ // Three lists
std::vector<int> v1{1, 2};
std::vector<int> v2{3, 4};
std::vector<int> v3{5, 6, 7};
std::vector<std::tuple<int, int, int>> expected{{1, 3, 5}, {1, 3, 6}, {1, 3, 7}, {1, 4, 5},
{1, 4, 6}, {1, 4, 7}, {2, 3, 5}, {2, 3, 6},
{2, 3, 7}, {2, 4, 5}, {2, 4, 6}, {2, 4, 7}};
auto result = kdalgorithms::cartesian_product(v1, v2, v3);
QCOMPARE(result, expected);
}

{ // Different types
std::vector<int> v1{1, 2};
std::vector<bool> v2{true, false};
std::vector<std::tuple<int, bool>> expected{{1, true}, {1, false}, {2, true}, {2, false}};
auto result = kdalgorithms::cartesian_product(v1, v2);
QCOMPARE(result, expected);
}

{ // Different container types
// Example from https://en.cppreference.com/w/cpp/ranges/cartesian_product_view
const std::array<char, 2> x = {'A', 'B'};
const std::vector<int> y = {1, 2, 3};
const std::list<std::string> z = {"α", "β", "γ", "δ"};

const auto expected = std::vector<std::tuple<char, int, std::string>>{
{'A', 1, "α"}, {'A', 1, "β"}, {'A', 1, "γ"}, {'A', 1, "δ"}, {'A', 2, "α"},
{'A', 2, "β"}, {'A', 2, "γ"}, {'A', 2, "δ"}, {'A', 3, "α"}, {'A', 3, "β"},
{'A', 3, "γ"}, {'A', 3, "δ"}, {'B', 1, "α"}, {'B', 1, "β"}, {'B', 1, "γ"},
{'B', 1, "δ"}, {'B', 2, "α"}, {'B', 2, "β"}, {'B', 2, "γ"}, {'B', 2, "δ"},
{'B', 3, "α"}, {'B', 3, "β"}, {'B', 3, "γ"}, {'B', 3, "δ"},
};
auto result = kdalgorithms::cartesian_product(x, y, z);
QCOMPARE(result, expected);
}

{ // Result Type given
std::vector<int> v1{1, 2};
std::list<bool> v2{true, false};
std::deque<std::tuple<int, bool>> expected{{1, true}, {1, false}, {2, true}, {2, false}};
auto result = kdalgorithms::cartesian_product<std::deque>(v1, v2);
QCOMPARE(result, expected);
}
}

QTEST_MAIN(TestAlgorithms)

#include "tst_kdalgorithms.moc"
Loading