-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #166 from tcbrindle/pr/cartesian_docs
Add documentation for cartesian adaptors
- Loading branch information
Showing
7 changed files
with
283 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
|
||
// Copyright (c) 2024 Tristan Brindle (tcbrindle at gmail dot com) | ||
// Distributed under the Boost Software License, Version 1.0. (See accompanying | ||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
|
||
|
||
#include <flux.hpp> | ||
|
||
#include "assert.hpp" | ||
#include <iostream> | ||
#include <vector> | ||
|
||
int main() | ||
{ | ||
std::string_view str = "ab"; | ||
std::vector<std::string> strings; | ||
|
||
// cartesian_power<3> gives us 3-tuples which we can destructure | ||
// The total number of elements in this case is size(str)^3 = 8 | ||
for (auto [x, y, z] : flux::cartesian_power<3>(str)) { | ||
strings.push_back(std::string{x, y, z}); | ||
} | ||
|
||
assert((strings == std::vector<std::string>{"aaa", "aab", "aba", "abb", | ||
"baa", "bab", "bba", "bbb"})); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
|
||
// Copyright (c) 2024 Tristan Brindle (tcbrindle at gmail dot com) | ||
// Distributed under the Boost Software License, Version 1.0. (See accompanying | ||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
|
||
|
||
#include <flux.hpp> | ||
|
||
#include "assert.hpp" | ||
#include <iostream> | ||
#include <vector> | ||
|
||
using namespace std::string_view_literals; | ||
|
||
int main() | ||
{ | ||
std::array nums{1, 2, 3}; | ||
|
||
// cartesian_power_map<N> takes a sequence and a callable of N arguments | ||
// Here we are using N=2 and the binary function object std::plus | ||
auto sums = flux::cartesian_power_map<2>(nums, std::plus{}).to<std::vector<int>>(); | ||
|
||
assert((sums == std::vector<int>{1 + 1, 1 + 2, 1 + 3, | ||
2 + 1, 2 + 2, 2 + 3, | ||
3 + 1, 3 + 2, 3 + 3})); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
|
||
// Copyright (c) 2024 Tristan Brindle (tcbrindle at gmail dot com) | ||
// Distributed under the Boost Software License, Version 1.0. (See accompanying | ||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
|
||
|
||
#include <flux.hpp> | ||
|
||
#include "assert.hpp" | ||
#include <iostream> | ||
#include <vector> | ||
|
||
using namespace std::string_view_literals; | ||
|
||
int main() | ||
{ | ||
std::string_view str = "abc"; | ||
std::array nums = {1, 2, 3}; | ||
|
||
// flux::cartesian_product(str, nums) yields all combinations of elements from | ||
// the two sequences as a tuple<char, int> | ||
auto pairs = flux::cartesian_product(str, nums).to<std::vector>(); | ||
|
||
using P = std::tuple<char, int>; | ||
|
||
assert((pairs == std::vector{P{'a', 1}, P{'a', 2}, P{'a', 3}, | ||
P{'b', 1}, P{'b', 2}, P{'b', 3}, | ||
P{'c', 1}, P{'c', 2}, P{'c', 3}})); | ||
|
||
// cartesian_product can take an arbitrary number of sequence arguments | ||
// The number of elements is the product of the sizes of the input sequences | ||
// It is bidirectional and random-access if all the input sequences | ||
// satisfy these concepts | ||
auto seq = flux::cartesian_product("xy"sv, | ||
std::array{1.0, 2.0}, | ||
std::vector{111, 222}).reverse(); | ||
|
||
using T = std::tuple<char, double, int>; | ||
|
||
assert(flux::equal(seq, std::vector<T>{{'y', 2.0, 222}, {'y', 2.0, 111}, | ||
{'y', 1.0, 222}, {'y', 1.0, 111}, | ||
{'x', 2.0, 222}, {'x', 2.0, 111}, | ||
{'x', 1.0, 222}, {'x', 1.0, 111}})); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
|
||
// Copyright (c) 2024 Tristan Brindle (tcbrindle at gmail dot com) | ||
// Distributed under the Boost Software License, Version 1.0. (See accompanying | ||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
|
||
|
||
#include <flux.hpp> | ||
|
||
#include "assert.hpp" | ||
#include <iostream> | ||
#include <vector> | ||
|
||
int main() | ||
{ | ||
std::array big = {10000L, 20000L}; | ||
std::array medium = {100.0f, 200.0f}; | ||
std::array small = {1, 2}; | ||
|
||
auto add3 = [](long a, float b, int c) { return double(a) + b + c; }; | ||
|
||
// Note that because cartesian_product_map takes a variadic number of | ||
// sequences, the function argument goes first | ||
auto vec = flux::cartesian_product_map(add3, big, medium, small) | ||
.to<std::vector>(); | ||
|
||
assert((vec == std::vector<double>{10101.0, 10102.0, | ||
10201.0, 10202.0, | ||
20101.0, 20102.0, | ||
20201.0, 20202.0})); | ||
} |
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